From 5732529d2bb22f9e01ece04f2fd0593087e94b4e Mon Sep 17 00:00:00 2001 From: Tim Curry Date: Tue, 23 Jan 2024 13:44:16 -0500 Subject: [PATCH] add initial game file --- pong.py | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 pong.py diff --git a/pong.py b/pong.py new file mode 100644 index 0000000..58a5d3d --- /dev/null +++ b/pong.py @@ -0,0 +1,127 @@ +import pygame, sys, random + +def reset_ball(): + global ball_speed_x, ball_speed_y + ball.x = screen_width/2 - 10 + ball.y = random.randint(10,100) + ball_speed_x *= random.choice([-1,1]) + ball_speed_y *= random.choice([-1,1]) + +def point_won(winner): + global cpu_points, player_points + + if winner == "cpu": + cpu_points += 1 + if winner == "player": + player_points += 1 + + reset_ball() + +def animate_ball(): + global ball_speed_x, ball_speed_y + ball.x += ball_speed_x + ball.y += ball_speed_y + + if ball.bottom >= screen_height or ball.top <= 0: + ball_speed_y *= -1 + + if ball.right >= screen_width: + point_won("cpu") + + if ball.left <= 0: + point_won("player") + + if ball.colliderect(player) or ball.colliderect(cpu): + ball_speed_x *= -1 + +def animate_player(): + player.y += player_speed + + if player.top <= 0: + player.top = 0 + + if player.bottom >= screen_height: + player.bottom = screen_height + +def animate_cpu(): + global cpu_speed + cpu.y += cpu_speed / 1.2 + + if ball.centery <= cpu.centery: + cpu_speed = -6 + if ball.centery >= cpu.centery: + cpu_speed = 6 + + if cpu.top <= 0: + cpu.top = 0 + if cpu.bottom >= screen_height: + cpu.bottom = screen_height + +pygame.init() + +screen_width = 1280 +screen_height = 800 + +screen = pygame.display.set_mode((screen_width, screen_height)) +pygame.display.set_caption("My Pong Game!") + +clock = pygame.time.Clock() + +ball = pygame.Rect(0,0,30,30) +ball.center = (screen_width/2, screen_height/2) + +cpu = pygame.Rect(0,0,20,100) +cpu.centery = screen_height/2 + +player = pygame.Rect(0,0,20,100) +player.midright = (screen_width, screen_height/2) + +ball_speed_x = 6 +ball_speed_y = 6 +player_speed = 0 +cpu_speed = 6 + +cpu_points, player_points = 0, 0 + +score_font = pygame.font.Font(None, 100) + +while True: + #Check for events + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + player_speed = -6 + if event.key == pygame.K_DOWN: + player_speed = 6 + if event.type == pygame.KEYUP: + if event.key == pygame.K_UP: + player_speed = 0 + if event.key == pygame.K_DOWN: + player_speed = 0 + + #Change the positions of the game objects + animate_ball() + animate_player() + animate_cpu() + + #Clear the screen + screen.fill('black') + + #Draw the score + cpu_score_surface = score_font.render(str(cpu_points), True, "white") + player_score_surface = score_font.render(str(player_points), True, "white") + screen.blit(cpu_score_surface,(screen_width/4,20)) + screen.blit(player_score_surface,(3*screen_width/4,20)) + + #Draw the game objects + pygame.draw.aaline(screen,'white',(screen_width/2, 0), (screen_width/2, screen_height)) + pygame.draw.ellipse(screen,'white',ball) + pygame.draw.rect(screen,'white',cpu) + pygame.draw.rect(screen,'white',player) + + #Update the display + pygame.display.update() + clock.tick(60) \ No newline at end of file