Pygame - 暂停菜单不起作用?

时间:2016-07-20 11:27:13

标签: python python-2.7 menu pygame pause

Over View

我正处于一个我想在游戏中添加几个“额外”菜单的阶段。我通过创建暂停菜单并计划使用相同的概念创建“商店”和“玩家创建”以及“迷你游戏”来启动此操作。

问题

到目前为止,我已经想到了一个菜单,但是如果鼠标移动它会停止,而我不确定当按下继续时它是否会继续。

代码

这是我的代码:     导入pygame,随机,时间     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
game_start = False
pause = False

#Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (51,51,51)
YELLOW =  (255, 255, 153)
PURPLE = (153, 102, 255)
RED = (255, 0 ,0)
GREEN = (0, 255, 0)


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

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

blink_clock = 0
blink = 0

hunger = 100
fun = 100
health = 100

feeling = 3

#Hunger
HUNGERFONT = FONT_HUD.render("Hunger:{0:03}".format(hunger),1, BLACK) #zero-pad day to 3 digits
HUNGERFONTR=HUNGERFONT.get_rect()
HUNGERFONTR.center=(886, 625)

HUNGERFONT_R = FONT_HUD.render("Hunger:{0:03}".format(hunger),1, RED) #zero-pad day to 3 digits
HUNGERFONT_RR=HUNGERFONT_R.get_rect()
HUNGERFONT_RR.center=(885, 625)

HUNGERFONT_G = FONT_HUD.render("Hunger:{0:03}".format(hunger),1, GREEN) #zero-pad day to 3 digits
HUNGERFONT_GR=HUNGERFONT_G.get_rect()
HUNGERFONT_GR.center=(885, 625)

#Fun
FUNFONT = FONT_HUD.render("Fun:{0:03}".format(fun),1, BLACK) #zero-pad day to 3 digits
FUNFONTR=FUNFONT.get_rect()
FUNFONTR.center=(626, 625)

FUNFONT_R = FONT_HUD.render("Fun:{0:03}".format(fun),1, RED) #zero-pad day to 3 digits
FUNFONT_RR=FUNFONT_R.get_rect()
FUNFONT_RR.center=(625, 625)

FUNFONT_G = FONT_HUD.render("Fun:{0:03}".format(fun),1, GREEN) #zero-pad day to 3 digits
FUNFONT_GR=FUNFONT_G.get_rect()
FUNFONT_GR.center=(625, 625)

#Health
HEALTHFONT = FONT_HUD.render("Health:{0:03}".format(health),1, BLACK) #zero-pad day to 3 digits
HEALTHFONTR=HEALTHFONT.get_rect()
HEALTHFONTR.center=(366, 625)

HEALTHFONT_R = FONT_HUD.render("Health:{0:03}".format(health),1, RED) #zero-pad day to 3 digits
HEALTHFONT_RR=HEALTHFONT_R.get_rect()
HEALTHFONT_RR.center=(365, 625)

HEALTHFONT_G = FONT_HUD.render("Health:{0:03}".format(health),1, GREEN) #zero-pad day to 3 digits
HEALTHFONT_GR=HEALTHFONT_G.get_rect()
HEALTHFONT_GR.center=(365, 625)

#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 (PURPLE)
        else:
            return (YELLOW)

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

