#๐Ÿ”’ Pygame basic movement error

25 messages ยท Page 1 of 1 (latest)

gleaming vectorBOT
#

@frail vortex

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

frail vortex
#

Hi I am trying to make basic movement for a rectangle but why wont it move when I press the key corresponding to movement?

import pygame

pygame.init()

SCREEN_HEIGHT = 500
SCREEN_WIDTH = 600

screen = pygame.display.set_mode((SCREEN_HEIGHT, SCREEN_WIDTH))

run = True

player = pygame.Rect((250, 250, 50, 50))

while run:
    pygame.draw.rect(screen, (255, 0, 0), player)

    key = pygame.key.get_pressed()
    if key[pygame.K_a] == True:
        player.move(-1, 0)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.display.update()

pygame.quit()
#

Hi I am trying to make basic movement for a rectangle but why wont it move when I press the key corresponding to movement?

heavy sage
#

first of all, you dont need to check if this is true if key[pygame.K_a] == True

#

you can just have it as if key[pygame.K_a]

serene pilot
heavy sage
#

and also, you need to assign the player to the player.move

serene pilot
#

lgtm

heavy sage
#

so it would be player=player.move(-1, 0)

serene pilot
heavy sage
serene pilot
#

doesnt he also want pygame.Rect().move_ip()?

#

i see a missing screen refresh call

serene pilot
serene pilot
#

put that

heavy sage
#

his current code says player.move(-1, 0) when it should be player = player.move(-1, 0)

serene pilot
#

i see nothing wrong with the code

serene pilot
#

oh yeah

#

but i think he should move in place

heavy sage
#

yeah, could also do that

#

i was just giving a fix, based on what he provided

gleaming vectorBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.