Skip to content

Commit

Permalink
Merge pull request #2032 from anuragdaksh7/master
Browse files Browse the repository at this point in the history
added ping pong
  • Loading branch information
geekcomputers authored Nov 9, 2023
2 parents 6ae406d + a344c4a commit 24af0b3
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
58 changes: 58 additions & 0 deletions PingPong/Ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pygame
pygame.init()

class Ball:

def __init__(self, pos, vel, win, rad, minCoord, maxCoord):

self.pos = pos
self.vel = vel
self.win = win
self.rad = rad
self.minCoord = minCoord
self.maxCoord = maxCoord


def drawBall(self):

pygame.draw.circle(self.win, (255,)*3, self.pos, self.rad, 0)


def doHorizontalFlip(self):

self.vel[0] *= -1


def doVerticalFlip(self):

self.vel[1] *= -1


def borderCollisionCheck(self):

if (self.pos[0] <= self.minCoord[0]) or (self.pos[0] >= self.maxCoord[0]):

self.doHorizontalFlip()

if (self.pos[1] <= self.minCoord[1]) or (self.pos[1] >= self.maxCoord[1]):

self.doVerticalFlip()


def updatePos(self):

self.pos = [self.pos[0]+self.vel[0], self.pos[1]+self.vel[1]]


def checkSlabCollision(self, slabPos): # slab pos = [xmin, ymin, xmax, ymax]
if (
self.pos[0] + self.rad > slabPos[0]
and self.pos[0] - self.rad < slabPos[2]
and self.pos[1] + self.rad > slabPos[1]
and self.pos[1] - self.rad < slabPos[3]
):
# Handle collision here (e.g., reverse ball's direction)
if self.pos[0] < slabPos[0] or self.pos[0] > slabPos[2]:
self.vel[0] *= -1
if self.pos[1] < slabPos[1] or self.pos[1] > slabPos[3]:
self.vel[1] *= -1
31 changes: 31 additions & 0 deletions PingPong/Slab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pygame
pygame.init()

class Slab:
def __init__(self, win, size, pos, player, minPos, maxPos):
self.win = win
self.size = size
self.pos = pos
self.player = player #player = 1 or 2
self.minPos = minPos
self.maxPos = maxPos


def draw(self):
pygame.draw.rect(self.win, (255, 255, 255), (self.pos[0], self.pos[1], self.size[0], self.size[1]))

def getCoords(self):
return [self.pos[0], self.pos[1], self.pos[0] + self.size[0], self.pos[1] + self.size[1]]

def updatePos(self):
keys = pygame.key.get_pressed()
if self.player == 1:
if keys[pygame.K_UP] and self.getCoords()[1]> self.minPos[1]:
self.pos[1] -= 0.3
if keys[pygame.K_DOWN] and self.getCoords()[3]< self.maxPos[1]:
self.pos[1] += 0.3
else:
if keys[pygame.K_w] and self.getCoords()[1]> self.minPos[1]:
self.pos[1] -= 0.3
if keys[pygame.K_s] and self.getCoords()[3]< self.maxPos[1]:
self.pos[1] += 0.3
40 changes: 40 additions & 0 deletions PingPong/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from Ball import Ball
from Slab import Slab
import pygame

WIDTH = 600
HEIGHT = 600
BLACK = (0,0,0)
WHITE = (255,)*3
pygame.init()

win = pygame.display.set_mode((WIDTH, HEIGHT ))

print("Controls: W&S for player 1 and arrow up and down for player 2")

ball = Ball([300,300 ], [0.3,0.1], win, 10, (0,0), (600,600))
slab = Slab(win, [10,100], [500, 300], 1, (0, 0), (600, 600))
slab2 = Slab(win, [10,100], [100, 300], 2, (0, 0), (600, 600))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()
win.fill(BLACK)

ball.borderCollisionCheck()
ball.checkSlabCollision(slab.getCoords())
ball.checkSlabCollision(slab2.getCoords())
ball.updatePos()
ball.drawBall()

slab.updatePos()
slab.draw()

slab2.updatePos()
slab2.draw()

pygame.display.update()
pygame.quit()

0 comments on commit 24af0b3

Please sign in to comment.