|
Strelok
|
 |
« on: June 02, 2012, 11:52:12 AM » |
|
I have this doubt. Is it possible that two lines of code(or to functions) work at the same time in the same program? I think it's called multithreading, I don't know.
It's for my text game where the player will have to find out the code for a bomb and there is this time that runs out. I want the time to runs at real time while the player has to try to discover the code. I'm not using any library, just Python. Is it possible?
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Strelok
|
 |
« Reply #2 on: June 02, 2012, 12:22:34 PM » |
|
Totaly... there is no way I can use that right now. Looks like I'll have to try something else. Maybe make the program check at each minute and then each ten seconds.
|
|
|
|
|
Logged
|
|
|
|
|
Christian Knudsen
|
 |
« Reply #3 on: June 02, 2012, 12:25:39 PM » |
|
If you have a normal gameloop, just add a timer that counts down each loop.
|
|
|
|
|
Logged
|
|
|
|
|
Dacke
|
 |
« Reply #4 on: June 02, 2012, 12:36:15 PM » |
|
In a normal gameloop with, say, 60 FPS (frames per second) everything in the game is updated 60 times every second. So if you just do something this: def update(): checkIfDisarmed() checkIfBlowUp()
It will do it approximately once every 0.02 seconds. Which is very often. The chance that the player disarms the bomb at the exact same millisecond as the bomb was supposed to blow up is very low. And even if the two things do happen at the same update, just be nice and let the player win. This will actually give more reliable results than multi-threading. Because with multi-threading the code doesn't actually run at the same time (at least if you only have one processor), so you rely on the operating system or Python-VM to let the different threads take turns. If you are not using a gameloop, you can instead use an event-system. If you need help with any one of these options, please let us know. We'll be able to give you better help if you tell us what libraries you are using and show us your code 
|
|
|
|
|
Logged
|
vegan • socialist • atheist • humanist • liberal • FOSSer programmer • feminist • animal rights activist • pacifist • teetotaller
|
|
|
|
Strelok
|
 |
« Reply #5 on: June 02, 2012, 08:38:21 PM » |
|
In a normal gameloop with, say, 60 FPS (frames per second) everything in the game is updated 60 times every second. So if you just do something this: def update(): checkIfDisarmed() checkIfBlowUp()
It will do it approximately once every 0.02 seconds. Which is very often. The chance that the player disarms the bomb at the exact same millisecond as the bomb was supposed to blow up is very low. And even if the two things do happen at the same update, just be nice and let the player win. This will actually give more reliable results than multi-threading. Because with multi-threading the code doesn't actually run at the same time (at least if you only have one processor), so you rely on the operating system or Python-VM to let the different threads take turns. If you are not using a gameloop, you can instead use an event-system. If you need help with any one of these options, please let us know. We'll be able to give you better help if you tell us what libraries you are using and show us your code  So if I use that code I'll have to call it. When do I call it? I'm not using any libraries until now. import random import time
tentativas = 0 contagemRegressiva = 0
def gameloop(): checarSeDesarmado() checarSeExplodiu()
def checarSeDesarmado(): time.sleep(1) contagemRegressiva = contagemRegressiva + 1 gameloop() def checarSeExplodiu(): if contagemRegressiva == 300 print("The bomb exploded, you and millions are dead.") print("Hello, welcome to the Nuclear Code Game") input("Press enter...") print(">>In this game you'll try to unveil the correct code to deactivate a nuclear artifact before it explodes.") print(">>After your first try, the detonator CPU will enter in state of alert, activating the artifact. After that you'll have only 5 (five) minutes to disarm it.") print(">>You can use a 'worm' virus to halt the countdown, giving you more time to disarm the bomb.") input("Press enter...") print("Are you prepared to continue?") That's what I have so far, didn't work with it, I spent the most part of the day studying. I need some light.
|
|
|
|
« Last Edit: June 02, 2012, 08:49:26 PM by Strelok »
|
Logged
|
|
|
|
|
Dacke
|
 |
« Reply #6 on: June 02, 2012, 09:22:19 PM » |
|
1. Always program in English. It will make your life much easier.
2. Writing real-time, terminal-based games is pretty hard. Getting your player input from the command line using "input" is not an option if you want something to be real-time. It locks down the program until the player does something. So you have to work around that, possibly implementing your own terminal input system from scratch. Which is pretty tricky for someone new to programming.
3. It's much easier to create something turn-based. You could, for example, have the timer count down a fixed amount every time the user makes an attempt.
|
|
|
|
|
Logged
|
vegan • socialist • atheist • humanist • liberal • FOSSer programmer • feminist • animal rights activist • pacifist • teetotaller
|
|
|
|
Strelok
|
 |
