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 8213341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:04:56+00:00 2026-06-07T11:04:56+00:00

I am developping a small wormy program with PyGame. I have a worm eating

  • 0

I am developping a small wormy program with PyGame. I have a worm eating apples and growing each time it eats an apple. When it meets its own tail, or a window bordure, it ‘games over’.
I want to add, at random times, bad apples with poison. These apples appear anywhere on screen, at some place not already occupied with an apple. On the contrary, the good apples appear one at a time. when wormy eats an apple, it grows by one, and one other apple appears on screen. So, I guess I should have the production of bad apples in a separated thread. However, I would like to have access to position etc… of bad apples in main thread, so that wormy dies when meeting a bad apple.

Do you have an idea how this can be written?

I thought of having this in main()

# Start bad apples thread
threading.Thread(target=badApples).start()

so that main() ends up looking like this :

def runGame():
# Set a random start point.
startx = random.randint(5, CELLWIDTH-5)
starty = random.randint(5, CELLHEIGHT-5)
wormCoords = [{'x': startx,     'y': starty},
              {'x': startx - 1, 'y': starty},
              {'x': startx - 2, 'y': starty}]
direction = RIGHT

# Start the apple in a random place.
apple = getRandomLocation()

# Start bad apples thread
threading.Thread(target=badApples).start()

while True: # main game loop
    for event in pygame.event.get(): # event handling loop
        if event.type == QUIT:
            terminate()
        elif event.type == KEYDOWN:
            if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
                direction = LEFT
            elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
                direction = RIGHT
            elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
                direction = UP
            elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
                direction = DOWN
            elif event.key == K_ESCAPE:
                terminate()

    # check if the worm has hit itself or the edge
    if wormCoords[HEAD]['x'] == -1 or wormCoords[HEAD]['x'] == CELLWIDTH or wormCoords[HEAD]['y'] == -1 or wormCoords[HEAD]['y'] == CELLHEIGHT:
        return # game over
    for wormBody in wormCoords[1:]:
        if wormBody['x'] == wormCoords[HEAD]['x'] and wormBody['y'] == wormCoords[HEAD]['y']:
            return # game over

    # etc... 

(code from http://inventwithpython.com/ mostly)

and a target method badApples starting with

def badApples():
    time.sleep(random.randint(200,500))
    badApple = getRandomLocation()

but how then to recover in main thread the location for bad apple, as to delete worm?

thanks and regards

  • 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-07T11:04:58+00:00Added an answer on June 7, 2026 at 11:04 am

    You absolutly don’t have to use threads for this. As you are in control of your game loop, you can just schedule the creating of a bad apple (using some kind of countdown).

    Create a variable that will store a value indicating when a new bad apple will show up, and a list of already created bad apples.

    def get_bad_apple_time():
        # create a bad apple every 5 to 15 seconds
        # assuming your FPS is 60 
        return random.randrange(5, 16) * 60 
    
    #  all bad apple coordinates go here
    bad_apples = []  
    
    # timeout until the next bad apple shows up
    next_bad_apple = get_bad_apple_time()
    

    In your main loop, decrease the value of next_bad_apple. If it reaches 0, create a bad apple and start over again.

    while True:
        ...
        next_bad_apple -= 1
        if not next_bad_apple:
            pos = getRandomLocation()
    
            # if there's already a bad apple on this position
            # create a new position
            while pos in bad_apples:
                pos = getRandomLocation()
    
            bad_apples.append(pos)
            next_bad_apple = get_bad_apple_time()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developping a card game. I have an ASP.NET page with some 52 small
I'm developing small FTP upload app. for mac (10.6 if it matters) Have problem
I have a little experience developing small command-line applications with Python. I want to
I'm developing a small application using Blend, basically what I have in my application
I'm developing a small game in Java and I have run into a problem
I am developing one small app in which i have multiple NSURLConnection.I have created
I have question I'm developing small application to desktop in win Forms. I'm using
I am developing a small scale patient record program. One of the functionalities they
Good day! I'm developping a small Java Application with NetBeans IDE that extends JFrame.
I am developping a DSL with its own graphical editor. Such files have a

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.