Pygame, 鼠标移动游戏停止

Pygame, game stops if mouse moves

信息: 我目前正在开发一款游戏,希望在继续之前了解开始菜单的想法,因为这将有助于我游戏的其他部分我想设计。基本上我想要它,所以当我按 START GAME 时游戏将开始,当我按 HELP 时它会显示帮助页面,当我按 QUIT 时游戏将退出。到目前为止,当按下 QUIT 时游戏退出,这相当简单,但我仍然坚持开始游戏。

问题: 当我按下开始游戏时,它会显示 Player Sprite 以及时钟。时钟和帧一样工作,直到鼠标移动。

代码

import pygame, random, time
pygame.init()

#Screen
SIZE = width, height = 1280, 720 #Make sure background image is same size
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Cube")

#Events
done = False
menu_on = True

#Colors
BLACK = 0, 0, 0
WHITE = 255, 255, 255

#Fonts
FONT = pygame.font.SysFont("Trebuchet MS", 25)
MENU_FONT = (FONT)

#Info
time = 0
minute = 0
hour = 0
day = 0
year = 0
counter = 0

blink_clock = 0
blink = 0

#Year
YEARFONT = FONT.render("Year:{0:03}".format(year),1, BLACK) #zero-pad day to 3 digits
YEARFONTR=YEARFONT.get_rect()
YEARFONTR.center=(885, 20)
#Day
DAYFONT = FONT.render("Day:{0:03}".format(day),1, BLACK) #zero-pad day to 3 digits
DAYFONTR=DAYFONT.get_rect()
DAYFONTR.center=(985, 20)
#Hour
HOURFONT = FONT.render("Hour:{0:02}".format(hour),1, BLACK) #zero-pad hours to 2 digits
HOURFONTR=HOURFONT.get_rect()
HOURFONTR.center=(1085, 20)
#Minute
MINUTEFONT = FONT.render("Minute:{0:02}".format(minute),1, BLACK) #zero-pad minutes to 2 digits
MINUTEFONTR=MINUTEFONT.get_rect()
MINUTEFONTR.center=(1200, 20)


#Characters
def load_image(cube):
    image = pygame.image.load(cube)
    return image

class Menu:

    hovered = False
    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()
    def draw(self):
        self.set_rend()
        SCREEN.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = MENU_FONT.render(self.text, 1, self.get_color())

    def get_color(self):
        if self.hovered:
            return (255, 255, 255)
        else:
            return (100, 100, 100)

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

class Cube(pygame.sprite.Sprite):
    def __init__(self):
        super(Cube, self).__init__()
        self.images = []
        self.images.append(load_image('Fine.png'))

        self.index = 0
        self.image = self.images[self.index]
        self.rect = pygame.Rect(440, 180, 74, 160)

    def update(self):
        self.index += 1
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]



class Blink(pygame.sprite.Sprite):
    def __init__(self):
        super(Blink, self).__init__()
        self.images = []
        self.images.append(load_image('Blink.png'))

        self.index = 0
        self.image = self.images[self.index]
        self.rect = pygame.Rect(440, 180, 74, 160)

    def update(self):
        self.index += 1
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]

class Blank(pygame.sprite.Sprite):
    def __init__(self):
        super(Blank, self).__init__()
        self.images = []
        self.images.append(load_image('Blank.png'))

        self.index = 0
        self.image = self.images[self.index]
        self.rect = pygame.Rect(440, 180, 74, 160)

    def update(self):
        self.index += 1
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]

allsprites = Cube()
group = pygame.sprite.Group(allsprites)
blink = Blink()
blinking = pygame.sprite.Group(blink)
blankcube = Blank()
blankgroup = pygame.sprite.Group(blankcube)

start_game = [Menu("START GAME", (140, 105))]
help_ = [Menu("HELP", (140, 155))]
quit_ = [Menu("QUIT", (140, 205))]

#Game Speed
clock = pygame.time.Clock()
FPS = 60
CLOCKTICK = pygame.USEREVENT+1
pygame.time.set_timer(CLOCKTICK, 1000)

