-1

This project is being done without pygame or other libraries that aren't built into Python 3. I have already made a pong game with a paddle and a ball moving around the screen. When the ball hits the paddle it bounces off like it normally does in any game like this. I want to generate a grid of rectangle shapes in the top middle part of the frame with the paddle and ball, and make it so when the ball hits a rectangle, the rectangle disappears. What is an effective way to do this and what would it look like, roughly speaking? Here is what I am working with currently:

from tkinter import *
import tkinter.font
import time

class Display(Frame):

    def __init__(self):
        Frame.__init__(self)
        self.master.title("Animation")
        self.grid()
        horizontal_direction = "east"
        vertical_direction = "south"
        self.canvas_width = 800
        self.canvas_height = 400
        self.paddle_x = 20
        self.paddle_y = 80
        self.left_rect_side = 360 #forposition
        self.canvas = Canvas(self, width=self.canvas_width, height=self.canvas_height, bg = "white")
        self.canvas.grid(row = 1, column = 0)
        self.master.bind('<Left>', lambda event: self.leftKey(self))
        self.master.bind('<Right>', lambda event: self.rightKey(self))
        self.x = 5
        self.y = 5
        diameter = 20
        self.canvas.create_oval(self.x, self.y, self.x + diameter, self.y + diameter, outline="#000000"
                           , fill="red", tags="circle")
        self.canvas.create_rectangle(self.canvas_width/2 - self.paddle_y/2, self.canvas_height - self.paddle_x,
                                     self.canvas_width/2 + self.paddle_y/2, self.canvas_height,
                                     fill="black", tags="paddle")

        fontx = tkinter.font.Font(family = "Verdana", size = 20)
        self.lives = 5
        self.lifedisplay = Label(self, width = -800, height = -20,
                                 font = fontx, text = "Lives left: " + str(self.lives))
        self.lifedisplay.grid(row = 0, column = 0)
        mvt = 2

        while True:

            if self.y + diameter > self.canvas_height:
                self.lives -= 1
                self.lifedisplay.configure(text = "Lives left: " + str(self.lives))

            if self.lives <= 0:
                self.canvas.move("circle", -self.x, -self.y)
                break

            if self.y + diameter >= self.canvas_height - self.paddle_x:
                if self.x + diameter > self.left_rect_side and self.x < self.left_rect_side + self.paddle_y:
                    vertical_direction = "north"

            if horizontal_direction == "east":
                if self.x + diameter >= self.canvas_width:
                    horizontal_direction = "west"
                else:
                    self.canvas.move("circle", mvt, 0)
                    self.x += mvt
            else:
                if self.x + diameter <= diameter:
                    horizontal_direction = "east"
                else:
                    self.canvas.move("circle", -mvt, 0)
                    self.x -= mvt

            if vertical_direction == "south":
                if self.y + diameter >= self.canvas_height:
                    vertical_direction = "north"
                    self.canvas.move("circle", 0, -mvt)
                    self.y -= mvt
                else:
                    self.canvas.move("circle", 0, mvt)
                    self.y += mvt
            else:
                if self.y + diameter <= diameter:
                    vertical_direction = "south"
                else:
                    self.canvas.move("circle", 0, -mvt)
                    self.y -= mvt

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

    @staticmethod
    def leftKey(self):
        if self.left_rect_side >= 10:
            self.canvas.move("paddle", -5, 0)
            self.left_rect_side -= 5
            self.canvas.update()

    @staticmethod
    def rightKey(self):
        if self.left_rect_side <= self.canvas_width - self.paddle_y - 5:
            self.canvas.move("paddle", 5, 0)
            self.left_rect_side += 5
            self.canvas.update()

def main():
    Display().mainloop()

main()
Ben Roux
  • 27
  • 8
  • Collision detection is covered well on Stack Overflow and elsewhere on the web. Where are you stuck after working through those examples? – Prune Apr 03 '18 at 17:12

1 Answers1

1

To do this, you should make a list of a bunch of rectangle objects that have 4 attributes:

  • x coordinate
  • y coordinate
  • width
  • height

Additionally, you will have to figure out the x/y coordinate for the center of your circle. Then, each frame, have a method that checks for a collision between the circle and each rectangle. Remove the rectangle from this list of rectangles if it is hit by the circle, then only draw the rectangles from the list onto the screen.

Checking for the collision is the hardest part. I would recommend checking out this StackOverflow answer:

Circle-Rectangle collision detection (intersection)

This next article might be a little harder to understand, but check this out too:

https://yal.cc/rectangle-circle-intersection-test/

I'm not going to write the code for you (that's not what SO is for), but I hope this helped. Let me know if you need help understanding.

MattCorr
  • 371
  • 3
  • 11
  • I never specified I wanted a finished piece code. What I meant was a general explanation or simple pseudocode of the process to check for object collision. I will look at the links provided though, thanks! – Ben Roux Apr 03 '18 at 20:11
  • Yeah, the links both have some psuedocode and explanations that should get you started. Sorry about the bluntness, some people on SO expect others to write their code for them, and I wasn’t sure if you were one of those people. – MattCorr Apr 03 '18 at 20:49