Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:56:43+00:00 2026-05-30T14:56:43+00:00

I am creating a game, where it will ask the user to click a

  • 0

I am creating a game, where it will ask the user to click a series of circles in numerical order. My question is between lines no. 91 and no. 110: why does the recursion logic generate circles that overlap even though I check for that condition, but the while loop doesn’t?

def checkCircleCoord(coord):
    global circleCoordCenter #global list
    global checkCircleCoords

    '''why do I work?'''
    flag = False
    while not flag:
        flag = True
        for coord2 in circleCoordCenter:
            if distance(midpoint(coord),coord2) < BALL_DIAMETER():
                coord = randomCircleCoord()
                flag = False
                break

    '''why don't I work?'''
##    for coord2 in circleCoordCenter:
##        if distance(midpoint(coord),coord2) < BALL_DIAMETER():
##            coord = randomCircleCoord()
##            checkCircleCoord(coord)
    ''''''
    return coord

here is my entire program:

'''
Daniel Chen
'''
from tkinter import *
from random import choice
from time import clock
import math

## constant variables
def NUM_CIRCLES():
    return 40
def FIELD_X_SIZE():
    return 800
def FIELD_Y_SIZE():
    return 800
def BALL_DIAMETER():
    return 75

# math functions
def square(x):
    return x*x

## use as global variables
upperLeftX, upperLeftY, upperLeftX, upperLeftY = 0, 0, 0, 0
circleCoordCenter = []

def demographicsGUI():
    root = Tk()
    #root['bg'] = 'light yellow'

    form = Frame(root)
    #form['bg'] = 'light green'

    fname = Label(form)
    fname['text'] = 'First Name: '
    fname.pack()

    lname = Label(form)
    lname['text'] = 'Last Name: '
    lname.pack()

    dob = Label(form)
    dob['text'] = 'Date of Birth: '
    dob.pack

    action = Frame(root)
    #action['bg'] = 'pink'

    saveAndStart = Button(action, command=startGame,
                          text = 'Review Information and Start')
    saveAndStart.pack()

    form.pack(expand=YES, fill=BOTH)
    action.pack()
'''
generates a random coordinate to be used as the upper left of circle boundary
the upper left corner will be (0,0)
subtract BALL_DIAMETER from the X and Y field size to prevent the circle being drawn out of bounds
returns only as coordinates, not as array: (x,y)
'''
def randomCircleCoord():
        global upperLeftX
        global upperLeftY
        upperLeftX = choice(list(range(FIELD_X_SIZE() - BALL_DIAMETER() )))
        upperLeftY = choice(list(range(FIELD_Y_SIZE() - BALL_DIAMETER() )))
        return (upperLeftX,upperLeftY)

'''
returns the *center* of the rectangular oval boundary,
it takes a coordinate, creates another coordinate that is (x+BALL_DIAMETER, y+BALL_DIAMETER)
and finds its midpoint using:
(x2+x1)/2, (y2+y1)/2
'''
def midpoint(coord):
    coord = [coord]
    coordDiameter = [( coord[0][0]+BALL_DIAMETER() , coord[0][1]+BALL_DIAMETER() )]
    return (((coordDiameter[0][0])+coord[0][0])/2, ((coordDiameter[0][1]+coord[0][1])/2))

'''
returns the distance between 2 coordinates using:
sqrt( (x2-x1)^2 + (y2-y1)^2 )
'''
def distance(coord1, coord2):
    d = math.sqrt(square(coord2[0]-coord1[0]) + square(coord2[1]-coord1[1]))
    return abs(d)

'''
compares random coord to list of accepted coordinates, circleCoordCenter
randomCircleCoord is passed into checkCircleCoord in startGame()
'''
def checkCircleCoord(coord):
    global circleCoordCenter #global list
    global checkCircleCoords

    '''why do i work?'''
    flag = False
    while not flag:
        flag = True
        for coord2 in circleCoordCenter:
            if distance(midpoint(coord),coord2) < BALL_DIAMETER():
                coord = randomCircleCoord()
                flag = False
                break

    '''why don't i work?'''
##    for coord2 in circleCoordCenter:
##        if distance(midpoint(coord),coord2) < BALL_DIAMETER():
##            coord = randomCircleCoord()
##            checkCircleCoord(coord)
    ''''''
    return coord

def startGame():
    root = Tk()
    field = Canvas(root, width=FIELD_X_SIZE(), height=FIELD_Y_SIZE(), bg='white')
    field.pack()

    global checkCircleCoords
    for circleNumber in range(NUM_CIRCLES()):
        print('------------------------')
        print('circlenumber', circleNumber+1)
        if circleNumber == 0:
            coord = randomCircleCoord()
            upperLeftX, upperLeftY = coord[0],coord[1]
            print('first x y coordinate:', upperLeftX, upperLeftY)
            circleCoordCenter.append(midpoint(coord))
            print('first x y coordinate center: ', circleCoordCenter) #test print first coord
            field.create_oval(upperLeftX, upperLeftY, upperLeftX+BALL_DIAMETER(), upperLeftY + BALL_DIAMETER(), fill='white')
            field.create_text(upperLeftX, upperLeftY, text = "Start", fill = "purple")
        else:
            '''
            gets random point, x
            goes into checkCircleCoord: x, which then will be checked by adding BALL_DIAMETER to both x and y coordinates, and finds the midpoint.
            This midpoint is then compated to the list of midpoints in circleCoordCenter
            if distance between midpoints is greater than BALL_DIAMETER, checkCircleCoord will return the coordinates
            '''
            upperLeftX, upperLeftY = checkCircleCoord(randomCircleCoord())
            print(circleNumber+1, ' x y coordinates: ', upperLeftX, upperLeftY)
            circleCoordCenter.append(midpoint((upperLeftX, upperLeftY)))
            field.create_oval(upperLeftX, upperLeftY, upperLeftX+BALL_DIAMETER(), upperLeftY + BALL_DIAMETER(), fill='white')

            print(circleNumber+1, ' x y center: ', midpoint((upperLeftX, upperLeftY)))

            '''
            labeling circles starting with 'start' finishing with 'finish'
            '''
            if circleNumber != NUM_CIRCLES()-1:
                field.create_text(upperLeftX, upperLeftY, text = str(circleNumber), fill = "purple")
            else:
                field.create_text(upperLeftX, upperLeftY, text = "Finish", fill = "purple")
    print("Circle Center list: ", circleCoordCenter)
##demographicsGUI()
startGame()
mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:56:45+00:00Added an answer on May 30, 2026 at 2:56 pm

    Distilling DSM’s answer:

    '''why am I not like this?'''
    for coord2 in circleCoordCenter:
        if distance(midpoint(coord),coord2) < BALL_DIAMETER():
            coord = randomCircleCoord()
            return checkCircleCoord(coord)
            ^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're creating a game that will run on iOS, Android and in the web
I am creating a little game client that will end up connecting to a
I'm thinking of creating a simple game in order to help get my head
I'm creating an iPhone Game where I want the user to get a unique
I am creating a game with jQuery and PHP that will simulate The Oregon
im creating a game that has multiple levels and the levels contain multiple textures
I'm creating a game with points for doing little things, so I have a
I'm creating a game where a lot of images are being used in Actionscript
I am creating a game where I want to determine the intersection of a
I'm creating a game in XNA and was thinking of creating my own scripting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.