python游戏代码大全可复制,python简单的小游戏代码
以下是一些Python编写的简单小游戏的示例代码。
- 猜数字游戏:
import random
number = random.randint(1, 100)
guess = None
while guess != number:
guess = int(input("请输入一个数字(1-100): "))
if guess < number:
print("猜的数字小了")
elif guess > number:
print("猜的数字大了")
print("恭喜你,猜对了数字!")
- 石头剪刀布游戏:
import random
rps = ["石头", "剪刀", "布"]
computer = random.choice(rps)
player = input("请出石头、剪刀、布(R/P/S): ")
print(f"你出的是: {player}, 电脑出的是: {computer}")
if player == computer:
print("平局")
elif (player == "R" and computer == "S") or (player == "P" and computer == "R") or (player == "S" and computer == "P"):
print("你赢了")
else:
print("你输了")
- 游戏:猜单词游戏:
import random
words = ["python", "game", "easy", "question"]
word = random.choice(words)
masked_word = "*" * len(word)
guessed_word = []
while not word == masked_word:
guess = input("请输入一个字母: ")
if len(guess) != 1 or not guess.isalpha():
print("请输入一个字母")
continue
if guess in guessed_word:
print("你已经猜过这个字母")
continue
guessed_word.append(guess)
for i in range(len(word)):
if word[i] == guess:
masked_word = masked_word[:i] + guess + masked_word[i+1:]
print(f"猜单词游戏,当前结果为: {masked_word}")
print("恭喜你,猜出了单词:", word)
这些游戏都是基于文本的,它们的核心是教育性的,展示了Python语言的基础功能。对于想要学习编程的初学者来说,这些游戏是很好的起点。
评论已关闭