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 the Pygame library in Python.
pip install pygame)import pygame
import sys
# Pygame Initialization
pygame.init()
# Screen size
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Paddle properties
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 100
PADDLE_SPEED = 5
# Ball properties
BALL_SIZE = 10
BALL_SPEED = 5
class Paddle(pygame.Rect):
def __init__(self, x, y):
super().__init__(x, y, PADDLE_WIDTH, PADDLE_HEIGHT)
self.speed = PADDLE_SPEED
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 - self.height:
self.y = SCREEN_HEIGHT - self.height
class Ball(pygame.Rect):
def __init__(self):
super().__init__(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, BALL_SIZE, BALL_SIZE)
self.speed_x = BALL_SPEED
self.speed_y = BALL_SPEED
def move(self):
self.x += self.speed_x
self.y += self.speed_y
if self.y < 0 or self.y > SCREEN_HEIGHT - self.height:
self.speed_y *= -1
def reset(self):
self.x = SCREEN_WIDTH / 2
self.y = SCREEN_HEIGHT / 2
self.speed_x *= -1
def draw_text(text, font, color, surface, x, y):
text_obj = font.render(text, 1, color)
text_rect = text_obj.get_rect()
text_rect.topleft = (x, y)
surface.blit(text_obj, text_rect)
def main():
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
paddle1 = Paddle(0, SCREEN_HEIGHT / 2 - PADDLE_HEIGHT / 2)
paddle2 = Paddle(SCREEN_WIDTH - PADDLE_WIDTH, SCREEN_HEIGHT / 2 - PADDLE_HEIGHT / 2)
ball = Ball()
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()
if ball.x < 0:
score2 += 1
ball.reset()
elif ball.x > SCREEN_WIDTH - ball.width:
score1 += 1
ball.reset()
if ball.colliderect(paddle1) or ball.colliderect(paddle2):
ball.speed_x *= -1
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, paddle1)
pygame.draw.rect(screen, WHITE, paddle2)
pygame.draw.ellipse(screen, WHITE, ball)
pygame.draw.aaline(screen, WHITE, (SCREEN_WIDTH / 2, 0), (SCREEN_WIDTH / 2, SCREEN_HEIGHT))
draw_text(str(score1), font, WHITE, screen, SCREEN_WIDTH / 4, 20)
draw_text(str(score2), font, WHITE, screen, SCREEN_WIDTH - SCREEN_WIDTH / 4, 20)
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()
pong.py.pip install pygame in your terminal.python pong.py in your terminal.W and S keys to move up and down.UP and DOWN arrow keys to move up and down.The game is a simple implementation of Pong. Two players control their respective paddles to hit the ball back and forth. The game keeps track of the score and resets the ball when a point is scored.