import pygame
pygame.init()
screen_width = 480
screen_height = 360
player_x = screen_width/2
player_y = screen_height/2
player_width = 4
player_height = 4
player = pygame.Rect((player_x, player_y, player_width, player_height))
screen = pygame.display.set_mode((screen_width, screen_height))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.K_w:
player_y = player_y + 4
if event.type == pygame.K_s:
player_y = player_y - 4
if event.type == pygame.K_d:
player_x = player_x + 4
if event.type == pygame.K_a:
player_x = player_x - 4
if event.type == pygame.QUIT:
run = False
player = pygame.Rect((player_x, player_y, player_width, player_height))
pygame.draw.rect(screen, (255, 0, 0), player)
pygame.display.update()
pygame.quit()
#๐ Hi, does anyone know why my player doesnt move?
11 messages ยท Page 1 of 1 (latest)
Hey @timber estuary!
Please edit your message to use a code block
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
@timber estuary
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.
Closes after a period of inactivity, or when you send !close.
do you get any errors or anything?
the error is that pygame events can only detect if a key has been pressed, not what key has been pressed
to do that, you'd use pygame.key.get_pressed(), then checking it using the key
import pygame
pygame.init()
screen_width = 480
screen_height = 360
player_x = screen_width/2
player_y = screen_height/2
player_width = 4
player_height = 4
player = pygame.Rect((player_x, player_y, player_width, player_height))
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player_y -= 1
if keys[pygame.K_s]:
player_y += 1
if keys[pygame.K_a]:
player_x -= 1
if keys[pygame.K_d]:
player_x += 1
screen.fill((0,0,0))
player = pygame.Rect(player_x, player_y, player_width, player_height)
pygame.draw.rect(screen, (255,0,0), player, 0)
pygame.display.update()
clock.tick(60)
pygame.quit()```
I added a clock so it doesnt refresh out of control
I also added the fill so that the old position is removed
@timber estuary incase you didnt see it
the thing you were doing wrong is event.type == pygame.K_w you should've done event.type == pygame.KEY_DOWN and then event.key == pygame.K_ESCAPE for example
but Ryan studio made a better one, because this event for pressing a key just detects a keypress, it doesn't move the player too much.
This help channel has been closed. 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.