File size: 3,165 Bytes
caa3361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pyglet
from pyglet.window import key

# Set up constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 80
BALL_RADIUS = 10
PADDLE_SPEED = 5
BALL_SPEED = 3

class PongGame(pyglet.window.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Pong Game")
        self.player_paddle = pyglet.shapes.Rectangle(PADDLE_WIDTH // 2, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT, color=(255, 255, 255))
        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))
        self.ball = pyglet.shapes.Circle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, BALL_RADIUS, color=(255, 255, 255))
        self.player_score = 0
        self.opponent_score = 0
        self.ball_speed_x = BALL_SPEED
        self.ball_speed_y = BALL_SPEED
        self.player_speed = 0

    def on_draw(self):
        self.clear()
        self.player_paddle.draw()
        self.opponent_paddle.draw()
        self.ball.draw()
        score_text = f"Player: {self.player_score}   Opponent: {self.opponent_score}"
        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()

    def on_key_press(self, symbol, modifiers):
        if symbol == key.UP:
            self.player_speed = PADDLE_SPEED
        elif symbol == key.DOWN:
            self.player_speed = -PADDLE_SPEED

    def on_key_release(self, symbol, modifiers):
        if symbol == key.UP or symbol == key.DOWN:
            self.player_speed = 0

    def update(self, dt):
        self.player_paddle.y += self.player_speed
        self.opponent_paddle.y = self.ball.y - PADDLE_HEIGHT // 2

        if self.player_paddle.y < 0:
            self.player_paddle.y = 0
        elif self.player_paddle.y > SCREEN_HEIGHT - PADDLE_HEIGHT:
            self.player_paddle.y = SCREEN_HEIGHT - PADDLE_HEIGHT

        self.ball.x += self.ball_speed_x
        self.ball.y += self.ball_speed_y

        if self.ball.y < 0 or self.ball.y > SCREEN_HEIGHT:
            self.ball_speed_y = -self.ball_speed_y

        if (self.ball.x < self.player_paddle.x + PADDLE_WIDTH and
            self.player_paddle.y < self.ball.y < self.player_paddle.y + PADDLE_HEIGHT):
            self.ball_speed_x = -self.ball_speed_x
        elif (self.ball.x > self.opponent_paddle.x - PADDLE_WIDTH and
              self.opponent_paddle.y < self.ball.y < self.opponent_paddle.y + PADDLE_HEIGHT):
            self.ball_speed_x = -self.ball_speed_x

        if self.ball.x < 0:
            self.opponent_score += 1
            self.ball_restart()
        elif self.ball.x > SCREEN_WIDTH:
            self.player_score += 1
            self.ball_restart()

    def ball_restart(self):
        self.ball.x = SCREEN_WIDTH // 2
        self.ball.y = SCREEN_HEIGHT // 2
        self.ball_speed_x = -self.ball_speed_x
        self.ball_speed_y = -self.ball_speed_y

if __name__ == "__main__":
    game = PongGame()
    pyglet.clock.schedule_interval(game.update, 1/120.0)
    pyglet.app.run()