1

I tried adding a green block(zombie) that would follow the red block(player), but the player position updates and the zombie position won't, I have just began using python and I don't understand why it happens, thanks in advance.

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

player = pygame.Rect(0, 0, 20, 20)
player.center = window.get_rect().center
playerVel = 3

zombie = pygame.Rect(0, 0, 20, 20)
zombieVel = 2

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            print(pygame.key.name(event.key))

    keys = pygame.key.get_pressed()
    
    player.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * playerVel
    player.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * playerVel
        
    player.centerx = player.centerx % window.get_width()
    player.centery = player.centery % window.get_height()

    if zombie.x > player.x:
      zombie.x =- zombieVel

    if zombie.x < player.x:
      zombie.x =+ zombieVel

    if zombie.y > player.y:
      zombie.y =- zombieVel

    if zombie.y < player.y:
      zombie.y =+ zombieVel

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), player)
    pygame.draw.rect(window, (0, 255, 0), zombie)
    pygame.display.flip()


#https://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame

pygame.quit()
exit()
DeMauro
  • 27
  • 5

2 Answers2

1
if zombie.x > player.x:
      zombie.x =- zombieVel

    if zombie.x < player.x:
      zombie.x =+ zombieVel

    if zombie.y > player.y:
      zombie.y =- zombieVel

    if zombie.y < player.y:
      zombie.y =+ zombieVel

The operators sohould be += and -= instead of =+ =- i think

Swagnar
  • 127
  • 11
-1

I found the issue:

You just made an error adding the Zombie velocity

here is the updated code that works just fine

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

player = pygame.Rect(0, 0, 20, 20)
player.center = window.get_rect().center
playerVel = 3

zombie = pygame.Rect(0, 0, 20, 20)
zombieVel = 2

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            print(pygame.key.name(event.key))

    keys = pygame.key.get_pressed()

    player.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * playerVel
    player.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * playerVel

    player.centerx = player.centerx % window.get_width()
    player.centery = player.centery % window.get_height()
    # changed those lines
    #################################################
    if zombie.x > player.x: zombie.x -= zombieVel 

    else: zombie.x += zombieVel

    if zombie.y > player.y: zombie.y -= zombieVel

    else: zombie.y += zombieVel
    #################################################    

    print(zombie.x, zombie.y)
    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), player)
    pygame.draw.rect(window, (0, 255, 0), zombie)
    pygame.display.flip()


# https://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame

pygame.quit()
exit()

Tip if you want to add a value to a variable you can do it so var += 1 which is the short variant of var = var +1.

The problem with your code is that you put the plus or minus behind the equal sign, not in front. That's it such a simple Error. This error results in that that your zombie.x is calculated like this zombie.x = 0 + zombieVel same goes for the y value