Click here to Skip to main content
15,506,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 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:

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():
     win = GraphWin("Loose Ball", 400, 400)
     return win
def main():
     win = window()
     figure = Rectangle(Point(150,150), Point(150, 150))
     figure.move(170, 5)
     radius = 40
     ball = Circle(Point(300, 200), radius)
     dx = 1
     dy = 1
     yFloor = radius
     yCieling = win.getHeight() - radius
     xFloor = radius
     xCieling = win.getHeight() - radius

     while True:
         ball.move(dx, dy)
         key = win.checkKey()
         if key == "o":
         elif key == "Left":
         elif key == "Right":
         elif key == "Up":
         elif key == "Down":
     #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 !!!!")
Updated 19-Apr-21 21:34pm
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.

1 solution

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).
Share this answer

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

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