game_start = False

SCREEN.fill(WHITE)
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    if menu_on == True:
        for Menu in help_:
            if Menu.rect.collidepoint(pygame.mouse.get_pos()):
                Menu.hovered = True
            else:
                Menu.hovered = False
            Menu.draw()
        for Menu in quit_:
            if Menu.rect.collidepoint(pygame.mouse.get_pos()):
                Menu.hovered = True
                if event.type == pygame.MOUSEBUTTONDOWN:
                    done = True
            else:
                Menu.hovered = False
            Menu.draw()

        for Menu in start_game:
            if Menu.rect.collidepoint(pygame.mouse.get_pos()):
                Menu.hovered = True
                if event.type == pygame.MOUSEBUTTONDOWN:
                    game_start = True
            else:
                Menu.hovered = False
            Menu.draw()

        group.update()
        group.draw(SCREEN)

        if event.type == CLOCKTICK:
            blink_clock=blink_clock + 1
            if blink_clock == 60:
                blink_clock = 0
            if blink_clock == 0:
                blink = random.randint(0, 1)
            if blink == 1:
                blinking.update()
                blinking.draw(SCREEN)
                if blink_clock == 41:
                    blink = 0

        blankgroup.update()
        blankgroup.draw(SCREEN)

    if game_start == True:
        menu_on = False
        if event.type == CLOCKTICK:
            minute = minute + 1
            if minute == 60:
                hour = hour + 1
                minute = 0
            if hour == 24:
                day = day + 1
                hour = 0
            if day == 365:
                year = year + 1
                day = 0
        SCREEN.fill(WHITE)
        MINUTEFONT = FONT.render("Minute:{0:02}".format(minute), 1, BLACK)
        SCREEN.blit(MINUTEFONT, MINUTEFONTR)

        HOURFONT = FONT.render("Hour:{0:02}".format(hour), 1, BLACK)
        SCREEN.blit(HOURFONT, HOURFONTR)

        DAYFONT = FONT.render("Day:{0:03}".format(day), 1, BLACK)
        SCREEN.blit(DAYFONT, DAYFONTR)

        YEARFONT = FONT.render("Year:{0:03}".format(year), 1, BLACK)
        SCREEN.blit(YEARFONT, YEARFONTR)

        group.update()
        group.draw(SCREEN)

        if event.type == CLOCKTICK:
            blink_clock=blink_clock + 1
            if blink_clock == 60:
                blink_clock = 0
            if blink_clock == 0:
                blink = random.randint(0, 1)
            if blink == 1:
                blinking.update()
                blinking.draw(SCREEN)
                if blink_clock == 41:
                    blink = 0

        blankgroup.update()
        blankgroup.draw(SCREEN)


    clock.tick(FPS) 
    pygame.display.flip()

pygame.quit()

感谢任何帮助,我很乐意看到您的反馈:)

我已经找到你的问题了。问题出在游戏循环中。

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    if menu_on == True:
        **do stuff 1**

    if game_start == True:
        **do stuff 2**

在 **do stuff 1** 中,您没有检查事件循环中的任何事件,因此它工作正常。但是在 **do stuff 2** 中,您正在尝试检查事件 CLOCKTICK,您将其定义为每 1000 毫秒(每秒)发生一次。

事件变量将引用事件循环后的最后一个事件,这意味着如果有任何事件(例如移动鼠标或按下按钮)发生它可能会检查该事件而不是 CLOCKTICK.

另外,如果没有事件发生,事件变量将仍然是前一个事件。这就是为什么你的时钟走得如此之快,而不是每秒。

解决这个问题的方法是为每个状态设置一个事件循环。

while not done: 
    if menu_on == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        ** the rest of the code **

    if game_start == True:
        menu_on = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == CLOCKTICK:  # Make sure this code is inside the event loop!
                minute = minute + 1
                if minute == 60:
                    hour = hour + 1
                    minute = 0
                if hour == 24:
                    day = day + 1
                    hour = 0
                if day == 365:
                    year = year + 1
                    day = 0

        ** the rest of the code **