Pygame-检测是否按下了键? [英] Pygame- detect if a key is held down?

查看:268
本文介绍了Pygame-检测是否按下了键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在做一件简单的事情,并且一直在关注 youtube 上的教程.我可以使用 WASD 移动女妖(我在我的船上使用了 Halo 中的图像),但是我必须重复点击按键,而我希望能够通过按住按键来移动它.这是代码;

So I am doing a simple thing, and have been following a tutorial on youtube. I have the ability to move the banshee (I used an image from Halo for my ship) using WASD, but I have to repetitively tap keys, whereas I want to be able to move it by holding down the keys. Here's the code;

import pygame
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((1440,900))

pygame.display.update()

black=(0,0,0)
white=(255,255,255)

background = pygame.image.load("G:/starfield.jpg")##loads the background
banshee = pygame.image.load("G:/banshee.png")###loads sprite of a spaceship that will move.
x=1
y=1


while True:
    gamexit = False

    while not gamexit:
        screen.blit(background,(0,0))
        screen.blit(banshee,(x,y))
        pygame.display.update()
        if x==1440 or x==0 or y==900 or y==0:#if it touches the sides of the window, the window closes
                pygame.quit()
                quit()
        else:
            for event in pygame.event.get():
                pressed= pygame.key.get_pressed()
                if event.type == pygame.QUIT:
                    gamexit=True
                    pygame.quit()
                    quit()
                elif event.type==KEYDOWN:
                    if event.key==K_w:#moves banshee up if w pressed, same for the other WASD keys below
                        y=y-5
                        x=x
                        screen.blit(banshee,(x,y))
                        pygame.display.update()
                    elif event.key==K_a:
                        x=x-5
                        y=y
                        screen.blit(banshee,(x,y))
                        pygame.display.update()
                    elif event.key==K_d:
                        x=x+5
                        y=y
                        screen.blit(banshee,(x,y))
                        pygame.display.update()
                    elif event.key==K_s:
                        y=y+5
                        x=x
                        screen.blit(banshee,(x,y))
                        pygame.display.update()

我尝试了许多不同的方法来做到这一点(在这里和其他地方),但收效甚微.有什么我可以在这里做而不需要重写一大段代码的吗?

I have tried many different methods of doing this (On here and elsewhere), to little avail. Is there anything I can do here that doesn't need to rewrite a large section of code?

谢谢.

推荐答案

首先,尝试添加变量 dxdy 来存储键的状态

As a first go, try adding variables dx and dy to store the state of the keys

           elif event.type==KEYDOWN:
                if event.key==K_w:#moves banshee up if w pressed, same for the other WASD keys below
                    dy = -5
                elif event.key==K_a:
                    dx = -5
                elif event.key==K_d:
                    dx = 5
                elif event.key==K_s:
                    dy = 5
           elif event.type==KEYUP:
                if event.key==K_w:#moves banshee up if w pressed, same for the other WASD keys below
                    dy = 0
                elif event.key==K_a:
                    dx = 0
                elif event.key==K_d:
                    dx = 0
                elif event.key==K_s:
                    dy = 0

    x += dx
    y += dy
    screen.blit(banshee,(x,y))
    pygame.display.update()

这篇关于Pygame-检测是否按下了键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