I just started writing code for fun and to learn something new, however I’ve run into this issue and would like some specific input as to what I’m doing wrong. At this point in my project I’m trying to make an image move left and right with the arrow keys. I’ve gotten the image to display properly but it does not move in response to pressing the keys. Any input would be welcome.
`import pygame, sys, random
pygame.init()
clock = pygame.time.Clock()
screen_width = 1280
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Invader slim")
surface = screen.get_rect()
DIS = (100, 100)
pygame.image.load('sprite_soldier.png')
player_image = pygame.image.load('sprite_soldier.png').convert_alpha()
player_soldier = pygame.transform.scale(player_image, DIS)
player = screen.blit(player_soldier, (screen_width/2 - 10, screen_height - 100))
bg_color = pygame.Color(0, 100, 100)
green = pygame.Color(0, 255, 0)
white = pygame.Color('white')
light_gray = pygame.Color(200, 200, 200)
torpedo = pygame.Rect(screen_width/2 - 10, screen_height - 100, 20, 20)
player_x = player.x
move_left = False
move_right = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
move_right = True
if event.key == pygame.K_LEFT:
move_left = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
move_right = False
if event.key == pygame.K_LEFT:
move_left = False
if move_right:
player_x += 10
elif move_left:
player_x -= 10
if player.x >= screen_width:
move_right = False
if player.x <= 0:
move_left = False
pygame.display.flip()
clock.tick(60)
>Solution :
You missed to draw the player at its new position in the application loop:
while True:
# [...]
screen.fill(0)
player = screen.blit(player_soldier, (player_x, screen_height - 100))
pygame.display.flip()
clock.tick(60)
Use pygame.key.get_pressed() instead of the KEYDOWN event:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
player_x += 10 * (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT])
screen.fill(0)
player = screen.blit(player_soldier, (player_x, screen_height - 100))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
The typical PyGame application loop has to:
- limit the frames per second to limit CPU usage with
pygame.time.Clock.tick - handle the events by calling either
pygame.event.pump()orpygame.event.get(). - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blitall the objects) - update the display by calling either
pygame.display.update()orpygame.display.flip()
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement
Complete example:
import pygame, sys, random
pygame.init()
clock = pygame.time.Clock()
screen_width = 1280
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Invader slim")
surface = screen.get_rect()
DIS = (100, 100)
player_image = pygame.image.load('sprite_soldier.png').convert_alpha()
player_soldier = pygame.transform.scale(player_image, DIS)
player_rect = player_soldier.get_rect(midbottom = (screen_width/2, screen_height))
bg_color = pygame.Color(0, 100, 100)
green = pygame.Color(0, 255, 0)
white = pygame.Color('white')
light_gray = pygame.Color(200, 200, 200)
torpedo = pygame.Rect(screen_width/2 - 10, screen_height - 100, 20, 20)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
player_rect.x += 10 * (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT])
player_rect.clamp_ip(surface)
screen.fill(0)
player = screen.blit(player_soldier, player_rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()