Skip to content

Commit a344c4a

Browse files
committed
added ping pong
1 parent 6ae406d commit a344c4a

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

PingPong/Ball.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pygame
2+
pygame.init()
3+
4+
class Ball:
5+
6+
def __init__(self, pos, vel, win, rad, minCoord, maxCoord):
7+
8+
self.pos = pos
9+
self.vel = vel
10+
self.win = win
11+
self.rad = rad
12+
self.minCoord = minCoord
13+
self.maxCoord = maxCoord
14+
15+
16+
def drawBall(self):
17+
18+
pygame.draw.circle(self.win, (255,)*3, self.pos, self.rad, 0)
19+
20+
21+
def doHorizontalFlip(self):
22+
23+
self.vel[0] *= -1
24+
25+
26+
def doVerticalFlip(self):
27+
28+
self.vel[1] *= -1
29+
30+
31+
def borderCollisionCheck(self):
32+
33+
if (self.pos[0] <= self.minCoord[0]) or (self.pos[0] >= self.maxCoord[0]):
34+
35+
self.doHorizontalFlip()
36+
37+
if (self.pos[1] <= self.minCoord[1]) or (self.pos[1] >= self.maxCoord[1]):
38+
39+
self.doVerticalFlip()
40+
41+
42+
def updatePos(self):
43+
44+
self.pos = [self.pos[0]+self.vel[0], self.pos[1]+self.vel[1]]
45+
46+
47+
def checkSlabCollision(self, slabPos): # slab pos = [xmin, ymin, xmax, ymax]
48+
if (
49+
self.pos[0] + self.rad > slabPos[0]
50+
and self.pos[0] - self.rad < slabPos[2]
51+
and self.pos[1] + self.rad > slabPos[1]
52+
and self.pos[1] - self.rad < slabPos[3]
53+
):
54+
# Handle collision here (e.g., reverse ball's direction)
55+
if self.pos[0] < slabPos[0] or self.pos[0] > slabPos[2]:
56+
self.vel[0] *= -1
57+
if self.pos[1] < slabPos[1] or self.pos[1] > slabPos[3]:
58+
self.vel[1] *= -1

PingPong/Slab.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pygame
2+
pygame.init()
3+
4+
class Slab:
5+
def __init__(self, win, size, pos, player, minPos, maxPos):
6+
self.win = win
7+
self.size = size
8+
self.pos = pos
9+
self.player = player #player = 1 or 2
10+
self.minPos = minPos
11+
self.maxPos = maxPos
12+
13+
14+
def draw(self):
15+
pygame.draw.rect(self.win, (255, 255, 255), (self.pos[0], self.pos[1], self.size[0], self.size[1]))
16+
17+
def getCoords(self):
18+
return [self.pos[0], self.pos[1], self.pos[0] + self.size[0], self.pos[1] + self.size[1]]
19+
20+
def updatePos(self):
21+
keys = pygame.key.get_pressed()
22+
if self.player == 1:
23+
if keys[pygame.K_UP] and self.getCoords()[1]> self.minPos[1]:
24+
self.pos[1] -= 0.3
25+
if keys[pygame.K_DOWN] and self.getCoords()[3]< self.maxPos[1]:
26+
self.pos[1] += 0.3
27+
else:
28+
if keys[pygame.K_w] and self.getCoords()[1]> self.minPos[1]:
29+
self.pos[1] -= 0.3
30+
if keys[pygame.K_s] and self.getCoords()[3]< self.maxPos[1]:
31+
self.pos[1] += 0.3

PingPong/main.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from Ball import Ball
2+
from Slab import Slab
3+
import pygame
4+
5+
WIDTH = 600
6+
HEIGHT = 600
7+
BLACK = (0,0,0)
8+
WHITE = (255,)*3
9+
pygame.init()
10+
11+
win = pygame.display.set_mode((WIDTH, HEIGHT ))
12+
13+
print("Controls: W&S for player 1 and arrow up and down for player 2")
14+
15+
ball = Ball([300,300 ], [0.3,0.1], win, 10, (0,0), (600,600))
16+
slab = Slab(win, [10,100], [500, 300], 1, (0, 0), (600, 600))
17+
slab2 = Slab(win, [10,100], [100, 300], 2, (0, 0), (600, 600))
18+
run = True
19+
while run:
20+
for event in pygame.event.get():
21+
if event.type == pygame.QUIT:
22+
run = False
23+
24+
keys = pygame.key.get_pressed()
25+
win.fill(BLACK)
26+
27+
ball.borderCollisionCheck()
28+
ball.checkSlabCollision(slab.getCoords())
29+
ball.checkSlabCollision(slab2.getCoords())
30+
ball.updatePos()
31+
ball.drawBall()
32+
33+
slab.updatePos()
34+
slab.draw()
35+
36+
slab2.updatePos()
37+
slab2.draw()
38+
39+
pygame.display.update()
40+
pygame.quit()

0 commit comments

Comments
 (0)