0% found this document useful (0 votes)
21 views3 pages

Game

its a game made from python codding language

Uploaded by

tasleemafzal84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Game

its a game made from python codding language

Uploaded by

tasleemafzal84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import sys

# Initialize Pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100
BALL_RADIUS = 10
PADDLE_SPEED = 10
FPS = 60

WHITE = (255, 255, 255)


BLACK = (0, 0, 0)

# Create screen and set caption


screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2-Player Tennis")

# Create clock object for controlling frame rate


clock = pygame.time.Clock()

# Game objects
player1_paddle = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH,
PADDLE_HEIGHT)
player2_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT
// 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS
* 2, BALL_RADIUS * 2)

# Ball movement variables


ball_velocity_x = 5
ball_velocity_y = 5

# Scores
score_player1 = 0
score_player2 = 0

# Fullscreen flag
fullscreen = False

# Function to toggle fullscreen mode


def toggle_fullscreen():
global fullscreen
if fullscreen:
screen = pygame.display.set_mode((WIDTH, HEIGHT))
else:
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
fullscreen = not fullscreen

# Function to handle movement of paddles


def move_paddles():
keys = pygame.key.get_pressed()

# Player 1 controls (Left paddle): W and S


if keys[pygame.K_w] and player1_paddle.top > 0:
player1_paddle.y -= PADDLE_SPEED
if keys[pygame.K_s] and player1_paddle.bottom < HEIGHT:
player1_paddle.y += PADDLE_SPEED

# Player 2 controls (Right paddle): Arrow Up and Arrow Down


if keys[pygame.K_UP] and player2_paddle.top > 0:
player2_paddle.y -= PADDLE_SPEED
if keys[pygame.K_DOWN] and player2_paddle.bottom < HEIGHT:
player2_paddle.y += PADDLE_SPEED

# Function to handle ball movement and collisions


def move_ball():
global ball_velocity_x, ball_velocity_y, score_player1, score_player2

ball.x += ball_velocity_x
ball.y += ball_velocity_y

# Ball collision with top and bottom walls


if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_velocity_y *= -1

# Ball collision with paddles


if ball.colliderect(player1_paddle) or ball.colliderect(player2_paddle):
ball_velocity_x *= -1

# Ball out of bounds (left side or right side)


if ball.left <= 0:
score_player2 += 1
reset_ball()

if ball.right >= WIDTH:


score_player1 += 1
reset_ball()

# Function to reset the ball position to the center


def reset_ball():
ball.x = WIDTH // 2 - BALL_RADIUS
ball.y = HEIGHT // 2 - BALL_RADIUS
ball_velocity_x *= -1 # Reverse direction

# Function to draw the game objects on the screen


def draw():
screen.fill(BLACK)

# Draw paddles
pygame.draw.rect(screen, WHITE, player1_paddle)
pygame.draw.rect(screen, WHITE, player2_paddle)

# Draw ball
pygame.draw.ellipse(screen, WHITE, ball)

# Draw the score


font = pygame.font.Font(None, 36)
score_text = font.render(f"Player 1: {score_player1} - Player 2:
{score_player2}", True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))

# Update the screen


pygame.display.flip()

# Main game loop


def game_loop():
global fullscreen
global score_player1, score_player2

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Listen for F11 to toggle full screen


if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F11:
toggle_fullscreen()

# Listen for Escape to exit full screen


if event.key == pygame.K_ESCAPE:
if fullscreen:
toggle_fullscreen()

# Handle movement
move_paddles()
move_ball()

# Draw everything
draw()

# Limit the frame rate


clock.tick(FPS)

# Run the game


game_loop()

You might also like