viernes, 23 de mayo de 2014

Guess the number, part 2

In this post I'm going to add more functionalty and structure to the game. If you ever downloaded an open source game's code in order to see how it was made, you surely saw that it's not in a single file. A game's code is usually spread across multiple files and directories. This helps a lot because it keeps things organized.

I'm not an expert programmer (hell, I'll be glad if somebody considered me even a novice programmer), so I'm not sure if I'm doing this right, but this is how I broke down the game:

- A main file, which handles the main game loop and calls all of the main functions.

- A file with the functions, besides the main game.

- A file with the actual game function.

This results in small files with readable code in each one. Here's how it worked out:



Main File:

from random import *
from functions import *
from mainGame import *

seed()

gamestate = 'menu'
   
def GameManager(state):


    while True:

        if state == 'menu':

            state = ShowMenu()

        elif state == 'game':

            MainGame()

            state = ShowMenu()

        elif state == 'score':

            ViewScores()

            state = ShowMenu()

        elif state  == 'quit':

            print("\nSee you soon!\n")
            break


GameManager(gamestate)

The functions file:

def MenuInput():

    while True:

        string = input("Your choice: ")

        if string.upper() == 'P':

            return 'game'

        elif string.upper() == 'S':

            return 'score'

        elif string.upper() == 'Q':

            return 'quit'

        else:

            print("\nThat is not a valid choice.")

def ShowMenu():

    print(""" * * * * * * * * * *
Welcome to \"Guess the number\"
* * * * * * * * * *

- Type \'p\' to play the game
- Type \'s\' to view the high scores
- Type \'q\' at any time to quit
""")

    return MenuInput()

def ViewScores():
    pass

def GiveHint(number,secretNumber):

    if number > secretNumber:

        print("\nThat's not it. It's too high.\n")

    elif number < secretNumber:

        print("\nThat's not it. It's too low.\n")

And finally the actual game:

from random import randint
from functions import GiveHint

def MainGame():

    secretNumber = randint(1,10)

    print("\nI'm thinking of a number between 1 and 10. Can you guess what it is?")

    while True: #Loop until the player wins or quits

        while True: #Loop until a valid answer is given

            answer = input("Your guess: ")

            if answer.upper() == 'Q':

                break

            else:

                try:

                    answerInNumerical = int(answer)

                    break

                except:

                    print("That's not a number.")

 

        if answer.upper() == 'Q':

            break

        elif answerInNumerical == secretNumber:

            print("\n * * * You are correct! * * *\n")

            break

        else:
           
            GiveHint(answerInNumerical,secretNumber)

A lot has changed since the last version! First, I added something I like to call 'Game Manager'. It's a function that decides which way the program should go. A variable called Gamestate is modified every time the game needs to head into a new direction, and gets read by the manager.

The game has some structure now. The player starts out by being greeted by a menu screen. He can choose to start the game, view the high scores (not implemented yet!) or quit the game. When the game starts, he can choose to keep going or quit at any time. And, until somebody finds a way to break it, the game appears to be free of errors.

Also, the usual hint system for this kind of game was implemented, letting the player know if his guess was above or below the secret number.

In the next post, I'll try to add the high score system. If everything goes well, I'll have a full featured game going!

If you have any questions or comments, please feel free to use the comment section below. See you soon!

No hay comentarios:

Publicar un comentario