功能无法结束并返回原始屏幕(pygame)

时间:2017-03-08 11:17:31

标签: python pygame

在其中一个功能中,如果按4键,它将结束该功能并返回原始屏幕。此代码将在按下4键时粘贴原始背景,但不允许任何WASD移动,只会重新启动该功能:

Turns = 100 #tried to implement a loop in order to be able to flip between the Player's turn and the Monster's turn
Player_Turn = True#it is the player's turn 
while Turns != 0:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if Player_Turn == True: #while it is the player's turn
            #use the number buttons to attack
                if event.key == K_1: #'attack' 
                    TextOnScreen("You attack!", (60, 90), 34) #text appears on the screen for 600 miliseconds
                    pygame.display.update() #updates screen
                    pygame.time.delay(700) #delay for the text to appear on screen
                    MonsterHP = MonsterHP - 2 #takes damage
                    print("MonsterHP: " + str(MonsterHP))
                    if MonsterHP <= 0: #monster is defeated
                        print("The monster is dead!")
                        TextOnScreen("The Enemy is dead! You win!", (60, 200), 34) #win message
                        pygame.time.delay(700) #text stays on screen
                        PlayerHP = PlayerHP #updates global variable
                        Combat_Mode_Start = False #combat mode ends
                        PlayerStep = 10 #resets player step value
                        PlayerSprite.speed = 5 #returns player speed to original value
                        PlayerSprite.nomove == False #sprite can move again
                        screen.blit(background, (0,0)) #blits original background
                        screen.blit(PlayerSprite.image, PlayerSprite.rect) #blits sprite in same position
                        pygame.display.update() #updates screen
                        break #breaks loop
                        return #ends function
                    Player_Turn = False #ends player's turn

                if event.key == K_2: #heal option
                    PlayerHP = PlayerHP + 10 #player regains health
                    TextOnScreen("You healed yourself!", (60, 90), 34)
                    pygame.time.delay(700) #text stays on screen for 700 miliseconds
                    pygame.display.update() #updates screen
                    print("PlayerHP: " + str(PlayerHP))
                    if PlayerHP > 15: #max health
                        TextOnScreen("You're at full health", (60,115), 34)
                        pygame.time.delay(700)
                        PlayerHP = 15 #value is reset to max health
                    Player_Turn = False #ends player's turn

                if event.key == K_3:#'item's option
                    screen.fill((232,232,232)) #fills the background with the grey
                    pygame.draw.rect(screen, (255,250,191), [150,50,400,300]) #the yellow box
                    pygame.draw.rect(screen, (168, 157, 0), [150,50,400,300], 4) #the darker outline
                    TextOnScreen("No items collected", (160, 70), 36) #no items for the duration of the project
                    pygame.display.update()
                    #square that appears on screen listing items - currently empty
                    pygame.time.delay(1000)#keeps square on screen for 1000 miliseconds
                    Combat_Mode(MonsterHP, PlayerHP)#screen is redrawn
                    Player_Turn = False #ends player's turn

                if event.key == K_4: #'flee' option - ends Combat Mode and returns to main screen
                    TextOnScreen("You escaped!", (60, 90), 34) #flee text
                    pygame.time.delay(900) #text stays on screen for 700 miliseconds
                    print("Combat Over")
                    PlayerHP = PlayerHP #updates global variable
                    Combat_Mode_Start = False #combat mode ends
                    PlayerSprite.speed = 5 #returns player speed to original value
                    PlayerSprite.canMove == True #sprite can move again
                    screen.blit(background, (0,0)) #blits original background
                    screen.blit(PlayerSprite.image, PlayerSprite.rect) #blits sprite in same position
                    pygame.display.update()#updates screen
                    break
                    return

最初调用该函数的代码:

    #the sprite movement in the main portion of the game
while True: #while the screen is up and the program is running
    for event in pygame.event.get(): #while an event takes place
        if event.type == KEYDOWN and PlayerSprite.canMove: #if a button is pressed and the sprite can move
            #actions based on which key is pressed - no diagonal movement
            if event.key == K_w: #sprite moves up
                PlayerSprite.moveup() #calls the moveup() function

            elif event.key == K_s: #sprite moves down
                PlayerSprite.movedown() #calls the movedown() function

            elif event.key == K_a: #sprite moves right
                PlayerSprite.moveright() #calls the moveright() function

            elif event.key == K_d: #sprite moves left
                PlayerSprite.moveleft() #calls the moveleft() function

            elif event.key == K_z: #brings up the player info
                pygame.draw.rect(screen, (255,255,255), [150,80,400,300]) #draws a box 400px by 300px on screen
                pygame.draw.rect(screen, (0,0,0), [150,80,400,300], 4) #outlines the box
                TextOnScreen("PlayerName", (160, 90), 36) #pastes player name on menu
                TextOnScreen(("HP:" + str(PlayerHP)),(160,110),36) #pastes text on screen showing Player's health
                TextOnScreen("EXP: 0",(160,130),36)#pastes text screen showing player experience points
                TextOnScreen("LVL: 1",(160,150),36) #pastes text on screen  showing player Level
                pygame.time.delay(1000) #text and box appear on screen for 1000 miliseconds
                pygame.display.update() #updates the screen

            PlayerStep = PlayerStep - 1 #PlayerStep decreases by 1
            pygame.event.pump() #event queue continues 
            pygame.display.update() #updates the screen


   #gives Player warning when PlayerStep is low 
    if PlayerStep < 5 and PlayerStep != 0: #when PlayerStep is low
        TextOnScreen("Wait!", (255,50), 30) #warning text
        pygame.event.pump() #event queue continues 
    elif PlayerStep <= 0 and Combat_Mode_Start == False: #causes Combat Mode when PlayerStep is 0
        PlayerSprite.nomove() #sprite cannot move
        Combat_Mode(7,15) #Combat Mode starts with the value for MonsterHP as 7 and the PlayerHP as 15
        Combat_Mode_Start = True
    pygame.display.update() #updates the screen

有没有办法打破现在的功能? 任何和所有的帮助将不胜感激!

0 个答案:

没有答案