python实现简单小游戏

python实现简单小游戏

import random  # 导入随机函数
import sys  # 到后面如果一个玩家赢了用于结束整个程序

"""
1.把牌初始化:创建4*9张筒子,4*9张萬,4*9张条子,4*7张风
    先通过列表单独创建其单一的,通过 列表名 * 4 再把所有
    的牌连接起来。
2.洗牌:通过for循环任意次,每次通过引用一个变量把任意两张
    牌交换
3.发牌:通过for循环,依次把前52张发给玩家,并帮玩家把牌
    的顺序排好
4.摸牌:到摸牌的玩家按顺序通过 tablet[0]每次获取当前的
    最前一张牌,用户自己判断是否赢,如果没赢,通过打出
    打出一张牌通过 del play_5[下标 - 1]  删除出的牌
    杠 ;如果玩家的手里有3张相同的牌时,别人打出剩余一
    张和你相同的牌,你可以选择杠,杠完把牌堆的最后一张
    牌摸走,判断是否赢,如果没赢,通过打出打出一张牌通
    过 del play_5[下标 - 1]  删除出的牌。
    碰 ;如果玩家的手里有2张相同的牌时,别人打出剩余一
    张和你相同的牌,你可以选择碰,判断是否赢,如果没赢,
    通过打出打出一张牌通过 del play_5[下标 - 1]  删除出
    的牌。
直到摸完最后一张牌游戏结束。
"""
# 全局变量初始化定义
play_1 = []
play_2 = []
play_3 = []
play_4 = []
play_1_need = []
play_2_need = []
play_3_need = []
play_4_need = []
poke = []
need_poke = []


