The Ball
Part of the Projects section of Coddy's Pygame journey. Lesson 11 of 31.
Pong is two paddles and a ball. Start with the ball: a small square that moves by itself.
Its speed has two parts, how far it moves across in a frame and how far down. These are the ball's velocity, often called vx and vy:
ball = pygame.Rect(293, 193, 14, 14)
vx, vy = 5, 4
while running:
# events
ball.x += vx
ball.y += vyWith both parts the ball moves on a slant. A negative vx moves it left and a negative vy moves it up. Changing a sign later is how the ball bounces.
vx, vy = 5, 4 sets two variables in one line.
Challenge
Easy- Before the loop, make
ball = pygame.Rect(293, 193, 14, 14)andvx, vy = 5, 4. - In the loop, add
vxtoball.xandvytoball.y. - Draw the ball in yellow
(255, 196, 0)after the fill.
Try it yourself
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((30, 30, 46))
pygame.display.flip()
clock.tick(60)
pygame.quit()
All lessons in Projects
Practice on your own: Pygame playground