Code me a pong game in python
- User
Code me a pong game in python
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Code me a pong game in python
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple implementation of the classic Pong game using Python and the Pygame library.
pip install pygame)import pygame
import sys
# Game Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BALL_RADIUS = 20
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 100
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
class Ball:
def __init__(self):
self.x = SCREEN_WIDTH // 2
self.y = SCREEN_HEIGHT // 2
self.vx = 5
self.vy = 5
def move(self):
self.x += self.vx
self.y += self.vy
if self.y < BALL_RADIUS or self.y > SCREEN_HEIGHT - BALL_RADIUS:
self.vy *= -1
def reset(self):
self.x = SCREEN_WIDTH // 2
self.y = SCREEN_HEIGHT // 2
self.vx *= -1
class Paddle:
def __init__(self, x, y):
self.x = x
self.y = y
self.speed = 5
def move_up(self):
self.y -= self.speed
if self.y < 0:
self.y = 0
def move_down(self):
self.y += self.speed
if self.y > SCREEN_HEIGHT - PADDLE_HEIGHT:
self.y = SCREEN_HEIGHT - PADDLE_HEIGHT
def draw_text(text, font_size, x, y):
font = pygame.font.SysFont('Arial', font_size)
text_surface = font.render(text, True, WHITE)
screen.blit(text_surface, (x, y))
def main():
global screen, clock
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
ball = Ball()
paddle1 = Paddle(0, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2)
paddle2 = Paddle(SCREEN_WIDTH - PADDLE_WIDTH, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2)
score1 = 0
score2 = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
paddle1.move_up()
if keys[pygame.K_s]:
paddle1.move_down()
if keys[pygame.K_UP]:
paddle2.move_up()
if keys[pygame.K_DOWN]:
paddle2.move_down()
ball.move()
# Collision with paddles
if (ball.x - BALL_RADIUS < PADDLE_WIDTH and
ball.y > paddle1.y and
ball.y < paddle1.y + PADDLE_HEIGHT):
ball.vx *= -1
elif (ball.x + BALL_RADIUS > SCREEN_WIDTH - PADDLE_WIDTH and
ball.y > paddle2.y and
ball.y < paddle2.y + PADDLE_HEIGHT):
ball.vx *= -1
# Score points
if ball.x < BALL_RADIUS:
score2 += 1
ball.reset()
elif ball.x > SCREEN_WIDTH - BALL_RADIUS:
score1 += 1
ball.reset()
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, (paddle1.x, paddle1.y, PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.rect(screen, WHITE, (paddle2.x, paddle2.y, PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.circle(screen, WHITE, (ball.x, ball.y), BALL_RADIUS)
pygame.draw.aaline(screen, WHITE, (SCREEN_WIDTH // 2, 0), (SCREEN_WIDTH // 2, SCREEN_HEIGHT))
draw_text(str(score1), 32, SCREEN_WIDTH // 4, 20)
draw_text(str(score2), 32, SCREEN_WIDTH - SCREEN_WIDTH // 4, 20)
pygame.display.flip()
clock.tick(FPS)
if __name__ == '__main__':
main()
pip install pygame in your terminal/command prompt.pong.py.python pong.py in your terminal/command prompt.W and S keys to control the left paddle.UP and DOWN keys to control the right paddle.FPS constant.speed attribute in the Paddle class and the vx/vy attributes in the Ball class.Enjoy playing Pong!