pygame

时间:2015-04-22 13:58:01

标签: python pygame

这是我的第一个pygame代码并不多,非常简单。当我想移动我的播放器它有效但但我希望稳定地运动。就像当我向左按下时我希望它向左移动。目前我必须每按一次左按钮,所以玩家向左移动。你有什么建议吗?

import pygame, sys

pygame.init()

window_size = ( 400, 400 )

white = ( 255, 255, 255 )

class Player():
  image = pygame.image.load( 'foo.png')
  rect = image.get_rect()

player = Player()

screen = pygame.display.set_mode( window_size )

done = False

while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      sys.exit()

    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LEFT:
        move = (-10, 0 )
        player.rect = player.rect.move(move)
      if event.key == pygame.K_RIGHT:
        move = ( 10, 0 )
        player.rect = player.rect.move(move)
      if event.key == pygame.K_UP:
        move = ( 0,-10 )
        player.rect = player.rect.move(move)
      if event.key == pygame.K_DOWN:
        move = ( 0, 10 )
        player.rect = player.rect.move(move)

  screen.fill( white )
  screen.blit( player.image, player.rect )

  pygame.display.flip()

编辑我对Aarons的态度回答:

while not done:

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      sys.exit()

  pressed = pygame.key.get_pressed()
  if pressed[pygame.K_LEFT]:
    move = ( 2, 0 )
    player.rect = player.rect.move(move)
  if pressed[pygame.K_RIGHT]:
    move = ( 2, 0 )
    player.rect = player.rect.move(move)
  if pressed[pygame.K_UP]:
    move = ( 0,-2 )
    player.rect = player.rect.move(move)
  if pressed[pygame.K_DOWN]:
    move = ( 0, 2 )
    player.rect = player.rect.move(move)

2 个答案:

答案 0 :(得分:3)

您可以跟踪KEYUPKEYDOWN个事件,然后为您要跟踪的每个密钥存储一个按下状态。

现在,无论何时用户按下某个键,都可以测试按键的状态,看看是否按下按键并将动作应用到播放器上。

作为简化示例:

pressed_keys = {
    'left': false,
    ...define other keys...
}

while not done:

    # Check for key events
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                pressed_keys['left'] = True
            ...check other keys...

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                pressed_keys['left'] = False
            ...check other keys...

   # Move the player
   if pressed_keys['left']:
       ...move player left...

答案 1 :(得分:2)

在类似问题上查看Sloth's answer。您似乎有两种可能的方法,使用set_repeat或get_pressed。

<强> set_repeat

  

启用键盘重复时,按下的键将生成多个pygame.KEYDOWN事件。延迟是第一次重复发送pygame.KEYDOWN之前的毫秒数。之后,每隔一个毫秒发送另一个pygame.KEYDOWN。如果没有传递参数,则禁用密钥重复。    Source

这样的事情可以胜任:

# set_repeat(delay, interval)
pygame.key.set_repeat(1, 10) 
# A delay of 0 disables the repeat.
# The lower the interval, the faster the movement would be.

<强> get_pressed

  

返回一系列布尔值,表示键盘上每个键的状态。使用键常量值来索引数组。 True值表示按下该按钮。   使用此功能获取按下的按钮列表不是处理来自用户的文本输入的正确方法。您无法知道按下的键的顺序,并且在两次调用pygame.key.get_pressed()之间可以完全忽略快速按键。也无法将这些推送的密钥转换为完全翻译的字符值。请参阅事件队列上的pygame.KEYDOWN事件以获取此功能。   Source

这样的东西应该很适合你的代码:

pressed = pygame.key.get_pressed()
if pygame.K_LEFT in pressed:
   # move left
if pygame.K_RIGHT in pressed:
   # move right

在@ ZedsWhatSheSaid的代码中实现的解决方案,还添加了PEP-8格式:

import pygame
import sys

pygame.init()

window_size = (400, 400)

white = (255, 255, 255)


class Player():
    image = pygame.image.load('foo.png')
    rect = image.get_rect()

player = Player()

screen = pygame.display.set_mode(window_size)

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    pressed = pygame.key.get_pressed()
    if pygame.K_LEFT in pressed:
        move = (-10, 0)
    player.rect = player.rect.move(move)
    if pygame.K_RIGHT in pressed:
        move = (10, 0)
    player.rect = player.rect.move(move)
    if pygame.K_UP in pressed:
        move = (0, -10)
    player.rect = player.rect.move(move)
    if pygame.K_DOWN in pressed:
        move = (0, 10)