文章目录
前言
这是博主之前写的人生重开模拟器的升级版,原版请看博主的文章Python人生重开模拟器(高级版).
那个程序我们写完了,但大家有没有觉得,那太枯燥了呢?现在我们来让它变得有趣起来.
游戏成果
截图
可以看到,游戏里增加了剧情的功能.
开始吧
增加引用
有如下引用:
import sys
import time
import ctypes
import inspect
import random
import threading
函数
定义新函数
关闭进程函数
如下
def _async_raise(tid, exctype):
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("Invalid tid")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise RuntimeError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
这是关闭进程的函数,我也不懂,到时候会用到的.
修改部分函数
修改显示主菜单函数
如下
def showMainMenu():
"""显示主菜单"""
while True:
print('主菜单')
print('命令:')
print('去商店(1)')
print('打开背包(2)')
print('学习(3)')
print('显示属性(4)')
if my.old <= 22:
print('去学校(5)')
if my.old >= 30:
print('工作(6)')
try:
command = eval(input('输入命令...'))
if command == 1:
store.init()
elif command == 2:
my.openBag()
elif command == 3:
my.study()
elif command == 4:
my.show()
elif command == 5:
school.init()
elif command == 6:
work.init()
else:
pass
except:
pass
有些实例我们后面在定义,这里直接写上就行了.
大家都看得懂吧,我不多讲了.
类
定义新类
定义工作类
定义工作类框架
如下
class Work:
"""
定义一个工作类
"""
def __init__(self):
pass
定义工作类方法
找工作方法
如下
def findWork(self, knowledge):
"""找工作"""
work = {
'公司高管': 200,
'公司中管': 150,
'公司低管': 100,
'公司员工 高': 80,
'公司员工 中': 70,
'公司员工 低': 60,
'流水线': 50,
'搬运工': 40,
'工人': 30,
'农民': 20,
'环卫工人': 10,
'摆烂': 0
}
print('你开始找工作')
print('工作列表:')
print('╔════════════════════════╦═══════════════════════╗')
print('║ 工 作 ║ 所需知识 ║')
print('╠════════════════════════╬═══════════════════════╣')
for key, value in work.items():
print(f'║ {
key} ║ {
value} ║')
print('╠═══════════════════════╬═══════════════════════╣')
print(f'你的知识是:{
knowledge}')
try:
a = input('请输入你想要参加的工作:')
k = work[a]
except:
print('错误.')
self.findWork(knowledge)
else:
try:
my.work = k
except:
print('错误.')
self.findWork(knowledge)
else:
print(f'找工作成功!工作为:{
my.work}')
大家都看得懂吧,我不多讲了.
工作方法
如下
def init(self):
"""工作"""
# {
# '公司高管': 200,
# '公司中管': 150,
# '公司低管': 100,
# '公司员工·高': 80,
# '公司员工·中': 70,
# '公司员工·低': 60,
# '流水线': 50,
# '搬运工': 40,
# '工人': 30,
# '农民': 20,
# '环卫工人': 10,
# '摆烂': 0
# }
print('开始工作')
type_ = 1
try:
if my.work == '摆烂':
pass
elif my.work == '环卫工人':
type_ = 10
elif my.work == '农民':
type_ = 20
elif my.work == '工人':
type_ = 30
elif my.work == '搬运工':
type_ = 40
elif my.work == '流水线':
type_ = 50
elif my.work == '公司员工 低':
type_ = 60
elif my.work == '公司员工 中':
type_ = 70
elif my.work == '公司员工 高':
type_ = 80
elif my.work == '公司低管':
type_ = 100
elif my.work == '公司中管':
type_ = 150
elif my.work == '公司高管':
type_ = 200
else:
print('错误')
except:
print('错误')
else:
print(f'工作中,每次赚{
type_}')
print('退出工作:0')
print('按Enter工作一次')
def working():
while True:
a = input()
if a == '0':
break
else:
my.dollars = my.dollars + type_
print(f'钱数加{
type_}')
thread1 = threading.Thread(group = working())
thread1.start()
大家都看得懂吧,我不多讲了.
工作类
工作类代码如下:
class Work:
"""
定义一个工作类
"""
def __init__(self):
pass
def findWork(self, knowledge):
"""找工作"""
work = {
'公司高管': 200,
'公司中管': 150,
'公司低管': 100,
'公司员工 高': 80,
'公司员工 中': 70,
'公司员工 低': 60,
'流水线': 50,
'搬运工': 40,
'工人': 30,
'农民': 20,
'环卫工人': 10,
'摆烂': 0
}
print('你开始找工作')
print('工作列表:')
print('╔════════════════════════╦═══════════════════════╗')
print('║ 工 作 ║ 所需知识 ║')
print('╠════════════════════════╬═══════════════════════╣')
for key, value in work.items():
print(f'║ {
key} ║ {
value} ║')
print('╠═══════════════════════╬═══════════════════════╣')
print(f'你的知识是:{
knowledge}')
try:
a = input('请输入你想要参加的工作:')
k = work[a]
except:
print('错误.')
self.findWork(knowledge)
else:
try:
my.work = k
except:
print('错误.')
self.findWork(knowledge)
else:
print(f'找工作成功!工作为:{
my.work}')
def init(self):
"""工作"""
# {
# '公司高管': 200,
# '公司中管': 150,
# '公司低管': 100,
# '公司员工·高': 80,
# '公司员工·中': 70,
# '公司员工·低': 60,
# '流水线': 50,
# '搬运工': 40,
# '工人': 30,
# '农民': 20,
# '环卫工人': 10,
# '摆烂': 0
# }
print('开始工作')
type_ = 1
try:
if my.work == '摆烂':
pass
elif my.work == '环卫工人':
type_ = 10
elif my.work == '农民':
type_ = 20
elif my.work == '工人':
type_ = 30
elif my.work == '搬运工':
type_ = 40
elif my.work == '流水线':
type_ = 50
elif my.work == '公司员工 低':
type_ = 60
elif my.work == '公司员工 中':
type_ = 70
elif my.work == '公司员工 高':
type_ = 80
elif my.work == '公司低管':
type_ = 100
elif my.work == '公司中管':
type_ = 150
elif my.work == '公司高管':
type_ = 200
else:
print('错误')
except:
print('错误')
else:
print(f'工作中,每次赚{
type_}')
print('退出工作:0')
print('按Enter工作一次')
def working():
while True:
a = input()
if a == '0'