I can't move my spaceship in my spaceinvader type video game in pygame
Question:
I set all my variable but when I press the arrow keys the ship doesn’t move.
code:
import pygame
# Internationalizing Pygame
pygame.init()
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('alien-ship-icon.png')
pygame.display.set_icon(icon)
# player
playerIMG = pygame.image.load('App-launch-spaceship-icon.png')
playerX = 370
playerY = 480
playerX_change = 0
screen = pygame.display.set_mode((800, 600))
def player(x, y):
"""
:param y:
:type x: object
"""
screen.blit(playerIMG, (x, y))
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check if ity is right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 1
# Indentation
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
I did all the recommendations in pycharm to fix my code and it says it has no problems. But my spaceship still doesn’t move.
Answers:
It is a matter of Indentation. You have draw the scene in the application loop, not in the event loop. The event loop is executed only when an event occurs, the application loop is executed once per frame:
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Indentation
#<--|
# if keystroke is pressed check if ity is right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# Indentation
#<--|
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
Also see How can I make a sprite move when key is held down
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5
screen.fill((222, 222, 222))
player(playerX, playerY)
pygame.display.update()
I set all my variable but when I press the arrow keys the ship doesn’t move.
code:
import pygame
# Internationalizing Pygame
pygame.init()
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('alien-ship-icon.png')
pygame.display.set_icon(icon)
# player
playerIMG = pygame.image.load('App-launch-spaceship-icon.png')
playerX = 370
playerY = 480
playerX_change = 0
screen = pygame.display.set_mode((800, 600))
def player(x, y):
"""
:param y:
:type x: object
"""
screen.blit(playerIMG, (x, y))
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check if ity is right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 1
# Indentation
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
I did all the recommendations in pycharm to fix my code and it says it has no problems. But my spaceship still doesn’t move.
It is a matter of Indentation. You have draw the scene in the application loop, not in the event loop. The event loop is executed only when an event occurs, the application loop is executed once per frame:
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Indentation
#<--|
# if keystroke is pressed check if ity is right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# Indentation
#<--|
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
Also see How can I make a sprite move when key is held down
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5
screen.fill((222, 222, 222))
player(playerX, playerY)
pygame.display.update()