This is a question about tkinter during Python Guy programming.

Asked 2 years ago, Updated 2 years ago, 100 views

Looking at a book called Python programming that everyone learns easily

I'm making a brick breaking game like the image below.

I want to hit a block while the ball is moving, but the ball passes through the block.

If you're going to crack a brick, you're going to have to hit something LOL

Please help me with this part The source below is the part of the book about the collision between a bar and a ball.

class Ball:

    def __init__(self,canvas,paddle,score,color):
        self.canvas = canvas
        self.paddle = paddle
        self.score = score
        self.id = canvas.create_oval(10,10,25,25,fill = color)
        self.canvas.move(self.id,245,100)
        starts = [-3,-2,-1,1,2,3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False

    def hit_paddle(self,pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                self.x += self.paddle.x
                self.score.hit()
                return True
        return False


    def draw(self):
        self.canvas.move(self.id,self.x,self.y)y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if self.hit_paddle(pos) == True:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

This is where the ball and the bar in the book collide.

From here, I added a few lines as I thought.

class Ball:

    def __init__(self,canvas,paddle,score,block,color):
        self.canvas = canvas
        self.paddle = paddle
        self.block = block
        self.score = score
        self.id = canvas.create_oval(10,10,25,25,fill = color)
        self.canvas.move(self.id,245,100)
        starts = [-3,-2,-1,1,2,3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False
        self.hit_block = False

    def hit_paddle(self,pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                self.x += self.paddle.x
                self.score.hit()
                return True
        return False

    def hit_block(self,pos):
        block_pos = self.canvas.coords(self.block.id)
        if pos[2] >= block_pos[0] and pos[0] <= block_pos[2]:
            if pos[3] >= block_pos[1] and pos[3] <= block_pos[3]:
                return True
        return False

    def draw(self):
        self.canvas.move(self.id,self.x,self.y)y)
        pos = self.canvas.coords(self.id)
        # # pos2 = self.block.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if self.hit_paddle(pos) == True:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3
        if self.hit_block(pos) == True:
            self.x = -3

If I add a few lines, an error occurs like this. I think there's a problem with hit_block...

I don't know, but logically? You're saying it doesn't fit, right?

The following is the source of the book.

from tkinter import *
import random
import time

class Ball:

    def __init__(self,canvas,paddle,score,color):
        self.canvas = canvas
        self.paddle = paddle
        self.score = score
        self.id = canvas.create_oval(10,10,25,25,fill = color)
        self.canvas.move(self.id,245,100)
        starts = [-3,-2,-1,1,2,3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False

    def hit_paddle(self,pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                self.x += self.paddle.x
                self.score.hit()
                return True
        return False

    def draw(self):
        self.canvas.move(self.id,self.x,self.y)y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if self.hit_paddle(pos) == True:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

class Paddle:

    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0,0,100,10,fill=color)
        self.canvas.move(self.id,200,300)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        self.started = False
        self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
        self.canvas.bind_all('<Button-1>',self.start_game)

    def draw(self):
        self.canvas.move(self.id,self.x,0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.canvas_width:
            self.x = 0

    def turn_left(self,evt):
        self.x = -2
    def turn_right(self,evt):
        self.x = 2
    def start_game(self,evt):
        self.started = True
class Score:

    def __init__(self,canvas,color):
        self.score = 0
        self.canvas = canvas
        self.id = canvas.create_text(450,10,text = self.score,\
                                     fill = color)
    def hit(self):
        self.score +=1
        self.canvas.itemconfig(self.id,text = self.score)

tk = Tk()

tk.title ("Game") #GamingPaneTitle

tk.resizable(0,0)# Fix window size

tk.wm_attributes ("-topmost",1) #Fix to the front of the screen

canvas = Canvas (tk, width = 500, height = 500, bd = 0, highlightthickness = 0) #CanvasPretty

canvas.pack()

tk.update() #Required!! Self-initialization for animation

score = Score(canvas,'green')

paddle = Paddle(canvas,'black')

ball = Ball(canvas,paddle,score,'red')

game_over_text = canvas.create_text(250,200,text='GAME OVER',\
                                    state = 'hidden')

while 1: #MainLoop

    if ball.hit_bottom == False and paddle.started == True:
        ball.draw()
        paddle.draw()
    If ball.hit_bottom == True: #When the ball touches the floor, rest 0.1 seconds and gameover output->hidden mode->Viewable mode
        time.sleep(0.1)
        canvas.itemconfig(game_over_text,state='normal')
        # # time.sleep(2)
        # # exit()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.0000001)

Thank you for your hard work reading the long text. Thank you for reading all of it

I don't know. Wouldn't it help a little bit? If you want, please leave comments Thank you.

python gui tkinter

2022-09-21 14:17

1 Answers

You have duplicated members named hit_block in the class.

Please change the name of one of the two. Looking at the code, you wrote hit_block(...) because you wanted to write a function, but Python interpreter accepts hit_block as variable 1 instead of method, causing an error.

For example,

def __init__(self,canvas,paddle,score,block,color):
    self.canvas = canvas
    self.paddle = paddle
    self.block = block
    self.score = score
    self.id = canvas.create_oval(10,10,25,25,fill = color)
    self.canvas.move(self.id,245,100)
    starts = [-3,-2,-1,1,2,3]
    random.shuffle(starts)
    self.x = starts[0]
    self.y = -3
    self.canvas_height = self.canvas.winfo_height()
    self.canvas_width = self.canvas.winfo_width()
    self.hit_bottom = False
    #self.hit_block = False #Annotation Processing

Remove hit_block declared in init like this, or rename hit_block to something else.


2022-09-21 14:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.