Pong Game

Pong Game by Amany Horani
Code:
import random
from pygame import *
import pygame
pygame.display.set_caption("Pong Game")
# Screen dimensions
SCREENWIDTH = 900
SCREENHEIGHT = 600
screen = display.set_mode((SCREENWIDTH, SCREENHEIGHT))
# Initialise a font for text
font.init()
calibriBold35 = font.SysFont("Calibri Bold", 35)
calibriBold30 = font.SysFont("Calibri Bold", 30)
# Background color
WHITE = (255, 255, 255)
BGCOLOR = (50, 50, 50)
BLUE = (50, 100, 240)
RED = (230, 50, 100)
# Player1 and 2 paddle definitions
p1Y = 250
p2Y = 250
paddleWidth = 30
paddleHeight = 100
# Score text definitions
p1Points = 0
p2Points = 0
# Ball properties
ball_radius = 5
ball_color = WHITE
# ball move right if positive and left if negative
ballX = 450
# ball move down if positive and up if negative
ballY = 300
# ball speed for the x component
ballDx = 0
# ball speed for the y component
ballDy = 0
running = True
myClock = time.Clock()
ball_direction_set = False
game_over = False
while running:
for evt in event.get():
if evt.type == QUIT:
running = False
if not game_over:
p1Paddle = Rect(50, p1Y, paddleWidth, paddleHeight)
p2Paddle = Rect(820, p2Y, paddleWidth, paddleHeight)
ball = Rect(ballX, ballY, 10, 10)
#This variable gets the most key presses
keys = key.get_pressed()
# Reset ball position only if a point is scored
if (ballX >= SCREENWIDTH or ballX if ballX >= SCREENWIDTH: # if ball collides with right side of screen (p1 score)
p1Points += 1
elif ballX p2Points += 1
ballX = 450
ballY = 300
p1Y = 250
p2Y = 250
ballDx = 0
ballDy = 0
ball_direction_set = False
# Reset the flag for ball direction
if any([keys[K_w] or keys[K_s] or keys[K_UP] or keys[K_DOWN]]) and not ball_direction_set:
ballX = 450
ballY = 300
ballDx = random.choice([-6, 6])
ballDy = random.choice([-6, 6])
ball_direction_set = True
#keys[K_w] refers to the player1 pressing the w key
if (keys[K_w] and p1Y > 0):
p1Y -= 5
#keys[K_s] refers to the player1 pressing the s key
elif (keys[K_s] and p1Y+paddleHeight p1Y += 5
if (keys[K_UP] and p2Y > 0):
p2Y -= 5
elif (keys[K_DOWN] and p2Y+paddleHeight p2Y += 5
ballX += ballDx
ballY += ballDy
# if ball collides with left paddle then it will change direction to right
if (ball.colliderect(p1Paddle)):
ballDx = abs(ballDx)
# if ball collides with right paddle then it will change direction to left
elif (ball.colliderect(p2Paddle)):
ballDx = abs(ballDx) * -1
# if ball collides with top of the screen then it will change direction to down
elif (ballY ballDy = abs(ballDy)
# if ball collides with bottom of screen then it will change direction to up
elif (ballY >= SCREENHEIGHT):
ballDy = abs(ballDy) * -1
if p1Points == 5 and p2Points game_over = True
winner = calibriBold35.render("P1 WINS!", True, BLUE)
replay_text_color = BLUE
elif p2Points == 5 and p1Points game_over = True
winner = calibriBold35.render("P2 WINS!", True, RED)
replay_text_color = RED
if game_over:
replay_text = calibriBold35.render("Play Again?", True, replay_text_color)
# Reset points if "Play Again?" is clicked
if evt.type == MOUSEBUTTONDOWN and replay_text.get_rect(topleft=(390, 550)).collidepoint(evt.pos):
p1Points = 0
p2Points = 0
game_over = False
# basically repainting the screen or refreshing
screen.fill(BGCOLOR)
# drawing the objects to the screen and display them
draw.rect(screen, BLUE, p1Paddle)
draw.rect(screen, RED, p2Paddle)
draw.circle(screen, WHITE, (ballX, ballY), ball_radius)
p1PtsTxt = calibriBold35.render("P1 POINTS: " + str(p1Points), True, BLUE)
p2PtsTxt = calibriBold35.render("P2 POINTS: " + str(p2Points), True, RED)
quit_text = calibriBold35.render("QUIT", True, WHITE)
my_name = calibriBold30.render("By Amany Horani", True, WHITE)
if evt.type == MOUSEBUTTONDOWN and quit_text.get_rect(topleft=(420, 20)).collidepoint(evt.pos):
running = False
screen.blit(p1PtsTxt, (170, 20))
screen.blit(p2PtsTxt, (580, 20))
screen.blit(quit_text, (420, 20))
screen.blit(my_name, (20, 560))
if p1Points == 5 and p2Points screen.blit(winner, (400, 250))
screen.blit(replay_text, (390, 550))
p1Points == 0
elif p2Points == 5 and p1Points screen.blit(winner, (400, 250))
screen.blit(replay_text, (390, 550))
p1Points == 0
display.flip()
myClock.tick(60)
quit()