2

I am making a game in which u have to carry move objects from one place to another. I can move my character to the zone in which I need to put something. I want the player to wait in the zone for 5 secs before the object is placed there, however, if i do this you cannot move anymore if u decide u dont want to place the object in the zone as the whole script would be paused.


Is there a way to make one part of the script wait while the rest of it runs?

9
  • have you considered creating a thread? Commented Jul 17, 2019 at 19:14
  • @JakeP whats that? is that how u run things at same time? Commented Jul 17, 2019 at 19:21
  • Allows you to run code simultaneously. Information here. thread.join() (see link for code) would make the main thread wait until the started thread has completed to continue running. Commented Jul 17, 2019 at 19:24
  • @JakeP ok could u answer wth a breif example of how to do it in this situation? Commented Jul 17, 2019 at 19:27
  • then ull get reputation Commented Jul 17, 2019 at 19:28

2 Answers 2

3

Every game needs one clock to keep the game loop in sync and to control timing. Pygame has a pygame.time.Clock object with a tick() method. Here's what a game loop could look like to get the behaviour you want (not complete code, just an example).

clock = pygame.time.Clock()

wait_time = 0
have_visited_zone = False
waiting_for_block_placement = False

# Game loop.
while True:

    # Get the time (in milliseconds) since last loop (and lock framerate at 60 FPS).
    dt = clock.tick(60)

    # Move the player.
    player.position += player.velocity * dt

    # Player enters the zone for the first time.
    if player.rect.colliderect(zone.rect) and not have_visited_zone:
        have_visited_zone = True            # Remember to set this to True!
        waiting_for_block_placement = True  # We're now waiting.
        wait_time = 5000                    # We'll wait 5000 milliseconds.

    # Check if we're currently waiting for the block-placing action.
    if waiting_for_block_placement:
        wait_time -= dt                          # Decrease the time if we're waiting.
        if wait_time <= 0:                       # If the time has gone to 0 (or past 0)
            waiting_for_block_placement = False  # stop waiting
            place_block()                        # and place the block.
Sign up to request clarification or add additional context in comments.

2 Comments

I want to be able to move while waiting
@Glitchd Then you just need to remove the if statement
1

Example with threading:

from threading import Thread

def threaded_function(arg):
    # check if it's been 5 seconds or user has left

thread = Thread(target = threaded_function, args = (10, ))
if user is in zone:
    thread.start()
# continue normal code

Another potential solution is to check the time the user went into the zone and continuously check the current time to see if it's been 5 seconds

Time check example:

import time

entered = false
while true:
    if user has entered zone:
        entered_time = time.time()
        entered = true
    if entered and time.time() - entered_time >= 5: # i believe time.time() is in seconds not milliseconds
        # it has been 5 seconds
    if user has left:
        entered=false
    #other game code

10 Comments

When i was trying to think of a logical solution i was thinking of something like the 2nd one but didnt know how to do it XD ty for the help
pygame has pygame.time.get_ticks which you could use instead time
@furas yes that would work the same by just changing the 5 to 5000 to convert to millisecond
@JakeP in ur thread example it says check if its been 5 secs. would i be able to use pygame.wait(5) and then drop the object?
If believe so, if you use sleep(5) in the threaded function it will not affect the main thread, i'm not sure how pygame.wait(5) behaves
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.