如何在PyGame代码中添加开始和结束屏幕?

2024-06-26 00:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何开始或结束屏幕。我还在学游戏。如何将此添加到我的游戏中

我的完整代码太长,无法放在这里:

script

我有一个玩家的健康状况,这是我的伪代码:

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!

Tags: the代码游戏if屏幕玩家scriptload
2条回答

您可以使用if和else:

编辑

import time
page = 'startscreen'
while True:
  if page == 'startscreen':
      # display startscreen image
      if startbutton is clicked:
           page = 'game'
  elif page == 'game':
      #game code here

      if playerhealth <= -1:
          page = 'endscreen'
  elif page == 'endscreen':
      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

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

我给你我下面使用的按钮类

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

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

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

另外,当您的健康状况降至0以下(或任何其他情况)时,请务必调用主菜单。 希望这有帮助

编辑 要将整个代码放入函数中,请执行以下操作

def main_loop():
    global dependencies for edits
    while running:
        ....your code...

编辑2

下面是我的主菜单,请根据需要更改变量。还要确保用maingameloop函数替换main()

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))
    # pygame.mixer.music.load('background_music_wav.wav')
    # pygame.mixer.music.play(-1)
    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)) 

            screen.blit(font_large.render("Game-Name", True, (255, 255, 255)), (325, 50)) 
            screen.blit(font.render("Play", True, (0, 0, 0)), (417, 285)) 


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

main_menu()

相关问题 更多 >