This is pong game python 3 here is my code I need additional
This is pong game (python 3), here is my code.
I need additional
If the ball hits the facing right or left corner of the paddle, you may use an angle of reflection that is different from the angle of incidence. If the ball hits the right or left corner of the paddle, you may bounce back to follow a path up that is the opposite of the ball\'s path down (i.e, angle of reflection + angle of incidence = 360 degrees.)
Add obstacles on the ball\'s way. When the ball hits an obstacle, the obstacle disappears or explodes. The game ends with a win if all obstacles (add at least 5) are destroyed before all lives are lost.
Remove any delays that might occur after the user presses the right or left arrow keys or when both the paddle and the ball are moving at the same time.
Make the program ends gracefully (with no error messages) when the user clicks the close button to close the application before the game is over.
Uses a class to represent obstacles so that collision detection, obstacle dimensions, colors ...etc. are stored as object properties and methods.
Allow the user to increase and decrease the animation speed. For example, increase the speed when the user presses the i key and decrease the speed when the user clicks the d key.
Make the ball bounce with a random angle. Notice, in this case dx and dy will be calculated based on the angle and speed of the ball.
Allow the user to specify initial speed (in pixels per second) and initial angle (in degrees).
from tkinter import *
class Pong(Frame):
     def __init__(self):
         self.root = Tk()
         Frame.__init__(self)
         self.master.title(\"Pong Game\")
         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 = (\"Verdana\", 20))
         lives_remaining = 5
         var.set(\"Lives left: \" + str(lives_remaining))
         label.pack()
        #draw a paddle
         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\")
         self.top_paddle = self.canvas_width / 2
         #move the paddle with left and right with arrow keys
         self.root.bind(\'<Left>\', self.left)
         self.root.bind(\'<Right>\', self.right)
         #move the paddle with left and right with mouse
         self.root.bind(\"<ButtonPress-1>\", self.left)
         self.root.bind(\"<ButtonPress-3>\", self.right)
        ball_diameter = 20   #draw a ball in pixels
         top_x = 2
         top_y = 2
         ball = self.canvas.create_oval(top_x, top_y, top_x + ball_diameter,
                                        top_y + ball_diameter, fill = \"red\" ,tags = \"ball\")
 ##        self.root.bind(\'<i>\', self.speedUp)
 ##        self.root.bind(\'<k>\', self.speedDown)
        vertical_direction = \"south\"
         horizontal_direction = \"east\"
         dx = dy = 2
         while True:
             if vertical_direction == \"south\":
                 top_y += dy   #dy is 2 because the ball moves 2 pixels vertically every 15 milliseconds
                 if top_y >= (self.canvas_height - ball_diameter) - 20:   # ball has hit the paddle
                     if self.top_paddle <= top_x and self.top_paddle + 80 >= top_x:   #when the ball has hit the top_paddle
                         vertical_direction = \"north\"   #change direction
                     else:   #when the ball has not hit the top_paddle
                         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\") #move the ball initial place(top left corner) of the canvas
                             var.set(\"Lives left: \" + str(lives_remaining))          
                          else:   #after lives_remaining 0 restatrt the game
                             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)   #move the ball vertically dy pixels to the down/south
             else:
                 top_y -= dy
                 if top_y <= 0:   #ball has hit top wall
                     top_y = 0
                     vertical_direction = \"south\"
                 self.canvas.move(\"ball\", 0, -dy)   #move the ball vertically dy pixels to the top/north
             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:   #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.after(15)   #sleep for 15 milliseconds
             self.canvas.update()    #update canvas
    def left(self, event):   #the paddle moves 5 pixels to the left
         if self.top_paddle >= 5:
             self.top_paddle -= 5
             self.canvas.move(\"paddle\", -5, 0)
    def right(self, event):   #the paddle moves 5 pixels to the right
         if self.top_paddle < self.canvas_width - 5:
             self.top_paddle += 5
             self.canvas.move(\"paddle\", 5, 0)
##    def speedUp(self, event):
 ##       
 ##
 ##    def speedDown(self, event):
        
        
def main():
     Pong().mainloop()
main()
Solution
Hey buddy,
I have added code to add the obstacles and increase/decrease the ball speed.
Few suggestions for you , it is good programming practice to modularize your code eg. create a different function
to intitialize your attributes, the while loop can be inside a function name \"start_game\" thenn it will be very helpful to debug your code , keep it more object oriented .
from tkinter import *
class Obstacle:
 def __init__(self):
 self.color = \"blue\"
 self.width = 50
 self.height = 50
 self.obstacles = []
   
 def drawObstacle(self, canvas):
 count = 0;
 first_x = 10
 first_y = 10
 gap = 60
 for i in range(10):
 count += 10;
 obstacle = canvas.create_rectangle(first_x + gap*i, first_y, first_x+self.width+gap*i, first_y+self.height, fill = self.color, tags = \"obstacle\"+str(i))
 self.obstacles.append(obstacle)
   
 class Pong(Frame):
 def __init__(self):
 self.root = Tk()
 Frame.__init__(self)
 self.master.title(\"Pong Game\")
 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 = (\"Verdana\", 20))
 lives_remaining = 5
 var.set(\"Lives left: \" + str(lives_remaining))
 label.pack()
 #draw a paddle
 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\")
 self.top_paddle = self.canvas_width / 2
 #move the paddle with left and right with arrow keys
 self.root.bind(\'<Left>\', self.left)
 self.root.bind(\'<Right>\', self.right)
 #move the paddle with left and right with mouse
 self.root.bind(\"<ButtonPress-1>\", self.left)
 self.root.bind(\"<ButtonPress-3>\", self.right)
 ball_diameter = 20 #draw a ball in pixels
 top_x = 2
 top_y = 2
 ball = self.canvas.create_oval(top_x, top_y, top_x + ball_diameter,
 top_y + ball_diameter, fill = \"red\" ,tags = \"ball\")
 self.root.bind(\'<i>\', self.speedUp)
 self.root.bind(\'<k>\', self.speedDown)
 vertical_direction = \"south\"
 horizontal_direction = \"east\"
   
 #create properties to hold x-speed and y-speed of ball
 self.dx = 2
 self.dy = 2
   
 #create obstacles
 obstacle = Obstacle()
 obstacle.drawObstacle(self.canvas)
 while True:
 dx = self.dx
 dy = self.dy
 if vertical_direction == \"south\":
 top_y += dy #dy is 2 because the ball moves 2 pixels vertically every 15 milliseconds
 if top_y >= (self.canvas_height - ball_diameter) - 20: # ball has hit the paddle
 if self.top_paddle <= top_x and self.top_paddle + 80 >= top_x: #when the ball has hit the top_paddle
 vertical_direction = \"north\" #change direction
 else: #when the ball has not hit the top_paddle
 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\") #move the ball initial place(top left corner) of the canvas
 var.set(\"Lives left: \" + str(lives_remaining))
 else: #after lives_remaining 0 restatrt the game
 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) #move the ball vertically dy pixels to the down/south
 else:
 top_y -= dy
 if top_y <= 0: #ball has hit top wall
 top_y = 0
 vertical_direction = \"south\"
 self.canvas.move(\"ball\", 0, -dy) #move the ball vertically dy pixels to the top/north
 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: #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.after(15) #sleep for 15 milliseconds
 self.canvas.update() #update canvas
  
 def left(self, event): #the paddle moves 5 pixels to the left
 if self.top_paddle >= 5:
 self.top_paddle -= 5
 self.canvas.move(\"paddle\", -5, 0)
 def right(self, event): #the paddle moves 5 pixels to the right
 if self.top_paddle < self.canvas_width - 5:
 self.top_paddle += 5
 self.canvas.move(\"paddle\", 5, 0)
   
 #increse the speed by 0.5
 def speedUp(self, event):
 self.dy += 0.5;
 self.dx += 0.5;
 #decrease the speed by 0.5
 def speedDown(self, event):
 self.dy -= 0.5;
 self.dx -= 0.5;
 
 def main():
 Pong().mainloop()
 main()





