简体   繁体   English

变量和Surface在Python / Pygame中不会更新

[英]Variables and Surface won't Update in Python/Pygame

I am trying to make my game have special abilities for the player, but am having some issues. 我试图使我的游戏对玩家具有特殊的能力,但存在一些问题。 For some reason user_health_active and user_health_display_active do not update after reaching round 10. I can't figure out why this is and have been attempting to for about two hours. 由于某种原因,第10轮后user_health_active和user_health_display_active不会更新。我不知道为什么会这样,并且已经尝试了大约两个小时。 By the seems of it, not only does the surface not update, but the actual background function doesn't either. 从表面上看,不仅表面不会更新,而且实际的背景功能也不会更新。 If anyone can provide me some insight on why this isn't working, please let me know. 如果有人可以为我提供一些无法解决问题的见解,请告诉我。 Here is my code. 这是我的代码。

"""
Game Developer: Austin H.
Game Owner: Austin H.
Licensed Through: theoiestinapps
Build: 2
Version: 1.0.1
"""

import os
import pygame as pygame
import random
import sys
import time

pygame.init()

left = False
right = False
playerDead = False
devMode = False

game_completed = False
game_level_score = (0)
game_display_score = (0)
enemy_speed = (5)

user_teleport_active = False
user_health_active = False
user_teleport_display_active = ("False")
user_health_display_active = ("False")

display_width = 800
display_height = 600

customBlue = (17, 126, 194)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
white = (255, 255, 255)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Space Dodge")
clock = pygame.time.Clock()

backgroundMusic = pygame.mixer.music.load('C:/Program Files/Space Dodge/game_background_music.mp3')
enemyImg = pygame.image.load('C:/Program Files/Space Dodge/enemy_image.png')
backgroundImg = pygame.image.load('C:/Program Files/Space Dodge/background_image.png')
rocketImg = pygame.image.load('C:/Program Files/Space Dodge/player_image.png')
injuredSound = pygame.mixer.Sound('C:/Program Files/Space Dodge/player_hurt_sound.wav')

def teleport_powerup(user_teleport_display_active):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Teleport Powerup: " + str(user_teleport_display_active), True, red)
    gameDisplay.blit(text, (display_width - 205, 5))
def ehealth_powerup(user_health_display_active):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Ehealth Powerup: " + str(user_health_display_active), True, red)
    gameDisplay.blit(text, (display_width - 205, 25))

def enemies_dodged(enemy_objects_dodged):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodge Asteroids: " + str(enemy_objects_dodged), True, green)
    gameDisplay.blit(text, (5, 5))
def game_level(game_display_score):
    font = pygame.font.SysFont(None, 25)
    game_display_score = game_level_score + 1
    text = font.render("Game Level: " + str(game_display_score), True, green)
    gameDisplay.blit(text, (5, 25))
def enemies(enemyx, enemyy, enemyw, enemyh, color):
    pygame.draw.rect(gameDisplay, color, [enemyx, enemyy, enemyw, enemyh])

def rocket(x, y):
    gameDisplay.blit(rocketImg, (x, y))
def background(cen1, cen2):
    gameDisplay.blit(backgroundImg, (cen1, cen2))

def text_objects(text, font):
    textSurface = font.render(text, True, blue)
    return textSurface, textSurface.get_rect() 
def message_display(text):
    global game_completed
    largeText = pygame.font.Font('freesansbold.ttf', 70)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    if game_completed == True:
        time.sleep(300)

    else:
        time.sleep(5)
        if game_level_score > 0:
            pass
        else:
            pygame.mixer.music.play()
        game_loop()

def crash():
    injuredSound.play()
    message_display("You Died. Game Over!")

