Python pygame按住键移动

Python pygame按住键移动,python,pygame,Python,Pygame,我试图在四个主要方向向左移动播放器,但它只在按下而不是按下时移动,所以我必须反复按下按钮在屏幕上移动,上下都不起作用 import pygame #Start pygame pygame.init() #Window/Screen/Display display_x = 1280 display_y = 720 display = pygame.display.set_mode((display_x,display_y)) pygame.display.set_caption('Plat

我试图在四个主要方向向左移动播放器,但它只在按下而不是按下时移动,所以我必须反复按下按钮在屏幕上移动,上下都不起作用

import pygame

#Start pygame

pygame.init()

#Window/Screen/Display

display_x = 1280
display_y = 720
display = pygame.display.set_mode((display_x,display_y))
pygame.display.set_caption('Platforms')

clock = pygame.time.Clock()

#Colors

black = (0,0,0)
green = (1,166,17)

#Images
character = pygame.image.load('character.gif')
def chrctr(x,y):
display.blit(character,(x,y))
x_c = (display_x  / 2)
y_c = (display_y / 2)
x_change = 0
y_change = 0

floor_1 = pygame.image.load('wood.jpg')
def floor(x,y):
    display.blit(floor_1,(x,y))
x = (display_x * 0)
y = (display_y * 0.9)

not_dead=True
while not_dead:
    for event in pygame.event.get():
        if (event.type==pygame.QUIT):
            not_dead=False 

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = - 5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.key == pygame.K_UP:
                y_change = - 5
        elif event.key == pygame.K_DOWN:
                y_change = 5


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 5

    x_c += x_change
    if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_change = 0

    y_c += y_change

    display.fill(green) 
    pygame.draw.rect(display, black, [0, 550, 200, 50])
    pygame.draw.rect(display, black, [0, 450, 200, 50])
    pygame.draw.rect(display, black, [0, 350, 200, 50])
    pygame.draw.rect(display, black, [0, 250, 200, 50])
    pygame.draw.rect(display, black, [0, 150, 200, 50])
    pygame.draw.rect(display, black, [0, 50, 200, 50])
    pygame.draw.rect(display, black, [1080, 550, 200, 50])
    pygame.draw.rect(display, black, [1080, 450, 200, 50])
    pygame.draw.rect(display, black, [1080, 350, 200, 50])
    pygame.draw.rect(display, black, [1080, 250, 200, 50])
    pygame.draw.rect(display, black, [1080, 150, 200, 50])
    pygame.draw.rect(display, black, [1080, 50, 200, 50])

    floor(0,display_y * 0.9)
    floor(236, display_y * 0.9)
    floor(472, display_y * 0.9)
    floor(708, display_y * 0.9)
    floor(944, display_y * 0.9)
    floor(1180, display_y * 0.9)
    chrctr(x_c, y_c)


    pygame.display.update()
    clock.tick(60)
print ("Hello")

pygame.quit()

我做了一个简单的演示,你的角色(英雄)在屏幕中央,你可以用箭头键移动他。因为有很多重构,请让我知道我需要在评论中澄清哪些内容,我将对这个答案进行解释

import pygame
import sys

class Character(object):
    def __init__(self, x=0, y=0, speed=0):
        self.x = x
        self.y = y
        self.speed = speed
        self.image = pygame.image.load('hero.png')

    def get_size(self):
        return self.image.get_size()

    def draw(self):
        display.blit(self.image, (self.x, self.y))

pygame.init()
(width, height) = (800, 600)
display = pygame.display.set_mode((width, height))
pygame.display.set_caption('Platforms')
clock = pygame.time.Clock()
hero = Character(speed=5)
hero_width, hero_height = hero.get_size()
hero.x = width/2.0 - hero_width/2.0
hero.y = height/2.0 - hero_height/2.0
black = (0,0,0)
pressed_keys = {"left": False, "right": False, "up": False, "down": False}

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                pressed_keys["left"] = True
            if event.key == pygame.K_RIGHT:
                pressed_keys["right"] = True
            if event.key == pygame.K_UP:
                pressed_keys["up"] = True
            if event.key == pygame.K_DOWN:
                pressed_keys["down"] = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                pressed_keys["left"] = False
            if event.key == pygame.K_RIGHT:
                pressed_keys["right"] = False
            if event.key == pygame.K_UP:
                pressed_keys["up"] = False
            if event.key == pygame.K_DOWN:
                pressed_keys["down"] = False

    if pressed_keys["left"]:# == True is implied here
        hero.x -= hero.speed
    if pressed_keys["right"]:
        hero.x += hero.speed
    if pressed_keys["up"]:
        hero.y -= hero.speed
    if pressed_keys["down"]:
        hero.y += hero.speed

    display.fill(black) 
    hero.draw()
    pygame.display.update()
    clock.tick(60)
编辑: 我看到按下字典上的
键造成了一些混乱,所以我会解释一下。如果我的解释不清楚,你可能会想读一些类似的东西

python中的词典基本上类似于现实生活中的物理词典。假设您正在查找一个类似“土豚”(a键)的单词,并想知道它的意思。你可以打开字典,找到“aardvark”的定义(avalue)。字典就是这些
key:value
对的组合

最初创建
pressed_keys
变量时,我定义了四对
key:value
对,每个方向一对。键为
向上
,和
向下
,而相应的值均为
。这应该是有意义的,因为在游戏开始时,我们没有按下任何按钮,玩家也没有移动。根据pygame,每当按下按键时,
pressed_keys[“left”]=True
等代码行将更新与按下的_keys中的键
left
匹配的值


在我获得pygame中在特定时刻发生的所有事件(窗口关闭、鼠标移动、键盘按下等)后,我现在可以检查我的
按键的状态。如果字典键
left
对应的值是
True
,那么我只需将英雄的x位置向左移动一点。其他所有方向也是如此。

尽管如此,您可能需要反复按下的原因是pygame的
pygame.KEYDOWN
事件只有在最初按下按钮时才会触发。不要将
x\u change
y\u change
放在那里,而是在按下键时设置一个变量,如
is\u left\u arrow\u down=True
,在按下键时设置False。然后(在循环事件之后)检查
是否为“左箭头向下”
是否为真。我是python和pygame新手,知道我将如何做我的意思是不知道我将如何做我添加了一个答案,但它使用了一些你可能以前没有见过的棘手的东西,如
字典
,如果您是python新手。让我知道代码是否清晰,或者需要解释哪些行。欢迎使用堆栈溢出!请阅读我们的文章,以帮助您提出一个好问题,从而得到一个好答案。我只需要了解什么是有效的,为什么以及如何在我自己的代码中使用它。那么,您在代码中到底混淆了哪些具体部分?你试过运行这个演示吗?试着在while循环中使用print语句将按下的按键打印到你的控制台我主要对如何使用按下的按键感到困惑我以前试过,但不管我把它放在哪里都不起作用,所以我想这是一个开始,可以帮助我理解为什么你用这个方法来移动你的英雄是的,这肯定是很多一目了然。我希望我写的解释能澄清一些问题。您可能应该先看一些python教程,因为循环、列表、字典、类和函数等功能对于编写游戏非常重要,而不会让您感到毛骨悚然。我在看Sendex的pygame教程,而pygame.KEYDOWN对他很有用,这就是为什么我如此困惑的原因