1

Ok so im new to PyGame, and im making a space invader game, im currently building the movement script.

for some reason my character is infinitely moving to the right when i press the right key.. i know this must be a really simple solution i am just not seeing it lol i would appreciate an answer.

import pygame

pygame.init()   
screen = pygame.display.set_mode((800, 600))  



pygame.display.set_caption('Space Invader')  
icon = pygame.image.load('pepper.png')   
pygame.display.set_icon(icon)            


#Player
playerImg = pygame.image.load('sombreroship.png')
playerX = 370  
playerY = 480
playerX_Change = 0

def player (x, y): screen.blit(playerImg, (x, y) )      


running = True
while running:

# RGB  STANDS FOR RED, GREEN, BLUE   THE NUMBERS GOES MAX TO  255 FOR EXAMPLE_:

screen.fill((0, 0, 30))  # this will display yellow

for event in pygame.event.get():
    if event.type == pygame.QUIT:   #if we click the X the program ends
        running = False

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:

         playerX_Change = -0.1
    if event.key == pygame.K_RIGHT:

        playerX_Change = 0.1
if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or  event.key == pygame.K_RIGHT:

        playerX_Change = 0.1
playerX += playerX_Change
player(playerX, playerY)  

pygame.display.update() 
GS_TEC
  • 1
  • 1
  • 1
    Please explain what this line is meant to do: [if event.type == pygame.KEYUP:// if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:// playerX_Change = 0.1] To me this is saying when the key isn't pressed then move right. have you simply made a typo here? – The Grand J Mar 15 '21 at 03:26
  • oh nevermind you right, i just saw that lmao see how a little dumb thing can change your whole life thanks bro – GS_TEC Mar 15 '21 at 03:30
  • 1
    Why have you used 0.1 to stay still? isn't 0.1 to move right – The Grand J Mar 15 '21 at 03:31
  • anyway thanks againn you helped me fix this problem – GS_TEC Mar 15 '21 at 03:36

1 Answers1

1

I suggest to use pygame.key.get_pressed() instead of the key board events.

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:

import pygame

pygame.init()   
screen = pygame.display.set_mode((800, 600))  

pygame.display.set_caption('Space Invader')  
icon = pygame.image.load('pepper.png')   
pygame.display.set_icon(icon)            

#Player
playerImg = pygame.image.load('sombreroship.png')
playerX = 370  
playerY = 480

def player (x, y): 
    screen.blit(playerImg, (x, y) )      

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:   #if we click the X the program ends
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] :
        playerX += 0.1
    if keys[pygame.K_LEFT] :
        playerX -= 0.1

    # RGB  STANDS FOR RED, GREEN, BLUE   THE NUMBERS GOES MAX TO  255 FOR EXAMPLE_:
    screen.fill((0, 0, 30))  # this will display yellow
    player(playerX, playerY)  
    pygame.display.update() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174