« Reply #7 on: June 02, 2012, 10:26:24 PM » |
|
3. It's much easier to create something turn-based. You could, for example, have the timer count down a fixed amount every time the user makes an attempt.
That's a good idea. It also gives me another good idea. I could make a terminal game with command prompts like those old computers, like a hacker game other than a "unveil the password" game.
|
|
|
|
|
Logged
|
|
|
|
|
rivon
|
 |
« Reply #8 on: June 03, 2012, 05:56:40 AM » |
|
You all should also know that multithreading doesn't work in Python because of GIL. You would need to use either Stackless Python, PyPy, Jython (JVM) or IronPython (.NET). They don't have GIL and therefore multithreading is really multithreading. There's also the possibility of using more processes but that's an overkill for a game (and especially for a text game).
|
|
|
|
|
Logged
|
|
|
|
|
SuperDisk
|
 |
« Reply #9 on: June 03, 2012, 01:43:37 PM » |
|
I suggest using WConio if you are on Windows or Curses if you are on Macintosh/Linux. You can then do realtime text games by having nonblocking input, plus you can also use pretty ascii art. 
|
|
|
|
|
Logged
|
|
|
|
|
Dacke
|
 |
« Reply #10 on: June 03, 2012, 01:50:05 PM » |
|
SuperDisk, you are technically correct; that is the way to do real-time terminal games.
But my impression is that Strelok is just starting out as a programmer. So it's probably better to stick to basic, built-in python stuff. Just practice doing stuff that doesn't require external libraries. It's also good to learn to simplify your designs whenever possible.
|
|
|
|
|
Logged
|
vegan • socialist • atheist • humanist • liberal • FOSSer programmer • feminist • animal rights activist • pacifist • teetotaller
|
|
|
|
Strelok
|
 |
« Reply #11 on: June 03, 2012, 06:21:28 PM » |
|
You're right. I am just starting as a programmer. I'll keep with only Python. I'm not read to use external libraries, and I'll try to simplify my design until I have something I can program without problems. After finishing it I'll be able to update it as soon as my skills become better.
Thanks guys.
|
|
|
|
|
Logged
|
|
|
|
|
SuperDisk
|
 |
« Reply #12 on: June 03, 2012, 06:42:58 PM » |
|
Perhaps you could have one of these: import thread timerdead = False timer = 100000
def inputthread(): while 1: processinput(raw_input())
def gamethread(): while 1: if not timerdead:timer -= 1
def processinput(stuff): if stuff == 'stoptimer': timerdead = True
thread.start_new_thread(inputthread, (,)) gamethread() Obviously, it's not meant to be used in a real game, but it gives you an idea on how such things would work. Also, this is assuming you are using Python 2. If you are using Python 3, thread is called _thread and raw_input() should be input().
|
|
|
|
|
Logged
|
|
|
|
|
Strelok
|
 |
« Reply #13 on: June 03, 2012, 08:43:08 PM » |
|
I didn't understand very well. To be honest, I didn't understand what the code does. Can you explain me a bit?
|
|
|
|
|
Logged
|
|
|
|
bluej774
Level 1
It smells like wet fur in here!
|
 |
« Reply #14 on: June 03, 2012, 08:57:10 PM » |
|
You all should also know that multithreading doesn't work in Python because of GIL. Although that's a good rule of thumb, there are actually plenty of cases where you can use multithreading in CPython without the GIL getting in your way. After all, the multithreading module is there for a reason. Specifically, the only time when you can't use multithreading is when the two (or more) threads consist entirely of interpreted python code that you wrote yourself and is probably going to be CPU-bound. This is probably the case for Strelok. However, I've used the multithreading module successfully plenty of times. You're fine if the bulk of your thread's code is run in an external library that's not written in python or if you're spending most of the time in the thread blocking, waiting for an HTTP response, for example. Also, database queries should be fine. I suspect you should be okay if you're waiting for any type of I/O like disk reads/writes, though YMMV since I've never tested that myself. Basically, if the thread's code will be running in a non-python library or isn't CPU-bound, it should be okay.
|
|
|
|
|
Logged
|
|
|
|
|