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

do = 1 over =0 ball = 180 up=1 down=0 selfmaster = Tk() selfmaster.title(\"pingPong\") y = Canvas(selfmaster,width=800,height=400) y.pack() xc = float(2) zc = float(random.randint(-5,5)) while(zc==0): zc = float(random.randint(-5,5)) ball = y.create_oval(ball-5,ballX+5) player = y.create_rectangle(width=80, height=20) lives = 5 font = tkFont.Font(family = \"Verdana\", size = 20) moveup(event): global player, ball ; up = 1 movedown(event): global player, ball; down = 1 finish(event): global do, lives lives = -1 do = 0 selfmaster.quit selfmaster.quit() released(event): global up, down, do, lives key = event.keysym if (str(key)==\"r\"): up = 0 if (str(key)==\"w\"): down = 0 if (str(key)==\"finish\" and over==1): selfmaster.quit do = 0 lives = -1 selfmaster.quit() button(): selfmaster.bind(\"\",up) selfmaster.bind(\"\",down) selfmaster.bind(\"`\",finish) selfmaster.bind(\'\', released) button()
This is python 3, and here is my code. from tkinter import * class Pong(Frame): def __init__(self): Frame.__init__(self) self.master.title(\
This is python 3, and here is my code. from tkinter import * class Pong(Frame): def __init__(self): Frame.__init__(self) self.master.title(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site