即使空闲没有显示错误,pygame事件键也没有响应?

时间:2017-12-03 20:07:45

标签: python-3.x pygame

import pygame
from pygame import *
pygame.init

pygame.display.init
pygame.display.set_caption("Test")
bg = pygame.image.load("bg.jpg")
human = pygame.image.load("human1.bmp")
display_screen = pygame.display.set_mode((1000,500))
keyboard_input = 0
clock = pygame.time.Clock()
def screen_Quit():
running = False
while not running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                keyboard_input = (90)
                print("Left arrow key has been pressed.")

            if event.key == pygame.K_RIGHT:
                keyboard_input = (-90)
                print("Right arrow key has been pressed.")
            if event.key == pygame.K_UP:
                keyboard_input = (20)
                print("Up arrow key has been pressed.")
            if event.key == pygame.K_DOWN:
                keyboard_input = (-20)
                print("Down arrow key has been pressed.")

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                keyboard_input = (0)
                print("Left arrow key has been released.")

            if event.key == pygame.K_RIGHT:
                keyboard_input = (0)
                print("Right arrow key has been released.")
            if event.key == pygame.K_UP:
                keyboard_input = (0)
                print("Up arrow key has been released.")
            if event.key == pygame.K_DOWN:
                keyboard_input = (0)
                print("Down arrow key has been released.")


        # this code here has been left for later --> print(event)

display_screen.blit(bg, [0,0])
display_screen.blit(human, [50,87])
pygame.display.update()
clock.tick(60)
screen_Quit()
pygame.quit
quit()

我不明白这个问题,我已经发布了我的完整代码,因为问题可能是其他问题。我在程序中遇到的问题是python中的事件键,它没有响应输入,键盘是我使用pygame创建游戏但是无法理解为什么事件键不工作。我到处检查,无法理解。代码对我来说似乎很好,python idle没有给出任何错误我甚至在空闲时输入但是角色没有移动那里应该没有理由不移动。

这是一张图片和一个视频链接,它更深入地解决了问题(我自己制作了视频):

an image of the problem

视频链接: https://www.youtube.com/watch?v=QVFaiuF9KY8&feature=youtu.be

视频最有可能比图像更有用。

2 个答案:

答案 0 :(得分:1)

简短回答

您的代码没有任何告诉图片移动的命令。我修复了这个并在底部发布了完整的工作代码。如果您想要更深入的解释,请继续阅读。

说明

如果那是你的完整代码,那么问题是首先你甚至没有任何代码可以告诉角色移动。其次,pygame.display.update()位于while循环之外,因此对屏幕所做的任何更改都不会显示。

对于您刚刚告诉python打印消息的每个事件:

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
         keyboard_input = (90)
         print("Left arrow key has been pressed.")

你想要做的是在那里添加另一条线以实际移动角色。 这可以通过改变它的x和y位置来完成。我建议使用sprite,但是现在因为你正在使用图像,我们只是坚持这一点。

要更改图像的位置,您必须使用所需的(x,y)运行新的window.blit()。这意味着如果您需要大量移动它,您将不得不不断迭代这个,所以最好将 INSIDE 运行while循环。因此,将这部分代码转换为缩进:

    display_screen.blit(bg, [0,0])
    display_screen.blit(human, [50,87])
    pygame.display.update()
    clock.tick(60)
    screen_Quit()
    pygame.quit
    quit()

现在屏幕定期更新。您要做的第二件事是创建2个新变量xy并更改

display_screen.blit(human, [50,87])  至 display_screen.blit(human, [x,y])

这基本上意味着你现在可以在你选择的位置对你的角色进行blitting,而不是在一个不会改变的固定位置。现在,只要点击左键,您就可以将+添加到x位置,或者只要点击右键,就可以从x位置添加-

以下是完整的工作代码:

import pygame
from pygame import *
pygame.init

pygame.display.init
pygame.display.set_caption("Test")
bg = pygame.image.load("bg.jpg")
human = pygame.image.load("human1.bmp")
display_screen = pygame.display.set_mode((1000,500))
keyboard_input = 0
clock = pygame.time.Clock()
running = False
x=0
y=0
moving = "none"
white = (255,255,255)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                moving = "left"
                print("Left arrow key has been pressed.")
            elif event.key == pygame.K_RIGHT:
                moving = "right"
                print("Right arrow key has been pressed.")
            elif event.key == pygame.K_UP:
                moving = "up"
                print("Up arrow key has been pressed.")
            elif event.key == pygame.K_DOWN:
                moving = "down"
                print("Down arrow key has been pressed.")
        else:
            moving = "hello"

    if moving == "left":
        x -= 5
    if moving == "right":
        x += 5
    if moving == "up":
        y -= 5
    if moving == "down":
        y += 5

            # this code here has been left for later --> print(event)

    display_screen.blit(bg,[0,0])
    display_screen.blit(human, [x,y])
    clock.tick(60)
    pygame.display.update()

答案 1 :(得分:0)

您永远不会更新播放器的位置,只需在[50,87]处进行blit即可。如果你想让它移动,你首先必须存储位置,例如player_pos = [50, 87]并定义速度velocity_x = 0的变量,然后在按下某个键时设置事件循环中的速度:

if event.key == pygame.K_RIGHT:
    velocity_x = 5

并更新while循环中的位置:

player_pos[0] += velocity_x
player_pos[1] += velocity_y