awacke1 commited on
Commit
7a8a0eb
·
verified ·
1 Parent(s): ed9dbe9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pyglet
2
+ from pyglet.window import key
3
+
4
+ # Set up constants
5
+ SCREEN_WIDTH = 800
6
+ SCREEN_HEIGHT = 600
7
+ PADDLE_WIDTH = 10
8
+ PADDLE_HEIGHT = 80
9
+ BALL_RADIUS = 10
10
+ PADDLE_SPEED = 5
11
+ BALL_SPEED = 3
12
+
13
+ class PongGame(pyglet.window.Window):
14
+ def __init__(self):
15
+ super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Pong Game")
16
+ self.player_paddle = pyglet.shapes.Rectangle(PADDLE_WIDTH // 2, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT, color=(255, 255, 255))
17
+ self.opponent_paddle = pyglet.shapes.Rectangle(SCREEN_WIDTH - PADDLE_WIDTH // 2, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT, color=(255, 255, 255))
18
+ self.ball = pyglet.shapes.Circle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, BALL_RADIUS, color=(255, 255, 255))
19
+ self.player_score = 0
20
+ self.opponent_score = 0
21
+ self.ball_speed_x = BALL_SPEED
22
+ self.ball_speed_y = BALL_SPEED
23
+ self.player_speed = 0
24
+
25
+ def on_draw(self):
26
+ self.clear()
27
+ self.player_paddle.draw()
28
+ self.opponent_paddle.draw()
29
+ self.ball.draw()
30
+ score_text = f"Player: {self.player_score} Opponent: {self.opponent_score}"
31
+ pyglet.text.Label(score_text, font_size=14, x=SCREEN_WIDTH // 2, y=SCREEN_HEIGHT - 20, anchor_x='center', color=(255, 255, 255, 255)).draw()
32
+
33
+ def on_key_press(self, symbol, modifiers):
34
+ if symbol == key.UP:
35
+ self.player_speed = PADDLE_SPEED
36
+ elif symbol == key.DOWN:
37
+ self.player_speed = -PADDLE_SPEED
38
+
39
+ def on_key_release(self, symbol, modifiers):
40
+ if symbol == key.UP or symbol == key.DOWN:
41
+ self.player_speed = 0
42
+
43
+ def update(self, dt):
44
+ self.player_paddle.y += self.player_speed
45
+ self.opponent_paddle.y = self.ball.y - PADDLE_HEIGHT // 2
46
+
47
+ if self.player_paddle.y < 0:
48
+ self.player_paddle.y = 0
49
+ elif self.player_paddle.y > SCREEN_HEIGHT - PADDLE_HEIGHT:
50
+ self.player_paddle.y = SCREEN_HEIGHT - PADDLE_HEIGHT
51
+
52
+ self.ball.x += self.ball_speed_x
53
+ self.ball.y += self.ball_speed_y
54
+
55
+ if self.ball.y < 0 or self.ball.y > SCREEN_HEIGHT:
56
+ self.ball_speed_y = -self.ball_speed_y
57
+
58
+ if (self.ball.x < self.player_paddle.x + PADDLE_WIDTH and
59
+ self.player_paddle.y < self.ball.y < self.player_paddle.y + PADDLE_HEIGHT):
60
+ self.ball_speed_x = -self.ball_speed_x
61
+ elif (self.ball.x > self.opponent_paddle.x - PADDLE_WIDTH and
62
+ self.opponent_paddle.y < self.ball.y < self.opponent_paddle.y + PADDLE_HEIGHT):
63
+ self.ball_speed_x = -self.ball_speed_x
64
+
65
+ if self.ball.x < 0:
66
+ self.opponent_score += 1
67
+ self.ball_restart()
68
+ elif self.ball.x > SCREEN_WIDTH:
69
+ self.player_score += 1
70
+ self.ball_restart()
71
+
72
+ def ball_restart(self):
73
+ self.ball.x = SCREEN_WIDTH // 2
74
+ self.ball.y = SCREEN_HEIGHT // 2
75
+ self.ball_speed_x = -self.ball_speed_x
76
+ self.ball_speed_y = -self.ball_speed_y
77
+
78
+ if __name__ == "__main__":
79
+ game = PongGame()
80
+ pyglet.clock.schedule_interval(game.update, 1/120.0)
81
+ pyglet.app.run()