Franco Pettigrosso Franco Pettigrosso - 4 months ago 10
Python Question

key event triggers and moving objects on screen

So I am making a game with the pygame module in python. The game is Breakout. One of the mechanics of the game is move the player left and right. How am I doing this is when the user presses the left or right arrow key, the player brick moves the left or right depending on what key is pressed, but the catch is that it if the player presses and holds the left or right button; the player brick will not continue to move... My question is how do I make the player brick continue to move instead of moving once when the key button is held down?!

here is my code

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()

Answer
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()