Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

2 different lines of code doing the same thing returns a different output

So I was trying to implement basic movement into my "game" when I ran into an issue which I ended up solving, but not understanding why my previous solution didn’t work. At first I tried doing pygame.draw.rect(screen, character_color, character) where "character" was character = pygame.Rect(character_pos_y, character_pos_x, character_size_y, character_size_x) and it didn’t move. then I decided to do pygame.draw.rect(screen, character_color, (character_pos_y, character_pos_x, character_size_y, character_size_x)) and it worked. And it started working.

When I ran pygame.draw.rect(screen, character_color, character) my character position was changing in code, but not on the screen. Anyone knows why my previous solution didn’t work?

import pygame
import time
pygame.init()
#defining
ground_pos_x = 0
ground_pos_y = 400
ground_size_x = 500
ground_size_y = 50
ground_color = (0, 0, 0)
ground = pygame.Rect(ground_pos_x, ground_pos_y, ground_size_x, ground_size_y)
character_pos_x = 200
character_pos_y = 200
character_size_y = 100
character_size_x = 100
character_color = (50, 50, 50)
character = pygame.Rect(character_pos_y, character_pos_x, character_size_y, character_size_x)
fps = 60
clock = pygame.time.Clock()
background_colour = (111, 222, 111)
screen = pygame.display.set_mode((500, 500))

pygame.display.set_caption("test")
screen.fill(background_colour)

pygame.draw.rect(screen, character_color, character)
pygame.draw.rect(screen, ground_color, ground)
pygame.display.update()
running = True
while running:
    clock.tick(fps)
    screen.fill(background_colour)
    #movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        character_pos_x -= 10
    if keys[pygame.K_LEFT]:
        character_pos_y -= 10
    if keys[pygame.K_DOWN]:
        character_pos_x += 10
    if keys[pygame.K_RIGHT]:
        character_pos_y += 10

    pygame.draw.rect(screen, character_color, (character_pos_y, character_pos_x, character_size_y, character_size_x))
    pygame.draw.rect(screen, ground_color, ground)

    #if character.colliderect(ground):
    #    print("works")
    #else:
    #    print("nope")

    pygame.display.update()
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The position stored in character does not magically change when you change character_pos_x or character_pos_y. character is not somehow tied to character_pos_x and character_pos_y. You have to recreate the pygame.Rect object after changing character_pos_x or character_pos_y:

while running:
    # [...]

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        character_pos_x -= 10
    if keys[pygame.K_LEFT]:
        character_pos_y -= 10
    if keys[pygame.K_DOWN]:
        character_pos_x += 10
    if keys[pygame.K_RIGHT]:
        character_pos_y += 10

    character = pygame.Rect(character_pos_y, character_pos_x, character_size_y, character_size_x)
    pygame.draw.rect(screen, character_color, character)

Alternatively, you can also change character.x and character.y instead of character_pos_x or character_pos_y:

while running:
    # [...]

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        character.x -= 10
    if keys[pygame.K_LEFT]:
        character.y -= 10
    if keys[pygame.K_DOWN]:
        character.x += 10
    if keys[pygame.K_RIGHT]:
        character.y += 10

    pygame.draw.rect(screen, character_color, character)

Also note that your code can be simplified (also see How can I make a sprite move when key is held down):

while running:
    # [...]

    keys = pygame.key.get_pressed()
    character.x += (keys[pygame.K_RIGHT] - [pygame.K_LEFT]) * 10
    character.y += (keys[pygame.K_DOWN] - [pygame.K_UP]) * 10
    pygame.draw.rect(screen, character_color, character)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading