I want to hold down a button for constant movement, but KEYUP is reading true even if I don’t lift the key

I want to be able to hold the key down and move my little black dot, and then release the key and have it stop. However even if I hold the key down my if statement for key up is being executed. Here is my code:

manmovemaster = 0
l = 1
f1 = 0
from random import randint
from math import sin, cos, tan, pi
import pygame
from pygame.locals import*
Fps = pygame.time.Clock()
#starting variables
sw = 1200
sh = 650
r = 250
g = 250
b = 250
framerate = 40


#beginning of a man
man1x = 650
man1y = 50
mancolor=(0,0,0)
manlocation = (man1x, man1y)
Diamaterofhead = (5)
man1ymovedown = 0
man1ymoveup = False
man1xmoveright = False
man1xmoveleft = False


#game logic
while l:
    pygame.init()
    screen = pygame.display.set_mode((sw,sh))
    pygame.display.set_caption("WGD THE GAME")
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((r,g,b))



    man1 = pygame.draw.circle(background,mancolor,(man1x,man1y),Diamaterofhead)
    if man1ymovedown== True:
        man1y += 1
        manmovemaster = 0

    if man1ymoveup == True:
        man1y -= 1

    if man1xmoveright == True:
        man1x +=1

    if man1xmoveleft == True:
        man1x -=1



#screen refresh

    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                man1xmoveleft = True
            if event.key == pygame.K_RIGHT:
                man1xmoveright = True
            if event.key == pygame.K_UP:
                man1ymoveup = True
            if event.key == pygame.K_DOWN:
                man1ymovedown = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT: 
                man1xmoveleft = False
            if event.key == pygame.K_RIGHT:
                man1xmoveright = False
            if event.key == pygame.K_UP:
                man1ymoveup = False
            if event.key == pygame.K_DOWN:
                man1ymovedown = False

    else:
        screen.blit(background, (0,0))
        pygame.display.flip()
        pygame.time.get_ticks
        Fps.tick(framerate)

Answer

Use this instead:

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("WGD THE GAME")

Fps = pygame.time.Clock()

man1x = 200
man1y = 200

while True:
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    pygame.draw.circle(screen,(255, 255, 255),(man1x,man1y), 25)

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_LEFT]:
        man1x -= 1
    if pressed[pygame.K_RIGHT]:
        man1x += 1
    if pressed[pygame.K_UP]:
        man1y -= 1
    if pressed[pygame.K_DOWN]:
        man1y += 1

    pygame.display.flip()
    Fps.tick(60)

using pygame.key.get_pressed() instead of pygame.event.get() returns the state of the keys at all times, not just when you press it. So you can hold a key down and it will, and when you release it, it will stop. This will also work for diagonal movement. because it checks the state of all keys, it will check up and left so both will be true (pressed) and the character will move diagonally.

https://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed

and then you can take all the event checking and boolean movement flags out. Also, each time through the loop you are calling pygame.init(). This isn’t necessary. you also don’t need to set the mode each time through the game loop, as well as the caption, and background

Leave a Reply

Your email address will not be published. Required fields are marked *