|
| 1 | +import sys |
| 2 | +import pygame |
| 3 | +import random |
| 4 | +import time |
| 5 | + |
| 6 | +print('\n\tWelCome to Dou Pong Game by DarBar Bros') |
| 7 | +print('\n\t\tPlayer Blue: Up and Down keys') |
| 8 | +print('\t\tPlayer Orange: Z and X keys') |
| 9 | +print("\n\nPress Q/q for Quit") |
| 10 | + |
| 11 | +pygame.init() |
| 12 | + |
| 13 | +time.sleep(5) |
| 14 | +clock = pygame.time.Clock() |
| 15 | + |
| 16 | +score1 = 0 |
| 17 | +score2 = 0 |
| 18 | +player_speed = 0 |
| 19 | + |
| 20 | +screen_width = 800 |
| 21 | +screen_height = 600 |
| 22 | +screen = pygame.display.set_mode((screen_width, screen_height)) |
| 23 | +pygame.display.set_caption("Pong game") |
| 24 | + |
| 25 | +ball = pygame.Rect(screen_width / 2 - 8.5, screen_height / 2 - 8.5, 17, 17) |
| 26 | +player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10, 100) |
| 27 | +opponent = pygame.Rect(10, screen_height / 2 - 70, 10, 100) |
| 28 | +bg_color = pygame.Color("grey20") |
| 29 | +light_grey = pygame.Color(200, 200, 200) |
| 30 | +ball_speed_x = 8 * random.choice((1, -1)) |
| 31 | +ball_speed_y = 8 * random.choice((1, -1)) |
| 32 | + |
| 33 | +opponent_speed = 0 |
| 34 | + |
| 35 | + |
| 36 | +def ball_animation(): |
| 37 | + global ball_speed_x, ball_speed_y |
| 38 | + ball.x += ball_speed_x |
| 39 | + ball.y += ball_speed_y |
| 40 | + |
| 41 | + if ball.top <= 0 or ball.bottom >= screen_height: |
| 42 | + ball_speed_y *= -1 |
| 43 | + if ball.left <= 0: |
| 44 | + ball_restart() |
| 45 | + score1 + 1 |
| 46 | + if ball.right >= screen_width: |
| 47 | + ball_restart() |
| 48 | + score2 + 1 |
| 49 | + if ball.colliderect(player) or ball.colliderect(opponent): |
| 50 | + ball_speed_x *= -1 |
| 51 | + |
| 52 | + |
| 53 | +def ball_restart(): |
| 54 | + global ball_speed_y, ball_speed_x |
| 55 | + ball.center = (screen_width / 2, screen_height / 2) |
| 56 | + ball_speed_y *= random.choice((1, -1)) |
| 57 | + ball_speed_x *= random.choice((1, -1)) |
| 58 | + |
| 59 | + |
| 60 | +while True: |
| 61 | + for event in pygame.event.get(): |
| 62 | + if event.type == pygame.QUIT: |
| 63 | + print("\n\tThank You!! :-)") |
| 64 | + pygame.quit() |
| 65 | + sys.exit() |
| 66 | + if event.type == pygame.KEYDOWN: |
| 67 | + if event.key == pygame.K_q: |
| 68 | + print("\n\tThank You!! :-)") |
| 69 | + pygame.quit() |
| 70 | + sys.exit() |
| 71 | + if event.type == pygame.KEYDOWN: |
| 72 | + if event.key == pygame.K_DOWN: |
| 73 | + player_speed += 7 |
| 74 | + if event.type == pygame.KEYDOWN: |
| 75 | + if event.key == pygame.K_UP: |
| 76 | + player_speed -= 7 |
| 77 | + if event.type == pygame.KEYUP: |
| 78 | + if event.key == pygame.K_DOWN: |
| 79 | + player_speed -= 7 |
| 80 | + if event.type == pygame.KEYUP: |
| 81 | + if event.key == pygame.K_UP: |
| 82 | + player_speed += 7 |
| 83 | + if event.type == pygame.KEYDOWN: |
| 84 | + if event.key == pygame.K_DOWN: |
| 85 | + player_speed += 7 |
| 86 | + if event.type == pygame.KEYDOWN: |
| 87 | + if event.key == pygame.K_UP: |
| 88 | + player_speed -= 7 |
| 89 | + if event.type == pygame.KEYUP: |
| 90 | + if event.key == pygame.K_DOWN: |
| 91 | + player_speed -= 7 |
| 92 | + if event.type == pygame.KEYUP: |
| 93 | + if event.key == pygame.K_UP: |
| 94 | + player_speed += 7 |
| 95 | + |
| 96 | + if event.type == pygame.KEYDOWN: |
| 97 | + if event.key == pygame.K_x: |
| 98 | + opponent_speed += 7 |
| 99 | + if event.type == pygame.KEYDOWN: |
| 100 | + if event.key == pygame.K_z: |
| 101 | + opponent_speed -= 7 |
| 102 | + if event.type == pygame.KEYUP: |
| 103 | + if event.key == pygame.K_x: |
| 104 | + opponent_speed -= 7 |
| 105 | + if event.type == pygame.KEYUP: |
| 106 | + if event.key == pygame.K_z: |
| 107 | + opponent_speed += 7 |
| 108 | + |
| 109 | + screen.fill(bg_color) |
| 110 | + pygame.draw.rect(screen, ("royalblue"), player) |
| 111 | + pygame.draw.rect(screen, (255, 85, 0), opponent) |
| 112 | + pygame.draw.ellipse(screen, light_grey, ball) |
| 113 | + pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0), |
| 114 | + (screen_width / 2, screen_height)) |
| 115 | + |
| 116 | + ball_animation() |
| 117 | + player.y += player_speed |
| 118 | + if player.top <= 0: |
| 119 | + player.top = 0 |
| 120 | + if player.bottom >= screen_height: |
| 121 | + player.bottom = screen_height |
| 122 | + opponent.y += opponent_speed |
| 123 | + if opponent.top <= 0: |
| 124 | + opponent.top = 0 |
| 125 | + if opponent.bottom >= screen_height: |
| 126 | + opponent.bottom = screen_height |
| 127 | + pygame.display.flip() |
| 128 | + clock.tick(60) |
0 commit comments