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()