pygame按住键移动

时间:2017-01-29 04:56:26

标签: 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('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()

1 个答案:

答案 0 :(得分:1)

好的,所以我做了一个简单的演示,你的角色(英雄)在屏幕中居中,你可以用箭头键移动他。由于有很多重构,让我知道我需要在评论中清楚哪些内容,我将在这个答案中添加解释。

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)

修改 我看到pressed_keys字典引起了一些混乱,所以我会解释一下。如果我的解释不清楚,您可能希望阅读this之类的内容。

python中的字典基本上就像现实生活中的物理字典。假设您正在查找“aardvark”()这样的词,并想知道它的含义。您将打开字典书并找到“aardvark”的定义()。字典只是一堆key:value对。

当我最初创建pressed_keys变量时,我定义了四个key:value对,每个方向一个。密钥为leftrightupdown,而相应的值均为False。这应该是有意义的,因为在游戏开始时,我们没有按任何按钮而玩家没有移动。每当按照pygame发生按键时,像pressed_keys["left"] = True这样的代码行会更新与pressed_keys中的键left匹配的值。

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