def game_loop():
    global left
    global right
    global playerDead
    global game_level_score
    global enemy_speed
    global game_completed

    global user_teleport_active
    global user_teleport_display_active
    global user_health_active
    global user_health_display_active

    x = (display_width * 0.43)
    y = (display_height * 0.74)
    cen1 = (0)
    cen2 = (0)
    x_change = 0

    rocket_width = (86)
    game_score = (0)
    enemy_objects_dodged = (0)

    enemy_startx = random.randrange(0, display_width)
    enemy_starty = -600
    enemy_width = 100
    enemy_height = 100

    while not playerDead:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.mixer.music.stop()
                playerDead = True

            if devMode == True:
                print(event)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    left = True
                if event.key == pygame.K_d:
                    right = True

                if event.key == pygame.K_LEFT:
                    left = True
                if event.key == pygame.K_RIGHT:
                    right = True

                if event.key == pygame.K_KP4:
                    left = True
                if event.key == pygame.K_KP6:
                    right = True

                if event.key == pygame.K_ESCAPE:
                    playerDead = True

                if event.key == pygame.K_SPACE:
                    game_level_score += 1

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or pygame.K_d:
                    left = False
                if event.key == pygame.K_d:
                    right = False

                if event.key == pygame.K_LEFT:
                    left = False
                if event.key == pygame.K_RIGHT:
                    right = False

                if event.key == pygame.K_KP4:
                    left = False
                if event.key == pygame.K_KP6:
                    right = False

                if event.key == pygame.K_SPACE:
                    pass

        if left and right:
            x_change *= 1
        elif left and x > -86:
            x_change = -5
        elif right and x < (display_width - 89):
            x_change = 5
        else:
            x_change = 0

        if game_score == 10:
            enemy_speed += 0.5
            game_level_score += 1

            if game_level_score == 49:
                game_completed = True
                message_display('Game Complete!')

            else:
                message_display('Levels Completed: %s' % game_level_score)

        if game_level_score > 4:
            user_teleport_active = True
            user_teleport_display_active = ("True")
        elif game_level_score > 9:
            user_health_active = True
            user_health_display_active = ("True")

        if user_teleport_active == True:

            if x < -0:
                x = 850

        if enemy_starty > display_height:
            enemy_starty = 0 - enemy_height
            enemy_startx = random.randrange(0, display_width)
            game_score += 1
            enemy_objects_dodged += 1

        if y < enemy_starty + enemy_height:

            if x > enemy_startx and x < enemy_startx + enemy_width or x + rocket_width > enemy_startx and x + rocket_width < enemy_startx + enemy_width:
                pygame.mixer.music.stop()
                game_level_score = (0)
                user_teleport_active = False
                user_teleport_display_active = ("False")
                crash()

        x += x_change

        background(cen1, cen2)
        enemies(enemy_startx, enemy_starty, enemy_width, enemy_height, customBlue)
        enemy_starty += enemy_speed
        rocket(x, y)
        enemies_dodged(enemy_objects_dodged)
        game_level(game_display_score)
        teleport_powerup(user_teleport_display_active)
        ehealth_powerup(user_health_display_active)
        pygame.display.update()
        clock.tick(90)

pygame.mixer.music.set_volume(0.20)
pygame.mixer.music.play(-1)        
game_loop()
pygame.quit()
quit()

The code that modifies the variables you mention will never be run. 修改您提到的变量的代码将永远不会运行。 Here's the relevant bit: 这是相关的位:

    if game_level_score > 4:
        user_teleport_active = True
        user_teleport_display_active = ("True")
    elif game_level_score > 9:
        user_health_active = True
        user_health_display_active = ("True")

Because you're using an if and an elif , the condition for the second block is only ever tested if the first block's condition was false. 因为您使用的是ifelif ,所以只有在第一个块的条件为false时,才会测试第二个块的条件。 Since any value greater than 9 is also going to be greater than 4 , the second block will never run. 由于任何大于9值也将大于4 ,因此第二个块将永远不会运行。

If you want the two conditions to be tested independently, you need to just use plain if statements for both conditions. 如果要两个条件独立测试,则只需要对两个条件使用纯if语句。 If you only want one block to run , you either need to extend the first condition to 4 < game_level_score <= 9 or you need to change the order so that the > 9 test comes before the > 4 test. 如果只希望运行一个块,则需要将第一个条件扩展为4 < game_level_score <= 9或者需要更改顺序,以使> 9测试先于> 4测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM