Skip to content

lightningcell/python-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mad Guess Game

Author @lightningcell

Yes, this guessing game is mad. Because if this was just an ordinary guessing game, it wouldn't be this awesome. No more words... Check out my code structure and be impressed.

What makes it awesome ?

  • Definitely, modular structure. I know it sounds basic. But this is good. Because with this simple example, I want to explain how to program comfortably.

Let's look at the project structure:

   Guess Game/
   ├── gamedata.json
   ├── menus.py
   ├── run.py
   └── utils.py

NOTE: Don't forget to click the arrow on the left side of the file name. You can see the content of the file.

🔴 gamedata.json

This file contains the game data. Here is the default content

   {"MIN_VALUE": 0, "MAX_VALUE": 100, "BEST_SCORE": "0"}

🟢 utils.py

This file contains the utility functions. We don't want to see utility functions chilling around. Probably, the file we're working with is already messed up. So, we can put them in a separate file.

An example from utils.py:

def set_game_data(**kwargs) -> dict:
    data = get_game_data()
    for key in kwargs:
        data[key] = kwargs[key]

    with open("gamedata.json", "w") as data_file:
        data_file.write(json.dumps(data))

    return data   

I'm sure you don't want to see this function in your main file. So, you can import it from utils.py and use it. That's all.


🟡 menus.py

This file contains the menu actions of the game. In here, every function represents a menu. They don't affect each other. So, you can add or remove menus easily.

This file uses the functions from utils.py. So, you can see the power of modular structure.

from utils import *

An example from menus.py:

 def display_settings():
    data = get_game_data()
    print(f"""SETTINGS:
            MINIMUM NUMBER: {data['MIN_VALUE']}
            MAXIMUM NUMBER: {data['MAX_VALUE']}""")

🔵 run.py

This file contains the main flow of the game. When you run this file, you play the game.

So, what makes this file special. Let's look at the code:

def flow():
    WELCOME_MESSAGE = "Welcome to the guessing game."
    print(WELCOME_MESSAGE)
    while True:
        # SHOW MAIN MENU
        action = main_menu()

        if action == 1:
            game()
        elif action == 2:
            display_best_score()
        elif action == 3:
                   ......
                   ....
                   ...

As you can see, this file is the main flow of the game. It's not messy. It's not complicated. It's just a flow. And it's really easy to understand. When you want to change something of the game, you can easily find the place you want to change. And you can change it. That's all.

Next Features Challenge

Created by GitHub Copilot.

  • Add custom colors with colorama module.
  • Background music. Yeah, unnecessary but cool.
  • Add about menu, and display these:
    • Author
    • Version
    • GitHub Repository
    • License
    • Contact
    • Website

🌐 Socials:

JOIN DEVERDOCK Instagram LinkedIn Stack Overflow Hackerrank

💰 You can help me by Donating:

BuyMeACoffee

About

Python examples to understand everything about Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages