为什么我的死亡矩形没有向 python pygame 移动

Why is my death rectangle not moving in python pygame

所以我正在制作一款游戏,其中包含可以向前冲刺的关卡,而我对关卡 3 的想法是一个移动的死亡广场。它向左和向右移动 100 像素,但我无法让它工作。这是我的代码:

import pygame
import keyboard as k
import sys

# Bools
r = True
canPressSpace = True

# Floats

# Ints
level = 1
playerX = 50.0
playerY = 350.0

# Lists
deaths = {
    "1": 0,
    "2": 0,
    "3": 0,
}

# Strings

# Pygame events
resetCanPressSpace = pygame.USEREVENT

# Pygame init
pygame.init()
pygame.font.init()

# Other Pygame things
screen = pygame.display.set_mode((900,750))
font = pygame.font.SysFont("Arial", 30)

def resetPlayerPos():
    global playerX, playerY
    playerX = 50.0
    playerY = 350.0

def nextLevel():
    global level
    level += 1
    resetPlayerPos()
while r:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            r = False
        if event.type == resetCanPressSpace:
            canPressSpace = True
    screen.fill((55,55,55))
    if level == 1:
        pygame.draw.rect(screen, (255,0,0), (340, 0, 10, 750))
        pygame.draw.rect(screen, (0,255,0), (750,300,50,100))
        if playerX+25 > 339 and playerX+25 < 351:
            resetPlayerPos()
            deaths["1"] += 1
        elif playerX > 339 and playerX < 351:
            resetPlayerPos()
            deaths["1"] += 1
        elif playerX > 749 and playerX < 801:
            if playerY > 299 and playerY < 401:
                nextLevel()
            elif playerY+25 > 299 and playerY+25 < 401:
                nextLevel()
        elif playerX+25 > 749 and playerX+25 < 801:
            if playerY > 299 and playerY < 401:
                nextLevel()
            elif playerY+25 > 299 and playerY+25 < 401:
                nextLevel()
        if deaths["1"] > 5:
            hinttext = font.render("Press space to dash!", False, (0,255,0))
            screen.blit(hinttext, (75,200))
    elif level == 2:
        pygame.draw.rect(screen, (255,0,0), (340,0,150,750))
        pygame.draw.rect(screen, (0, 255, 0), (750, 300, 50, 100))
        if playerX > 339 and playerX < 491:
            resetPlayerPos()
            deaths["2"] += 1
        elif playerX > 749 and playerX < 801:
            if playerY > 299 and playerY < 401:
                nextLevel()
            elif playerY+25 > 299 and playerY+25 < 401:
                nextLevel()
        elif playerX+25 > 749 and playerX+25 < 801:
            if playerY > 299 and playerY < 401:
                nextLevel()
            elif playerY+25 > 299 and playerY+25 < 401:
                nextLevel()
    elif level == 3:
        pygame.draw.rect(screen, (0, 255, 0), (750, 300, 50, 100))
        if playerX > 749 and playerX < 801:
            if playerY > 299 and playerY < 401:
                nextLevel()
            elif playerY+25 > 299 and playerY+25 < 401:
                nextLevel()
        elif playerX+25 > 749 and playerX+25 < 801:
            if playerY > 299 and playerY < 401:
                nextLevel()
            elif playerY+25 > 299 and playerY+25 < 401:
                nextLevel()
        level3DeathRectX = 500.0
        level3DeathRectVector = "left"
        if level3DeathRectVector == "left":
            if level3DeathRectX != 400.0:
                level3DeathRectX -= 0.5
            else:
                level3DeathRectVector = "right"
        else:
            if level3DeathRectX != 600.0:
                level3DeathRectX += 0.5
            else:
                level3DeathRectVector = "left"
        pygame.draw.rect(screen, (255, 0, 0), (level3DeathRectX, 0, 100, 750))

    if k.is_pressed("w") and playerY > 0.0:
        facing = "up"
        playerY -= 0.4
    if k.is_pressed("s") and playerY < 726.0:
        facing = "down"
        playerY += 0.4
    if k.is_pressed("a") and playerX > 0.0:
        facing = "left"
        playerX -= 0.4
    if k.is_pressed("d") and playerX < 876.0:
        facing = "right"
        playerX += 0.4
    if k.is_pressed("SPACE") and canPressSpace:
        canPressSpace = False
        if facing == "up" and playerY > 200:
            playerY -= 200
        elif facing == "down" and playerY < 550:
            playerY += 200
        elif facing == "left" and playerX > 200:
            playerX -= 200
        elif facing == "right" and playerX < 700:
            playerX += 200
        pygame.time.set_timer(resetCanPressSpace, 500, loops=0)
    pygame.draw.rect(screen, (0,55,255), (playerX,playerY,25,25))
    deathtext = font.render(f"Deaths: {deaths[str(level)]}", False, (0,0,0))
    screen.blit(deathtext, (10,10))
    pygame.display.flip()





pygame.quit()
sys.exit()

level3DeathRectVector字符串是为了知道它必须移动的方向。但它根本不动。有人可以帮助我吗?

实际上矩形的位置和方向在每一帧都被重置。需要在应用循环之前初始化一次位置和方向,而不是一直在循环中:

level3DeathRectX = 500.0                        # <-- INSERT
level3DeathRectVector = "left"

while r:
    # [...]

    elif level == 3:
        # [...]

        # level3DeathRectX = 500.0              # <-- DELETE
        # level3DeathRectVector = "left"
        
        if level3DeathRectVector == "left":
            # [...]