├── Ball.png ├── pong.ogg ├── score.ogg ├── Paddle.png ├── Pong1_Setup.py ├── Pong2_Drawing.py ├── Pong3_BallAnimation.py ├── Pong4_Input.py ├── Pong5_Opponent.py ├── Pong6_Restart.py ├── Pong7_Score.py ├── Pong9_collision.py ├── Pong8_Timer.py ├── Pong10_sound.py └── pong11_sprites.py /Ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/Pong_in_Pygame/HEAD/Ball.png -------------------------------------------------------------------------------- /pong.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/Pong_in_Pygame/HEAD/pong.ogg -------------------------------------------------------------------------------- /score.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/Pong_in_Pygame/HEAD/score.ogg -------------------------------------------------------------------------------- /Paddle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/Pong_in_Pygame/HEAD/Paddle.png -------------------------------------------------------------------------------- /Pong1_Setup.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | 3 | # General setup 4 | pygame.init() 5 | clock = pygame.time.Clock() 6 | 7 | # Setting up the main window 8 | screen_width = 1280 9 | screen_height = 960 10 | screen = pygame.display.set_mode((screen_width,screen_height)) 11 | pygame.display.set_caption('Pong') 12 | 13 | while True: 14 | #Handling input 15 | for event in pygame.event.get(): 16 | if event.type == pygame.QUIT: 17 | pygame.quit() 18 | sys.exit() 19 | 20 | # Updating the window 21 | pygame.display.flip() 22 | clock.tick(60) -------------------------------------------------------------------------------- /Pong2_Drawing.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | 3 | # General setup 4 | pygame.init() 5 | clock = pygame.time.Clock() 6 | 7 | # Main Window 8 | screen_width = 1280 9 | screen_height = 960 10 | screen = pygame.display.set_mode((screen_width,screen_height)) 11 | pygame.display.set_caption('Pong') 12 | 13 | # Colors 14 | light_grey = (200,200,200) 15 | bg_color = pygame.Color('grey12') 16 | 17 | # Game Rectangles 18 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 19 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 20 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 21 | 22 | while True: 23 | for event in pygame.event.get(): 24 | if event.type == pygame.QUIT: 25 | pygame.quit() 26 | sys.exit() 27 | 28 | # Visuals 29 | screen.fill(bg_color) 30 | pygame.draw.rect(screen, light_grey, player) 31 | pygame.draw.rect(screen, light_grey, opponent) 32 | pygame.draw.ellipse(screen, light_grey, ball) 33 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 34 | 35 | pygame.display.flip() 36 | clock.tick(60) -------------------------------------------------------------------------------- /Pong3_BallAnimation.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | if ball.left <= 0 or ball.right >= screen_width: 12 | ball_speed_x *= -1 13 | 14 | if ball.colliderect(player) or ball.colliderect(opponent): 15 | ball_speed_x *= -1 16 | 17 | # General setup 18 | pygame.init() 19 | clock = pygame.time.Clock() 20 | 21 | # Main Window 22 | screen_width = 1280 23 | screen_height = 960 24 | screen = pygame.display.set_mode((screen_width,screen_height)) 25 | pygame.display.set_caption('Pong') 26 | 27 | # Colors 28 | light_grey = (200,200,200) 29 | bg_color = pygame.Color('grey12') 30 | 31 | # Game Rectangles 32 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 33 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 34 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 35 | 36 | # Game Variables 37 | ball_speed_x = 7 38 | ball_speed_y = 7 39 | 40 | while True: 41 | for event in pygame.event.get(): 42 | if event.type == pygame.QUIT: 43 | pygame.quit() 44 | sys.exit() 45 | 46 | # Game logic 47 | ball_animation() 48 | 49 | screen.fill(bg_color) 50 | pygame.draw.rect(screen, light_grey, player) 51 | pygame.draw.rect(screen, light_grey, opponent) 52 | pygame.draw.ellipse(screen, light_grey, ball) 53 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 54 | 55 | pygame.display.flip() 56 | clock.tick(60) -------------------------------------------------------------------------------- /Pong4_Input.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | if ball.left <= 0 or ball.right >= screen_width: 12 | ball_speed_x *= -1 13 | 14 | if ball.colliderect(player) or ball.colliderect(opponent): 15 | ball_speed_x *= -1 16 | 17 | def player_animation(): 18 | player.y += player_speed 19 | 20 | if player.top <= 0: 21 | player.top = 0 22 | if player.bottom >= screen_height: 23 | player.bottom = screen_height 24 | 25 | # General setup 26 | pygame.init() 27 | clock = pygame.time.Clock() 28 | 29 | # Main Window 30 | screen_width = 1280 31 | screen_height = 960 32 | screen = pygame.display.set_mode((screen_width,screen_height)) 33 | pygame.display.set_caption('Pong') 34 | 35 | # Colors 36 | light_grey = (200,200,200) 37 | bg_color = pygame.Color('grey12') 38 | 39 | # Game Rectangles 40 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 41 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 42 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 43 | 44 | # Game Variables 45 | ball_speed_x = 7 46 | ball_speed_y = 7 47 | player_speed = 0 48 | 49 | while True: 50 | for event in pygame.event.get(): 51 | if event.type == pygame.QUIT: 52 | pygame.quit() 53 | sys.exit() 54 | if event.type == pygame.KEYDOWN: 55 | if event.key == pygame.K_UP: 56 | player_speed -= 7 57 | if event.key == pygame.K_DOWN: 58 | player_speed += 7 59 | if event.type == pygame.KEYUP: 60 | if event.key == pygame.K_UP: 61 | player_speed += 7 62 | if event.key == pygame.K_DOWN: 63 | player_speed -= 7 64 | 65 | # Game Logic 66 | ball_animation() 67 | player_animation() 68 | 69 | # Visuals 70 | screen.fill(bg_color) 71 | pygame.draw.rect(screen, light_grey, player) 72 | pygame.draw.rect(screen, light_grey, opponent) 73 | pygame.draw.ellipse(screen, light_grey, ball) 74 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 75 | 76 | pygame.display.flip() 77 | clock.tick(60) -------------------------------------------------------------------------------- /Pong5_Opponent.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | if ball.left <= 0 or ball.right >= screen_width: 12 | ball_speed_x *= -1 13 | 14 | if ball.colliderect(player) or ball.colliderect(opponent): 15 | ball_speed_x *= -1 16 | 17 | def player_animation(): 18 | player.y += player_speed 19 | 20 | if player.top <= 0: 21 | player.top = 0 22 | if player.bottom >= screen_height: 23 | player.bottom = screen_height 24 | 25 | def opponent_ai(): 26 | if opponent.top < ball.y: 27 | opponent.y += opponent_speed 28 | if opponent.bottom > ball.y: 29 | opponent.y -= opponent_speed 30 | 31 | if opponent.top <= 0: 32 | opponent.top = 0 33 | if opponent.bottom >= screen_height: 34 | opponent.bottom = screen_height 35 | 36 | # General setup 37 | pygame.init() 38 | clock = pygame.time.Clock() 39 | 40 | # Main Window 41 | screen_width = 1280 42 | screen_height = 960 43 | screen = pygame.display.set_mode((screen_width,screen_height)) 44 | pygame.display.set_caption('Pong') 45 | 46 | # Colors 47 | light_grey = (200,200,200) 48 | bg_color = pygame.Color('grey12') 49 | 50 | # Game Rectangles 51 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 52 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 53 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 54 | 55 | # Game Variables 56 | ball_speed_x = 7 57 | ball_speed_y = 7 58 | player_speed = 0 59 | opponent_speed = 7 60 | 61 | while True: 62 | for event in pygame.event.get(): 63 | if event.type == pygame.QUIT: 64 | pygame.quit() 65 | sys.exit() 66 | if event.type == pygame.KEYDOWN: 67 | if event.key == pygame.K_UP: 68 | player_speed -= 6 69 | if event.key == pygame.K_DOWN: 70 | player_speed += 6 71 | if event.type == pygame.KEYUP: 72 | if event.key == pygame.K_UP: 73 | player_speed += 6 74 | if event.key == pygame.K_DOWN: 75 | player_speed -= 6 76 | 77 | #Game Logic 78 | ball_animation() 79 | player_animation() 80 | opponent_ai() 81 | 82 | # Visuals 83 | screen.fill(bg_color) 84 | pygame.draw.rect(screen, light_grey, player) 85 | pygame.draw.rect(screen, light_grey, opponent) 86 | pygame.draw.ellipse(screen, light_grey, ball) 87 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 88 | 89 | pygame.display.flip() 90 | clock.tick(60) -------------------------------------------------------------------------------- /Pong6_Restart.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | if ball.left <= 0 or ball.right >= screen_width: 12 | ball_start() 13 | 14 | if ball.colliderect(player) or ball.colliderect(opponent): 15 | ball_speed_x *= -1 16 | 17 | def player_animation(): 18 | player.y += player_speed 19 | 20 | if player.top <= 0: 21 | player.top = 0 22 | if player.bottom >= screen_height: 23 | player.bottom = screen_height 24 | 25 | def opponent_ai(): 26 | if opponent.top < ball.y: 27 | opponent.y += opponent_speed 28 | if opponent.bottom > ball.y: 29 | opponent.y -= opponent_speed 30 | 31 | if opponent.top <= 0: 32 | opponent.top = 0 33 | if opponent.bottom >= screen_height: 34 | opponent.bottom = screen_height 35 | 36 | def ball_start(): 37 | global ball_speed_x, ball_speed_y 38 | 39 | ball.center = (screen_width/2, screen_height/2) 40 | ball_speed_y *= random.choice((1,-1)) 41 | ball_speed_x *= random.choice((1,-1)) 42 | 43 | # General setup 44 | pygame.init() 45 | clock = pygame.time.Clock() 46 | 47 | # Main Window 48 | screen_width = 1280 49 | screen_height = 960 50 | screen = pygame.display.set_mode((screen_width,screen_height)) 51 | pygame.display.set_caption('Pong') 52 | 53 | # Colors 54 | light_grey = (200,200,200) 55 | bg_color = pygame.Color('grey12') 56 | 57 | # Game Rectangles 58 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 59 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 60 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 61 | 62 | # Game Variables 63 | ball_speed_x = 7 * random.choice((1,-1)) 64 | ball_speed_y = 7 * random.choice((1,-1)) 65 | player_speed = 0 66 | opponent_speed = 7 67 | 68 | while True: 69 | for event in pygame.event.get(): 70 | if event.type == pygame.QUIT: 71 | pygame.quit() 72 | sys.exit() 73 | if event.type == pygame.KEYDOWN: 74 | if event.key == pygame.K_UP: 75 | player_speed -= 6 76 | if event.key == pygame.K_DOWN: 77 | player_speed += 6 78 | if event.type == pygame.KEYUP: 79 | if event.key == pygame.K_UP: 80 | player_speed += 6 81 | if event.key == pygame.K_DOWN: 82 | player_speed -= 6 83 | 84 | #Game Logic 85 | ball_animation() 86 | player_animation() 87 | opponent_ai() 88 | 89 | # Visuals 90 | screen.fill(bg_color) 91 | pygame.draw.rect(screen, light_grey, player) 92 | pygame.draw.rect(screen, light_grey, opponent) 93 | pygame.draw.ellipse(screen, light_grey, ball) 94 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 95 | 96 | pygame.display.flip() 97 | clock.tick(60) -------------------------------------------------------------------------------- /Pong7_Score.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y, player_score, opponent_score 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | 12 | # Player Score 13 | if ball.left <= 0: 14 | ball_start() 15 | player_score += 1 16 | 17 | # Opponent Score 18 | if ball.right >= screen_width: 19 | ball_start() 20 | opponent_score += 1 21 | 22 | if ball.colliderect(player) or ball.colliderect(opponent): 23 | ball_speed_x *= -1 24 | 25 | def player_animation(): 26 | player.y += player_speed 27 | 28 | if player.top <= 0: 29 | player.top = 0 30 | if player.bottom >= screen_height: 31 | player.bottom = screen_height 32 | 33 | def opponent_ai(): 34 | if opponent.top < ball.y: 35 | opponent.y += opponent_speed 36 | if opponent.bottom > ball.y: 37 | opponent.y -= opponent_speed 38 | 39 | if opponent.top <= 0: 40 | opponent.top = 0 41 | if opponent.bottom >= screen_height: 42 | opponent.bottom = screen_height 43 | 44 | def ball_start(): 45 | global ball_speed_x, ball_speed_y 46 | 47 | ball.center = (screen_width/2, screen_height/2) 48 | ball_speed_y *= random.choice((1,-1)) 49 | ball_speed_x *= random.choice((1,-1)) 50 | 51 | 52 | # General setup 53 | pygame.init() 54 | clock = pygame.time.Clock() 55 | 56 | # Main Window 57 | screen_width = 1280 58 | screen_height = 960 59 | screen = pygame.display.set_mode((screen_width,screen_height)) 60 | pygame.display.set_caption('Pong') 61 | 62 | # Colors 63 | light_grey = (200,200,200) 64 | bg_color = pygame.Color('grey12') 65 | 66 | # Game Rectangles 67 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 68 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 69 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 70 | 71 | # Game Variables 72 | ball_speed_x = 7 * random.choice((1,-1)) 73 | ball_speed_y = 7 * random.choice((1,-1)) 74 | player_speed = 0 75 | opponent_speed = 7 76 | 77 | # Score Text 78 | player_score = 0 79 | opponent_score = 0 80 | basic_font = pygame.font.Font('freesansbold.ttf', 32) 81 | 82 | 83 | while True: 84 | for event in pygame.event.get(): 85 | if event.type == pygame.QUIT: 86 | pygame.quit() 87 | sys.exit() 88 | if event.type == pygame.KEYDOWN: 89 | if event.key == pygame.K_UP: 90 | player_speed -= 6 91 | if event.key == pygame.K_DOWN: 92 | player_speed += 6 93 | if event.type == pygame.KEYUP: 94 | if event.key == pygame.K_UP: 95 | player_speed += 6 96 | if event.key == pygame.K_DOWN: 97 | player_speed -= 6 98 | 99 | #Game Logic 100 | ball_animation() 101 | player_animation() 102 | opponent_ai() 103 | 104 | # Visuals 105 | screen.fill(bg_color) 106 | pygame.draw.rect(screen, light_grey, player) 107 | pygame.draw.rect(screen, light_grey, opponent) 108 | pygame.draw.ellipse(screen, light_grey, ball) 109 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 110 | 111 | player_text = basic_font.render(f'{player_score}',False,light_grey) 112 | screen.blit(player_text,(660,470)) 113 | 114 | opponent_text = basic_font.render(f'{opponent_score}',False,light_grey) 115 | screen.blit(opponent_text,(600,470)) 116 | 117 | pygame.display.flip() 118 | clock.tick(60) -------------------------------------------------------------------------------- /Pong9_collision.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y, player_score, opponent_score, score_time 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | 12 | # Player Score 13 | if ball.left <= 0: 14 | score_time = pygame.time.get_ticks() 15 | player_score += 1 16 | 17 | # Opponent Score 18 | if ball.right >= screen_width: 19 | score_time = pygame.time.get_ticks() 20 | opponent_score += 1 21 | 22 | if ball.colliderect(player) and ball_speed_x > 0: 23 | if abs(ball.right - player.left) < 10: 24 | ball_speed_x *= -1 25 | elif abs(ball.bottom - player.top) < 10 and ball_speed_y > 0: 26 | ball_speed_y *= -1 27 | elif abs(ball.top - player.bottom) < 10 and ball_speed_y < 0: 28 | ball_speed_y *= -1 29 | 30 | if ball.colliderect(opponent) and ball_speed_x < 0: 31 | if abs(ball.left - opponent.right) < 10: 32 | ball_speed_x *= -1 33 | elif abs(ball.bottom - opponent.top) < 10 and ball_speed_y > 0: 34 | ball_speed_y *= -1 35 | elif abs(ball.top - opponent.bottom) < 10 and ball_speed_y < 0: 36 | ball_speed_y *= -1 37 | 38 | 39 | def player_animation(): 40 | player.y += player_speed 41 | 42 | if player.top <= 0: 43 | player.top = 0 44 | if player.bottom >= screen_height: 45 | player.bottom = screen_height 46 | 47 | def opponent_ai(): 48 | if opponent.top < ball.y: 49 | opponent.y += opponent_speed 50 | if opponent.bottom > ball.y: 51 | opponent.y -= opponent_speed 52 | 53 | if opponent.top <= 0: 54 | opponent.top = 0 55 | if opponent.bottom >= screen_height: 56 | opponent.bottom = screen_height 57 | 58 | def ball_start(): 59 | global ball_speed_x, ball_speed_y, ball_moving, score_time 60 | 61 | ball.center = (screen_width/2, screen_height/2) 62 | current_time = pygame.time.get_ticks() 63 | 64 | if current_time - score_time < 700: 65 | number_three = basic_font.render("3",False,light_grey) 66 | screen.blit(number_three,(screen_width/2 - 10, screen_height/2 + 20)) 67 | if 700 < current_time - score_time < 1400: 68 | number_two = basic_font.render("2",False,light_grey) 69 | screen.blit(number_two,(screen_width/2 - 10, screen_height/2 + 20)) 70 | if 1400 < current_time - score_time < 2100: 71 | number_one = basic_font.render("1",False,light_grey) 72 | screen.blit(number_one,(screen_width/2 - 10, screen_height/2 + 20)) 73 | 74 | if current_time - score_time < 2100: 75 | ball_speed_y, ball_speed_x = 0,0 76 | else: 77 | ball_speed_x = 7 * random.choice((1,-1)) 78 | ball_speed_y = 7 * random.choice((1,-1)) 79 | score_time = None 80 | 81 | # General setup 82 | pygame.mixer.pre_init(44100,-16,1, 1024) 83 | pygame.init() 84 | clock = pygame.time.Clock() 85 | 86 | # Main Window 87 | screen_width = 1280 88 | screen_height = 960 89 | screen = pygame.display.set_mode((screen_width,screen_height)) 90 | pygame.display.set_caption('Pong') 91 | 92 | # Colors 93 | light_grey = (200,200,200) 94 | bg_color = pygame.Color('grey12') 95 | 96 | # Game Rectangles 97 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 98 | player = pygame.Rect(screen_width - 20 -300, screen_height / 2 - 70, 10 +300,140) 99 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 100 | 101 | # Game Variables 102 | ball_speed_x = 7 * random.choice((1,-1)) 103 | ball_speed_y = 7 * random.choice((1,-1)) 104 | player_speed = 0 105 | opponent_speed = 7 106 | ball_moving = False 107 | score_time = True 108 | 109 | # Score Text 110 | player_score = 0 111 | opponent_score = 0 112 | basic_font = pygame.font.Font('freesansbold.ttf', 32) 113 | 114 | 115 | while True: 116 | for event in pygame.event.get(): 117 | if event.type == pygame.QUIT: 118 | pygame.quit() 119 | sys.exit() 120 | if event.type == pygame.KEYDOWN: 121 | if event.key == pygame.K_UP: 122 | player_speed -= 6 123 | if event.key == pygame.K_DOWN: 124 | player_speed += 6 125 | if event.type == pygame.KEYUP: 126 | if event.key == pygame.K_UP: 127 | player_speed += 6 128 | if event.key == pygame.K_DOWN: 129 | player_speed -= 6 130 | 131 | #Game Logic 132 | ball_animation() 133 | player_animation() 134 | opponent_ai() 135 | 136 | # Visuals 137 | screen.fill(bg_color) 138 | pygame.draw.rect(screen, light_grey, player) 139 | pygame.draw.rect(screen, light_grey, opponent) 140 | pygame.draw.ellipse(screen, light_grey, ball) 141 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 142 | 143 | if score_time: 144 | ball_start() 145 | 146 | player_text = basic_font.render(f'{player_score}',False,light_grey) 147 | screen.blit(player_text,(660,470)) 148 | 149 | opponent_text = basic_font.render(f'{opponent_score}',False,light_grey) 150 | screen.blit(opponent_text,(600,470)) 151 | 152 | pygame.display.flip() 153 | clock.tick(60) -------------------------------------------------------------------------------- /Pong8_Timer.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y, player_score, opponent_score, score_time 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | ball_speed_y *= -1 11 | pygame.mixer.Sound.play(plob_sound) 12 | 13 | # Player Score 14 | if ball.left <= 0: 15 | score_time = pygame.time.get_ticks() 16 | player_score += 1 17 | pygame.mixer.Sound.play(score_sound) 18 | 19 | # Opponent Score 20 | if ball.right >= screen_width: 21 | score_time = pygame.time.get_ticks() 22 | opponent_score += 1 23 | pygame.mixer.Sound.play(score_sound) 24 | 25 | 26 | if ball.colliderect(player) and ball_speed_x > 0: 27 | if abs(ball.right - player.left) < 10: 28 | ball_speed_x *= -1 29 | elif abs(ball.bottom - player.top) < 10 or abs(ball.top - player.bottom) < 10 : 30 | ball_speed_y *= -1 31 | pygame.mixer.Sound.play(plob_sound) 32 | 33 | 34 | if ball.colliderect(opponent): 35 | if abs(ball.left - opponent.right) < 10 or abs(ball.right - player.left) < 10: 36 | ball_speed_x *= -1 37 | else: 38 | ball_speed_y *= -1 39 | pygame.mixer.Sound.play(plob_sound) 40 | 41 | def player_animation(): 42 | player.y += player_speed 43 | 44 | if player.top <= 0: 45 | player.top = 0 46 | if player.bottom >= screen_height: 47 | player.bottom = screen_height 48 | 49 | def opponent_ai(): 50 | if opponent.top < ball.y: 51 | opponent.y += opponent_speed 52 | if opponent.bottom > ball.y: 53 | opponent.y -= opponent_speed 54 | 55 | if opponent.top <= 0: 56 | opponent.top = 0 57 | if opponent.bottom >= screen_height: 58 | opponent.bottom = screen_height 59 | 60 | def ball_start(): 61 | global ball_speed_x, ball_speed_y, ball_moving, score_time 62 | 63 | ball.center = (screen_width/2, screen_height/2) 64 | current_time = pygame.time.get_ticks() 65 | 66 | if current_time - score_time < 700: 67 | number_three = basic_font.render("3",False,light_grey) 68 | screen.blit(number_three,(screen_width/2 - 10, screen_height/2 + 20)) 69 | if 700 < current_time - score_time < 1400: 70 | number_two = basic_font.render("2",False,light_grey) 71 | screen.blit(number_two,(screen_width/2 - 10, screen_height/2 + 20)) 72 | if 1400 < current_time - score_time < 2100: 73 | number_one = basic_font.render("1",False,light_grey) 74 | screen.blit(number_one,(screen_width/2 - 10, screen_height/2 + 20)) 75 | 76 | if current_time - score_time < 2100: 77 | ball_speed_y, ball_speed_x = 0,0 78 | else: 79 | ball_speed_x = 7 * random.choice((1,-1)) 80 | ball_speed_y = 7 * random.choice((1,-1)) 81 | score_time = None 82 | 83 | # General setup 84 | pygame.init() 85 | clock = pygame.time.Clock() 86 | 87 | # Main Window 88 | screen_width = 1280 89 | screen_height = 960 90 | screen = pygame.display.set_mode((screen_width,screen_height)) 91 | pygame.display.set_caption('Pong') 92 | 93 | # Colors 94 | light_grey = (200,200,200) 95 | bg_color = pygame.Color('grey12') 96 | 97 | # Game Rectangles 98 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 99 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 100 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 101 | 102 | # Game Variables 103 | ball_speed_x = 7 * random.choice((1,-1)) 104 | ball_speed_y = 7 * random.choice((1,-1)) 105 | player_speed = 0 106 | opponent_speed = 7 107 | ball_moving = False 108 | score_time = True 109 | 110 | # Score Text 111 | player_score = 0 112 | opponent_score = 0 113 | basic_font = pygame.font.Font('freesansbold.ttf', 32) 114 | 115 | # sound 116 | plob_sound = pygame.mixer.Sound("Plob.ogg") 117 | score_sound = pygame.mixer.Sound("score.ogg") 118 | 119 | while True: 120 | for event in pygame.event.get(): 121 | if event.type == pygame.QUIT: 122 | pygame.quit() 123 | sys.exit() 124 | if event.type == pygame.KEYDOWN: 125 | if event.key == pygame.K_UP: 126 | player_speed -= 6 127 | if event.key == pygame.K_DOWN: 128 | player_speed += 6 129 | if event.type == pygame.KEYUP: 130 | if event.key == pygame.K_UP: 131 | player_speed += 6 132 | if event.key == pygame.K_DOWN: 133 | player_speed -= 6 134 | 135 | #Game Logic 136 | ball_animation() 137 | player_animation() 138 | opponent_ai() 139 | 140 | # Visuals 141 | screen.fill(bg_color) 142 | pygame.draw.rect(screen, light_grey, player) 143 | pygame.draw.rect(screen, light_grey, opponent) 144 | pygame.draw.ellipse(screen, light_grey, ball) 145 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 146 | 147 | if score_time: 148 | ball_start() 149 | 150 | player_text = basic_font.render(f'{player_score}',False,light_grey) 151 | screen.blit(player_text,(660,470)) 152 | 153 | opponent_text = basic_font.render(f'{opponent_score}',False,light_grey) 154 | screen.blit(opponent_text,(600,470)) 155 | 156 | pygame.display.flip() 157 | clock.tick(60) -------------------------------------------------------------------------------- /Pong10_sound.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def ball_animation(): 4 | global ball_speed_x, ball_speed_y, player_score, opponent_score, score_time 5 | 6 | ball.x += ball_speed_x 7 | ball.y += ball_speed_y 8 | 9 | if ball.top <= 0 or ball.bottom >= screen_height: 10 | pygame.mixer.Sound.play(plob_sound) 11 | ball_speed_y *= -1 12 | 13 | # Player Score 14 | if ball.left <= 0: 15 | pygame.mixer.Sound.play(score_sound) 16 | score_time = pygame.time.get_ticks() 17 | player_score += 1 18 | 19 | # Opponent Score 20 | if ball.right >= screen_width: 21 | pygame.mixer.Sound.play(score_sound) 22 | score_time = pygame.time.get_ticks() 23 | opponent_score += 1 24 | 25 | if ball.colliderect(player) and ball_speed_x > 0: 26 | pygame.mixer.Sound.play(plob_sound) 27 | if abs(ball.right - player.left) < 10: 28 | ball_speed_x *= -1 29 | elif abs(ball.bottom - player.top) < 10 and ball_speed_y > 0: 30 | ball_speed_y *= -1 31 | elif abs(ball.top - player.bottom) < 10 and ball_speed_y < 0: 32 | ball_speed_y *= -1 33 | 34 | if ball.colliderect(opponent) and ball_speed_x < 0: 35 | pygame.mixer.Sound.play(plob_sound) 36 | if abs(ball.left - opponent.right) < 10: 37 | ball_speed_x *= -1 38 | elif abs(ball.bottom - opponent.top) < 10 and ball_speed_y > 0: 39 | ball_speed_y *= -1 40 | elif abs(ball.top - opponent.bottom) < 10 and ball_speed_y < 0: 41 | ball_speed_y *= -1 42 | 43 | 44 | def player_animation(): 45 | player.y += player_speed 46 | 47 | if player.top <= 0: 48 | player.top = 0 49 | if player.bottom >= screen_height: 50 | player.bottom = screen_height 51 | 52 | def opponent_ai(): 53 | if opponent.top < ball.y: 54 | opponent.y += opponent_speed 55 | if opponent.bottom > ball.y: 56 | opponent.y -= opponent_speed 57 | 58 | if opponent.top <= 0: 59 | opponent.top = 0 60 | if opponent.bottom >= screen_height: 61 | opponent.bottom = screen_height 62 | 63 | def ball_start(): 64 | global ball_speed_x, ball_speed_y, ball_moving, score_time 65 | 66 | ball.center = (screen_width/2, screen_height/2) 67 | current_time = pygame.time.get_ticks() 68 | 69 | if current_time - score_time < 700: 70 | number_three = basic_font.render("3",False,light_grey) 71 | screen.blit(number_three,(screen_width/2 - 10, screen_height/2 + 20)) 72 | if 700 < current_time - score_time < 1400: 73 | number_two = basic_font.render("2",False,light_grey) 74 | screen.blit(number_two,(screen_width/2 - 10, screen_height/2 + 20)) 75 | if 1400 < current_time - score_time < 2100: 76 | number_one = basic_font.render("1",False,light_grey) 77 | screen.blit(number_one,(screen_width/2 - 10, screen_height/2 + 20)) 78 | 79 | if current_time - score_time < 2100: 80 | ball_speed_y, ball_speed_x = 0,0 81 | else: 82 | ball_speed_x = 7 * random.choice((1,-1)) 83 | ball_speed_y = 7 * random.choice((1,-1)) 84 | score_time = None 85 | 86 | # General setup 87 | pygame.mixer.pre_init(44100,-16,1, 1024) 88 | pygame.init() 89 | clock = pygame.time.Clock() 90 | 91 | # Main Window 92 | screen_width = 1280 93 | screen_height = 960 94 | screen = pygame.display.set_mode((screen_width,screen_height)) 95 | pygame.display.set_caption('Pong') 96 | 97 | # Colors 98 | light_grey = (200,200,200) 99 | bg_color = pygame.Color('grey12') 100 | 101 | # Game Rectangles 102 | ball = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30) 103 | player = pygame.Rect(screen_width - 20, screen_height / 2 - 70, 10,140) 104 | opponent = pygame.Rect(10, screen_height / 2 - 70, 10,140) 105 | 106 | # Game Variables 107 | ball_speed_x = 7 * random.choice((1,-1)) 108 | ball_speed_y = 7 * random.choice((1,-1)) 109 | player_speed = 0 110 | opponent_speed = 7 111 | ball_moving = False 112 | score_time = True 113 | 114 | # Score Text 115 | player_score = 0 116 | opponent_score = 0 117 | basic_font = pygame.font.Font('freesansbold.ttf', 32) 118 | 119 | # sound 120 | plob_sound = pygame.mixer.Sound("pong.ogg") 121 | score_sound = pygame.mixer.Sound("score.ogg") 122 | 123 | while True: 124 | for event in pygame.event.get(): 125 | if event.type == pygame.QUIT: 126 | pygame.quit() 127 | sys.exit() 128 | if event.type == pygame.KEYDOWN: 129 | if event.key == pygame.K_UP: 130 | player_speed -= 6 131 | if event.key == pygame.K_DOWN: 132 | player_speed += 6 133 | if event.type == pygame.KEYUP: 134 | if event.key == pygame.K_UP: 135 | player_speed += 6 136 | if event.key == pygame.K_DOWN: 137 | player_speed -= 6 138 | 139 | #Game Logic 140 | ball_animation() 141 | player_animation() 142 | opponent_ai() 143 | 144 | # Visuals 145 | screen.fill(bg_color) 146 | pygame.draw.rect(screen, light_grey, player) 147 | pygame.draw.rect(screen, light_grey, opponent) 148 | pygame.draw.ellipse(screen, light_grey, ball) 149 | pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0),(screen_width / 2, screen_height)) 150 | 151 | if score_time: 152 | ball_start() 153 | 154 | player_text = basic_font.render(f'{player_score}',False,light_grey) 155 | screen.blit(player_text,(660,470)) 156 | 157 | opponent_text = basic_font.render(f'{opponent_score}',False,light_grey) 158 | screen.blit(opponent_text,(600,470)) 159 | 160 | pygame.display.flip() 161 | clock.tick(60) -------------------------------------------------------------------------------- /pong11_sprites.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | class Block(pygame.sprite.Sprite): 4 | def __init__(self,path,x_pos,y_pos): 5 | super().__init__() 6 | self.image = pygame.image.load(path) 7 | self.rect = self.image.get_rect(center = (x_pos,y_pos)) 8 | 9 | class Player(Block): 10 | def __init__(self,path,x_pos,y_pos,speed): 11 | super().__init__(path,x_pos,y_pos) 12 | self.speed = speed 13 | self.movement = 0 14 | 15 | def screen_constrain(self): 16 | if self.rect.top <= 0: 17 | self.rect.top = 0 18 | if self.rect.bottom >= screen_height: 19 | self.rect.bottom = screen_height 20 | 21 | def update(self,ball_group): 22 | self.rect.y += self.movement 23 | self.screen_constrain() 24 | 25 | class Ball(Block): 26 | def __init__(self,path,x_pos,y_pos,speed_x,speed_y,paddles): 27 | super().__init__(path,x_pos,y_pos) 28 | self.speed_x = speed_x * random.choice((-1,1)) 29 | self.speed_y = speed_y * random.choice((-1,1)) 30 | self.paddles = paddles 31 | self.active = False 32 | self.score_time = 0 33 | 34 | def update(self): 35 | if self.active: 36 | self.rect.x += self.speed_x 37 | self.rect.y += self.speed_y 38 | self.collisions() 39 | else: 40 | self.restart_counter() 41 | 42 | def collisions(self): 43 | if self.rect.top <= 0 or self.rect.bottom >= screen_height: 44 | pygame.mixer.Sound.play(plob_sound) 45 | self.speed_y *= -1 46 | 47 | if pygame.sprite.spritecollide(self,self.paddles,False): 48 | pygame.mixer.Sound.play(plob_sound) 49 | collision_paddle = pygame.sprite.spritecollide(self,self.paddles,False)[0].rect 50 | if abs(self.rect.right - collision_paddle.left) < 10 and self.speed_x > 0: 51 | self.speed_x *= -1 52 | if abs(self.rect.left - collision_paddle.right) < 10 and self.speed_x < 0: 53 | self.speed_x *= -1 54 | if abs(self.rect.top - collision_paddle.bottom) < 10 and self.speed_y < 0: 55 | self.rect.top = collision_paddle.bottom 56 | self.speed_y *= -1 57 | if abs(self.rect.bottom - collision_paddle.top) < 10 and self.speed_y > 0: 58 | self.rect.bottom = collision_paddle.top 59 | self.speed_y *= -1 60 | 61 | def reset_ball(self): 62 | self.active = False 63 | self.speed_x *= random.choice((-1,1)) 64 | self.speed_y *= random.choice((-1,1)) 65 | self.score_time = pygame.time.get_ticks() 66 | self.rect.center = (screen_width/2,screen_height/2) 67 | pygame.mixer.Sound.play(score_sound) 68 | 69 | def restart_counter(self): 70 | current_time = pygame.time.get_ticks() 71 | countdown_number = 3 72 | 73 | if current_time - self.score_time <= 700: 74 | countdown_number = 3 75 | if 700 < current_time - self.score_time <= 1400: 76 | countdown_number = 2 77 | if 1400 < current_time - self.score_time <= 2100: 78 | countdown_number = 1 79 | if current_time - self.score_time >= 2100: 80 | self.active = True 81 | 82 | time_counter = basic_font.render(str(countdown_number),True,accent_color) 83 | time_counter_rect = time_counter.get_rect(center = (screen_width/2,screen_height/2 + 50)) 84 | pygame.draw.rect(screen,bg_color,time_counter_rect) 85 | screen.blit(time_counter,time_counter_rect) 86 | 87 | class Opponent(Block): 88 | def __init__(self,path,x_pos,y_pos,speed): 89 | super().__init__(path,x_pos,y_pos) 90 | self.speed = speed 91 | 92 | def update(self,ball_group): 93 | if self.rect.top < ball_group.sprite.rect.y: 94 | self.rect.y += self.speed 95 | if self.rect.bottom > ball_group.sprite.rect.y: 96 | self.rect.y -= self.speed 97 | self.constrain() 98 | 99 | def constrain(self): 100 | if self.rect.top <= 0: self.rect.top = 0 101 | if self.rect.bottom >= screen_height: self.rect.bottom = screen_height 102 | 103 | class GameManager: 104 | def __init__(self,ball_group,paddle_group): 105 | self.player_score = 0 106 | self.opponent_score = 0 107 | self.ball_group = ball_group 108 | self.paddle_group = paddle_group 109 | 110 | def run_game(self): 111 | # Drawing the game objects 112 | self.paddle_group.draw(screen) 113 | self.ball_group.draw(screen) 114 | 115 | # Updating the game objects 116 | self.paddle_group.update(self.ball_group) 117 | self.ball_group.update() 118 | self.reset_ball() 119 | self.draw_score() 120 | 121 | def reset_ball(self): 122 | if self.ball_group.sprite.rect.right >= screen_width: 123 | self.opponent_score += 1 124 | self.ball_group.sprite.reset_ball() 125 | if self.ball_group.sprite.rect.left <= 0: 126 | self.player_score += 1 127 | self.ball_group.sprite.reset_ball() 128 | 129 | def draw_score(self): 130 | player_score = basic_font.render(str(self.player_score),True,accent_color) 131 | opponent_score = basic_font.render(str(self.opponent_score),True,accent_color) 132 | 133 | player_score_rect = player_score.get_rect(midleft = (screen_width / 2 + 40,screen_height/2)) 134 | opponent_score_rect = opponent_score.get_rect(midright = (screen_width / 2 - 40,screen_height/2)) 135 | 136 | screen.blit(player_score,player_score_rect) 137 | screen.blit(opponent_score,opponent_score_rect) 138 | 139 | # General setup 140 | pygame.mixer.pre_init(44100,-16,2,512) 141 | pygame.init() 142 | clock = pygame.time.Clock() 143 | 144 | # Main Window 145 | screen_width = 1280 146 | screen_height = 960 147 | screen = pygame.display.set_mode((screen_width,screen_height)) 148 | pygame.display.set_caption('Pong') 149 | 150 | # Global Variables 151 | bg_color = pygame.Color('#2F373F') 152 | accent_color = (27,35,43) 153 | basic_font = pygame.font.Font('freesansbold.ttf', 32) 154 | plob_sound = pygame.mixer.Sound("pong.ogg") 155 | score_sound = pygame.mixer.Sound("score.ogg") 156 | middle_strip = pygame.Rect(screen_width/2 - 2,0,4,screen_height) 157 | 158 | # Game objects 159 | player = Player('Paddle.png',screen_width - 20,screen_height/2,5) 160 | opponent = Opponent('Paddle.png',20,screen_width/2,5) 161 | paddle_group = pygame.sprite.Group() 162 | paddle_group.add(player) 163 | paddle_group.add(opponent) 164 | 165 | ball = Ball('Ball.png',screen_width/2,screen_height/2,4,4,paddle_group) 166 | ball_sprite = pygame.sprite.GroupSingle() 167 | ball_sprite.add(ball) 168 | 169 | game_manager = GameManager(ball_sprite,paddle_group) 170 | 171 | while True: 172 | for event in pygame.event.get(): 173 | if event.type == pygame.QUIT: 174 | pygame.quit() 175 | sys.exit() 176 | if event.type == pygame.KEYDOWN: 177 | if event.key == pygame.K_UP: 178 | player.movement -= player.speed 179 | if event.key == pygame.K_DOWN: 180 | player.movement += player.speed 181 | if event.type == pygame.KEYUP: 182 | if event.key == pygame.K_UP: 183 | player.movement += player.speed 184 | if event.key == pygame.K_DOWN: 185 | player.movement -= player.speed 186 | 187 | # Background Stuff 188 | screen.fill(bg_color) 189 | pygame.draw.rect(screen,accent_color,middle_strip) 190 | 191 | # Run the game 192 | game_manager.run_game() 193 | 194 | # Rendering 195 | pygame.display.flip() 196 | clock.tick(120) --------------------------------------------------------------------------------