-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2032 from anuragdaksh7/master
added ping pong
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |