Hello everyone, the editor is here to answer the following questions for you, how to write a simple mini game in python, and a detailed tutorial on writing mini games in python. Let us take a look today!
Hello everyone, this article will focus on how to write a simple mini-game in python. A detailed tutorial on writing mini-games in python is something that many people want to understand. If you want to understand the code of mini-games in python, you need to first understand the following. matter.
1. How to download and install the printer driver to start making a mini game, and how to download the installation package to a USB flash drive .
Let’s start with a simple guessing number game called Locomotive Collector .
一段代码
temp = input("猜一下现在我想的是哪一个数字:")
guess = int(temp)
if guess == 8:
print("猜对啦!")
else:
print("猜错啦!")
print("游戏结束!")
The result is as follows:
The meaning of the code:
temp = input("猜一下现在我想的是哪一个数字:")
"=" is an assignment operator, which means assignment to; and "==" after if is a mathematical operator, which means equal.
The input function is used to receive user input and return it to temp, that is, assign this value to temp.
guess = int(temp)
The input function returns a string, which needs to be converted before it can be compared with a number, so use the int function for conversion.
if guess == 8:
print("猜对啦!")
else:
print("猜错啦!")
Here is a conditional branch statement, used for judgment, which is simply "if...else...".
2. Modification of the game
The modified code is as follows. Compared with the previous one, it is very user-friendly.
import random
counts = 3
answer = random.randint(1,10)
while counts > 0:
temp = input("猜一下现在我想的是哪一个数字:")
guess = int(temp)
if guess == answer:
print("猜对啦!")
break
else:
if guess < answer:
print("小啦~")
else:
print("大啦~")
counts = counts - 1
print("游戏结束!")
The above is the detailed code. If you want python learning materials, follow me to get it for free.