I am trying to work on a paddle ball game that entails keeping a ball bouncing away from the bottom for as long as possible. In this game, there are five lives, which can get depleted when the ball hits the bottom of the screen, and resets itself once it has reached 0 lives. However, the ball does not bounce off of the paddle as it should, nor does the label for the score update as it ought to. The code for how I am trying to achieve the collision currently is tied to the vertical placement of the ball, the relative section of which is as follows:

        if vertical_direction == "south":
            top_y += dy 
            if top_y >= (self.canvas_height - ball_diameter) - 20:
                if self.top_paddle <= top_y and self.top_paddle + 80 >= top_y:
                    top_y = self.canvas_height - ball_diameter
                    vertical_direction = "north"
                elif top_y >= self.canvas_height - ball_diameter:
                    lives -= 1
                    if (lives >= 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 = "blue", tags = "ball")
                        var.set("Lives: " +lives_Label)

                    else:
                        lives= 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 = "blue", tags = "ball")

I have looked at a bit of code which I found to be most similar to mine, and tried to implement its methods for detecting collision as displayed above. My only guess is there is some obvious logical inconsistency I failed to notice that is obvious to a more experienced coder, and that is the cause of both of my problems.

For reference, here is the complete code thus far:

from tkinter import *
import tkinter.font
class BallBounce (Frame):

    def __init__(self):
        Frame.__init__(self)
        self.master.title("Bouncing Ball")
        self.grid()

        lives = 5
        lives_Label = str(lives)

        var = StringVar()
        font = tkinter.font.Font(family = "Verdana", size = 20)
        self._label = Label(self, font = font, textvariable = var)
        var.set("Lives: "+lives_Label)
        self._label.grid(row = 0, column = 0)

        self.canvas_width = 800
        self.canvas_height = 400

        self.canvas = Canvas(self, width = self.canvas_width, height = self.canvas_height, 
            bg = "white")
        self.canvas.grid(row = 1, column = 0)

        frame = Frame(self)
        frame.grid(row = 1, column = 0)


        top_x = 0
        top_y = 0

        ball_diameter = 20

        self.canvas.create_oval(top_x, top_y, ball_diameter, ball_diameter, fill = "blue", tags = "ball")

        horizontal_direction = "east"
        vertical_direction = "south"


        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


        self.canvas.bind("<Left>", lambda event: self.canvas.move("paddle", -5, 0))

        self.canvas.bind("<Right>", lambda event: self.canvas.move("paddle", 5, 0))

        self.canvas.focus_set()



        dx = 2
        dy = 2
        while True:

            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


            if vertical_direction == "south":
                top_y += dy 
                if top_y >= (self.canvas_height - ball_diameter) - 20:
                    if self.top_paddle <= top_y and self.top_paddle + 80 >= top_y:
                        top_y = self.canvas_height - ball_diameter
                        vertical_direction = "north"
                    elif top_y >= self.canvas_height - ball_diameter:
                        lives -= 1
                        if (lives >= 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 = "blue", tags = "ball")
                            var.set("Lives: " +lives_Label)

                        else:
                            lives= 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 = "blue", tags = "ball")

                self.canvas.move("ball", 0, dy)
            else: 
                top_y -= dy
                if top_y <= 0:
                    top_y = 0 
                    vertical_direction = "south"
                self.canvas.move("ball", 0, -dy) 

            self.canvas.after(15) 
            self.canvas.update() 

def main():
    BallBounce().mainloop()

main()

0 Answers