python小练习____逻辑游戏DIY___简单易懂,可以用来练手

平台:PC端Pycharm IDE

利用基础知识写的三个简单的小游戏,通过写这三个小游戏,可以清楚的了解自己的逻辑基本功.

import random
from sys import exit
# 游戏菜单
# 1.猜数字
# 2.猜拳
# 3.恐怖游戏


# ------------------------------------------------------------------------------
# 1. Guess the number
def guess_the_number():
    print("^_^欢迎进入猜数字游戏")
    bot = int(input('请输入最小数字:\n'))
    top = int(input('请输入最大数字:\n'))
    rand = random.randint(bot, top)
    print('数字区间:[' + str(bot) + ',' + str(top) + ']')
    # print('Random number in [%d,%d] generated!' % (bot, top))
    num = int(input('###Guess the number###\n'))
    cnt = 1

    while num != rand:
        if num < rand:
            print('*_* 猜低了哦')
        else:
            print('T_T 猜高了哦')
        num = int(input('###Guess the number###\n'))
        cnt = cnt + 1
    dead('^_^ You get the answer with [%d] times' % cnt)


# ------------------------------------------------------------------------------
# 2. Mora --- 0 1 2 分别代表 剪刀 石头 布
def mora():
    print("^_^欢迎进入猜拳游戏")
    while True:
        player = int(input("请猜拳: "))
        computer = random.randint(0, 2)
        if player == computer:
            print("平局")
        elif (player == 0 and computer == 2) or (player == 1 and computer == 0) or (player == 2 and computer == 1):
            dead("赢")
        elif (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
            dead("输了,再来一局!")
        else:
            print("请正确划拳...")


# ------------------------------------------------------------------------------
# 3. Scary games
def gold_room():
    print("这个房间里装满了金子,你准备拿多少钱?\n please input [0~无穷]")
    while True:
        next_ = input("> ")
        if "0" in next_ or "1" in next_:
            money = int(next_)
            break
        else:
            dead("伙计,说个数吧.")
    if money < 50:
        dead("不错,你不贪心,你赢了!")
    else:
        dead("你贪婪的混蛋!")


def bear_room():
    print("这里有一只睡着的熊.")
    print("这只熊采集了一罐蜂蜜.")
    print("这只熊的前方有另一扇门里.")
    print("你打算怎么做?")
    print("你可以选择:\n\t1.take honey \n\t2.taunt bear\n\t3.open door PS:熊必须离开,才可以选择")
    bear_moved = False
    while True:
        next_ = input("> ")
        if next_ == "take honey":
            dead("熊看了你一眼,拍掉了你的脸.")
        elif next_ == "taunt bear" and not bear_moved:
            print("熊已经离开了门。你现在可以通过了.")
            bear_moved = True
        elif next_ == "taunt bear" and bear_moved:
            dead("熊生气了,咬掉了你的腿.")
        elif next_ == "open door" and bear_moved:
                gold_room()
        else:
            print("没有该选项,请重新选择.")


def cthulhu_room():
    cnt = 0
    while True:
        if cnt == 0:
            print("在这里你看到了邪恶的克苏鲁.")
            print("任何东西盯着你看,你就会发疯.")
            print("你是逃命呢,还是自己吃自己的头呢?\nplease input: flee or head")
        else:
            print("\t%s" % cnt)
        next_ = input("> ")
        if "flee" in next_:
            scary_games()
        elif "head" in next_:
            dead("Well that was tasty!")
        else:
            cnt += 1
            if cnt == 5:
                dead("你疯了,在风中飞翔,摔在地上死了!")
            print("好好选择吧,伙计!否则我会让你发疯的.")


def scary_games():
    print("你处在一个漆黑的房子里,什么也看不到.")
    print("在你的左边和右边有两扇门.")
    print("你选择打开哪扇门? \n Please input : right or left")
    next_ = input("> ")
    if next_ == "left":
        bear_room()
    elif next_ == "right":
        cthulhu_room()
    else:
        dead("你在房间里跌跌撞撞,直到饿坏为止.")


# ------------------------------------------------------------------------------
def game():
    print("请输入您想玩的游戏: ", end=' ')
    while True:
        select = input("> ")
        if select == "1":
            guess_the_number()
        elif select == "2":
            mora()
        elif select == "3":
            scary_games()
        else:
            print("选择BUG,请重新输入:")


def menu():
    print("----- 游戏菜单 -----")
    print("----> 1.猜数字")
    print("----> 2.猜拳")
    print("----> 3.恐怖游戏")


def dead(why):
    print(why, "Good job!")
    exit(0)


def main():
    menu()
    game()
    print("Game Over!")


main()

PS:大家可以在此代码基础上开发新的逻辑

参考:Zed Shaw编写的笨方法学python,挺好的入门书籍+PDF

链接:https://pan.baidu.com/s/1RWRWGu8CxW_ys6LeeFOSVA 
提取码:qwkj

猜你喜欢

转载自blog.csdn.net/weixin_41586634/article/details/85125245