【问题标题】:Python PyGame RestartPython PyGame 重启
【发布时间】:2020-08-22 18:24:13
【问题描述】:

所以我正在学习 python,我完成了游戏,一切正常,但现在我不知道如何重新启动游戏,我的意思是当游戏结束时下一步是什么?如何重新开始 我会为我的完整代码添加一个link,这是迄今为止所做的。

非常感谢!

if Bullet_state is "fire":
        fire(BulletX, BulletY)
        BulletY -= Bullet_MovementY
    player(playerX, playerY)
    score(textX, textY)
    pygame.display.update()

【问题讨论】:

    标签: python python-3.x python-2.7 python-requests


    【解决方案1】:

    对于重启逻辑,保持当前游戏循环,但添加一个标志来表示游戏已经结束。在循环中,检查此标志并在设置标志时显示重新启动提示。还将初始化过程封装在一个函数中,以便在每次重启时调用它。

    请注意,我在 Python 3.8 中对此进行了测试。

    试试这个代码。当分数达到3时,游戏结束并显示重启提示。

    import pygame
    import random
    import math
    
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('Space Invadors')
    icon = pygame.image.load('1.png')
    pygame.display.set_icon(icon)
    #Background
    Background = pygame.image.load('background.png')
    
    def SetGameStart():    # initialize variables
        global Player_Image,playerX,playerY,Key_Movement,Enemy_Image,EnemyX,EnemyY,Enemy_MovementX,Enemy_MovementY
        global Bullet_Image,BulletX,BulletY,Bullet_MovementX,Bullet_MovementY,Bullet_state,Score
        # Player
        Player_Image = pygame.image.load('player.png')
        playerX = 370
        playerY = 480
        Key_Movement = 0
        # Enemy
        Enemy_Image = pygame.image.load('monster2.png')
        EnemyX = random.randint(0, 735)
        EnemyY = random.randint(50, 150)
        Enemy_MovementX = 1.5
        Enemy_MovementY = 40
        # Bullet
        Bullet_Image = pygame.image.load('bullet.png')
        BulletX = 0
        BulletY = 480
        Bullet_MovementX = 0
        Bullet_MovementY = 10
        Bullet_state = "Ready"
    
        Score = 0
    
    def Fire(x, y):
        global Bullet_state
        Bullet_state = "Fire"
        screen.blit(Bullet_Image, (x + 16, y + 10))
    
    
    def player(x, y):
        # blit is draw! draw an image on the screen
        screen.blit(Player_Image, (x, y))
    
    
    def Enemy(x, y):
        screen.blit(Enemy_Image, (x, y))
    
    def is_Collided(BulletX, BulletY, EnemyX, EnemyY):
        distance = math.sqrt(math.pow(BulletX - EnemyX, 2) + math.pow(BulletY - EnemyY, 2))
        if distance < 27:
            return True
        else:
            return False
            
    
    SetGameStart()  # initialize variables
    GameDone = False
    
    def text_objects(text, font):
        textSurface = font.render(text, True, (0,200,0))
        return textSurface, textSurface.get_rect()
    
    Close = True
    while Close:
        screen.fill((0, 0, 0))
        screen.blit(Background, (0, 0))
    
        if GameDone:  # pause game loop, show restart prompt
            largeText = pygame.font.SysFont("comicsansms",20)
            TextSurf, TextRect = text_objects("Game Over.    Press Space to restart.", largeText)
            TextRect.center = ((800/2),(600/2))
            screen.blit(TextSurf, TextRect)
            pygame.display.update() 
            for event in pygame.event.get():
               if event.type == pygame.QUIT:
                   Close = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:  # press Q to quit
                    Close = False
                    break
                if event.key == pygame.K_SPACE:  # press space to restart
                    GameDone = False
                    SetGameStart()  # initialize variables
            continue  # skip game process
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                Close = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    Key_Movement = -4
                if event.key == pygame.K_RIGHT:
                    Key_Movement = 4
                if event.key == pygame.K_SPACE:
                    # to fix the Pressing on space will reload the Bullet
                    if Bullet_state is "Ready":
                        BulletX = playerX
                        Fire(BulletX, playerY)
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    Key_Movement = 0
        playerX += Key_Movement
        if playerX <= 0:
            playerX = 0
        elif playerX >= 736:
            playerX = 736
        # Enemy movement limit
        EnemyX += Enemy_MovementX
        if EnemyX <= 0:
            Enemy_MovementX = 1.5
            EnemyY += Enemy_MovementY
        elif EnemyX >= 736:
            Enemy_MovementX = -1.5
            EnemyY += Enemy_MovementY
        if BulletY <= 0:
            BulletY = 480
            Bullet_state = "Ready"
        if Bullet_state is "Fire":
            Fire(BulletX, BulletY)
            BulletY -= Bullet_MovementY
        Collision = is_Collided(EnemyX, EnemyY, BulletX, BulletY)
        if Collision:
            BulletY = 480
            Bullet_state = "Ready"
            Score += 1
            print(Score)
            EnemyX = random.randint(0, 735)
            EnemyY = random.randint(50, 150)
        Enemy(EnemyX, EnemyY)
        player(playerX, playerY)
        pygame.display.update() 
        if Score == 3:
            GameDone = True
    

    提示

    【讨论】:

    • 非常感谢 Mike,我可以请教你一些编程方面的技巧吗?我刚开始使用python,我有点不知道该做什么或如何做好,并理解和知道我需要做什么,再次感谢你:D
    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多