Help create a ball and platform collision in the roll function

from tkinter import * def roll(): global ball_y global dy global ball_x global dx ball_x = ball_x + dx if ball_x > 700 or ball_x < 0: dx = -dx ball_y = ball_y + dy if ball_y > 500 or ball_y < 0: dy = -dy canv.coords(ball, ball_x, ball_y, ball_x+100, ball_y+100,) form.after(10,roll) def pad_move(event): global pad_x pad_x = event.x canv.coords(platform, pad_x-50,500, pad_x+50, 530) form = Tk() form.title("Arkanoid TEST") form.geometry("800x600") canv = Canvas(form, width = 800, height = 600, bg="lightblue") canv.pack() ball_x=0 ball_y=0 dx = 5 dy = 5 ball = canv.create_oval(ball_x, ball_y, ball_x+100, ball_y+100, fill = "Black") form.after(0,roll) pad_x = 150 platform = canv.create_rectangle(pad_x, 500,pad_x+100, 530, fill = "Black") form.bind("<Motion>", pad_move) form.mainloop() 

Closed due to the fact that the essence of the issue is incomprehensible by the participants mkkik , Kromster , aleksandr barakin , 0xdb , RiotBr3aker 6 May at 21:46 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • If I'm not mistaken, this is already the third (or second) nickname in an attempt to write an arkanoid, while laying out the same code pattern. At least I remember these global variables in Python on the roll function .. - Alex Krass
  • @AlexKrass, is it? - aleksandr barakin
  • @aleksandrbarakin it. - Alex Krass

1 answer 1

We do not encourage direct work for the author, if he has not demonstrated his attempts to solve the problem. Therefore, let's do this - I will describe to you a simple solution algorithm, and you will try to implement it yourself in your program.

At the moment you have the coordinates of the sides of the ball and the rectangle, comparing the corresponding coordinates, you can determine how they are relative to each other.

enter image description here

The ball and the rectangle intersect under four conditions for the position along the X coordinate and the Y coordinate. Comparing the required coordinates, you will get the result.

  1. The right side of the ball is to the right of the left side of the rectangle.
  2. The left side of the ball is to the left of the right side of the rectangle.
  3. The bottom of the ball is below the top of the rectangle.
  4. The top of the ball is above the top side of the rectangle.

A collision occurs only when all four conditions are true.

enter image description here

After that, you only need to change the speed along the X or Y axis to the opposite one. To do this, you need to compare which sides are closer to each other and change the speed along the desired axis. Do not change the speed along only one vertical axis, otherwise the ball may come in from the side and you will get zigzag jumps inside the platform.

enter image description here

In this case, this is the easiest way to determine collisions for shapes. He is useful to you in order to determine the collision on the blocks.

  • Sorry did not know I will consider - ToujaKonea
  • @ToujaKonea, this is usually described in the tour and help: ru.stackoverflow.com/help/how-to-ask The platform is designed to help both beginners and professional programmers. But not as free answers for those who do not want to learn and understand. For people here, this is just a waste of time, unlike other forums. Therefore, the questions "here is a condition, answer the question" are not very positive here. If you provide your solutions (let them be non-working), explain where there is a misunderstanding, ask for advice, an algorithm, etc., then your chance of a response increases dramatically. - Alex Krass