目录
猜数字
猜单词
猜颜色
猜ASCII绘图图案
反向猜数字
石头剪刀布
快速打字游戏
简单数学挑战
好的,我会把上面提到的游戏整理一下,去掉重复的游戏,并梳理出来:
游戏1:猜数字 玩法: 玩家需要在指定的范围内猜一个随机生成的数字,系统会根据玩家的输入提示数字是大了还是小了。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import randomdef guess_number (): number_to_guess = random.randint(1 , 100 ) guess = None attempts = 0 print ("猜数字游戏开始!请在1到100之间猜一个数字。" ) while guess != number_to_guess: guess = int (input ("请输入你的猜测:" )) attempts += 1 if guess < number_to_guess: print ("太小了,再试一次。" ) elif guess > number_to_guess: print ("太大了,再试一次。" ) print (f"恭喜你,猜对了!你用了 {attempts} 次机会。" ) guess_number()
游戏2:猜单词 玩法: 玩家需要猜一个隐藏的单词,每次猜一个字母,系统会显示正确猜中的字母位置。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import randomdef guess_word (): words = ["python" , "algorithm" , "programming" , "computer" , "software" ] word_to_guess = random.choice(words) guessed_word = ["_" ] * len (word_to_guess) attempts = 10 print ("猜单词游戏开始!每次猜一个字母,你有10次机会。" ) print ("单词:" , " " .join(guessed_word)) while attempts > 0 and "_" in guessed_word: guess = input ("请输入一个字母:" ).lower() if guess in word_to_guess: for index, letter in enumerate (word_to_guess): if letter == guess: guessed_word[index] = guess print ("正确!" , " " .join(guessed_word)) else : attempts -= 1 print (f"错误!你还有 {attempts} 次机会。" ) if "_" not in guessed_word: print ("恭喜你,猜对了!" ) else : print (f"很遗憾,你没有猜出单词。单词是 {word_to_guess} 。" ) guess_word()
游戏3:猜颜色 玩法: 系统有一个预定义的颜色列表,玩家需要猜测系统随机选择的颜色。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import randomdef guess_color (): colors = ["red" , "blue" , "green" , "yellow" , "purple" , "orange" , "black" , "white" ] color_to_guess = random.choice(colors) attempts = 5 print ("猜颜色游戏开始!颜色列表:" , ", " .join(colors)) print ("你有5次机会猜对颜色。" ) while attempts > 0 : guess = input ("请输入你猜的颜色:" ).lower() attempts += 1 if guess == color_to_guess: print (f"恭喜你,猜对了!颜色是 {color_to_guess} 。" ) break else : attempts -= 1 print (f"错误!你还有 {attempts} 次机会。" ) if attempts == 0 : print (f"很遗憾,你没有猜对颜色。正确的颜色是 {color_to_guess} 。" ) guess_color()
游戏4:猜ASCII绘图图案 玩法: 玩家根据系统给出的提示猜出一个隐藏的ASCII艺术图案。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 import randomdef guess_ascii_art (): ascii_art_list = { "cat" : """ /\_/\ ( o.o ) > ^ < """ , "dog" : """ / \__ ( @\___ / O / (_____/ /_____/ U """ , "fish" : """ ><(((('> """ , "house" : """ _____ / \\ / \\ /_________\\ | | | _ _ | | |_| |_| | | | |_________| """ } art_name = random.choice(list (ascii_art_list.keys())) art_pattern = ascii_art_list[art_name] print ("猜ASCII艺术图案游戏开始!" ) print ("提示:" ) print (art_pattern) attempts = 3 while attempts > 0 : guess = input ("请输入你猜的图案名称(例如:cat, dog, fish, house):" ).lower() if guess == art_name: print ("恭喜你,猜对了!" ) break else : attempts -= 1 print (f"错误!你还有 {attempts} 次机会。" ) if attempts == 0 : print (f"很遗憾,你没有猜出图案。正确答案是 '{art_name} '。" ) guess_ascii_art()
游戏5:反向猜数字 玩法: 玩家心里想一个1到100之间的数字,系统会猜这个数字。玩家需要告诉系统猜的数字是大了还是小了,系统会根据反馈调整猜测。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 def reverse_guess_number (): print ("反向猜数字游戏开始!请在心里想一个1到100之间的数字,我会来猜。" ) low = 1 high = 100 attempts = 0 while low <= high: guess = (low + high) // 2 attempts += 1 print (f"我猜是 {guess} 。" ) feedback = input ("如果大了输入'h', 如果小了输入'l', 如果猜对了输入'y': " ).lower() if feedback == 'y' : print (f"太好了!我猜对了,你的数字是 {guess} 。总共用了 {attempts} 次。" ) break elif feedback == 'h' : high = guess - 1 elif feedback == 'l' : low = guess + 1 else : print ("输入无效,请重新输入。" ) reverse_guess_number()
游戏6:石头剪刀布 玩法: 玩家与系统进行石头剪刀布游戏,系统会随机选择一个动作,玩家需要输入自己的选择。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import randomdef rock_paper_scissors (): choices = ["石头" , "剪刀" , "布" ] computer_choice = random.choice(choices) print ("石头剪刀布游戏开始!" ) player_choice = input ("请输入你的选择(石头/剪刀/布):" ) if player_choice not in choices: print ("输入无效,请输入‘石头’,‘剪刀’或‘布’。" ) return 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 ("你输了!" ) rock_paper_scissors()
游戏7:快速打字游戏 玩法: 系统给出一个随机单词,玩家需要尽快输入这个单词,并计算输入时间。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import randomimport timedef typing_speed_test (): words = ["python" , "algorithm" , "programming" , "computer" , "software" ] word_to_type = random.choice(words) print ("快速打字游戏开始!" ) print (f"请尽快输入这个单词:{word_to_type} " ) start_time = time.time() user_input = input ("请输入:" ) end_time = time.time() time_taken = end_time - start_time if user_input == word_to_type: print (f"恭喜你,输入正确!你花了 {time_taken:.2 f} 秒。" ) else : print ("输入错误。" ) typing_speed_test()
游戏8:简单数学挑战 玩法: 系统会给出简单的数学问题(例如加减乘除),玩家需要在规定时间内回答正确。
代码:
import random
import time
def simple_math_challenge():
operations = ['+', '-', '*', '/']
num_questions = 5
correct_answers = 0
print("简单数学挑战游戏开始!")
for _ in range(num_questions):
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
operation = random.choice(operations)
if operation == '/':
num1 *= num2 # 确保结果是整数
question = f"{num1} {operation} {num2} = ?"
start_time = time.time()
answer = float(input(question + " "))
end_time = time.time()
if operation == '+':
correct_answer = num1 + num2
elif operation == '-':
correct_answer = num1 - num2
elif operation == '*':
correct_answer = num1 * num2
else:
correct