Skip to content

Commit b6ad0b4

Browse files
author
Francisco Laurindo
committed
add movement and collisions
1 parent 72fa0ce commit b6ad0b4

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

pong1_setup.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
import pygame
22
import sys
33

4+
5+
def ball_animation():
6+
# Fixing Bugs
7+
global ball_speed_x, ball_speed_y
8+
9+
# Defining the movement of the ball
10+
ball.x += ball_speed_x
11+
ball.y += ball_speed_y
12+
13+
# Defining the limitations of ball in screen
14+
if ball.top <= 0 or ball.bottom >= screen_height:
15+
ball_speed_y *= -1
16+
if ball.left <= 0 or ball.left >= screen_width:
17+
ball_speed_x *= -1
18+
19+
# Defining the colisions with the player and opponent
20+
if ball.colliderect(player) or ball.colliderect(opponent):
21+
ball_speed_x *= -1
22+
23+
24+
# Defining the limitations of screen of opponents and players
25+
def player_animation():
26+
player.y += player_speed
27+
if player.top <= 0:
28+
player.top = 0
29+
if player.bottom >= screen_height:
30+
player.bottom = screen_height
31+
32+
33+
def opponent_animation():
34+
opponent.y += opponent_speed
35+
if opponent.top <= 0:
36+
opponent.top = 0
37+
if opponent.bottom >= screen_height:
38+
opponent.bottom = screen_height
39+
40+
441
# Starting General Setup
542
pygame.init()
643
clock = pygame.time.Clock()
@@ -12,7 +49,7 @@
1249
pygame.display.set_caption('Pong Game')
1350

1451
# Defining Objects (Rectangles)
15-
ball = pygame.Rect(screen_width/2 - 15, screen_height/2 - 15, 30, 30)
52+
ball = pygame.Rect(screen_width/2 - 15, screen_height/2 - 15, 25, 25)
1653
player = pygame.Rect(10, screen_height/2 - 70, 10, 150)
1754
opponent = pygame.Rect(screen_width - 20, screen_height/2 - 70, 10, 150)
1855

@@ -22,12 +59,44 @@
2259
blue = (0, 139, 0)
2360
red = (139, 0, 0)
2461

62+
# Defining Speed of Ball
63+
ball_speed_x = 7
64+
ball_speed_y = 7
65+
66+
# Defining Speed of Player
67+
player_speed = 0
68+
opponent_speed = 0
69+
2570
while True:
2671
for event in pygame.event.get():
2772
if event.type == pygame.QUIT:
2873
pygame.quit()
2974
sys.exit()
3075

76+
# Defining player movement
77+
if event.type == pygame.KEYDOWN:
78+
if event.key == pygame.K_s:
79+
player_speed += 7
80+
if event.key == pygame.K_w:
81+
player_speed += -7
82+
if event.type == pygame.KEYUP:
83+
if event.key == pygame.K_s:
84+
player_speed += -7
85+
if event.key == pygame.K_w:
86+
player_speed += 7
87+
88+
# Defining opponent movement
89+
if event.type == pygame.KEYDOWN:
90+
if event.key == pygame.K_DOWN:
91+
opponent_speed += 3.5
92+
if event.key == pygame.K_UP:
93+
opponent_speed += -3.5
94+
if event.type == pygame.KEYUP:
95+
if event.key == pygame.K_DOWN:
96+
opponent_speed += -3.5
97+
if event.key == pygame.K_UP:
98+
opponent_speed += 3.5
99+
31100
# Defining object colors
32101
screen.fill(bg_color)
33102
pygame.draw.rect(screen, blue, player)
@@ -36,6 +105,12 @@
36105
pygame.draw.aaline(screen, light_grey, (screen_width/2,
37106
0), (screen_width/2, screen_height))
38107

108+
ball_animation()
109+
player_animation()
110+
opponent_animation()
111+
112+
opponent.y += opponent_speed
113+
39114
# Defining FPS
40115
pygame.display.flip()
41116
clock.tick(60)

0 commit comments

Comments
 (0)