【问题标题】:Pygame makes the game unplayable with lagPygame 让游戏因延迟而无法玩
【发布时间】:2021-11-18 08:29:02
【问题描述】:

我正在制作一款您只需点击圆圈的游戏,当我注意到游戏有时确实滞后时,我才开始工作。它只是不会产生新的位置。这是我的代码:

import pygame
from pygame.font import SysFont
from pygame.display import flip
from random import randint, choice

# Bools
r = True
title = True
game = False

# Floats

# Ints
points = 0
deletewhat = [True, False]

# Strings

# Lists
spots = []
pointchoice = [1,1,1,2]

# Colors
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
YELLOW = (255,255,0)
GRAY = (20,20,20)

# Pygame init
pygame.init()
pygame.font.init()

# Pygame userevents
delete = pygame.USEREVENT

# Pygame timers
pygame.time.set_timer(delete, 1000)

# Pygame Fonts
titlefont = SysFont("Arial", 70)
headfont = SysFont("Arial", 20)
startfont = SysFont("Arial", 80)
pointsfont = SysFont("Arial", 25)

# Pygame font one-time render
titletext = titlefont.render("The Spot Spotter", False, BLACK)
headtext = headfont.render("Click the spots and earn points!", False, BLACK)
starttext = startfont.render("START", False, RED)

# Other Pygame things
screen = pygame.display.set_mode((900,750))

class Spot:
    def __init__(self, x, y, points):
        global WHITE, GRAY
        self.x = x
        self.y = y
        self.points = points
        if self.points == 1:
            self.spotcolor = WHITE
        else:
            self.spotcolor = GRAY

    def draw(self):
        pygame.draw.ellipse(screen, self.spotcolor, (self.x, self.y, 50,50))

    def get_pos(self):
        return self.x, self.y

    def get_points(self):
        return self.points

spot = Spot
while r:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            r = False
        if event.type == delete and game:
            deleteone = choice(deletewhat)
            if deleteone:
                spot1 = spot(randint(20, 880), randint(20, 600), choice(pointchoice))
                spot1ready = True

                spot2 = spot(randint(20, 880), randint(20, 600), choice(pointchoice))
                spot2ready = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse = pygame.mouse.get_pos()
            if mouse[0] > 249 and mouse[0] < 801:
                if mouse[1] > 299 and mouse[1] < 426:
                    if title:
                        title = False
                        game = True
            try:
                spot1.get_pos()
            except NameError:
                pass
            else:
                spot1pos = spot1.get_pos()
                if mouse[0] > (spot1pos[0] - 1) and mouse[0] < (spot1pos[0] + 51):
                    if mouse[1] > (spot1pos[1] - 1) and mouse[1] < (spot1pos[1] + 51):
                        if spot1ready:
                            spot1ready = False
                            points += spot1.get_points()
            try:
                spot2.get_pos()
            except NameError:
                pass
            else:
                spot2pos = spot2.get_pos()
                if mouse[0] > (spot2pos[0] - 1) and mouse[0] < (spot2pos[0] + 51):
                    if mouse[1] > (spot2pos[1] - 1) and mouse[1] < (spot2pos[1] + 51):
                        if spot2ready:
                            spot2ready = False
                            points += spot2.get_points()

    if title:
        screen.fill(WHITE)
        screen.blit(titletext, (250, 0))
        screen.blit(headtext, (350, 100))
        pygame.draw.rect(screen, YELLOW, (200, 300, 550, 125)) # Start Button
        screen.blit(starttext, (375, 315))
        flip()
    elif game:
        pointstext = pointsfont.render(f"Points: {points}", False, BLACK)
        screen.fill(BLACK)
        pygame.draw.rect(screen, WHITE, (0, 600, 900, 150))
        screen.blit(pointstext, (20, 610))
        try:
            spot1.draw()
        except NameError:
            pass
        else:
            spot1.draw()

        try:
            spot2.draw()
        except NameError:
            pass
        else:
            spot2.draw()

        flip()


pygame.quit()

如果您想知道,布尔值gametitle 都用于检测需要呈现哪个选项卡。如果game 为真,title 为假,那么游戏就知道了,我需要渲染游戏。

另外请注意,我在 pygame 中不是那么好。

【问题讨论】:

    标签: python python-3.x pygame lag


    【解决方案1】:

    问题在于deleteone = choice(deletewhat) 行。这可能会连续生成多个False

    添加变量wait_delete。当delete 事件发生时减少变量。如果 wait_delete 为0,则使用randint 设置一个新的随机值。

    wait_delete = 1
    spot = Spot
    while r:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                r = False
    
            if event.type == delete and game:
                wait_delete -= 1
                if wait_delete == 0:
                    wait_delete = randint(1, 3)
    
                    spot1 = spot(randint(20, 880), randint(20, 600), choice(pointchoice))
                    spot1ready = True
    
                    spot2 = spot(randint(20, 880), randint(20, 600), choice(pointchoice))
                    spot2ready = True
            # [...]
    
    

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 1970-01-01
      • 2017-08-20
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2023-03-19
      • 2019-10-09
      相关资源
      最近更新 更多