|
| 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