Python小游戏
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
冒险游戏
import time
def start_game():
print("欢迎来到冒险游戏!")
time.sleep(1)
print("你醒来发现自己置身在一片陌生的森林中。")
time.sleep(1)
print("你必须找到回家的路。")
time.sleep(1)
choice = None
while choice not in ("1", "2", "3", "4"):
print("\n你的选择:")
print("1. 往左走")
print("2. 往右走")
print("3. 探索附近")
print("4. 等待帮助")
choice = input()
if choice == "1":
print("\n你往左走,迷失在森林中。")
time.sleep(1)
print("游戏结束!")
elif choice == "2":
print("\n你往右走,遇到了一条大河。")
time.sleep(1)
print("你成功游过河流,找到了回家的路!")
time.sleep(1)
print("恭喜你顺利回家!")
elif choice == "3":
print("\n你决定探索附近的地区。")
time.sleep(1)
print("你发现了一个神秘的洞穴。")
time.sleep(1)
print("你决定进入洞穴探险。")
time.sleep(1)
print("洞穴里面有两条路,你选择:")
inner_choice = None
while inner_choice not in ("1", "2"):
print("1. 向左走")
print("2. 向右走")
inner_choice = input()
if inner_choice == "1":
print("\n你向左走,突然掉进了一个陷阱!")
time.sleep(1)
print("游戏结束!")
elif inner_choice == "2":
print("\n你向右走,发现了一座宝藏山!")
time.sleep(1)
print("你成功找到宝藏,并顺利回家!")
time.sleep(1)
print("恭喜你成为冒险游戏的胜利者!")
elif choice == "4":
print("\n你决定等待帮助。")
time.sleep(1)
print("在等待的过程中,一个猎人路过并带你回家。")
time.sleep(1)
print("恭喜你顺利回家!")
start_game()
21点
import random
def start_game():
print("欢迎来到21点游戏!")
deck = create_deck()
player_hand = []
dealer_hand = []
# 发牌
for _ in range(2):
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
# 玩家回合
while True:
print("玩家手牌:", player_hand)
choice = input("选择要继续拿牌还是停止(继续拿牌输入'拿牌', 停止输入'停止'):")
if choice == "拿牌":
player_hand.append(deck.pop())
if get_hand_value(player_hand) > 21:
print("玩家手牌爆了!")
print("庄主赢了!")
return
elif choice == "停止":
break
# 庄主回合
while get_hand_value(dealer_hand) < 17:
dealer_hand.append(deck.pop())
# 比较手牌
player_score = get_hand_value(player_hand)
dealer_score = get_hand_value(dealer_hand)
print("玩家手牌:", player_hand)
print("庄主手牌:", dealer_hand)
if player_score > 21:
print("玩家手牌爆了!")
print("庄主赢了!")
elif dealer_score > 21:
print("庄主手牌爆了!")
print("玩家赢了!")
elif player_score == dealer_score:
print("游戏平局!")
elif player_score > dealer_score:
print("玩家赢了!")
else:
print("庄主赢了!")
def create_deck():
deck = []
suits = ["♠", "♥", "♣", "♦"]
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
for suit in suits:
for rank in ranks:
deck.append(rank + suit)
random.shuffle(deck)
return deck
def get_hand_value(hand):
value = 0
ace_count = 0
for card in hand:
rank = card[:-1] # 获取牌的点数,去掉花色
if rank.isdigit():
value += int(rank)
elif rank in ["J", "Q", "K"]:
value += 10
elif rank == "A":
value += 11
ace_count += 1
# 如果爆牌且手牌中有A,则将A的点数设为1
while value > 21 and ace_count > 0:
value -= 10
ace_count -= 1
return value
start_game()
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |