简体   繁体   中英

Pygame KEYDOWN not working

I am trying to make a rectangle move up and down on the screen. But when I press the keys to make it go up or down I have to continuously press it each time to make it move. The way I interpreted pygame.KEYDOWN was that I can hold down the desired key and the rectangle would move until I release the key.

Code for key pressed:

while True:
    for event in pygame.event.get():
        # all keys
        keys = pygame.key.get_pressed()
        # quit game
        if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
            pygame.quit()
            quit()

        # check key down presses
        elif event.type == pygame.KEYDOWN:
            # left paddle
            if keys[pygame.K_q]:
                left.move(-30)
            if keys[pygame.K_a]:
                left.move(30)

            # right paddle
            if keys[pygame.K_p]:
                right.move(-30)
            if keys[pygame.K_l]:
                right.move(30)

        # user let up on a key
        elif event.type == pygame.KEYUP:
            if event.key == keys[pygame.K_q] or event.key == keys[pygame.K_a]:
                left.move(0)
            elif event.key == pygame.K_p or event.key == pygame.K_l:
                right.move(0)

    # call animation function
    draw()

    # FPS
    clock.tick(120)

The left and right objects are coming from the following class:

import pygame


class Paddle:

    def __init__(self, screen, h, w, left):
        self.screen = screen
        self.width = w
        self.y = h / 2
        self.pad_width = 10
        self.pad_height = 70

        if left:
            self.x = self.pad_width / 2
        else:
            self.x = self.width - self.pad_width * 1.5

    def show(self):
        WHITE = (255, 255, 255)
        rect = pygame.Rect(self.x, self.y, self.pad_width, self.pad_height)
        pygame.draw.rect(self.screen, WHITE, rect)

    def move(self, speed):
        self.y += speed

Your game loop should comprise of three main steps:

  1. Handling user input
  2. Updating the game state
  3. Re-rendering your scene

I think by doing steps 1 and 2 together you may have confused yourself a bit.

Firstly, KEYDOWN detects a key being pressed , rather than being down . Because of this I suggest keeping track of current key states, as a basic example:

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_q:
        q_down = True
else:
    if event.key == pygame.K_q:
        q_down = False

Now user input is handled you can simply use:

if q_down: left.move(-30)

in your game loop to update the game state (note this should be outside your event loop).


I also suggest your read this post from game dev SE and have a look at the linked resources to better understand the game loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM