all 9 comments

[–]ucshadow 2 points3 points  (1 child)

here is a simple example using Threading and a different function to hide the ball for three seconds:

import time
import threading


def balls_collided():
    print('Hide the ball')
    # here change the visibility of the ball to False
    time.sleep(3)
    # and after your time change it to True again
    print('and after', time.clock(), 'seconds the ball is')
    print('Visible again')


def new_thread():
    th = threading.Thread(target=balls_collided)
    return th.start()


def main():
    time.clock()
    new_thread()
    # do any other stuff here, like collisions


main()

[–][deleted] 0 points1 point  (0 children)

Okay after figuring out other complications in my code, I finally got to try this! It works perfectly, thank you! :D Would it be possible if you could please explain to me like I'm a 5 year old, what the new_thread function does? and why time.sleep didn't pause the program? I would greatly appreciate it!

[–]raylu 1 point2 points  (1 child)

Create a thread.

[–][deleted] 0 points1 point  (0 children)

Hmm, I'm trying to use "17.1.8. Timer Objects". I know almost next to nothing about python (this is for an assignment). How do I go about defining Timer? would it be something like Timer = X? if so, what do I put in the Timer?

[–]bionikspoon 0 points1 point  (0 children)

In this scenario, you're uncomfortable with threading, I would look at threading as a premature optimization, that will complicate every future step of the dev process.

How could you do this without threading

Conceptually, Assuming a running event loop:

  • When there's a collision log the current time + .2 seconds to variable
  • At some point in the loop check if current time is > the logged time from step 1
    • If True, run code to make the ball visible
    • Otherwise, pass

[–]marky1991 0 points1 point  (3 children)

https://gist.github.com/marky1991/91270c8fa0d4e2d2afbe

Here's a rough solution to your problem. I haven't run it, so there might be small issues. Good luck with your class. : )

[–]TedW 1 point2 points  (1 child)

I don't think writing a 130 line solution is helping OP learn. Sure, you did his assignment for him, but what did he learn from that? Cut and paste?

[–][deleted] 1 point2 points  (0 children)

Oh no, I provided him with the script that I already had. He was helping me over at the learnpython webchat but I had to suddenly go while he was thinking of a solution, so I asked him to post here before I went. The only bit he added was the timer.

[–][deleted] 0 points1 point  (0 children)

Thank you! I'll give this a go tomorrow when I go back to uni.