Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created January 4, 2024 13:31
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/136e00a41ea54e5738e5b5f1e03fc459 to your computer and use it in GitHub Desktop.
Save ssaurel/136e00a41ea54e5738e5b5f1e03fc459 to your computer and use it in GitHub Desktop.
Pong Object for a Pong Game on the SSaurel's Blog
# Now, we can define the Pong Game Object
class Pong:
def __init__(self, root, width, height, margin):
paddlewidth = width / 50
paddleheight = height / 12
self.leftpoints = 0
self.lefttxt = None
self.rightpoints = 0
self.righttxt = None
self.render = True # True when we need to render the game on the canvas
# we manage left up / down for moving the left paddle
self.leftup = False
self.leftdown = False
# same for right paddle
self.rightup = False
self.rightdown = False
self.width = width
self.height = height
self.margin = margin
self.root = root
self.root.title("Pong Game - SSaurel's Blog")
self.root.geometry(str(width) + "x" + str(height))
# we create the canvas
self.canvas = tk.Canvas(self.root, width = width, height = height, bg = 'black')
self.paddleleft = Paddle(self.canvas, margin, height / 2 - paddleheight / 2, paddlewidth, paddleheight, height)
self.paddleright = Paddle(self.canvas, (width - margin) - paddlewidth, height / 2 - paddleheight / 2, paddlewidth, paddleheight, height)
self.ball = Ball(self.canvas, paddlewidth, VELOCITY, width, height)
self.canvas.pack()
self.drawmiddlelines()
self.drawboard()
self.move()
# we move draw middle lines for the board
def drawmiddlelines(self):
leftx = self.width / 2 - self.paddleleft.width / 2
for y in range(0, self.height, int(self.paddleleft.height + self.margin * 2)):
self.canvas.create_rectangle(leftx, y, leftx + self.paddleleft.width, y + self.paddleleft.height, fill = 'grey')
def drawboard(self):
try:
# draw the paddles
self.paddleleft.draw()
self.paddleright.draw()
# draw points
self.drawpoints()
# draw the ball
self.ball.draw()
except:
# some strange exception occur here when we quit the game. We need to call explicitly exit!
os._exit(0)
def drawpoints(self):
# we delete the previous score for the left paddle
if self.lefttxt != None:
self.canvas.delete(self.lefttxt)
# we write the new score
self.lefttxt = self.canvas.create_text(self.width / 2 - 50, 50, text = str(self.leftpoints), fill = 'grey', font = ("Helvetica 35 bold"))
# the same thing for the right paddle
if self.righttxt != None:
self.canvas.delete(self.righttxt)
# we write the new score
self.righttxt = self.canvas.create_text(self.width / 2 + 50, 50, text = str(self.rightpoints), fill = 'grey', font = ("Helvetica 35 bold"))
# we define the move method to update the game elements
def move(self):
if self.render:
# use a timer to call this method each X milliseconds
self.timer = threading.Timer(0.05, self.move)
self.timer.start()
# we manage touch events
if self.leftup:
self.paddleleft.top()
if self.leftdown:
self.paddleleft.down()
if self.rightup:
self.paddleright.top()
if self.rightdown:
self.paddleright.down()
# True if the Ball touched one of both sides of the board
state = self.ball.move(self, self.paddleright, self.paddleleft)
if state:
self.restart() # we need to restart the ball
self.drawboard()
def restart(self):
self.ball.restart()
# Time to manage keyboards event from users
# We need to make this special code to detect several keys used at the same time on the keyboard
# z / s for the left paddle - o / l for the right paddle
def keypress(self, event):
match event.char:
case 'z':
self.leftup = True
case 's':
self.leftdown = True
case 'o':
self.rightup = True
case 'l':
self.rightdown = True
def keyrelease(self, event):
match event.char:
case 'z':
self.leftup = False
case 's':
self.leftdown = False
case 'o':
self.rightup = False
case 'l':
self.rightdown = False
# last method: we define a method to kill the timer and stop the rendering of the game
def killtimer(self):
self.render = False
self.timer.cancel()
self.root.destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment