Hardware Reference
In-Depth Information
2. Insert the following line at the end of the program to call the theWall()
function—but this time it's going to be starting in a new thread:
thread.start_new_thread(theWall, (arenaPos, WALLZ))
3. Run the program.
You should see the same result as before, with the wall moving up and down in the
arena. The difference is that, this time, it is running in its own thread. Now you can
continue programming the rest of the game.
If you want to stop a multi-threaded program running in IDLE, click Shell Restart
Shell in the Python Shell. If you have been pressing Ctrl+C in the Python Shell,
this stops only the main program, not the other threads, so you will need to click
Shell Restart Shell to stop the main program and any other threads.
DIGGING INTO THE CODE
To run the theWall() function in its own thread, you use the code thread.
start_new_thread(theWall, (arena, WALLZ)) . The first parameter is
the name of the function— theWall —and the second parameter holds the vari-
ables the function expects to be passed— arena, WALLZ .
The variables (parameters) the function expects are passed in brackets (arena,
WALLZ) because start_new_thread expects them to be passed as a tuple .
Any Python function can be called in this way. If you had written the following
function to print a message to the screen:
def printMessage(message)
print message
you could run it in its own thread, using:
thread.start_new_thread(printMessage,
("Hello Minecraft World",))
The comma after "Hello Minecraft World" tells Python that this is a tuple,
but it's a tuple with only one item in it.
 
Search WWH ::




Custom Search