首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏,如果鼠标移动,游戏停止

游戏,如果鼠标移动,游戏停止
EN

Stack Overflow用户
提问于 2016-07-16 13:09:55
回答 1查看 478关注 0票数 0

信息:,我目前正在开发一款游戏,我想在继续之前先从下一个菜单中得到一个开始菜单,因为这将有助于我设计游戏的更多部分。基本上,我想要它,所以当我按下START GAME,游戏就会开始,当我按下HELP,它会显示一个帮助页面,当我按下QUIT,游戏就会退出。到目前为止,当QUIT被按下时,游戏就停止了--这很简单,但是我还是坚持要开始这个游戏。

问题:当我按下开始游戏时,它会显示玩家雪碧以及时钟。时钟和框架一样工作,直到鼠标被移动。

代码

代码语言:javascript
复制
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()

任何帮助都是非常感谢的,我希望看到你的反馈:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-17 12:42:49

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

代码语言:javascript
复制
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**

在**做事情1**你没有检查任何事件从事件循环,所以它的工作良好。但是在**do trying 2*中,您试图检查一个事件CLOCKTICK,您定义它每1000毫秒发生一次(每秒一次)。

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

另外,如果没有发生任何事件,事件变量仍然是。这就是为什么你的时钟速度如此之快,而不是每一秒。

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

代码语言:javascript
复制
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 **
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38411554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档