Pygame 使游戏因延迟而无法玩

Pygame makes the game unplayable with lag

我正在制作一个你只需点击圆圈的游戏,当我发现游戏有时真的很滞后时,我才开始运行它。它只是不会产生新的位置。这是我的代码:

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 方面表现不佳。

问题出在 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
        # [...]