Skip to content

Commit 25b82f6

Browse files
Initial commit
0 parents  commit 25b82f6

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.ipynb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f4169895-9110-4d6a-8552-3ee0d49d8270",
6+
"metadata": {},
7+
"source": [
8+
"# pygame-ping - simple ball game with python and pygame"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "e5363834-a363-4313-9ba8-73a15855c68a",
14+
"metadata": {},
15+
"source": [
16+
"I made a simple game with python and pygame\n",
17+
"\n",
18+
"A classic table tennis game"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "1a3ce75c-ed7c-483f-a295-716fc6205037",
24+
"metadata": {},
25+
"source": [
26+
"<img src=\"img/step1.gif\" width=\"320\" align=\"left\"><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"id": "74ce2b0b-7b66-45ee-8907-34a6ca2c8e48",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": []
36+
}
37+
],
38+
"metadata": {
39+
"kernelspec": {
40+
"display_name": "Rust",
41+
"language": "rust",
42+
"name": "rust"
43+
},
44+
"language_info": {
45+
"codemirror_mode": "rust",
46+
"file_extension": ".rs",
47+
"mimetype": "text/rust",
48+
"name": "Rust",
49+
"pygment_lexer": "rust",
50+
"version": ""
51+
}
52+
},
53+
"nbformat": 4,
54+
"nbformat_minor": 5
55+
}

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# pygame-ping - simple ball game with python and pygame
2+
3+
I made a simple game with python and pygame
4+
5+
A classic table tennis game
6+
7+
<img src="img/step1.gif" width="320" align="left"><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
8+
9+
10+
```Rust
11+
12+
```

img/step1.gif

445 KB
Loading

img/step1.mov

656 KB
Binary file not shown.

