【问题标题】:Pygame, game stops if mouse movesPygame,如果鼠标移动,游戏就会停止
【发布时间】:2016-07-16 13:09:55
【问题描述】:

信息:我目前正在开发一款游戏,并希望在继续之前先了解一下开始菜单的想法,因为这将有助于我想要设计的游戏的其他部分。基本上我想要它,所以当我按下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()

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

【问题讨论】:

    标签: python python-2.7 menu pygame startup


    【解决方案1】:

    我现在发现了你的问题。问题出在游戏循环中。

    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 毫秒(每秒)发生一次。

    event 变量将仅引用事件循环之后的最后一个事件,这意味着如果发生任何事件(例如移动鼠标或按下按钮)可能检查那个事件而不是 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 **
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      相关资源
      最近更新 更多