Reputation: 11
I have a problem with Python and pygame: I have no idea how to make a simple paddle move left and right. Basically I am working on a pong type game for my first project.
After reading a few articles online I thought of a way of how to do this. The code I have so far is:
PADDLE_WIDTH = 50
PADDLE_HEIGHT = 10
paddleSpeedX = 0
p1Paddle = pygame.Rect(10, 430, PADDLE_WIDTH, PADDLE_HEIGHT)
PADDLE_COLOR = pygame.color.Color("red")
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT:
p1Paddle.right = p1Paddle.left + paddleSpeedX - 10
if event.key == K_RIGHT:
p1Paddle.left = p1Paddle.left + paddleSpeedX + 10
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
pygame.display.update()
After entering this code the game runs but I still cannot move the paddle left and right. How can I do this?
Upvotes: 1
Views: 21011
Reputation: 1531
I have made some additions and modifications to make your code work. In each frame (iteration of loop) you must erase the screen and redraw the paddle. This is to make sure the paddle is redrawn with new coordinates when the LEFT/RIGHT arrows are pressed. Notice that I change p1Paddle.left instead of p1Paddle.right when LEFT is pressed.
import pygame, sys
from pygame import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Paddle Movement')
PADDLE_WIDTH = 50
PADDLE_HEIGHT = 10
paddleSpeedX = 0
p1Paddle = pygame.Rect(10, 430, PADDLE_WIDTH, PADDLE_HEIGHT)
PADDLE_COLOR = pygame.color.Color("red")
while True:
# clear screen with black color
screen.fill( (0,0,0) )
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT:
p1Paddle.left = p1Paddle.left + paddleSpeedX - 10
if event.key == K_RIGHT:
p1Paddle.left = p1Paddle.left + paddleSpeedX + 10
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
# draw the paddle
screen.fill( PADDLE_COLOR, p1Paddle );
pygame.display.update()
A more elegant solution would be the one below
import pygame, sys
from pygame import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Paddle Movement')
PADDLE_WIDTH = 50
PADDLE_HEIGHT = 10
paddleSpeedX = 0
p1Paddle = pygame.Rect(10, 430, PADDLE_WIDTH, PADDLE_HEIGHT)
PADDLE_COLOR = pygame.color.Color("red")
# clock object that will be used to make the game
# have the same speed on all machines regardless
# of the actual machine speed.
clock = pygame.time.Clock()
while True:
# limit the demo to 50 frames per second
clock.tick( 50 );
# clear screen with black color
screen.fill( (0,0,0) )
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
p1Paddle.left = p1Paddle.left + paddleSpeedX - 5
if keys[K_RIGHT]:
p1Paddle.left = p1Paddle.left + paddleSpeedX + 5
# draw the paddle
screen.fill( PADDLE_COLOR, p1Paddle );
pygame.display.update()
Notice that I check the key press using pygame.key.get_pressed()
. It allows to make a smooth movement as we check the key state and don't wait for an event to occur.
I added a clock object to limit the frame rate to 50 FPS (frames per second).
Please try both approaches.
Upvotes: 1
Reputation: 64058
Pygame raises a KEYDOWN
event only once, when you first hit a key.
Unfortunately, it won't continue to raise a KEYDOWN
event, so what's happening is that your paddle is jerking over only once, and won't move again unless you keep spamming the left or right key.
Instead, what you could do is set a velocity when you recieve a KEYDOWN
event, and set it back to zero when you get KEYUP
, like so:
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
p1Paddle.x_velocity = -10
elif event.key == pygame.K_RIGHT:
p1Paddle.x_velocity = 10
if event.type == pygame.KEYUP:
if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:
# if either the left or right arrow keys are released
p1Paddle.x_velocity = 0
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
p1Paddle.x_distance += p1Paddle.x_velocity
# other stuff here
# drawing code here
pygame.display.update()
(I also changed some of your variable names, since I couldn't figure out what p1Paddle.left
and p1Paddle.right
were -- I hope you don't mind).
Upvotes: 2