Re: simple game in python?

@dhruv
Here you go.  The forum will butcher this if you just copy/paste it because it's choosing to wrap some of the lines.  As an alternative, for a runnable file:
https://dl.dropboxusercontent.com/u/356 … inggame.py
yes, I tend to write long lines.  The coding of it took 6 minutes exactly, including a quick Google search on the "proper" way to do console input and get it as an int.  I've got an audiogame in python, but, well, that doesn't ever use raw_input.  The console for me these days is that useful thing I can spit debugging messages to.  I didn't include the explanatory comments in that time, and most of that 6 minutes was the quick Google search.

import random #Lets us make random numbers.
min = 1 #the lowest possible correct number.
max = 100#the highest possible correct number.
print "Welcome to the number guessing game."
print "Please enter a number between " + str(min) + " and " + str(max) + "."
#in the above, we chose concatenation because of some implicit spaces.
#Print can also print comma-separated lists of variables, but it puts spaces in for us, so we can't make it look like a sentence.
#thus, the str() which makes things into strings and the +, which gives us a sentence with no extra spaces anywhere.

actual = random.randrange(min, max+1) #calculate our guess. Randrange returns a number between min inclusive and max exclusive, so we add 1 to max here.

guess = None #None is a special value in python-it's kind of like NULL.  We don't know what the guess is, so we use it.
while(guess != actual): #comparing None to a number can never be equal.  This says continue until the player has guessed the correct number.
    try: #this is exception handling.  Most of the time, things go right.  When they go wrong, give us a chance to handle it.
        guess = int(raw_input("Enter a guess: ")) #try to get something from the user and convert it to a string.
        #the following lines print info on your guess and are very straight-forward.
        if guess < actual:
            print "Your guess was too low."
        elif guess > actual:
            print "Your guess was too high."
        elif guess == actual:
            print "Congradulations, you win!"
    #the one thing that can go wrong in the above is that you didn't input a number, or input something that can't be made into one.
    #In this case, everything else in the try gets skipped and python gives us an exception.
    #The exception for this is called a ValueError, so when it happens, we say we want to do whatever is here.
    #without this, inputting something that is not a number will crash the game.
    except ValueError:
        print "That is not a number, pleas try again."

#when the right number is entered, we automatically get here and exit because it's the end of the file.

@guitarman
I think I'm going to end up doing some sort of game tutorial of some sort in the somewhat near future.  The whole 9 yards, etc.  This seems to be needed, as the only platform that even tries is BGT, and I'm seeing a lot of interesting design patterns.  The thing that keeps popping up over and over is this: the design patterns and practices in use are not reusable, and better options exist.  The idea of a while loop and a bunch of if statements that check the keyboard is one option, but really not the best-just that the others are, at first (and only at first!) harder.  They require effort to understand, but allow for hard things to be simple: suddenly transitioning to a minigame, having menus that pop up while the game continues playing, having something happen in 5 seconds, etc.
Game_engine abstracts the entire main loop, and gives you the ability to do cool things, but intermediate programming skill is required to understand it and why it is good.  Alternatively, I need to put out a tutorial that covers that and Camlorn_audio at the same time, and leaves you with--for example--a really simplistic first-person shooter, or a space invaders, or something, as well as a good understanding of what a callback and event based model gives you.

URL: http://forum.audiogames.net/viewtopic.php?pid=163429#p163429

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
http://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • [Audiogames-re... AudioGames.net Forum — General Game Discussion: dhruv
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: camlorn
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: Guitarman
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: camlorn
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: dhruv
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: camlorn
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: dhruv
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: stewie
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: stewie
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: dhruv
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: camlorn
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: dhruv
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: camlorn

Reply via email to