Click to See Code
import pygame, random
#Object classes
class Ball:
def __init__(self, screen, color, xPos, yPos, radius):
self.screen = screen
self.color = color
self.xPos = xPos
self.yPos = yPos
self.radius = radius
self.dx = 0
self.dy = 0
self.show()
def show(self):
pygame.draw.circle(self.screen, self.color,(self.xPos,self.yPos), self.radius )
def moveStart(self):
self.dx = -10
self.dy = 5
def inMotion(self):
self.xPos += self.dx
self.yPos += self.dy
def paddleCollisions(self):
self.dx = -self.dx
def wallCollisions(self):
self.dy = -self.dy
def newRound(self):
self.xPos = WIDTH//2
self.yPos = HEIGHT//2
self.dx = 0
self.dy = 0
self.show()
class Paddle:
def __init__(self, screen, color, xPos, yPos, width, height):
self.screen = screen
self.color = color
self.xPos = xPos
self.yPos = yPos
self.width = width
self.height = height
self.state = 'still'
self.show()
def show(self):
pygame.draw.rect(self.screen, self.color,(self.xPos,self.yPos, self.width, self.height))
def move(self):
if self.state == 'up':
self.yPos -= 20
elif self.state == 'down':
self.yPos += 20
def aiMove(self):
if ball.yPos <= paddle2.yPos and ball.yPos + ball.radius >= paddle2.yPos:
paddle2.yPos -= random.randint(4,6)
elif ball.yPos >= paddle2.yPos and ball.yPos <= paddle2.yPos + paddle.height:
paddle2.yPos += random.randint(4,6)
def constraints(self):
if self.yPos <= 0:
self.yPos = 0
elif self.yPos + self.height >= HEIGHT:
self.yPos = HEIGHT - self.height
class Collisions:
def playerBall(self, ball, paddle):
if ball.xPos >= paddle.xPos and ball.xPos <= paddle.xPos + paddle.width:
if ball.yPos >= paddle.yPos and ball.yPos <= paddle.yPos + paddle.height:
return True
return False
def aiBall(self, ball, paddle2):
if ball.xPos > paddle2.xPos and ball.xPos < paddle2.xPos + paddle2.width:
if ball.yPos >= paddle2.yPos and ball.yPos <= paddle2.yPos + paddle2.height:
return True
return False
def wallBall(self,ball):
#top of screen
if ball.yPos - ball.radius <= 0:
return True
#bottom of screen
if ball.yPos + ball.radius >= HEIGHT:
return True
return False
def winCond(self, ball):
return ball.xPos - ball.radius >= WIDTH
def lossCond(self,ball):
return ball.xPos + ball.radius <= 0
class Score:
def __init__(self, screen, points, xPos, yPos):
self.screen = screen
self.points = points
self.xPos = xPos
self.yPos = yPos
self.font = pygame.font.SysFont("arial", 80, bold=True)
self.label = self.font.render(self.points, 0, WHITE)
self.show()
def show(self):
self.screen.blit(self.label,(self.xPos - self.label.get_rect().width , self.yPos))
def incScore(self):
points = int(self.points) + 1
self.points = str(points)
self.label = self.font.render(self.points, 0, WHITE)
pygame.init()
fps = pygame.time.Clock()
#Screen specifics
WIDTH, HEIGHT = 900,500
BLACK = (0,0,0)
RED = (255,20,40)
WHITE = (255,255,255)
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Paddle Ball")
def screenSet():
screen.fill(BLACK)
screenSet()
#Objects
ball = Ball(screen, RED, WIDTH//2, HEIGHT//2, 15)
paddle = Paddle(screen, WHITE, 20 , HEIGHT//2 - 60 , 20, 140)
paddle2 = Paddle(screen, WHITE, WIDTH - 35, HEIGHT//2 - 60 , 20, 140)
collisions = Collisions()
playerScore = Score(screen, '0', WIDTH//4, 15)
aiScore = Score(screen, '0', WIDTH - WIDTH//4, 15)
#Game Functions
running = True
playing = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
ball.moveStart()
playing = True
if event.key == pygame.K_w:
paddle.state = 'up'
if event.key == pygame.K_s:
paddle.state = 'down'
if event.type == pygame.KEYUP:
paddle.state = 'still'
if playing:
screenSet()
#ball's movement
ball.inMotion()
ball.show()
#player paddle
paddle.move()
paddle.constraints()
paddle.show()
#ai paddle
paddle2.aiMove()
paddle2.constraints()
paddle2.show()
#Collisions
if collisions.playerBall(ball,paddle) or collisions.aiBall(ball,paddle2):
ball.paddleCollisions()
if collisions.wallBall(ball):
ball.wallCollisions()
if collisions.winCond(ball):
playerScore.incScore()
ball.newRound()
if collisions.lossCond(ball):
aiScore.incScore()
ball.newRound()
playerScore.show()
aiScore.show()
pygame.display.update()
fps.tick(60)
pygame.quit()
Get Click to See a Simple Paddle Ball in Python
Click to See a Simple Paddle Ball in Python
W for up, S for down - P to begin
Status | Released |
Author | SpottedOtter23 |
Leave a comment
Log in with itch.io to leave a comment.