-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
218 lines (159 loc) · 4.82 KB
/
pong.py
File metadata and controls
218 lines (159 loc) · 4.82 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
Author: Sharome Burton
Date: 02/12/2022
"""
import pygame
from pygame.locals import *
import cv2
import cvGameDevTools
from cvGameDevTools import capture_frame
pygame.init()
screen_width = 600
screen_height = 500
cv = cvGameDevTools.CvTool(screen_width, screen_height)
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Pong')
#define font
font = pygame.font.SysFont('Constantia', 30)
#define game variables
margin = 50
cpu_score = 0
player_score = 0
fps = 1000
live_ball = False
winner = 0
speed_increase = 0
#define colours
bg = (50, 25, 50)
white = (255, 255, 255)
def draw_board():
screen.fill(bg)
pygame.draw.line(screen, white, (0, margin), (screen_width, margin), 2)
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
class paddle():
def __init__(self, x, y):
self.x = x
self.y = y
self.rect = Rect(x, y, 20, 100)
self.speed = 5
self.ai_speed = 15
def move(self):
key = pygame.key.get_pressed()
if key[pygame.K_UP] and self.rect.top > margin:
self.rect.move_ip(0, -1 * self.speed)
if key[pygame.K_DOWN] and self.rect.bottom < screen_height:
self.rect.move_ip(0, self.speed)
def draw(self):
pygame.draw.rect(screen, white, self.rect)
def ai(self):
#ai to move the paddle automatically
#move down
if self.rect.centery < pong.rect.top and self.rect.bottom < screen_height:
self.rect.move_ip(0, self.ai_speed)
#move up
if self.rect.centery > pong.rect.bottom and self.rect.top > margin:
self.rect.move_ip(0, -1 * self.ai_speed)
class ball():
def __init__(self, x, y):
self.reset(x, y)
def move(self):
#check collision with top margin
if self.rect.top < margin:
self.speed_y *= -1
#check collision with bottom of the screen
if self.rect.bottom > screen_height:
self.speed_y *= -1
if self.rect.colliderect(player_paddle) or self.rect.colliderect(cpu_paddle):
self.speed_x *= -1
#check for out of bounds
if self.rect.left < 0:
self.winner = 1
if self.rect.left > screen_width:
self.winner = -1
#update ball position
self.rect.x += self.speed_x
self.rect.y += self.speed_y
return self.winner
def draw(self):
pygame.draw.circle(screen, white, (self.rect.x + self.ball_rad, self.rect.y + self.ball_rad), self.ball_rad)
def reset(self, x, y):
self.x = x
self.y = y
self.ball_rad = 8
self.rect = Rect(x, y, self.ball_rad * 2, self.ball_rad * 2)
self.speed_x = -16
self.speed_y = 16
self.winner = 0# 1 is the player and -1 is the CPU
#create paddles
player_paddle = paddle(screen_width - 40, screen_height // 2)
cpu_paddle = paddle(20, screen_height // 2)
#create pong ball
pong = ball(screen_width - 60, screen_height // 2 + 50)
#create game loop
run = True
while run:
frame, overlayed, matimg, finger_inputs = cv.frame_step()
xp, yp, grid_row, grid_col, index_finger, two_fingers = finger_inputs
fpsClock.tick(fps)
# cv paddle control
if two_fingers:
print(f'yp: {yp}')
player_paddle = paddle(screen_width - 40, yp)
print(f'paddle y: {player_paddle.y}')
draw_board()
draw_text('CPU: ' + str(cpu_score), font, white, 20, 15)
draw_text('P1: ' + str(player_score), font, white, screen_width - 100, 15)
draw_text('BALL SPEED: ' + str(abs(pong.speed_x)), font, white, screen_width // 2 - 100 , 15)
#draw paddles
player_paddle.draw()
cpu_paddle.draw()
if live_ball == True:
speed_increase += 1
winner = pong.move()
if winner == 0:
#draw ball
pong.draw()
#move paddles
player_paddle.move()
cpu_paddle.ai()
else:
live_ball = False
if winner == 1:
player_score += 1
elif winner == -1:
cpu_score += 1
#print player instructions
if live_ball == False:
if winner == 0:
draw_text('CLICK ANYWHERE TO START', font, white, 100, screen_height // 2 -100)
if winner == 1:
draw_text('YOU SCORED!', font, white, 220, screen_height // 2 -100)
draw_text('CLICK ANYWHERE TO START', font, white, 100, screen_height // 2 -50)
if winner == -1:
draw_text('CPU SCORED!', font, white, 220, screen_height // 2 -100)
draw_text('CLICK ANYWHERE TO START', font, white, 100, screen_height // 2 -50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN and live_ball == False:
live_ball = True
pong.reset(screen_width - 60, screen_height // 2 + 50)
if speed_increase > 500:
speed_increase = 0
if pong.speed_x < 0:
pong.speed_x -= 1
if pong.speed_x > 0:
pong.speed_x += 1
if pong.speed_y < 0:
pong.speed_y -= 1
if pong.speed_y > 0:
pong.speed_y += 1
pygame.display.update()
frame_img = capture_frame()
cv2.imshow('frame cap', frame_img)
pygameoverlayed = cv2.addWeighted(frame_img, 0.3, overlayed, 0.7, 0)
cv2.imshow('game overlayed', cv2.resize(pygameoverlayed, (600, 500)))
pygame.quit()