Reputation: 11
I'm following this tutorial ( https://www.youtube.com/watch?v=FfWpgLFMI7w&list=PLuSWeF8J-0nBJv4hjjeUw5F2rxUdYy0tF&index=6 ) to create space invaders in pygame, using IDE: PyCharm (Community Edition 2022.3.2). I created movement code to make the spaceship move left and right, however, it only moves left and right when I move the mouse cursor in the game window. When pressing and holding left or right arrow, the spaceship doesnt move. When I move the mouse cursor in the window while pressing the key , then and only then it moves. I tried disabling mouse detection using pygame.event.set_blocked(pygame.MOUSEMOTION) but then the spaceship doesnt move at all when the key is pressed and held. The spaceship moves a tiny bit when I keep clicking the directional key (rapid tapping of key), but its supposed to be a smooth movement with pressing and holding the key.
Can someone help fix this? Thanks.
Heres the code: movement code towards bottom
import pygame
#Initialize the pygame
pygame.init()
#Create the screen (width by height)
#Top side and left side of screen is 0
screen = pygame.display.set_mode((800, 600))
#Title and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('spaceship.png')
playerX = 380
playerY = 480
playerX_change = 0
#Blit means to draw
def player(x,y):
screen.blit(playerImg, (x,y))
#Game loop
#Keeps game running, user clicks x to close out
running = True
while running:
# Values of RGB
# screen is drawn first, then draw player
screen.fill((0, 0, 128))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#pygame.event.set_blocked(pygame.MOUSEMOTION)
#If keystroke is pressed check whether left or right
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
#Updates playerX value to playerX_change value
playerX += playerX_change
#Calls function of player
player(playerX, playerY)
#Updates display score changing, bullets moving...
pygame.display.update()`
I created movement code to make the spaceship move left and right, however, it only moves left and right when I move the mouse cursor in the game window. When pressing and holding left or right arrow, the spaceship doesnt move. When I move the mouse cursor in the window while pressing the key , then and only then it moves. I tried disabling mouse detection using pygame.event.set_blocked(pygame.MOUSEMOTION) but then the spaceship doesnt move at all when the key is pressed and held. The spaceship moves a tiny bit when I keep clicking the directional key (rapid tapping of key), but its supposed to be a smooth movement with pressing and holding the key.
Upvotes: 1
Views: 74
Reputation: 210978
It is a matter of Indentation. You have to draw the scene in the application loop, not in the event loop. The application loop is executed once per frame, but the event loop is executed only when an event occurs (e.g. mouse move).
running = True
while running:
screen.fill((0, 0, 128))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
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
#<--|
#Updates playerX value to playerX_change value
playerX += playerX_change
#Calls function of player
player(playerX, playerY)
#Updates display score changing, bullets moving...
pygame.display.update()
The typical PyGame application loop has to:
pygame.time.Clock.tick
pygame.event.pump()
or pygame.event.get()
.blit
all the objects)pygame.display.update()
or pygame.display.flip()
Upvotes: 3