Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created January 4, 2024 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/7ff22fdeedadd690a31eeb62d0bf1e25 to your computer and use it in GitHub Desktop.
Save ssaurel/7ff22fdeedadd690a31eeb62d0bf1e25 to your computer and use it in GitHub Desktop.
Paddle Object for a Pong Game on the SSaurel's Blog
# Now, it is time to design the Paddle for our Pong Game
class Paddle:
def __init__(self, canvas, topx, topy, width, height, boardheight):
self.topx = topx
self.topy = topy
self.width = width
self.height = height
self.boardheight = boardheight
self.score = 0
self.canvas = canvas
# draw this paddle according positions passed in parameter
self.id = self.canvas.create_rectangle(self.topx, self.topy, self.topx + self.width, self.topy + self.height, fill = 'white')
# we update coords of this paddle
def draw(self):
self.canvas.coords(self.id, self.topx, self.topy, self.topx + self.width, self.topy + self.height)
# now, we need to manage down event then top event for the current paddle object
def top(self):
if self.topy - VELOCITY > 0:
self.topy = self.topy - VELOCITY
def down(self):
if (self.topy + self.height + VELOCITY) < self.boardheight:
self.topy = self.topy + VELOCITY
# use both methods to collide paddle right or left. As an exercise, you can improve this to make one generic method ;)
def collideright(self, ball):
if (ball.topx + ball.width) >= self.topx and (ball.topy >= self.topy or (ball.topy + ball.width) >= self.topy) and ((ball.topy + ball.width) <= (self.topy + self.height) or ball.topy <= (self.topy + self.height)):
return True
return False
def collideleft(self, ball):
if ball.topx <= (self.topx + self.width) and (ball.topy >= self.topy or (ball.topy + ball.width) >= self.topy) and ((ball.topy + ball.width) <= (self.topy + self.height) or ball.topy <= (self.topy + self.height)):
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment