-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPong.py
98 lines (82 loc) · 3.05 KB
/
Pong.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# october 2019
import pygame
import random
team1 = input('What is team one\'s name? ')
team2 = input('What is team two\'s name? ')
pygame.init()
windowWidth = 800
windowHeight = 500
window = pygame.display.set_mode((windowWidth, windowHeight))
winner = 'nobody'
ballwidth = 10
ballheight = 10
ball_x_speed = random.randint(9,15)
ball_y_speed = random.randint(-15, 15)
ball_accleration = 1.1
left_wall_x = 50
left_wall_y = 200
right_wall_x = windowWidth-50
right_wall_y = 200
wall_height = 100
wall_width = 10
wall_speed = 30
run = True
thing = True
right_score = 0
left_score = 0
while thing:
run = True
pygame.display.set_caption('Score: ' + team1 + ' = ' + str(left_score) + ', ' + team2 + ' = ' + str(right_score))
ballx = 250
bally = 150
ball_x_speed = random.randint(9, 15)
ball_y_speed = random.randint(-15, 15)
while run:
pygame.time.delay(10) # wait 100 milliseconds
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
thing = False
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN] and windowHeight - right_wall_y >= wall_height + wall_speed:
right_wall_y += wall_speed
if keys[pygame.K_UP] and right_wall_y > 0:
right_wall_y -= wall_speed
if keys[pygame.K_w] and left_wall_y > 0:
left_wall_y -= wall_speed
if keys[pygame.K_s] and windowHeight - left_wall_y >= wall_height + wall_speed:
left_wall_y += wall_speed
if ballx + ball_x_speed <= left_wall_x + wall_width and bally + ball_y_speed >= left_wall_y and bally + ball_y_speed <= left_wall_y + wall_height:
ball_x_speed *= -ball_accleration
#print ('x reveresed 1')
elif ballx + ball_x_speed + ballwidth >= right_wall_x and bally + ball_y_speed >= right_wall_y and bally + ball_y_speed <= right_wall_y + wall_height:
ball_x_speed *= -ball_accleration
#print('x reveresed 2', ballx)
if bally + ball_y_speed <= 0:
ball_y_speed *= -ball_accleration
#print('y reveresed 1')
elif bally + ballheight + ball_y_speed >= windowHeight:
ball_y_speed *= -ball_accleration
#print('y reveresed 2')
ballx += ball_x_speed
bally += ball_y_speed
if ballx < left_wall_x:
winner = 'right team'
run = False
elif ballx > right_wall_x:
winner = 'left team'
run = False
window.fill((0,0,0))
#draw ball
pygame.draw.rect(window, (255,0,0), (ballx, bally, ballwidth, ballheight))
#draw bouncers
pygame.draw.rect(window, (255,255,255), (left_wall_x, left_wall_y, wall_width, wall_height))
pygame.draw.rect(window, (255, 255, 255), (right_wall_x, right_wall_y, wall_width, wall_height))
pygame.display.update()
print ('The is winner is ' + winner)
if winner == 'left team':
left_score += 1
elif winner == 'right team':
right_score += 1
pygame.time.delay((500))
pygame.quit()