Skip to content

Commit 85fd9cc

Browse files
Merge pull request #1279 from siddharth2016/add-pongpong
Add pongpong, a game made using pyglet
2 parents ae4d962 + 68a098f commit 85fd9cc

File tree

9 files changed

+198
-0
lines changed

9 files changed

+198
-0
lines changed

PongPong_Game/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PongPong
2+
3+
Are you just starting your Game Development journey ?
4+
5+
Do you want to learn something new ?
6+
7+
PongPong, a game that every developer should try their hands on !
8+
9+
I really enjoyed making this game when I went ahead and completed a task from Zero To Mastery Academy monthly challenge.
10+
11+
It was super fun learning something new, the basics of game development and how to view a game as just like a geometry plane to work with, was simply mind blowing for me.
12+
13+
I chose pyglet for development work, motivation behind this was to completely learn something new and not to work with the good old pygame !
14+
15+
Go through the following parts to get familiar with pyglet game development style:
16+
17+
1. [Making PONGPONG - Game Development using Pyglet - Part 1](https://blog.codekaro.info/making-pongpong-game-development-using-pyglet-part-1)
18+
2. [Making PONGPONG - Game Development using Pyglet - Part 2](https://blog.codekaro.info/making-pongpong-game-development-using-pyglet-part-2)
19+
3. [Making PONGPONG - Game Development using Pyglet - Part 3](https://blog.codekaro.info/making-pongpong-game-development-using-pyglet-part-3)
20+
21+
I really loved writing my experience and how I approached the problem, hoping you will find it insightful, will learn something new and get to know basics of developing a game like PongPong.
22+
23+
---
24+
25+
Library used:
26+
27+
**pyglet**
28+
29+
Install to your virtual environment or global using pip:
30+
31+
*pip install pyglet*
32+
33+
Game Play Demo on MacOS:
34+
35+
![Game_play on mac](pong_game_play.gif)
36+
37+
---
38+
39+
*Actual game was developed using Pygame shown in a youtube tutorial and can be found at [Pong, Python & Pygame](https://www.youtube.com/watch?v=JRLdbt7vK-E)*

PongPong_Game/pong/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

PongPong_Game/pong/ball.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ./PongPong/pong/ball.py
2+
3+
import pyglet
4+
import random
5+
from typing import Tuple
6+
7+
8+
class BallObject(pyglet.shapes.Circle):
9+
10+
def __init__(self, *args, **kwargs):
11+
super(BallObject, self).__init__(*args, **kwargs)
12+
self.color = (255, 180, 0)
13+
self.velocity_x, self.velocity_y = 0.0, 0.0
14+
15+
def update(self, win_size: Tuple, border: Tuple, other_object, dt) -> None:
16+
speed = [2.37, 2.49, 2.54, 2.62, 2.71, 2.85, 2.96, 3.08, 3.17, 3.25] # more choices more randomness
17+
rn = random.choice(speed)
18+
newx = self.x + self.velocity_x
19+
newy = self.y + self.velocity_y
20+
21+
if newx < border + self.radius or newx > win_size[0] - border - self.radius:
22+
self.velocity_x = -(self.velocity_x/abs(self.velocity_x))*rn
23+
elif newy > win_size[1] - border - self.radius:
24+
self.velocity_y = -(self.velocity_y/abs(self.velocity_y))*rn
25+
elif (newy-self.radius < other_object.height) and (other_object.x <= newx <= other_object.rightx):
26+
self.velocity_y = -(self.velocity_y/abs(self.velocity_y))*rn
27+
else:
28+
self.x = newx
29+
self.y = newy

PongPong_Game/pong/load.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ./PongPong/pong/load.py
2+
3+
from . import ball, paddle, rectangle
4+
from typing import Tuple
5+
6+
def load_balls(win_size : Tuple, radius : float, speed : Tuple, batch=None):
7+
balls = []
8+
ball_x = win_size[0]/2
9+
ball_y = win_size[1]/2
10+
new_ball = ball.BallObject(x=ball_x, y=ball_y, radius=radius, batch=batch)
11+
new_ball.velocity_x, new_ball.velocity_y = speed[0], speed[1]
12+
balls.append(new_ball)
13+
return balls
14+
15+
16+
def load_paddles(paddle_pos : Tuple, width : float, height : float, acc : Tuple, batch=None):
17+
paddles = []
18+
new_paddle = paddle.Paddle(x=paddle_pos[0], y=paddle_pos[1], width=width, height=height, batch=batch)
19+
new_paddle.rightx = new_paddle.x + width
20+
new_paddle.acc_left, new_paddle.acc_right = acc[0], acc[1]
21+
paddles.append(new_paddle)
22+
return paddles
23+
24+
25+
def load_rectangles(win_size : Tuple, border : float, batch=None):
26+
rectangles = []
27+
top = rectangle.RectangleObject(x=0, y=win_size[1]-border, width=win_size[0], height=border, batch=batch)
28+
left = rectangle.RectangleObject(x=0, y=0, width=border, height=win_size[1], batch=batch)
29+
right = rectangle.RectangleObject(x=win_size[0] - border, y=0, width=border, height=win_size[1], batch=batch)
30+
rectangles.extend([left, top, right])
31+
return rectangles

PongPong_Game/pong/paddle.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ./PongPong/pong/paddle.py
2+
3+
import pyglet
4+
from pyglet.window import key
5+
from typing import Tuple
6+
7+
8+
class Paddle(pyglet.shapes.Rectangle):
9+
10+
def __init__(self, *args, **kwargs):
11+
super(Paddle, self).__init__(*args, **kwargs)
12+
13+
self.acc_left, self.acc_right = 0.0, 0.0
14+
self.rightx = 0
15+
self.key_handler = key.KeyStateHandler()
16+
self.event_handlers = [self, self.key_handler]
17+
18+
def update(self, win_size : Tuple, border : float, other_object, dt):
19+
20+
newlx = self.x + self.acc_left
21+
newrx = self.x + self.acc_right
22+
23+
if self.key_handler[key.LEFT]:
24+
self.x = newlx
25+
elif self.key_handler[key.RIGHT]:
26+
self.x = newrx
27+
28+
self.rightx = self.x + self.width
29+
30+
if self.x < border:
31+
self.x = border
32+
self.rightx = self.x + self.width
33+
elif self.rightx > win_size[0]-border:
34+
self.x = win_size[0]-border-self.width
35+
self.rightx = self.x + self.width

PongPong_Game/pong/rectangle.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ./PongPong/pong/rectangle.py
2+
3+
import pyglet
4+
5+
class RectangleObject(pyglet.shapes.Rectangle):
6+
7+
def __init__(self, *args, **kwargs):
8+
super(RectangleObject, self).__init__(*args, **kwargs)

PongPong_Game/pong_game_play.gif

342 KB
Loading

PongPong_Game/pongpong.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# ./PongPong/pongpong.py
2+
3+
import pyglet
4+
from pong import load
5+
6+
# Variables, Considering a vertical oriented window for game
7+
WIDTH = 600 # Game Window Width
8+
HEIGHT = 600 # Game Window Height
9+
BORDER = 10 # Walls Thickness/Border Thickness
10+
RADIUS = 12 # Ball Radius
11+
PWIDTH = 120 # Paddle Width
12+
PHEIGHT = 15 # Paddle Height
13+
ballspeed = (-2, -2) # Initially ball will be falling with speed (x, y)
14+
paddleacc = (-5, 5) # Paddle Acceleration on both sides - left: negative acc, right: positive acc, for x-axis
15+
16+
17+
class PongPongWindow(pyglet.window.Window):
18+
def __init__(self, *args, **kwargs):
19+
super(PongPongWindow, self).__init__(*args, **kwargs)
20+
21+
self.win_size = (WIDTH, HEIGHT)
22+
self.paddle_pos = (WIDTH/2-PWIDTH/2, 0)
23+
self.main_batch = pyglet.graphics.Batch()
24+
self.walls = load.load_rectangles(self.win_size, BORDER, batch=self.main_batch)
25+
self.balls = load.load_balls(self.win_size, RADIUS, speed=ballspeed, batch=self.main_batch)
26+
self.paddles = load.load_paddles(self.paddle_pos, PWIDTH, PHEIGHT, acc=paddleacc, batch=self.main_batch)
27+
28+
def on_draw(self):
29+
self.clear()
30+
self.main_batch.draw()
31+
32+
33+
game_window = PongPongWindow(width=WIDTH, height=HEIGHT, caption='PongPong')
34+
game_objects = game_window.balls + game_window.paddles
35+
36+
for paddle in game_window.paddles:
37+
for handler in paddle.event_handlers:
38+
game_window.push_handlers(handler)
39+
40+
41+
42+
def update(dt):
43+
global game_objects, game_window
44+
45+
for obj1 in game_objects:
46+
for obj2 in game_objects:
47+
if obj1 is obj2:
48+
continue
49+
obj1.update(game_window.win_size, BORDER, obj2, dt)
50+
51+
52+
if __name__ == '__main__':
53+
pyglet.clock.schedule_interval(update, 1/120.0)
54+
pyglet.app.run()

PongPong_Game/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyglet==1.5.14

0 commit comments

Comments
 (0)