python-3.xpygamegame-physicsatari-2600

I want my player to move while the button is pressed


So when i do movement only at one side, it works but once i finish them all, it doesn't work. I tried to switch the numbers, commands, but it was useless. I'm a begginer and my skills are not too good for this because I was using tutorials before. i wanted to do something by myself and not using tutorials for that so i decided to aska quiestion

import pygame
import sys
from pygame.locals import *

clock = pygame.time.Clock()

pygame.init()

pygame.display.set_caption("Neverout")
player = pygame.image.load("player.png")

green = (181, 231, 29)

WINDOW_SIZE = (800, 800)

screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)

player_pos = [385, 50]

up = False #So here I have my movement variables
down = False
right = False
left = False

while True:

    screen.blit(player, player_pos)

    #Here i check if my movement variable are true after I press them

    if left is True:
        player_pos[0] -= 5
    if right is True:
        player_pos[0] += 5
    if up is True:
        player_pos[1] -= 5
    if down is True:
        player_pos[1] += 5

    for event in pygame.event.get():
        #here i check if the button is pressed
        if event.type == KEYDOWN:
            if pygame.K_LEFT:
                left = True
            if pygame.K_RIGHT:
                right = True
            if pygame.K_UP:
                up = True
            if pygame.K_DOWN:
                down = True
        #Here I check if a button is not pressed
        if event.type == KEYUP:
            if pygame.K_LEFT:
                left = False
            if pygame.K_RIGHT:
                right = False
            if pygame.K_UP:
                up = False
            if pygame.K_DOWN:
                down = False
        #Checks for quiting the app
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    #No dates only updates
    pygame.display.update()
    screen.fill(green)
    clock.tick(60)

Solution

  • You're code is almost correct, it's just checking the events incorrectly. You need to check event.key is equal to the key-code your checking.

    The existing code just says things like:

    if pygame.K_LEFT:
    

    But pygame.K_LEFT is just a number, like saying if 27: which is always True.

    Just add the event.key in there to fix it:

    if event.key == pygame.K_LEFT:
    

    Also don't forget to use elif (else if), as it's more efficient. If the key event is, say LEFT, it can't be RIGHT too, so there's no need to check any other codes. You can skip checking them with if .. elif.

    Applying these fixes to your main loop makes the player move OK:

    for event in pygame.event.get():
        #here i check if the button is pressed
        if event.type == KEYDOWN:
            if event.key == pygame.K_LEFT:
                left = True
            elif event.key == pygame.K_RIGHT:
                right = True
            elif event.key == pygame.K_UP:
                up = True
            elif event.key == pygame.K_DOWN:
                down = True
        #Here I check if a button is not pressed
        elif event.type == KEYUP:
            if event.key == pygame.K_LEFT:
                left = False
            elif event.key == pygame.K_RIGHT:
                right = False
            elif event.key == pygame.K_UP:
                up = False
            elif event.key == pygame.K_DOWN:
                down = False
        #Checks for quiting the app
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()