ping.py

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import pygame
2+
3+
FRAMES_PER_SECOND = 30
4+
5+
6+
class Ping:
7+
HEIGHT = 600
8+
WIDTH = 800
9+
10+
PADDLE_WIDTH = 10
11+
PADDLE_HEIGHT = 100
12+
PADDLE_VELOCITY = 10
13+
14+
BALL_WIDTH = 10
15+
BALL_VELOCITY = 160
16+
17+
COLOR = (255, 255, 255)
18+
BK_COLOR = (0, 0, 0)
19+
20+
def __init__(self):
21+
pygame.init() # pygame instanz starten
22+
23+
self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) # Bildschirm
24+
pygame.display.set_caption("Ping")
25+
26+
self.clock = pygame.time.Clock() # Uhr
27+
28+
self.score_left = 0
29+
self.score_right = 0
30+
31+
self.central_line = pygame.Rect(self.WIDTH / 2, 0, 1, self.HEIGHT)
32+
33+
self.paddle_left = Paddle(0, self.HEIGHT / 2 - self.PADDLE_HEIGHT / 2,
34+
self.PADDLE_WIDTH, self.PADDLE_HEIGHT,
35+
self.COLOR, pygame.K_w, pygame.K_s,
36+
self.PADDLE_VELOCITY)
37+
38+
self.paddle_right = Paddle(self.WIDTH - self.PADDLE_WIDTH, self.HEIGHT / 2 - self.PADDLE_HEIGHT / 2,
39+
self.PADDLE_WIDTH, self.PADDLE_HEIGHT,
40+
self.COLOR, pygame.K_UP, pygame.K_DOWN,
41+
self.PADDLE_VELOCITY)
42+
43+
self.ball = Ball(self.WIDTH / 2 - self.BALL_WIDTH, self.HEIGHT / 2 - self.BALL_WIDTH / 2,
44+
self.PADDLE_WIDTH, self.COLOR,
45+
self.BALL_VELOCITY)
46+
47+
def game_loop(self):
48+
while True:
49+
for event in pygame.event.get():
50+
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) \
51+
or (event.type == pygame.QUIT):
52+
return
53+
54+
self.update()
55+
self.draw()
56+
57+
def draw(self):
58+
self.screen.fill(self.BK_COLOR)
59+
self.paddle_left.draw(self.screen)
60+
self.paddle_right.draw(self.screen)
61+
self.ball.draw(self.screen)
62+
pygame.draw.rect(self.screen, self.COLOR, self.central_line)
63+
64+
font = pygame.font.Font(None, 74)
65+
text = font.render(str(self.score_left), 1, self.COLOR)
66+
self.screen.blit(text, (self.WIDTH / 4, 10))
67+
text = font.render(str(self.score_right), 1, self.COLOR)
68+
self.screen.blit(text, (self.WIDTH / 4 * 3, 10))
69+
70+
pygame.display.flip()
71+
72+
def update(self):
73+
delta_time = self.clock.tick(FRAMES_PER_SECOND)
74+
self.paddle_left.update()
75+
self.ball.update(delta_time)
76+
if self.ball.collider_rect.colliderect(self.paddle_left.rect) \
77+
or self.ball.collider_rect.colliderect(self.paddle_right.rect):
78+
self.ball.collides()
79+
80+
if self.ball.pos.x - self.ball.radius < 0:
81+
self.score_right += 1
82+
self.ball.start_pos()
83+
if self.ball.pos.x + self.ball.radius > self.WIDTH:
84+
self.score_left += 1
85+
self.ball.start_pos()
86+
87+
self.paddle_right.update()
88+
89+
90+
class Paddle:
91+
92+
def __init__(self, left, top, width, height, color, up_key, down_key, velocity):
93+
self.rect = pygame.Rect(left, top, width, height)
94+
self._color = color
95+
self._up_key = up_key
96+
self._down_key = down_key
97+
self._velocity = velocity
98+
99+
def draw(self, surface):
100+
pygame.draw.rect(surface, self._color, self.rect)
101+
102+
def update(self):
103+
keys_pressed = pygame.key.get_pressed()
104+
105+
if keys_pressed[self._up_key]:
106+
if self.rect.y - self._velocity > 0:
107+
self.rect.move_ip(0, - self._velocity)
108+
if keys_pressed[self._down_key]:
109+
if self.rect.y + self._velocity < Ping.HEIGHT - self.rect.height:
110+
self.rect.move_ip(0, self._velocity)
111+
112+
113+
class Ball:
114+
def __init__(self, left, top, width, color, velocity):
115+
self.pos = pygame.math.Vector2(left, top)
116+
self._start_pos = pygame.math.Vector2(left, top)
117+
self.radius = int(width / 2)
118+
self._color = color
119+
self._velocity = velocity
120+
self._direction = pygame.math.Vector2(1, 1)
121+
self._start_direction = pygame.math.Vector2(1, 1)
122+
self._direction.normalize_ip()
123+
self.collider_rect = None
124+
self._set_collider_rect()
125+
126+
def _set_collider_rect(self):
127+
self.collider_rect = pygame.Rect(self.pos.x - self.radius, self.pos.y - self.radius, self.radius * 2,
128+
self.radius * 2)
129+
130+
def draw(self, surface):
131+
pygame.draw.circle(surface, self._color, (int(self.pos.x), int(self.pos.y)), int(self.radius))
132+
133+
def update(self, delta_time):
134+
self.pos += self._direction * self._velocity * delta_time / 1000
135+
self._set_collider_rect()
136+
if self.pos.y - self.radius < 0 or self.pos.y + self.radius > Ping.HEIGHT:
137+
self._direction.y = - self._direction.y
138+
139+
def start_pos(self):
140+
self.pos = pygame.math.Vector2(self._start_pos)
141+
self._direction = pygame.math.Vector2(self._start_direction)
142+
143+
def collides(self):
144+
self._direction.x = - self._direction.x
145+
146+
147+
if __name__ == '__main__':
148+
ping = Ping()
149+
ping.game_loop()

0 commit comments

Comments
 (0)