|
1 | 1 | import pygame
|
2 | 2 | import sys
|
3 | 3 |
|
| 4 | + |
| 5 | +def ball_animation(): |
| 6 | + # Fixing Bugs |
| 7 | + global ball_speed_x, ball_speed_y |
| 8 | + |
| 9 | + # Defining the movement of the ball |
| 10 | + ball.x += ball_speed_x |
| 11 | + ball.y += ball_speed_y |
| 12 | + |
| 13 | + # Defining the limitations of ball in screen |
| 14 | + if ball.top <= 0 or ball.bottom >= screen_height: |
| 15 | + ball_speed_y *= -1 |
| 16 | + if ball.left <= 0 or ball.left >= screen_width: |
| 17 | + ball_speed_x *= -1 |
| 18 | + |
| 19 | + # Defining the colisions with the player and opponent |
| 20 | + if ball.colliderect(player) or ball.colliderect(opponent): |
| 21 | + ball_speed_x *= -1 |
| 22 | + |
| 23 | + |
| 24 | +# Defining the limitations of screen of opponents and players |
| 25 | +def player_animation(): |
| 26 | + player.y += player_speed |
| 27 | + if player.top <= 0: |
| 28 | + player.top = 0 |
| 29 | + if player.bottom >= screen_height: |
| 30 | + player.bottom = screen_height |
| 31 | + |
| 32 | + |
| 33 | +def opponent_animation(): |
| 34 | + opponent.y += opponent_speed |
| 35 | + if opponent.top <= 0: |
| 36 | + opponent.top = 0 |
| 37 | + if opponent.bottom >= screen_height: |
| 38 | + opponent.bottom = screen_height |
| 39 | + |
| 40 | + |
4 | 41 | # Starting General Setup
|
5 | 42 | pygame.init()
|
6 | 43 | clock = pygame.time.Clock()
|
|
12 | 49 | pygame.display.set_caption('Pong Game')
|
13 | 50 |
|
14 | 51 | # Defining Objects (Rectangles)
|
15 |
| -ball = pygame.Rect(screen_width/2 - 15, screen_height/2 - 15, 30, 30) |
| 52 | +ball = pygame.Rect(screen_width/2 - 15, screen_height/2 - 15, 25, 25) |
16 | 53 | player = pygame.Rect(10, screen_height/2 - 70, 10, 150)
|
17 | 54 | opponent = pygame.Rect(screen_width - 20, screen_height/2 - 70, 10, 150)
|
18 | 55 |
|
|
22 | 59 | blue = (0, 139, 0)
|
23 | 60 | red = (139, 0, 0)
|
24 | 61 |
|
| 62 | +# Defining Speed of Ball |
| 63 | +ball_speed_x = 7 |
| 64 | +ball_speed_y = 7 |
| 65 | + |
| 66 | +# Defining Speed of Player |
| 67 | +player_speed = 0 |
| 68 | +opponent_speed = 0 |
| 69 | + |
25 | 70 | while True:
|
26 | 71 | for event in pygame.event.get():
|
27 | 72 | if event.type == pygame.QUIT:
|
28 | 73 | pygame.quit()
|
29 | 74 | sys.exit()
|
30 | 75 |
|
| 76 | + # Defining player movement |
| 77 | + if event.type == pygame.KEYDOWN: |
| 78 | + if event.key == pygame.K_s: |
| 79 | + player_speed += 7 |
| 80 | + if event.key == pygame.K_w: |
| 81 | + player_speed += -7 |
| 82 | + if event.type == pygame.KEYUP: |
| 83 | + if event.key == pygame.K_s: |
| 84 | + player_speed += -7 |
| 85 | + if event.key == pygame.K_w: |
| 86 | + player_speed += 7 |
| 87 | + |
| 88 | + # Defining opponent movement |
| 89 | + if event.type == pygame.KEYDOWN: |
| 90 | + if event.key == pygame.K_DOWN: |
| 91 | + opponent_speed += 3.5 |
| 92 | + if event.key == pygame.K_UP: |
| 93 | + opponent_speed += -3.5 |
| 94 | + if event.type == pygame.KEYUP: |
| 95 | + if event.key == pygame.K_DOWN: |
| 96 | + opponent_speed += -3.5 |
| 97 | + if event.key == pygame.K_UP: |
| 98 | + opponent_speed += 3.5 |
| 99 | + |
31 | 100 | # Defining object colors
|
32 | 101 | screen.fill(bg_color)
|
33 | 102 | pygame.draw.rect(screen, blue, player)
|
|
36 | 105 | pygame.draw.aaline(screen, light_grey, (screen_width/2,
|
37 | 106 | 0), (screen_width/2, screen_height))
|
38 | 107 |
|
| 108 | + ball_animation() |
| 109 | + player_animation() |
| 110 | + opponent_animation() |
| 111 | + |
| 112 | + opponent.y += opponent_speed |
| 113 | + |
39 | 114 | # Defining FPS
|
40 | 115 | pygame.display.flip()
|
41 | 116 | clock.tick(60)
|
0 commit comments