猫史档案馆


我的一个小作品

用户:纯数孤傲纯数孤傲查看:0 回复:0 评论:0 创建时间:2019-10-14T20:01:57


我的一个小作品,请各位大佬点评:

PS:还有一些bug,而且还有功能未完善

import random

import time


rand = random.randint


class Write:
    def __init__(self, path):
        self.path = path
        self.file = open(self.path, 'w')

    def write_file(self, text=''):
        self.file.write(text)

    def get_path(self):
        return self.path

    def set_path(self, path):
        self.path = path
        self.file.close()
        self.file = open(self.path, 'w')

    def close(self):
        self.file.close()
        self.path = None

    def is_empty(self):
        file = Read(self.path)
        if len(file.read_file()) != 0:
            return False
        else:
            return True


class Append:
    def __init__(self, path):
        self.path = path
        self.file = open(path, 'a')

    def append_file(self, text=''):
        self.file.write(text)

    def get_path(self):
        return self.path

    def set_path(self, path):
        self.path = path
        self.file.close()
        self.file = open(self.path, 'a')

    def close(self):
        self.file.close()
        self.path = None

    def is_empty(self):
        file = Read(self.path)
        if len(file.read_file()) != 0:
            return False
        else:
            return True


class Read:
    def __init__(self, path):
        self.path = path
        self.file = open(self.path, 'r')

    def read_file(self):
        self.file.seek(0)
        print(self.file.read())
        return self.file.read()

    def get_path(self):
        return self.path

    def set_path(self, path):
        self.path = path
        self.file.close()
        self.file = open(self.path, 'r')

    def close(self):
        self.file.close()
        self.path = None

    def is_empty(self):
        print(self.read_file())
        if len(self.read_file()) != 0:
            return False
        else:
            return True


def save():
    global money, gold, work_money, work_time, automatic_level, automatic_required, automatic_money, company_level, \
        probability, price
    global my_data
    my_data['money'] = money
    my_data['gold'] = gold
    my_data['work_money'] = work_money
    my_data['work_time'] = work_time
    my_data['automatic_level'] = automatic_level
    my_data['automatic_required'] = automatic_required
    my_data['automatic_money'] = automatic_money
    my_data['company_level'] = company_level
    my_data['probability'] = probability
    my_data['price'] = price
    return my_data


def read():
    global money, gold, work_money, work_time, automatic_level, automatic_required, automatic_money, company_level, \
        probability, price
    global my_data
    money = my_data['money']
    gold = my_data['gold']
    work_money = my_data['work_money']
    work_time = my_data['work_time']
    automatic_level = my_data['automatic_level']
    automatic_required = my_data['automatic_required']
    automatic_money = my_data['automatic_money']
    company_level = my_data['company_level']
    probability = my_data['probability']
    price = my_data['price']
    return my_data


def print_menu():
    global my_data
    global money, gold, work_money, work_time, automatic_level, automatic_required, automatic_money, company_level, \
        probability, price
    number = 0
    read()
    print('')
    print('请根据序号选择事件,当前金币%s枚,黄金%s块,物价是%s' % (money, gold, price))
    print('1.上班赚钱,可赚%s元,所需时间%s秒' % (work_money, work_time))
    a = str(100 / probability)
    array = a.split('.')
    if len(array[1]) <= 2:
        print('2.去应聘,应聘概率为%s' % (a + '%'))
    else:
        print('2.去应聘,应聘概率为%s' % (array[0] + '.' + array[1][0] + array[1][1] + '%'))
    print('3.升级赚钱机器,当前赚钱机器有%s级,升级需要%s枚金币,当前赚钱速度为%s' % (automatic_level, automatic_required, automatic_money))
    print('4.将钱兑换成黄金')
    print('5.将黄金兑换成钱')
    print('--------------------其他操作--------------------')
    print('6.查看当前个人信息')
    print('7.保存游戏进度')
    print('8.获取游戏进度')
    print('9.删除游戏进度')
    print('10.退出游戏')
    print('11.重置游戏')
    print('12.获得官方建议(需要5000黄金)')
    while True:
        try:
            number = int(input())
        except ValueError:
            print('输入内容不是数字!')
            continue

        if number in list(range(1, 13)):
            break
        else:
            print('只能输入1~12的数字!')
            continue

    return number


