1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import pygame, sys
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
PADDLE_START_X = 10
PADDLE_START_Y = 20
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 100
BALL_SPEED = 10
BALL_WIDTH_HEIGHT = 16
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pong")
# This is a rect that contains the ball at the beginning it is set in the center of the screen
ball_rect = pygame.Rect((SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), (BALL_WIDTH_HEIGHT, BALL_WIDTH_HEIGHT))
# Speed of the ball (x, y)
ball_speed = [BALL_SPEED, BALL_SPEED]
# Your paddle vertically centered on the left side
paddle_rect = pygame.Rect((PADDLE_START_X, PADDLE_START_Y), (PADDLE_WIDTH, PADDLE_HEIGHT))
# Scoring: 1 point if you hit the ball, -5 point if you miss the ball
score = 0
# Load the font for displaying the score
font = pygame.font.Font(None, 30)
# Game loop
while True:
# Event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
pygame.quit()
# Control the paddle with the mouse
elif event.type == pygame.MOUSEMOTION:
paddle_rect.centery = event.pos[1]
# correct paddle position if it's going out of window
if paddle_rect.top < 0:
paddle_rect.top = 0
elif paddle_rect.bottom >= SCREEN_HEIGHT:
paddle_rect.bottom = SCREEN_HEIGHT
# This test if up or down keys are pressed; if yes, move the paddle
if pygame.key.get_pressed()[pygame.K_UP] and paddle_rect.top > 0:
paddle_rect.top -= BALL_SPEED
elif pygame.key.get_pressed()[pygame.K_DOWN] and paddle_rect.bottom < SCREEN_HEIGHT:
paddle_rect.top += BALL_SPEED
elif pygame.key.get_pressed()[pygame.K_ESCAPE]:
sys.exit(0)
pygame.quit()
# Update ball position
ball_rect.left += ball_speed[0]
ball_rect.top += ball_speed[1]
# Ball collision with rails
if ball_rect.top <= 0 or ball_rect.bottom >= SCREEN_HEIGHT:
ball_speed[1] = -ball_speed[1]
if ball_rect.right >= SCREEN_WIDTH or ball_rect.left <= 0:
ball_speed[0] = -ball_speed[0]
# Test if the ball is hit by the paddle; if yes reverse speed and add a point
if paddle_rect.colliderect(ball_rect):
ball_speed[0] = -ball_speed[0]
score += 1
# Clear screen
screen.fill((255, 255, 255))
# Render the ball, the paddle, and the score
pygame.draw.rect(screen, (0, 0, 0), paddle_rect) # Your paddle
pygame.draw.circle(screen, (0, 0, 0), ball_rect.center, ball_rect.width / 2) # The ball
score_text = font.render(str(score), True, (0, 0, 0))
screen.blit(score_text, ((SCREEN_WIDTH / 2) - font.size(str(score))[0] / 2, 5)) # The score
# Update screen and wait 20 milliseconds
pygame.display.flip()
pygame.time.delay(20)
|