I'm going to make a small Python comprehensive game.
For example, if you make game 1, game 2, game 3, game 1 pops up when you press 1, and game 2 pops up when you press 2
How do you write it like this?
Game 1, Game 2, Game 3 are all written separately.
python
import os
import argparse
from argparse import RawTextHelpFormatter
def main():
parser = argparse.ArgumentParser(
description="Example Games!",
For opening in formatter_class=RawTextHelpFormatter #helptext
)
parser.add_argument(
"--mode", #Printer Options
type=int,
help="Please select the game mode to play. \n1: Action\n2: Shooting \n3: RPG"
)
args = parser.parse_args()
if args.mode is None:
print("usage: {} [-h] [--mode MODE]".format(
os.path.basename(__file__)
))
return
print(args.mode) # Now, we can use args.mode to create a condition to call those games, right?
if __name__ == '__main__':
main()
How to run:
python filename.py -h
python filename.py --mode 1
python filename.py --mode 2
python filename.py --mode 3
usage: example.py [-h] [--mode MODE]
Example Games! --mode
optional arguments:
-h, --help show this help message and exit
--mode MODE Select the game mode to play.
1: Action
2: Shooting
3: RPG
In python2 years, the argv of the sys module was mainly used to obtain factors
If you use argparse, which has been added since Python 3.2, it is convenient and easy to use, and please refer to the official document!
Official document - argsparse
I'm also a beginner in coding, but I recently made a comprehensive game with Python as a college assignment. In my case, I made a new Python file pygame, and I put the name of the import game (excluding .py), and I put the title and start button as a picture, and I put the condition that I click the start button for the game in the if statement, and I put the function that runs the game (main(in my case)) in the if clause. There was a problem here, and when I started the comprehensive game file, the first game I put in was run right away. To solve this problem, you have to subtract the code that runs the game files that you put in the comprehensive game. And I think it will be solved if you put the code in the if clause that clicks the start button. If you have any other questions, please leave comments!
546 Uncaught (inpromise) Error on Electron: An object could not be cloned
758 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
545 GDB gets version error when attempting to debug with the Presense SDK (IDE)
551 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.