Pong Python

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

import pygame

import random

# Initialize Pygame
pygame.init()

# Define constants
WIDTH = 800
HEIGHT = 600
BALL_RADIUS = 10
PAD_WIDTH = 8
PAD_HEIGHT = 80
HALF_PAD_WIDTH = PAD_WIDTH // 2
HALF_PAD_HEIGHT = PAD_HEIGHT // 2
BALL_POS = [WIDTH // 2, HEIGHT // 2]
BALL_VEL = [0, 0]
LEFT = False
RIGHT = True
PADDLE_SPEED = 5
SCORE1 = 0
SCORE2 = 0
FONT = pygame.font.Font(None, 36)

# Set up the display


screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong Game")

# Define the ball spawn function


def ball_spawn(direction):
global BALL_POS, BALL_VEL # Update global variables
BALL_POS = [WIDTH // 2, HEIGHT // 2]
horz = random.randrange(120, 240) / 60.0
vert = random.randrange(60, 180) / 60.0
if direction == LEFT:
horz = -horz
BALL_VEL = [horz, -vert]

# Define the Paddle class


class Paddle:
def __init__(self, x):
self.y = HEIGHT // 2
self.color = (255, 255, 255)
self.vel = 0
self.rect = pygame.Rect(x, self.y - HALF_PAD_HEIGHT, PAD_WIDTH, PAD_HEIGHT)

def move_up(self):
self.vel = -PADDLE_SPEED

def move_down(self):
self.vel = PADDLE_SPEED

def stop(self):
self.vel = 0

def update(self):
self.y += self.vel
if self.y - HALF_PAD_HEIGHT < 0:
self.y = HALF_PAD_HEIGHT
elif self.y + HALF_PAD_HEIGHT > HEIGHT:
self.y = HEIGHT - HALF_PAD_HEIGHT
self.rect.centery = self.y

def draw(self):
pygame.draw.rect(screen, self.color, self.rect)

# Set up the game objects


left_paddle = Paddle(0)
right_paddle = Paddle(WIDTH - PAD_WIDTH)
ball_spawn(RIGHT)

# Set up the game clock


clock = pygame.time.Clock()

# Start the game loop


while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
left_paddle.move_up()
elif event.key == pygame.K_s:
left_paddle.move_down()
elif event.key == pygame.K_UP:
right_paddle.move_up()
elif event.key == pygame.K_DOWN:
right_paddle.move_down()
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_w, pygame.K_s):
left_paddle.stop()
elif event.key in (pygame.K_UP, pygame.K_DOWN):
right_paddle.stop()

# Move the paddles and ball


left_paddle.update()
right_paddle.update()
BALL_POS[0] += int(BALL_VEL[0])
BALL_POS[1] += int(BALL_VEL[1])

# Handle ball collisions with top and bottom walls


if BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT - BALL_VEL[1]:
# Handle ball collisions with paddles

if BALL_POS[0] <= BALL_RADIUS + PAD_WIDTH and


left_paddle.rect.collidepoint(BALL_POS):
BALL_VEL[0] = -BALL_VEL[0] * 1.1
BALL_VEL[1] = BALL_VEL[1] * 1.1
elif BALL_POS[0] >= WIDTH - BALL_RADIUS - PAD_WIDTH and
right_paddle.rect.collidepoint(BALL_POS):
BALL_VEL[0] = -BALL_VEL[0] * 1.1
BALL_VEL[1] = BALL_VEL[1] * 1.1

# Handle ball going off the screen


if BALL_POS[0] <= BALL_RADIUS + PAD_WIDTH:
SCORE2 += 1
ball_spawn(RIGHT)
elif BALL_POS[0] >= WIDTH - BALL_RADIUS - PAD_WIDTH:
SCORE1 += 1
ball_spawn(LEFT)
elif BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT - BALL_RADIUS:
BALL_VEL[1] = -BALL_VEL[1]

# Draw the game objects


screen.fill((0, 0, 0))
pygame.draw.line(screen, (255, 255, 255), (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))
pygame.draw.circle(screen, (255, 255, 255), (int(BALL_POS[0]), int(BALL_POS[1])),
BALL_RADIUS)
left_paddle.draw()
right_paddle.draw()
score_text = FONT.render(str(SCORE1) + " - " + str(SCORE2), True, (255, 255, 255))
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))

