有人可以帮我制作开始屏幕和结束屏幕吗?

问题描述 投票:0回答:2

我不确定如何制作开始屏幕或结束屏幕,我需要帮助来指导我!!我的代码很长,我仍在学习pygame。如果您有答案,请提供如何将其添加到代码中,并解释代码的工作原理,非常感谢!

我的完整代码它很长时间才能放在这里 script

like I have a player health  if the player health reaches -1 it should load a end screen 
if playerman.health > -1:
then load the end screen with some restart buttons!
python pygame
2个回答
0
投票

您应该使用if和else:

import time
while True:
  if playerhealth > -1:
      #game code here

  elif playerhealth == -1:
      endscreen = pygame.Surface((screenwidth, screenheight))
      display.blit(endscreen, (0, 0))
      pygame.display.flip()
      time.sleep(1)
      break
      # blit an endscreen as a surface on your display, wait 1 second and stop the game

0
投票

请确保将您的主代码放入函数中。创建另一个保留下一个主菜单屏幕的功能。启动游戏时,请调用主菜单屏幕,并确保在其上包含一个调用主游戏功能的按钮。

我正在为您提供我在下面使用的按钮类。

class Button():
    def __init__(self, color, x, y, width, height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self, win, outline=None):

        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 20)
            text = font.render(self.text, 1, (0, 0, 0))
            win.blit(text, (
                self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))

接下来创建如下的主菜单。

    def main_menu():
        setDefaults()
        pygame.display.set_caption("Main Menu")
        run = True
        bright_green = (0, 255, 0)
        green = (0, 200, 0)
        screen.fill((163, 163, 194))

设置默认值是将所有值转换为原始值的功能。在此基本主菜单之后,请确保使用我之前给您的按钮类添加一个按钮,并将其链接到您的主要功能或执行此操作。

    while run:
        mouse = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
                run = False

            if 400 + 100 > mouse[0] > 400 and 275 + 50 > mouse[1] > 275:
                pygame.draw.rect(screen, bright_green, (400, 275, 100, 50))

                if event.type == pygame.MOUSEBUTTONDOWN:
                    main()
            else:
                pygame.draw.rect(screen, green, (400, 275, 100, 50))

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.