Search code examples
pythonpygamemovelevels

Why is my death rectangle not moving in python pygame


So i am making a game with levels where you can dash forward, and my idea for level 3 was a moving death square. It moves 100 pixels to the left and right, but i cant get it to work. Here is my code:

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()

The level3DeathRectVector string is for knowing what direction it has to move. But it doesnt move at all. Can someone help me?


Solution

  • Actually the position and the direction of the rectangle is reset in every frame. You need to initialize the position and direction once before the application loop, instead of continuously in the loop:

    level3DeathRectX = 500.0                        # <-- INSERT
    level3DeathRectVector = "left"
    
    while r:
        # [...]
    
        elif level == 3:
            # [...]
    
            # level3DeathRectX = 500.0              # <-- DELETE
            # level3DeathRectVector = "left"
            
            if level3DeathRectVector == "left":
                # [...]