小游戏代码大全可复制python
时间: 2023-08-25 18:07:43 浏览: 2845
以下是一个简单的Python小游戏代码,可供参考:
1. 猜数字游戏
```python
import random
print("欢迎来到猜数字游戏!")
print("我已经想好了一个1到100之间的数字。")
number = random.randint(1, 100)
while True:
guess = int(input("请猜一个数字:"))
if guess == number:
print("恭喜你,猜对了!")
break
elif guess < number:
print("太小了,请再猜一次。")
else:
print("太大了,请再猜一次。")
```
2. 石头剪刀布游戏
```python
import random
print("欢迎来到石头剪刀布游戏!")
while True:
player_choice = input("请输入你的选择(石头/剪刀/布):")
computer_choice = random.choice(["石头", "剪刀", "布"])
print(f"你的选择是:{player_choice}")
print(f"电脑的选择是:{computer_choice}")
if player_choice == computer_choice:
print("平局!")
elif (player_choice == "石头" and computer_choice == "剪刀") \
or (player_choice == "剪刀" and computer_choice == "布") \
or (player_choice == "布" and computer_choice == "石头"):
print("你赢了!")
else:
print("你输了!")
play_again = input("是否继续游戏?(是/否)")
if play_again == "否":
break
```
3. 猜单词游戏
```python
import random
words = ["apple", "banana", "cherry", "orange", "pear"]
word = random.choice(words)
letters_guessed = set()
print("欢迎来到猜单词游戏!")
print(f"单词有{len(word)}个字母。")
while True:
guessed_word = ""
for letter in word:
if letter in letters_guessed:
guessed_word += letter
else:
guessed_word += "_"
if guessed_word == word:
print(f"恭喜你,猜对了!答案是{word}。")
break
print(f"你猜的单词是:{guessed_word}")
guess = input("请猜一个字母:")
letters_guessed.add(guess)
if guess not in word:
print("猜错了!")
```
以上是三个简单的Python小游戏,希望能对你有所帮助!