键事件触发器和屏幕上的移动对象

时间:2016-07-16 00:14:04

标签: python pygame

所以我在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()

1 个答案:

答案 0 :(得分:1)

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
change = 0
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:
                change = -1                
            if event.key == pygame.K_RIGHT:
                change = 1
        elif event.type ==pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                change = 0
                print("yup")
            if event.key == pygame.K_RIGHT:
                change = 0
    player1.move(change)
    #art
    screen.fill(BLACK)
    player1.draw()
    #screent
    pygame.display.flip()
    clock.tick(60)

pygame.quit()