awacke1 commited on
Commit
093ecd6
·
verified ·
1 Parent(s): 1e5d149

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import arcade
2
+
3
+ # Set up constants
4
+ SCREEN_WIDTH = 800
5
+ SCREEN_HEIGHT = 600
6
+ PADDLE_WIDTH = 10
7
+ PADDLE_HEIGHT = 80
8
+ BALL_RADIUS = 10
9
+ PADDLE_SPEED = 5
10
+ BALL_SPEED = 3
11
+
12
+ class PongGame(arcade.Window):
13
+ def __init__(self):
14
+ super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Pong Game")
15
+ self.player_paddle = None
16
+ self.opponent_paddle = None
17
+ self.ball = None
18
+ self.player_score = 0
19
+ self.opponent_score = 0
20
+
21
+ def setup(self):
22
+ self.player_paddle = arcade.SpriteSolidColor(PADDLE_WIDTH, PADDLE_HEIGHT, arcade.color.WHITE)
23
+ self.player_paddle.center_x = PADDLE_WIDTH // 2
24
+ self.player_paddle.center_y = SCREEN_HEIGHT // 2
25
+
26
+ self.opponent_paddle = arcade.SpriteSolidColor(PADDLE_WIDTH, PADDLE_HEIGHT, arcade.color.WHITE)
27
+ self.opponent_paddle.center_x = SCREEN_WIDTH - PADDLE_WIDTH // 2
28
+ self.opponent_paddle.center_y = SCREEN_HEIGHT // 2
29
+
30
+ self.ball = arcade.SpriteSolidColor(BALL_RADIUS * 2, BALL_RADIUS * 2, arcade.color.WHITE)
31
+ self.ball.center_x = SCREEN_WIDTH // 2
32
+ self.ball.center_y = SCREEN_HEIGHT // 2
33
+ self.ball.change_x = BALL_SPEED
34
+ self.ball.change_y = BALL_SPEED
35
+
36
+ def on_draw(self):
37
+ arcade.start_render()
38
+ self.player_paddle.draw()
39
+ self.opponent_paddle.draw()
40
+ self.ball.draw()
41
+ arcade.draw_text(f"Player: {self.player_score}", 10, SCREEN_HEIGHT - 20, arcade.color.WHITE, 14)
42
+ arcade.draw_text(f"Opponent: {self.opponent_score}", SCREEN_WIDTH - 100, SCREEN_HEIGHT - 20, arcade.color.WHITE, 14)
43
+
44
+ def on_update(self, delta_time):
45
+ self.player_paddle.center_y += self.player_paddle.change_y
46
+ self.opponent_paddle.center_y = self.ball.center_y
47
+
48
+ if self.player_paddle.top > SCREEN_HEIGHT:
49
+ self.player_paddle.top = SCREEN_HEIGHT
50
+ if self.player_paddle.bottom < 0:
51
+ self.player_paddle.bottom = 0
52
+
53
+ self.ball.update()
54
+
55
+ if self.ball.collides_with_sprite(self.player_paddle) or self.ball.collides_with_sprite(self.opponent_paddle):
56
+ self.ball.change_x *= -1
57
+
58
+ if self.ball.top > SCREEN_HEIGHT or self.ball.bottom < 0:
59
+ self.ball.change_y *= -1
60
+
61
+ if self.ball.left < 0:
62
+ self.opponent_score += 1
63
+ self.ball_restart()
64
+ elif self.ball.right > SCREEN_WIDTH:
65
+ self.player_score += 1
66
+ self.ball_restart()
67
+
68
+ def on_key_press(self, key, modifiers):
69
+ if key == arcade.key.UP:
70
+ self.player_paddle.change_y = PADDLE_SPEED
71
+ elif key == arcade.key.DOWN:
72
+ self.player_paddle.change_y = -PADDLE_SPEED
73
+
74
+ def on_key_release(self, key, modifiers):
75
+ if key == arcade.key.UP or key == arcade.key.DOWN:
76
+ self.player_paddle.change_y = 0
77
+
78
+ def ball_restart(self):
79
+ self.ball.center_x = SCREEN_WIDTH // 2
80
+ self.ball.center_y = SCREEN_HEIGHT // 2
81
+ self.ball.change_x *= -1
82
+ self.ball.change_y *= -1
83
+
84
+ def main():
85
+ game = PongGame()
86
+ game.setup()
87
+ arcade.run()
88
+
89
+ if __name__ == "__main__":
90
+ main()