class Cube_black(pygame.sprite.Sprite):
    def __init__(self):
        super(Cube_black, self).__init__()
        self.images = []
        self.images.append(load_image('Cube_black.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 Eye_black(pygame.sprite.Sprite):
    def __init__(self):
        super(Eye_black, self).__init__()
        self.images = []
        self.images.append(load_image('Eye_grey.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 Mood_good(pygame.sprite.Sprite):
    def __init__(self):
        super(Mood_good, self).__init__()
        self.images = []
        self.images.append(load_image('Good.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 Mood_fine(pygame.sprite.Sprite):
    def __init__(self):
        super(Mood_fine, 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]

cube_color = Cube_black()
cube = pygame.sprite.Group(cube_color)

eye_color = Eye_black()
eye = pygame.sprite.Group(eye_color)

mood_feeling_g = Mood_good()
mood_good = pygame.sprite.Group(mood_feeling_g)

mood_feeling_f = Mood_fine()
mood_fine = pygame.sprite.Group(mood_feeling_f)

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))]
pause = [Menu("PAUSE GAME", (140, 105))]
continue_game = [Menu("CONTINUE", (140, 55))]

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

SCREEN.fill(WHITE)
while not done:

    if pause == False:
        for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        pause = True

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

            SCREEN.fill(GREY)

            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()

            cube.update()
            cube.draw(SCREEN)

            eye.update()
            eye.draw(SCREEN)

            mood_good.update()
            mood_good.draw(SCREEN)

            if blink == 1:
                blinking.update()
                blinking.draw(SCREEN)

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

            blankgroup.update()
            blankgroup.draw(SCREEN)

    if pause == True:
        game_start == False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            SCREEN.fill(GREY)

            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 continue_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()

            cube.update()
            cube.draw(SCREEN)

            eye.update()
            eye.draw(SCREEN)

            mood_good.update()
            mood_good.draw(SCREEN)

            if blink == 1:
                blinking.update()
                blinking.draw(SCREEN)

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

            blankgroup.update()
            blankgroup.draw(SCREEN)

    if game_start == True:
        menu_on = False

        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_p:
                     pause = True
                 else: pause = False

            if event.type == pygame.QUIT:
                done = True

            elif event.type == CLOCKTICK:
                minute = minute + 1
                if minute == 60:
                    hour = hour + 1
                    minute = 0
                if minute < 60:

                    if hunger > 0:
                        hunger = hunger - 2
                    else: hunger = hunger

                    if fun > 0:
                        fun = fun - 2
                    else: fun = fun

                    if health > 0:
                        health = health - 1
                    else: health = health

                if hour == 24:
                    day = day + 1
                    hour = 0
                if day == 365:
                    year = year + 1
                    day = 0

                blink_clock = blink_clock + 1
                if blink_clock == 1:
                    blink_clock = 0
                    blink = random.randint(0, 1)
                if blink_clock == 1:
                    blink = 0


            SCREEN.fill(WHITE)

            cube.update()
            cube.draw(SCREEN)

            eye.update()
            eye.draw(SCREEN)

            if feeling >= 4:
                mood_good.update()
                mood_good.draw(SCREEN)

            if feeling == 3 :
                mood_fine.update()
                mood_fine.draw(SCREEN)

            if blink == 1:
                blinking.update()
                blinking.draw(SCREEN)

            blankgroup.update()
            blankgroup.draw(SCREEN)

            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)

            #Hunger


            HUNGERFONT = FONT_HUD.render("Hunger:{0:03}".format(hunger),1, BLACK)
            SCREEN.blit(HUNGERFONT, HUNGERFONTR)

            if hunger >= 75:
                HUNGERFONT_G = FONT_HUD.render("Hunger:{0:03}".format(hunger),1, GREEN)
                SCREEN.blit(HUNGERFONT_G, HUNGERFONT_RR)
                feeling = feeling + 1
            if hunger <= 25:
                HUNGERFONT_R = FONT_HUD.render("Hunger:{0:03}".format(hunger),1, RED)
                SCREEN.blit(HUNGERFONT_R, HUNGERFONT_GR)
                feeling = feeling - 1
            if hunger == 74:
                feeling = feeling -1

            #Fun

            FUNFONT = FONT_HUD.render("Fun:{0:03}".format(fun),1, BLACK)
            SCREEN.blit(FUNFONT, FUNFONTR)

            if fun >= 75:
                FUNFONT_G = FONT_HUD.render("Fun:{0:03}".format(fun),1, GREEN)
                SCREEN.blit(FUNFONT_G, FUNFONT_RR)
                feeling = feeling + 1
            if fun <= 25:
                FUNFONT_R = FONT_HUD.render("Fun:{0:03}".format(fun),1, RED)
                SCREEN.blit(FUNFONT_R, FUNFONT_GR)

            if fun == 74:
                feeling = feeling -1

            #Health

            HEALTHFONT = FONT_HUD.render("Health:{0:03}".format(health),1, BLACK)
            SCREEN.blit(HEALTHFONT, HEALTHFONTR)

            if health >= 75:
                HEALTHFONT_G = FONT_HUD.render("Health:{0:03}".format(health),1, GREEN)
                SCREEN.blit(HEALTHFONT_G, HEALTHFONT_RR)
                feeling = feeling + 1
            if health <= 25:
                HEALTHFONT_R = FONT_HUD.render("Health:{0:03}".format(health),1, RED)
                SCREEN.blit(HEALTHFONT_R, HEALTHFONT_GR)
                feeling = feeling - 1

            if health == 74:
                feeling = feeling -1
                print(event)

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

pygame.quit()

1 个答案:

答案 0 :(得分:0)

暂停应该来自一个事件。

你想要按键'P',对吗?

paused = False
while game_loop:
    for event in pygame.event.get():
        if event.type==KEYDOWN:
            if event.key=='P':
                paused = not paused
        //other events
    if paused:
        //draw pause menu here
        pause() //This function will have its own pause loop running until
                //you select an item in the pause menu 
                //and will set paused = not paused on exit
    else:
        //draw main game here

这是通过添加到主事件队列来实现的。始终在其中添加事件。

你也可以查看这个蛇游戏。它有暂停菜单。按ESC键即可!

编辑:对不起,我没有提供蛇游戏的任何链接。 https://github.com/sk364/fun-snake-attack/blob/master/game.py#L508

然后,您可以创建一个暂停菜单,该菜单将有自己的事件循环