【发布时间】:2016-11-19 06:41:46
【问题描述】:
所以我正在用 python 中的 pygame 模块制作游戏。游戏是Breakout。游戏的机制之一是左右移动玩家。我是如何做到这一点的,当用户按下向左或向右箭头键时,玩家砖块会根据按下的键向左或向右移动,但要注意的是,如果玩家按住向左或向右按钮;玩家砖不会继续移动...我的问题是如何让玩家砖在按住键时继续移动而不是移动一次?!
这是我的代码
import pygame
pygame.init()
#colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
#the Brick
class goodbrick:
def __init__ (self, color):
self.color_scheme=color
##################X, Y L F
self.cordinates= [20, 450, 100, 0]
def move (self, x):
self.cordinates[0]+=x
def draw (self):
pygame.draw.rect(screen, self.color_scheme, self.cordinates, 0)
#class enemyBrick:
#the ball
#pygame stuff
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("BREAKOUT")
done= False
clock = pygame.time.Clock()
#init stuff
player1= goodbrick(GREEN)
#main loop
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT:
done=True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player1.move(-1)
if event.key == pygame.K_RIGHT:
player1.move(1)
elif event.type ==pygame.KEYUP:
if event.key == pygame.K_LEFT:
player1.move(-1)
print("yup")
if event.key == pygame.K_RIGHT:
player1.move(1)
#art
screen.fill(BLACK)
player1.draw()
#screent
pygame.display.flip()
clock.tick(60)
pygame.quit()
【问题讨论】: