简体   繁体   English

Pygame KEYDOWN无法识别

[英]Pygame KEYDOWN not being recognised

I am making a game in python pygame and I am on setting up the keyinput and have came across a part where I don't know what to do at all. 我正在用python pygame制作游戏,并且正在设置keyinput,并且遇到了一个我根本不知道该怎么做的部分。

All of the variables are defined as there are no errors, but no key detections are being made at all so I don't know what to do. 所有变量均已定义为没有错误,但是根本没有进行键检测,因此我不知道该怎么做。

I have had a look at other questions and tried there answers but haven't solved the problem yet 我看过其他问题并尝试了答案,但尚未解决问题

class Player(object):


    def __init__(self, x, y, velocity):
        self.x = x
        self.y = y

        self.width = 32
        self.height = 32

        self.velocity = velocity
        self.render()
        self.tick()

    def movement(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                print("done")
                if event.key == pygame.K_w:
                    self.y -= self.velocity
                elif event.key == pygame.K_s:
                    self.y += self.velocity
                elif event.key == pygame.K_d:
                    self.x += self.velocity
                elif event.key == pygame.K_a:
                    self.x -= self.velocity
                elif event.key == pygame.K_ESCAPE:
                    pygame.quit()
            elif event.type == pygame.QUIT:
                pygame.quit()

    def tick(self):
        self.movement()

    def render(self):
        pygame.draw.rect(window.WINDOW, (255, 0, 0), (self.x, self.y, 
        self.width, self.height))
        pygame.display.update()

There are no errors but when it is supposed to print out "done" it doesn't so I think it has something to do with the KEYDOWN at the start or before that. 没有错误,但是当它应该打印出“完成”时没有错误,所以我认为它与开始或之前的KEYDOWN有关。

Okay your code isn't complete, but I can see you're trying to move an image or object up , down , left , right on the pygame display. 好你的代码是不完整的,但我可以看到你想移动的图像或对象 Pygame的显示屏上。 I recreated your code from what you gave here, and everything seems to be running fine I'm not sure about K_w , K_s , K_d , K_a keys though since I don't have the full code and you are concerned with it not printing 'done' . 我根据您在此处提供的代码重新创建了您的代码,并且一切似乎都运行良好,尽管我没有完整的代码,并且您担心它不会打印' ,我不确定K_wK_sK_dK_a完成” NOTE that I changed them to print something out instead of actually moving an object. 注意 ,我更改了它们以打印出一些东西,而不是实际移动对象。 the only errors I found is with your exit pygame.K_ESCAPE and pygame.QUIT events which is: 我发现的唯一错误与您的退出pygame.K_ESCAPEpygame.QUIT事件有关:

Traceback (most recent call last): File "main.py", line 13, in for event in pygame.event.get(): pygame.error: video system not initialized 追溯(最近一次通话最近):文件“ main.py”,第13行,用于pygame.event.get()中的事件:pygame.error:视频系统未初始化

Solve this using pygame.quit() together with sys.exit() , first import sys on top of your code. sys.exit(),首次导入SYS您的代码的顶部()一起解决这个使用pygame.quit。

this how it looks and It prints out done on KEYDOWN: 这看起来如何,并在KEYDOWN上完成打印:

<code>
from pygame.locals import *
import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((200,200))




while 1:
    for event in pygame.event.get():
        print(event) # logs every event happening on the pygame display

        if event.type == pygame.KEYDOWN:

            print("done")
            if event.key == pygame.K_w:
                print("self.y -= self.velocity") # not doesn't decrement it prints for testing 
            elif event.key == pygame.K_s:
                print("self.y += self.velocity") # not doesn't increment it prints for testing
            elif event.key == pygame.K_d:
                print("self.x += self.velocity") # not doesn't increment it prints for testing
            elif event.key == pygame.K_a:
                print("self.x -= self.velocity") # not doesn't decrement it prints for testing
            elif event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit() # add this
            elif event.type == QUIT:
                pygame.quit()
                sys.exit() # add this
</code>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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