def play():
    global money, gold, work_money, work_time, automatic_level, automatic_required, automatic_money, company_level, \
        probability, price
    global my_data
    read()
    while True:
        number = print_menu()
        if number == 1:
            print('上班中······')
            time.sleep(work_time)
            money += work_money
            print('下班了,当前金币%s' % money)
        elif number == 2:
            a = rand(1, probability)
            b = a >= 1
            c = a <= 10
            if b and c:
                if money < company_level + 1:
                    print('钱不够!')
                else:
                    money -= company_level
                    company_level += 1
                    work_money += company_level * 1.5
                    work_time += 0.2
                    print('应聘成功!当前公司等级为:%s,赚钱速度为%s枚金币/秒' % (company_level, work_money / work_time))
            else:
                print('应聘失败!')
            probability = rand(2, 100)
            del a, b, c
        elif number == 3:
            if money < automatic_required:
                print('钱不够,无法升级')
            else:
                money -= automatic_required
                automatic_level += 1
                automatic_required += 30
                automatic_money += 25
                print('升级成功!')
                print('当前自动赚钱机器的等级为:', automatic_level, ',升级需要', automatic_required, '枚金币,当前速度为:', automatic_money,
                      sep='')
        elif number == 4:
            a = int(input('请输入想要兑换的黄金的数量:'))
            if a * price > money:
                print('钱不够!')
            else:
                money -= a * price
                gold += a
                print('兑换成功!')
            del a
        elif number == 5:
            a = int(input('请输入想要兑换成钱的黄金的数量:'))
            if gold < a:
                print('黄金不够!')
            else:
                money += a * price
                gold -= a
                print('兑换成功!')
            del a
        elif number == 6:
            print('当前金币数量:', money, sep='')
            print('当前黄金数量:', gold, sep='')
            print('当前物价:', price, sep='')
            print('当前赚钱速度:', work_money / work_time, '枚金币/秒', sep='')
            print('当前自动赚钱机器的等级为:', automatic_level, ',升级需要', automatic_required, '枚金币,当前速度为:', automatic_money, sep='')
            print('当前公司等级为:', company_level, ',应聘几率为:', 100 / probability, '%', sep='')
        elif number == 7:
            name = input('请输入进度名称:')
            save_file = Write('C:/Users/fanshi/Desktop/编程/python and pygame/my_game/moneyGame/' + name + '.txt')
            print('正在保存中,请稍后······')
            '''
            格式:
                金币
                黄金
                工资
                工作时间
                赚钱机器等级
                赚钱机器升级所需金币
                赚钱机器等级
                公司等级
                应聘概率
                物价
            '''
            text = str(money) + '\n' + str(gold) + '\n' + str(work_money) + '\n' + str(work_time) + '\n' + str(
                automatic_level) + '\n' + str(automatic_required) + '\n' + str(automatic_money) + '\n' + str(
                company_level) + '\n' + str(probability) + '\n' + str(price)
            save_file.write_file(text)
            save_file.close()
            print('保存成功!')
            del name, save_file, text
        elif number == 8:
            name = input('请输入进度名称:')
            read_file = Read('C:/Users/fanshi/Desktop/编程/python/my_game/moneyGame/' + name + '.txt')
            file = None
            array = []
            name_list = ['money', 'gold', 'work_money', 'work_time', 'automatic_level', 'automatic_required',
                         'automatic_money', 'company_level', 'probability', 'price']
            i = 0
            key = None
            if read_file.is_empty():
                print('不存在此进度!')
            else:
                file = read_file.file
                file.seek(0)
                for i in range(10):
                    array.append(list(file.readline())[0:-1])
                # print(array)
                save()
                i = 0
                for key in name_list:
                    my_data[key] = ''
                    for a in array[i]:
                        my_data[key] += str(a)
                    # print(my_data[key])
                    if key in ('money', 'work_money', 'work_time', 'probability'):
                        my_data[key] = float(my_data[key])
                    elif key in ('gold', 'automatic_level', 'automatic_required', 'automatic_money', 'company_level',
                                 'price'):
                        my_data[key] = int(my_data[key])
                # print(my_data)
                read()
                print('读取成功!')
            del name, read_file, file, array, name_list, i, key
        elif number == 9:
            name = input('请输入进度名称:')
            delete_file = Write('C:/Users/fanshi/Desktop/编程/python and pygame/my_game/moneyGame/' + name + '.txt')
            if delete_file.is_empty():
                print('不存在此进度!')
            else:
                delete_file.write_file()
                delete_file.close()
                print('删除成功!')
            del name, delete_file
        elif number == 10:
            break
        elif number == 11:
            money = 0
            work_money = 1
            work_time = 1
            automatic_level = 0
            automatic_money = 0
            automatic_required = 100
            company_level = 0
            probability = 10
        elif number == 12:
            if gold < 5000:
                print('黄金不够!')
            else:
                gold -= 5000
                print('请稍等······')
                time.sleep(3)
                

        money += automatic_money
        price += rand(-30, 30)
        if price <= 0:
            price = rand(10, 50)
        save()


if __name__ == '__main__':
    my_data = {'money': 0,
               'gold': 0,
               'work_money': 1,
               'work_time': 1,
               'automatic_level': 0,
               'automatic_money': 0,
               'automatic_required': 100,
               'company_level': 0,
               'probability': 10,
               'price': 100}
    money = 0
    gold = 0
    work_money = 1
    work_time = 1
    automatic_level = 0
    automatic_money = 0
    automatic_required = 100
    company_level = 0
    probability = 10
    price = 100
    play()
    print('游戏结束')

 


回复

上一页1 页 / 共 0下一页