15,506,209 members
See more:
I am creating a game which i called loose ball... for it i made two figures one ball and one square basically what i try to achieve is that the game continue to move that is the ball keeps moving as a long as the ball does not touch the square.. if it touches the figure then simple game over..it happens that the ball in not touching the figure and it throws me the game over message.... Ill leave down the code that i try...

What I have tried:

Python
```from graphics import *

def figure_left(figure):
figure.move(-15, 0)

def figure_right(figure):
figure.move(15, 0)

def figure_up(figure):
figure.move(0, -15)

def figure_down(figure):
figure.move(0, 15)

def window():
#Window
win = GraphWin("Loose Ball", 400, 400)
win.setBackground("green")
return win

def main():
win = window()
#figure
figure = Rectangle(Point(150,150), Point(150, 150))
figure.setOutline("blue")
figure.setFill("blue")
figure.draw(win)
figure.move(170, 5)
figure.setWidth(40)
#Ball
ball = Circle(Point(300, 200), radius)
ball.setOutline("red")
ball.setFill("red")
ball.draw(win)
dx = 1
dy = 1
yCieling = win.getHeight() - radius
xCieling = win.getHeight() - radius

while True:
time.sleep(.01)
ball.move(dx, dy)
key = win.checkKey()

if key == "o":
break
elif key == "Left":
figure_left(figure)
elif key == "Right":
figure_right(figure)
elif key == "Up":
figure_up(figure)
elif key == "Down":
figure_down(figure)
#Move Ball
if ball.getCenter().getY() <= yFloor or ball.getCenter().getY() >= yCieling:
dy = -dy
elif ball.getCenter().getX() <= xFloor or ball.getCenter().getX() >= xCieling:
dx = -dx
#Ball Collision with figure
elif ball.getCenter().getY() <= figure.getCenter().getY() or ball.getCenter().getX() >= figure.getCenter().getX():
text = Text(Point(200, 200), "GAME OVER !!!!")
text.draw(win)
break```
Posted
Updated 19-Apr-21 21:34pm
v2
Dave Kreskowiak 19-Apr-21 15:02pm
It seems you forgot to spell out a problem you're having or even ask a question.
Member 15159787 19-Apr-21 15:25pm
I only have doubts in the part of #ball collision with figure.. my question is
what can I do so that when the ball collides or touch the figure the game stop and does not stop before touching the figure?
Dave Kreskowiak 19-Apr-21 17:30pm
Why is your ball collision check sitting in an Else If? That should not be the case. It should sit out as it's own If.

In fact, your code shouldn't have ANY Else If's. Each check should stand on its own.

## Solution 1

Your 'collision with figure' logic should be the opposite: the ball should be near the center of the figure in order to have a collision happen (instead you used the same logic that, correctly, detects if the ball is outside the borders).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 150 Richard Deeming 100 OriginalGriff 70 Dave Kreskowiak 40 Rick York 25
 Richard MacCutchan 405 OriginalGriff 363 Richard Deeming 258 CPallini 105 Dave Kreskowiak 80

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900