'ball has no collision with moving paddle
the ball doesn't collide with the top moving paddle. I have to make a game like pong but vertical. But i can't figure out with my programming skills how the ball doesn't collide with the top paddle. I think it has to do with the hit paddle_2 but i don't really know how the paddle_pos works.
def hit_paddle_2(self, pos):
paddle_2_pos = self.canvas.coords(self.paddle_2.id)
if pos[2] >= paddle_2_pos[0] and pos[0] <= paddle_2_pos[2]:
if pos[3] >= paddle_2_pos[1] and pos[3] <= paddle_2_pos[3]:
return True
return False
def draw(self):
self.canvas.move(self.id, self.x, self.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_2(pos) == True:
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
class Paddle_2:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 200, 10, fill=color)
self.canvas.move(self.id, 200, 150)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-a>', self.turn_left)
self.canvas.bind_all('<KeyPress-d>', self.turn_right)
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