# Update the display


pygame.display.update()

# Set the frame rate


clock.tick(60)
# Initialize Pygame
pygame.init()

# Set the window size


WIDTH = 600
HEIGHT = 400
WINDOW_SIZE = (WIDTH, HEIGHT)

# Set the paddle and ball parameters


PAD_WIDTH = 8
PAD_HEIGHT = 80
BALL_RADIUS = 20
BALL_VEL = [5, -5]

# Set the colors


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Set the font


FONT = pygame.font.SysFont("Arial", 36)

# Set the scores


SCORE1 = 0
SCORE2 = 0

# Set the paddle class


class Paddle:
def __init__(self, side):
self.width = PAD_WIDTH
self.height = PAD_HEIGHT
self.pos = (0, HEIGHT // 2 - self.height // 2)
if side == "left":
self.pos = (0, HEIGHT // 2 - self.height // 2)
elif side == "right":
self.pos = (WIDTH - self.width, HEIGHT // 2 - self.height // 2)
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.width, self.height)

def draw(self):
pygame.draw.rect(screen, WHITE, self.rect)

def update(self, vel):


new_pos = self.pos[1] + vel
if new_pos >= 0 and new_pos <= HEIGHT - self.height:
self.pos = (self.pos[0], new_pos)
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.width,
self.height)

# Set the ball class


class Ball:
def __init__(self):
self.pos = (WIDTH // 2, HEIGHT // 2)

def update(self):
global BALL_POS, BALL_VEL
BALL_POS[0] += BALL_VEL[0]
BALL_POS[1] += BALL_VEL[1]

# Set the function to spawn the ball


def ball_spawn(direction):
global BALL_POS, BALL_VEL
BALL_POS = [WIDTH // 2, HEIGHT // 2]
if direction == LEFT:
BALL_VEL = [-5, -5]
elif direction == RIGHT:
BALL_VEL = [5, -5]

# Set the screen


screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Pong")

# Set the clock


clock = pygame.time.Clock()

# Spawn the paddles and ball


left_paddle = Paddle("left")
right_paddle = Paddle("right")
ball = Ball()
ball_spawn(random.choice([LEFT, RIGHT]))

# Run the game loop


running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
left_paddle.update(-5)
elif event.key == pygame.K_s:
left_paddle.update(5)
elif event.key == pygame.K_UP:
right_paddle.update(-5)
elif event.key == pygame.K_DOWN:
right_paddle.update(5)

# Update the paddles and ball


ball.update()
left_paddle.draw()
right_paddle.draw()

# Handle ball collisions with the top and bottom of the screen
if BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT - BALL_RADIUS:
BALL_VEL[1] = -BALL_VEL[1]

# Handle ball collisions with the paddles


if ball_rect.colliderect(left_paddle.rect) or
ball_rect.colliderect(right_paddle.rect):
BALL_VEL[0] = -BALL_VEL[0]

# Handle ball going out of bounds


if BALL_POS[0] <= BALL_RADIUS:
SCORE2 += 1
ball_spawn(RIGHT)
elif BALL_POS[0] >= WIDTH - BALL_RADIUS:
SCORE1 += 1
ball_spawn(LEFT)

# Update the ball position


ball_rect = pygame.draw.circle(screen, WHITE, BALL_POS, BALL_RADIUS)

# Draw the scores


score_text = FONT.render(str(SCORE1) + " - " + str(SCORE2), True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 10))

# Update the display


pygame.display.flip()
screen.fill(BLACK)

# Set the game speed


clock.tick(60)

# Quit the game


pygame.quit()
sys.exit()

You might also like