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

  • Home
  • SEARCH
  • 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 8378019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:49:29+00:00 2026-06-09T15:49:29+00:00

I am a begginner at python and I’m trying to make a circle game.

  • 0

I am a begginner at python and I’m trying to make a circle game. So far it draws a circle at your mouse with a random color and radius when you click.

Next, I would like the circle to fly off the screen in a random direction. How would I go about doing this? This is the main chunk of my code so far:

check1 = None
check2 = None

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit

        if event.type == MOUSEBUTTONDOWN:
            last_mouse_pos = pygame.mouse.get_pos()



    if last_mouse_pos:

        global check

        color1 = random.randint(0,255)
        color2 = random.randint(0,255)
        color3 = random.randint(0,255)
        color = (color1,color2,color3)

        radius = random.randint (5,40)
        posx,posy = last_mouse_pos

        if posx != check1 and posy != check2:
            global check1, check2
            screen.lock()
            pygame.draw.circle(screen, color, (posx,posy), radius)
            screen.unlock()
            check1,check2 = posx,posy


    pygame.display.update()

Again, I want the circle to fly off the screen in a random direction.
I have made a few attempts but no successes yet.
Also, thanks to jdi who helped me s

  • 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-06-09T15:49:30+00:00Added an answer on June 9, 2026 at 3:49 pm

    Right now, you are doing the following (drastically simplifying your code)…

    while True:
        if the mouse was clicked:
            draw a circle on the screen where the mouse was clicked
    

    Let’s make things a little easier, and build up gradually.

    Start with the circle without the user clicking

    To keep things simple, let’s make the circle near the top left of the screen, that way we can always assume there will be a circle (making some of the logic easier)

    circle_x, circle_y = 10,10
    while True:
        draw the circle at circle_x, circle_y
    
        pygame.display.update()
    

    Animate the circle

    Before going into too much about “random directions”, let’s just make it easy and go in one direction (let’s say, always down and to the right).

    circle_x, circle_y = 0,0
    while True:
        # Update
        circle_x += 0.1
        circle_y += 0.1
    
        # Draw
        draw the circle at circle_x, circle_y
        update the display
    

    Now, every time through the loop, the center of the circle moves a bit, and then you draw it in its new position. Note that you might need to reduce the values that you add to circle_x and y (in my code, 0.1) in case the circle moves too fast.

    However, you’ll notice that your screen is now filling up with circles! Rather than one circle that is “moving”, you’re just drawing the circle many times! To fix this, we’re going to “clear” the screen before each draw…

    screen = ....
    BLACK = (0,0,0) # Defines the "black" color
    circle_x, circle_y = 0,0
    while True:
        # Update
        circle_x += 0.1
        circle_y += 0.1
    
        # Draw
        screen.fill(BLACK)
        draw the circle at circle_x, circle_y
        update the display
    

    Notice that we are “clearing” the screen by painting the entire thing black right before we draw our circle.

    Now, you can start work the rest of what you want back into your code.

    Keep track of multiple circles

    You can do this by using a list of circles, rather than two circle variables

    circles = [...list of circle positions...]
    while True:
        # Update
        for circle in circles:
            ... Update the circle position...
    
    
        # Draw
        screen.fill(BLACK)
        for circle in circles:
            draw the circle at circle position # This will occur once for each circle
        update the display
    

    One thing to note is that if you keep track of the circle positions in a tuple, you won’t be able to change their values. If you’re familiar with Object Oriented Programming, you could create a Circle class, and use that to keep track of the data relating to your circles. Otherwise, you can every loop create a list of updated coordinates for each circle.

    Add circle when the user clicks

    circles = []
    while True:
        # event handling
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                circles.append( pos ) # Add a new circle to the list
    
        # Update all the circles
        # ....
    
        # Draw
        clear the screen
        for circle_position in circles:
            draw the circle at circle_position # This will occur once for each circle
        update the display
    

    Have the circle move in a random direction

    This is where a good helping of math comes into play. Basically, you’ll need a way to determine what to update the x and y coordinate of the circle by each loop. Keep in mind it’s completely possible to just say that you want it to move somewhere between -1 and 1 for each axis (X, y), but that isn’t necessarily right. It’s possible that you get both X and Y to be zero, in which case the circle won’t move at all! The next Circle could be 1 and 1, which will go faster than the other circles.

    I’m not sure what your math background is, so you might have a bit of learning to do in order to understand some math behind how to store a “direction” (sometimes referred to as a “vector”) in a program. You can try Preet’s answer to see if that helps. True understanding is easier with a background in geometry and trigonometry (although you might be able to get by without it if you find a good resource).

    Some other thoughts

    Some other things you’ll want to keep in mind:

    1. Right now, the code that we’re playing with “frame rate dependent”. That is, the speed in which the circles move across the screen is entirely dependent on how fast the computer can run; a slower computer will see the circles move like snails, while a faster computer will barely see the circles before they fly off the screen! There are ways of fixing this, which you can look up on your own (do a search for “frame rate dependence” or other terms in your favorite search engine).

    2. Right now, you have screen.lock() and screen.unlock(). You don’t need these. You only need to lock/unlock the screen’s surface if the surface requires it (some surfaces do not) and if you are going to manually access the pixel data. Doing things like drawing circles to the screen, pygame in lock/unlock the surfaces for you automatically. In short, you don’t need to deal with lock/unlock right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in python, and I'm trying to draw a circle wherever
I am a beginner of Python. I am trying now figuring out why the
I am quite a beginner to Python and trying to get my head around
I am a beginner with Python and trying few programs. I have something like
I'm a beginner in python and I'm trying to use a octal number in
Python beginner here. I am using the matplotlib library to make graphs from tab
I am a python beginner and I am trying to average two NumPy 2D
Using: Ubuntu 11.04 Django 1.3 Python 2.7 Following the tutorial at Writing your first
I am a beginner at python (one week). Here I am trying print the
I'm a beginner to Python trying to decode this javascript sequence. I'm not only

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.