【问题标题】:Pygame quitting to gameOver screenPygame 退出到 gameOver 屏幕
【发布时间】:2015-11-15 00:28:33
【问题描述】:

python 新手,制作蛇游戏,但 Pygame 一直退出到屏幕,但我基本上希望能够随时使用 X(窗口的 x)按钮退出游戏。我已经尝试过调试,但由于某种原因,我在 PyCharm 中调试它时总是没有响应,gameOver 屏幕只假设在 bool 设置为 true 时运行,但它与我的游戏暂停代码发生冲突。非常感谢您的帮助!

这是代码: http://pastebin.com/eFMU3HH9

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    在gameOver屏幕中你运行第二个gameloop(),所以当你退出时你会从第二个gameloop()返回到第一个gameloop()(或者从第三个到第二个,等等)。

    【讨论】:

    • 我明白,但你知道如何解决这个问题吗?
    • 代替gameloop()复制行gameExit = Falseresume = False(行号从32到45)-pastebin.com/Hj7A2r0e
    • 谢谢你,我真的很感激。你的方法比我的好很多。
    【解决方案2】:

    可能是这样的:

    import pygame
    
    # Define some colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    
    pygame.init()
    
    # Set the height and width of the screen
    size = [700, 500]
    screen = pygame.display.set_mode(size)
    
    pygame.display.set_caption("Game Over Example")
    
    # Loop until the user clicks the close button.
    done = False
    
    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()
    
    # Starting position of the rectangle
    rect_x = 50
    rect_y = 50
    
    # Speed and direction of rectangle
    rect_change_x = 5
    rect_change_y = 5
    
    # This is a font we use to draw text on the screen (size 36)
    font = pygame.font.Font(None, 36)
    
    # Use this boolean variable to trigger if the game is over.
    game_over = False
    
    # -------- Main Program Loop -----------
    while not done:
    
        # --- Event Processing
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
    
            # We will use a mouse-click to signify when the game is
            # over. Replace this, and set game_over to true in your
            # own game when you know the game is over. (Like lives==0)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                game_over = True
    
        # --- Game Logic
    
        # Only move and process game logic if the game isn't over.
        if not game_over:
            # Move the rectangle starting point
            rect_x += rect_change_x
            rect_y += rect_change_y
    
            # Bounce the ball if needed
            if rect_y > 450 or rect_y < 0:
                rect_change_y = rect_change_y * -1
            if rect_x > 650 or rect_x < 0:
                rect_change_x = rect_change_x * -1
    
        # --- Draw the frame
    
        # Set the screen background
        screen.fill(BLACK)
    
        # Draw the rectangle
        pygame.draw.rect(screen, GREEN, [rect_x, rect_y, 50, 50])
    
        if game_over:
            # If game over is true, draw game over
            text = font.render("Game Over", True, WHITE)
            text_rect = text.get_rect()
            text_x = screen.get_width() / 2 - text_rect.width / 2
            text_y = screen.get_height() / 2 - text_rect.height / 2
            screen.blit(text, [text_x, text_y])
    
        else:
            # If game isn't over, draw this stuff.
            text = font.render("Click to end game", True, WHITE)
            text_rect = text.get_rect()
            text_x = screen.get_width() / 2 - text_rect.width / 2
            text_y = screen.get_height() / 2 - text_rect.height / 2
            screen.blit(text, [text_x, text_y])
    
        # Limit frames per second
        clock.tick(60)
    
        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
    
    pygame.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多