Answer the question
In order to leave comments, you need to log in
Why doesn't pygame detect keystrokes on the keyboard?
I tried to move the character, but
the
pygame
does
not
see
the
button .load('img.png')
bg = pygame.image.load('bg.png')
playmusic = True
if(playmusic == True):
pygame.mixer.music.load('02629.mp3')
pygame. mixer.music.play(-1)
if(playmusic == False):
pygame.mixer.music.stop('02629.mp3')
x = 50
y = 50
bgx = 0
bgy = 0
speed = 5
win.blit( bg, (bgx, bgy))
win.blit(player, (x, y))
pygame.display.update()
clock = pygame.time.Clock()
run = True
while run:
clock.tick(30)
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sys.exit()
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
x -= speed
elif key[pygame.K_RIGHT] and x < 500:
x += speed
if key[pygame. K_UP]:
y += speed
elif key[pygame.K_DOWN]:
y -= speed
pygame.quit()
Answer the question
In order to leave comments, you need to log in
You need to draw the object in the main while loop. And before that, paint over the screen.
And it would be nice if next time you put the code as code, and not as text, so that the indentation is preserved and there is syntax highlighting. It's so hard to understand your code.
in the menu above the text field 8 item with the following signs: >
- choose
python
import pygame
import sys
x = 50
y = 50
speed = 5
run = True
clock = pygame.time.Clock()
pygame.init()
win = pygame.display.set_mode((500, 500))
image = pygame.image.load('img.png').convert_alpha()
rect = image.get_rect()
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key = pygame.key.get_pressed()
if key[pygame.K_a]:
x += -speed
if key[pygame.K_d]:
x += speed
if key[pygame.K_w]:
y += -speed
if key[pygame.K_s]:
y += speed
win.fill('white')
rect.x = x
rect.y = y
win.blit(image, rect)
pygame.display.update()
clock.tick(30)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question