class PlayGame:

    def initialize(self): # 初始化
        bobbin = ['1筒', '2筒', '3筒', '4筒', '5筒', '6筒', '7筒', '8筒', '9筒']
        miriade = ['1萬', '2萬', '3萬', '4萬', '5萬', '6萬', '7萬', '8萬', '9萬']
        strip = ['1条', '2条', '3条', '4条', '5条', '6条', '7条', '8条', '9条']
        wind = ['東', '西', '南', '北', '中', '發','白']
        tablet = bobbin * 4 + miriade * 4 + strip * 4 + wind * 4
        return tablet


    def shuffle(self,tablet):  # 洗牌
        for i in range(0,20000): # 定义循环范围,把牌打乱20000次
            x = random.randint(0, 135) # 取两个随机数
            y = random.randint(0, 135)
            # 交换两张牌的位置
            t = tablet[x]
            tablet[x] = tablet[y]
            tablet[y] = t
        return tablet

    def deal(self, tablet):  # 发牌
        global play_1
        global play_2
        global play_3
        global play_4
        for i in range(0, 52, 4): # 每人发13张牌
             play_1.append(tablet[i])
             play_2.append(tablet[i+1])
             play_3.append(tablet[i+2])
             play_4.append(tablet[i+3])
            # 给每个人按顺序理好牌
        def my_key2(x):
            return x[-1]
        play_1.sort()  # 先按数字排
        play_1.sort(key=my_key2) # 在按汉字排
        play_2.sort()
        play_2.sort(key=my_key2)
        play_3.sort()
        play_3.sort(key=my_key2)
        play_4.sort()
        play_4.sort(key=my_key2)
        # 把发了的52张牌删了
        del tablet[0:52]
        return tablet

    def draw(self,tablet):
        global play_1
        global play_2
        global play_3
        global play_4
        i = []
        while len(tablet) > 0: # 如果剩余牌不为0,一直摸牌打牌
            for x in range(1, 5): # 通过一个变量每次可以修改四个人的牌
                if x == 1:
                    play_5 = play_1
                    play_5_need = play_1_need
                if x == 2:
                    play_5 = play_2
                    play_5_need = play_2_need
                if x == 3:
                    play_5 = play_3
                    play_5_need = play_3_need
                if x == 4:
                    play_5 = play_4
                    play_5_need = play_4_need
                play_5.append(tablet[0])  # 每次从牌堆里把剩余第一张牌取出
                # play_5 = ['東', '西', '南','東', '西', '南', '北','東', '西', '南', '北','東', '西', '南']
                # def my_key2(x):
                #     return x[-1]
                # play_5.sort()
                # play_5.sort(key=my_key2)
                play_5_need = i  # 把碰的 杠的牌赋值进去

                def my_key2(x):  # 通过数字和条 万,筒 排序
                    return x[-1]
                play_5.sort()
                play_5.sort(key=my_key2)
                print("用户%d的牌为:%s   %s" %(x, play_5,play_5_need))  # 输出用户的牌
                success = input("是否赢了?(“*”代表赢了)")   # 用户自己判断是否胜利
                if success == "*":
                    def my_key2(x):  # 通过数字和条 万,筒 排序
                        return x[-1]
                    play_5.sort()
                    play_5.sort(key=my_key2)
                    print(play_5,play_5_need)
                    print("恭喜你胜利,游戏结束")
                    tablet.clear()
                    sys.exit(0)  # 中止程序,不继续向下执行
                play_poker = int(input("请输入你打的牌下标:"))
                while play_poker > len(play_5):   # 如果下出牌下标超出当前手里牌的范围,就一直循环,直到出牌正确
                    play_poker = int(input("你输入的牌不合理,请重新输入:"))
                poke = play_5[play_poker - 1]  # 接收用户当前出的牌,等待有没有其他人需要
                del play_5[play_poker - 1] # 删除出的牌
                for x in range(1, 5):
                    if x == 1:
                        play_6 = play_1
                    if x == 2:
                        play_6 = play_2
                    if x == 3:
                        play_6 = play_3
                    if x == 4:
                        play_6 = play_4
                    if play_6.count(poke) == 3:  # 如果你手里有3张相同的牌,等待有用户打出则可以杠
                        print("%s,%s你杠吗?" %(poke,str(play_6)))
                        i_need = int(input("(1杠,0不杠):"))
                        if i_need == 1:
                            play_6.append(tablet[-1])  # 去牌的末尾摸一张牌
                            play_6.remove(poke)  # 移除手里的3张相同牌
                            play_6.remove(poke)
                            play_6.remove(poke)
                            print(play_6)
                            play_5_need.append(poke * 4)  # 把4张相同牌输出,全部人都能看到
                            success = input("是否赢了?(“*”代表赢了)")  # 用户自己判断是否胜利
                            if success == "*":
                                def my_key2(x):  # 通过数字和条 万,筒 排序
                                    return x[-1]

                                play_5.sort()
                                play_5.sort(key=my_key2)
                                print(play_5, play_5_need)
                                print("恭喜你胜利,游戏结束")
                                sys.exit(0)
                            i_disneed = int(input("请输入你打的牌的下标:"))
                            while i_disneed > len(play_6):
                                i_disneed = int(input("你输入的牌不合理,请重新输入"))
                            del play_6[i_disneed-1]
                    if play_6.count(poke) >= 2: # 如果你手里有2张相同的牌,等待有用户打出则可以碰
                        print("%s,%s你碰吗?" %(poke,str(play_6)))
                        i_need = int(input("(1碰,0不碰):"))
                        if i_need == 1:  # 代表碰了
                            play_6.remove(poke)  # 移除两张相同的牌
                            play_6.remove(poke)
                            play_5_need.append(poke * 3)  # 把3张相同的牌放置全部人都能看见
                            print(play_5_need)
                            print(play_6)
                            success = input("是否赢了?(“*”代表赢了)")  # 用户自己判断是否胜利
                            if success == "*":
                                def my_key2(x):  # 通过数字和条 万,筒 排序
                                    return x[-1]

                                play_5.sort()
                                play_5.sort(key=my_key2)
                                print(play_5, play_5_need)
                                print("恭喜你胜利,游戏结束")
                                sys.exit(0)
                            i_disneed = int(input("请输入你打的牌的下标:"))
                            while i_disneed > len(play_6):
                                i_disneed = int(input("你输入的牌不合理,请重新输入"))
                            del play_6[i_disneed-1]
                del tablet[0]

                def my_key2(x):
                    return x[-1]
                play_5.sort()
                play_5.sort(key=my_key2)
        print("game over")


def main():
    playgame = PlayGame()  # 定义一个游戏类
    tablet = playgame.initialize()  # 初始化麻将
    playgame.shuffle(tablet)  # 洗麻将
    playgame.deal(tablet)  # 发麻将
    playgame.draw(tablet)
main()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值