User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Writing a command line game

Fri May 17, 2013 1:58 pm

So I have a good knowledge of python. Ish. Enough to be getting on with.
I want to put my knowledge to use and learn more by making a basic game that runs in the command line (like crawl but a LOT simpler, more like ninvaders).
How do I start doing this? Eg make it so that key presses do not come up on the screen, getting rid of the [email protected]:~$ bit, etc

Thanks!

dr_d_gee
Posts: 84
Joined: Fri Jan 04, 2013 1:30 pm

Re: Writing a command line game

Fri May 17, 2013 2:49 pm

I'd suggest having a look at Pygcurse: http://inventwithpython.com/pygcurse/

It does need Pygame to be installed but that is almost certainly there anyway.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Writing a command line game

Fri May 17, 2013 2:53 pm

Forget Python, to clear and re-print something like a ninvaders screen would be intolerably slow.

Sorry.
Dave.
Apple say... Monkey do !!

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Writing a command line game

Fri May 17, 2013 3:25 pm

If you want a command-line game, here's one I wrote earlier: http://www.soronlin.org.uk/goldhorse

It isn't screen-based like the ones you are talking about, but it's a lot easier to write something like this, and there are all sorts of games you can write in this style.

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Writing a command line game

Fri May 17, 2013 3:59 pm

The python Cmd Module is a really useful tool when writing command line interfaces, it takes away a lot of the complexity of managing strings.

http://wiki.python.org/moin/CmdModule

I used it for the minecraft cannon program I wrote, which might be useful as an example.

http://www.stuffaboutcode.com/2013/04/r ... annon.html

Mart
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Sat May 18, 2013 10:08 am

Ok cheers all :)
rurwin yours was a text based adventury type game... Is it true that writing a realtime one in python would be too
davef21370 wrote:intolerably slow
?
Most command line games are in C++ I guess.
But then mart managed to do something in realtime, even though it was in minecraft.

Basically, I want to consolidate my python knowledge before moving on to learning the vocab for pygame.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Writing a command line game

Sat May 18, 2013 3:22 pm

My guess is that if you kept the code efficient, you should be able to do something real-time. Using an inefficient library and rewriting the entire screen several times a second is a recipe for failure, but if you only rewrite stuff that changes, and you keep that to a minimum, Python has a lot of potential.

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Sun May 19, 2013 10:16 am

rurwin wrote: if you only rewrite stuff that changes, and you keep that to a minimum, Python has a lot of potential.
Thanks very much rurwin. I'll give it a go, see what I come out with. Would you recommend starting with something similar to Gold Horse before I try and do a realtime thingy?

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Tue May 21, 2013 12:58 pm

dr_d_gee wrote:I'd suggest having a look at Pygcurse
Pygcurse is just a pygame port of curses, which I have discovered will probably be how to only have the current messages displayed- ie unlike GoldHorse, where the screen is not cleared.
Are there any good tutorials of curses?

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Tue May 21, 2013 6:45 pm

Code: Select all

def strike(e_attack, p_defence):            # The enemy's attack
    if temp_defence != 0 :
        p_damage = randint(1,6)*e_attack-(p_defence+temp_defence)
    else:
        p_damage = randint(1,6)*e_attack-p_defence
    if p_damage < 0:
        p_damage = 0
    return p_damage


while Enemy.isalive() and Player.isalive():
    print ("The ", str(Enemy.name), " strikes you!"
    strike(Enemy.attack, Player.defence)
This throws up a syntax error at "strike" when I call it. Does anyone have any ideas why?

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Writing a command line game

Tue May 21, 2013 7:41 pm

Could you post the full error?

Dave.
Apple say... Monkey do !!

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Thu May 23, 2013 8:04 pm

davef21370 wrote:Could you post the full error?
I was running in idle, it just said that there was a syntax error and highlighted the word "strike" in red. I commented it out and then it did the same thing on the next line.

Embarrassingly I think that I forgot to generate an enemy. I had the EnemyType Class and the Enemy Class which should have resulted in a goblin but I omitted to actually call all of the bits of code.
For example in the function "strike" I tell python to use the enemy's attack value, but I had not actually picked an enemy from the list and set it as the opponent for this round.

How would I do this? Should I post my complete code somewhere so that you can see a bit more context?

I foolishly decided to write absolutely everything before testing anything. This was probably a bit of a bad move. I haven't managed to debug yet as this syntax error was the first thing that went wrong.

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Tue May 28, 2013 7:47 pm

bump

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Writing a command line game

Tue May 28, 2013 7:56 pm

theoB610 wrote: How would I do this? Should I post my complete code somewhere so that you can see a bit more context?
Yes. It's the only way we'll find it.

User avatar
theoB610
Posts: 233
Joined: Sat Jan 05, 2013 12:40 pm
Location: Devon

Re: Writing a command line game

Wed May 29, 2013 8:09 am

Should I just copy it into the forum, or is there a website that I can upload it to?

Return to “Python”