Click here to Skip to main content
15,881,803 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 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
     radius = 40
     ball = Circle(Point(300, 200), radius)
     ball.setOutline("red")
     ball.setFill("red")
     ball.draw(win)
     dx = 1
     dy = 1
     yFloor = radius
     yCieling = win.getHeight() - radius
     xFloor = 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 20:34pm
v2
Comments
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