import pygame
#initialize the screen
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 700))
#tile and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("spaceship.png")
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load("player.png")
playerx = 370
playery = 600
playerx_change = 0.39
def player(x,y):
screen.blit(playerImg, (x,y))
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#keystroke
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerx_change = -0.39
if event.key== pygame.K_RIGHT:
playerx_change = 0.39
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.type == pygame.K_RIGHT:
playerx_change = 0
playerx += playerx_change
player(playerx,playery)
pygame.display.update()
My spaceship keeps on moving even when i don't press side keys. i am creating space invaders using python and pygame.the ship doesn't stop as it should have according to the code. i use the community version of visual studio 2019.