Untitled
unknown
plain_text
3 years ago
1.4 kB
9
Indexable
# Import necessary modules
import pygame
import random
# Initialize the game engine
pygame.init()
# Set the window size
width, height = 640, 480
# Create the screen
screen = pygame.display.set_mode((width, height))
# Set the title of the window
pygame.display.set_caption("Pong")
# Create the player's paddle
paddle_width = 15
paddle_height = 100
paddle_x = 20
paddle_y = height / 2 - paddle_height / 2
# Create the ball
ball_x = width / 2
ball_y = height / 2
ball_radius = 20
# Create the game loop
running = True
while running:
# Handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the screen
screen.fill((0, 0, 0)) # Clear the screen
# Check if a point was scored and create confetti if so
if ball_x > width:
pixels = pygame.PixelArray(screen)
for i in range(100):
x = random.randint(0, width - 1)
y = random.randint(0, height - 1)
pixels[x][y] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
del pixels
pygame.draw.rect(screen, (255, 255, 255), (paddle_x, paddle_y, paddle_width, paddle_height)) # Draw the paddle
pygame.draw.circle(screen, (0, 0, 255), (ball_x, ball_y), ball_radius) # Draw the blue ball
pygame.display.flip() # Update the screen
# Close the game engine
pygame.quit()
Editor is loading...