相关疑难解决方法(0)

如何在pygame中获得键盘输入?

我在pygame 1.9.2中制作游戏.这是一个非常简单的游戏,其中一艘船在五列坏人之间移动,他们通过缓慢向下移动进行攻击.我试图使船只用左右箭头键左右移动.这是我的代码:

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    location-=1
    if location==-1:
        location=0
if keys[K_RIGHT]:
    location+=1
    if location==5:
        location=4
Run Code Online (Sandbox Code Playgroud)

它运作得很好.船移得太快了.它几乎不可能只移动一个位置,左或右.我怎么能这样做,所以每次按下按键时船只移动一次?

keyboard pygame

28
推荐指数
3
解决办法
12万
查看次数

如何在Pygame中移动Sprite

即时通讯试图让我的形象(鸟)在屏幕上上下移动,但我无法弄清楚如何做到这里是我试图确定它的方式,但我试图找出来,如果有人可以帮助那将是大!

import pygame
import os

screen = pygame.display.set_mode((640, 400))

running = 1

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    screen.fill([255, 255, 255])
    clock = pygame.time.Clock()
    clock.tick(0.5)
    pygame.display.flip()

    bird = pygame.image.load(os.path.join('C:\Python27', 'player.png'))
    screen.blit( bird, ( 0, 0 ) )

    pygame.display.update()

class game(object):
    def move(self, x, y):
        self.player.center[0] += x
        self.player.center[1] += y


    if event.key == K_UP:
        player.move(0,5)
    if event.key == K_DOWN:
        player.move(0,-5)

game()
Run Code Online (Sandbox Code Playgroud)

我试图让它按下向下按钮按下向上按UP按键

python pygame sprite

4
推荐指数
1
解决办法
3万
查看次数

pygame.key.get_pressed() 和 event.type 有什么区别?

这两段代码输出相同的内容。它们之间有什么区别?什么时候我们应该更喜欢使用其中一种而不是另一种?

   if event.type==pygame.KEYDOWN:
        if event.key==pygame.K_LEFT:
            xc=-4
        if event.key==pygame.K_RIGHT:
            xc=+4
        if event.key==pygame.K_UP:
            yc=-4
        if event.key==pygame.K_DOWN:
            yc=+4

    if event.type==pygame.KEYUP:
        if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
            xc=0
        if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            yc=0
Run Code Online (Sandbox Code Playgroud)
        pressed=pygame.key.get_pressed()
    if pressed[pygame.K_LEFT]:
        xc=-4
    elif pressed[pygame.K_RIGHT]:
        xc=+4
    else:
        xc=0

    if pressed[pygame.K_UP]:
        yc=-4
    elif pressed[pygame.K_DOWN]:
        yc=+4
    else:
        yc=0
Run Code Online (Sandbox Code Playgroud)

python pygame

4
推荐指数
1
解决办法
4102
查看次数

标签 统计

pygame ×3

python ×2

keyboard ×1

sprite ×1