Amazing-Python-Scripts
84 строки · 3.3 Кб
1import pygame2# Initialize pygame
3
4pygame.init() # Initialize pygame5
6# Set the window size
7screen = pygame.display.set_mode((600, 400)) # Set the window size8
9# Set the title of the window
10pygame.display.set_caption("Pong") # Set the title of the window11
12# Set up the game clock
13clock = pygame.time.Clock() # Set up the game clock14
15# Load the images for the ball and paddles
16ball_image = pygame.image.load("Assets/ball.png")17paddle_image = pygame.image.load("Assets/paddle.png")18
19# Create the ball sprite
20ball = pygame.sprite.Sprite() # Create the ball sprite21ball.image = ball_image # Load the ball image22ball.rect = ball.image.get_rect() # Get the ball's rects23
24# Create the paddles
25left_paddle = pygame.sprite.Sprite() # Create the left paddle26left_paddle.image = paddle_image # Load the left paddle image27left_paddle.rect = left_paddle.image.get_rect() # Get the left paddle's rect28
29right_paddle = pygame.sprite.Sprite() # Create the right paddle30right_paddle.image = paddle_image # Load the right paddle image31right_paddle.rect = right_paddle.image.get_rect() # Get the right paddle's rect32
33# Set the initial positions of the ball and paddles
34ball.rect.center = (300, 200) # Set the ball's initial position35left_paddle.rect.left = 20 # Set the left paddle's initial position36left_paddle.rect.centery = 200 # Set the left paddle's initial position37right_paddle.rect.right = 580 # Set the right paddle's initial position38right_paddle.rect.centery = 200 # Set the right paddle's initial position39
40# Set the ball's initial velocity
41ball_vx = 542ball_vy = 543
44# Game loop
45while True:46# Handle events47for event in pygame.event.get(): # Handle events48if event.type == pygame.QUIT: # Handle the QUIT event49pygame.quit() # Quit pygame50exit() # Exit the program51
52# Update game state53ball.rect.x += ball_vx # Update the ball's x position54ball.rect.y += ball_vy # Update the ball's y position55
56# Update game state57keys = pygame.key.get_pressed() # Get the keys that are pressed58if keys[pygame.K_UP]: # Check if the up key is pressed59right_paddle.rect.y -= 5 # Update the right paddle's y position60if keys[pygame.K_DOWN]: # Check if the down key is pressed61right_paddle.rect.y += 5 # Update the right paddle's y position62if keys[pygame.K_w]: # Check if the w key is pressed63left_paddle.rect.y -= 5 # Update the left paddle's y position64if keys[pygame.K_s]: # Check if the s key is pressed65left_paddle.rect.y += 5 # Update the left paddle's y position66
67# Check for ball collision with paddles68# Check for ball collision with paddles69if ball.rect.colliderect(left_paddle.rect) or ball.rect.colliderect(right_paddle.rect):70ball_vx = -ball_vx # Reverse the ball's x velocity71
72# Check for ball collision with walls73if ball.rect.top < 0 or ball.rect.bottom > 400: # Check for ball collision with walls74ball_vy = -ball_vy # Reverse the ball's y velocity75
76# Draw the screen77screen.fill((0, 0, 0)) # Fill the screen with black78screen.blit(ball.image, ball.rect) # Draw the ball79screen.blit(left_paddle.image, left_paddle.rect) # Draw the left paddle80screen.blit(right_paddle.image, right_paddle.rect) # Draw the right paddle81pygame.display.flip() # Update the display82
83# Limit the frame rate84clock.tick(60)85