0
我已經做了一個簡單的網格和一個簡單的精靈作爲「球員」。Pygame.key.get_pressed - 如何添加間隔?
,我的問題是::但是當我使用箭頭鍵移動,如下圖所示圖片中的人物動作太快如何設置延遲或區間內的各按鍵事件後防止這種問題?
player.py
#!/usr/bin/python
import os, sys, pygame, random
from os import path
from pt import WIDTH,HEIGHT
from pygame.locals import *
img_dir = path.join(path.dirname(__file__), 'img')
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.width = 64
self.height = 64
self.image = pygame.image.load(path.join(img_dir, "player.png")).convert_alpha()
self.image = pygame.transform.scale(self.image,(self.width, self.height))
self.rect = self.image.get_rect()
self.speed = 64
# self.rect.x =
# self.rect.y =
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.x > 0:
self.rect.x -= self.speed
elif keys[pygame.K_RIGHT] and self.rect.x < (WIDTH-self.width):
self.rect.x += self.speed
elif keys[pygame.K_UP] and self.rect.y > 0:
self.rect.y -= self.speed
elif keys[pygame.K_DOWN] and self.rect.y < (HEIGHT-self.height):
self.rect.y += self.speed