This is python 3 and here is my code from tkinter import cl
This is python 3, and here is my code.
from tkinter import *
class Pong(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title(\"Bouncing Ball\")
self.grid()
canvas_width = 800 # canvas width
canvas_height = 400 # canvas height
self.canvas = Canvas(width = canvas_width, height = canvas_height , bg = \"white\")
self.canvas.grid()
# draw ball near the top left corner of the canvas - top-left corner of surrounding box is at (2,2)
ball_diameter = 20 # in pixels
top_x = 2 # top-left coordinate of circle\'s bounding rectangle
top_y = 2
self.canvas.create_oval(top_x, top_y, top_x + ball_diameter,
top_y + ball_diameter, fill = \"red\", tags = \"ball\")
direction = \"south\" # ball\'s direction
horizontal_direction = \"east\"
dx = dy = 2
# move ball
while True:
# keep trak of ball\'s movement, update direction, and draw ball as needed
if direction == \"south\":
top_y += dy
if top_y >= (canvas_height - ball_diameter): # ball has hit bottom wall
top_y = canvas_height - ball_diameter
direction = \"north\"
self.canvas.move(\"ball\", 0, dy)
else: # i.e., direction is \"up\"
top_y -= dy
if top_y <= 0: # ball has hit top wall
top_y = 0
direction = \"south\"
self.canvas.move(\"ball\", 0, -dy)
#self.canvas.move(\"ball\", 0, dy)
self.canvas.after(15) # Sleep for 15 milliseconds
# horizontal movement
if horizontal_direction == \"east\":
top_x += dx # dx is 2 because the ball moves 2 pixels horizontally every 15 milliseconds
if top_x >= canvas_width - ball_diameter: # ball has hit east wall
top_x = canvas_width - ball_diameter
horizontal_direction = \"west\" # change direction
self.canvas.move(\"ball\", dx, 0) # move ball horizontally dx pixels to the right/east
else: # i.e., horizontal_direction is \"west\"
top_x -= dx
if top_x <= 0: # ball has hit west wall
top_x = 0 # you may need to adjust this a little
horizontal_direction = \"east\" # change direction
self.canvas.move(\"ball\", -dx, 0) # move ball horizontally dx pixels to the left/west
self.canvas.update() # Update canvas
def main():
Pong().mainloop()
I would like to
Add a label at the top with the same width as the canvas to show the number of lives left until the game is over. The width of the label is the same as the canvas\' width. Notice that the width attribute sets a label\'s width in characters. Using a negative value sets the width in pixels. The diagram above uses a Verdana 20pt font but you may use any font that is different from tkinter\'s default font.
Draw a paddle at the center of the canvas\' bottom edge as shown in the above diagram. The paddle\'s width and height are 80 and 20 pixels, respectively. Whenever the user presses the left arrow button, the paddle moves 5 pixels to the left. Likewise, when the user presses the right arrow bottom, the paddle moves 5 pixels to the right. The paddle never moves beyond the right or left edges of the canvas.
The user wants to use the paddle to catch the ball so that it does not hit the bottom wall. Every time the ball hits the bottom wall, the player loses a life and the number of lives left is updated on the label. The game will end when number of lives left is 0. When the game ends, move the ball to its initial position near the top left corner of the canvas. Start the game with 5 lives. Every time the ball hits the paddle, it will bounce off the top of the paddle. Notice that a collision between the paddle and the ball occurs when any part of the ball hits the top surface of the paddle. Sample screen shots are shown below.
This is example picture.
Solution
import tkinter as tk
from tkinter import *
class Pong(Frame):
def __init__(self):
self.root = tk.Tk()
Frame.__init__(self)
self.master.title(\"Bouncing Ball\")
self.grid()
self.canvas_width = 800 # canvas width
self.canvas_height = 400 # canvas height
self.canvas = Canvas(width = self.canvas_width, height = self.canvas_height , bg = \"white\")
self.canvas.grid()
var = StringVar()
label = Label(self, textvariable=var, font=(\"Helvetica\", 16))
# draw ball near the top left corner of the canvas - top-left corner of surrounding box is at (2,2)
ball_diameter = 20 # in pixels
top_x = 2 # top-left coordinate of circle\'s bounding rectangle
top_y = 2
ball = self.canvas.create_oval(top_x, top_y, top_x + ball_diameter, top_y + ball_diameter, fill = \"red\", tags = \"ball\")
paddle = self.canvas.create_rectangle(self.canvas_width/2, self.canvas_height-20, self.canvas_width/2+80, self.canvas_height, fill=\"black\", tags=\"paddle\")
lives_remaining = 5
# self.canvas.
var.set(\"Lives left: \"+str(lives_remaining))
label.pack()
direction = \"south\" # ball\'s direction
horizontal_direction = \"east\"
self.top_paddle_x = self.canvas_width/2
self.root.bind(\'<Left>\', self.move_paddle_left)
self.root.bind(\'<Right>\', self.move_paddle_right)
dx = dy = 2
# move ball
while True:
# keep trak of ball\'s movement, update direction, and draw ball as needed
if direction == \"south\":
top_y += dy
if top_y >= (self.canvas_height - ball_diameter) - 20: # ball has hit bottom wall
if self.top_paddle_x <= top_x and self.top_paddle_x+80>=top_x:
top_y = self.canvas_height - ball_diameter
direction = \"north\"
else:
lives_remaining -= 1
if(lives_remaining>=0):
top_x = 2
top_y = 2
self.canvas.delete(ball)
ball = self.canvas.create_oval(top_x, top_y, top_x + ball_diameter, top_y + ball_diameter, fill = \"red\", tags = \"ball\")
var.set(\"Lives left: \"+str(lives_remaining))
else:
lives_remaining = 5
top_x = 2
top_y = 2
self.canvas.delete(ball)
ball = self.canvas.create_oval(top_x, top_y, top_x + ball_diameter, top_y + ball_diameter, fill = \"red\", tags = \"ball\")
var.set(\"Lives left: \"+str(lives_remaining))
self.canvas.move(\"ball\", 0, dy)
else: # i.e., direction is \"up\"
top_y -= dy
if top_y <= 0: # ball has hit top wall
top_y = 0
direction = \"south\"
self.canvas.move(\"ball\", 0, -dy)
#self.canvas.move(\"ball\", 0, dy)
self.canvas.after(15) # Sleep for 15 milliseconds
# horizontal movement
if horizontal_direction == \"east\":
top_x += dx # dx is 2 because the ball moves 2 pixels horizontally every 15 milliseconds
if top_x >= self.canvas_width - ball_diameter: # ball has hit east wall
top_x = self.canvas_width - ball_diameter
horizontal_direction = \"west\" # change direction
self.canvas.move(\"ball\", dx, 0) # move ball horizontally dx pixels to the right/east
else: # i.e., horizontal_direction is \"west\"
top_x -= dx
if top_x <= 0: # ball has hit west wall
top_x = 0 # you may need to adjust this a little
horizontal_direction = \"east\" # change direction
self.canvas.move(\"ball\", -dx, 0) # move ball horizontally dx pixels to the left/west
self.canvas.update() # Update canvas
def move_paddle_left(self, event):
if self.top_paddle_x>=5:
self.top_paddle_x -= 5
self.canvas.move(\"paddle\", -5, 0)
def move_paddle_right(self, event):
if self.top_paddle_x<self.canvas_width-5:
self.top_paddle_x += 5
self.canvas.move(\"paddle\", 5, 0)
# print (str(event) + \" key pressed\")
def main():
Pong().mainloop()
main()



