[RenPy] 纯文本查看 复制代码
init python:
from operator import attrgetter
class Student_store(object):
"""学生商店."""
def select_xs(self):
pass
def xs_order(self,xs_type):
return self.select_xs(xs_type)
class Yz_xs_store(Student_store):
"""一中学生商店."""
#去一中学生商店下订单买一个学生
def select_xs(self, xs_type):
#商店又找厂家生产一个学生
return Xs_factory().select_xs_type(xs_type)
class Ez_xs_store(Student_store):
"""二中学生商店."""
def select_xs(self, xs_type):
return Xs_factory().select_xs_type(xs_type)
class Xs_factory(object):
"""学生工厂"""
def select_xs_type(self,xs_type):
#如果学生的种类是一中学生,那就生产一中学生
if xs_type == "一中":
return Yz_xs()
elif xs_type == "二中":
return Ez_xs()
class Student_sx(object):
"""学生属性基类."""
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
def __eq__(self, other):
"""python中的对象是否相等有两个层面,一个层面是是否是同一个对象,及在内存中是否共用一个内存区域,
用is判断,另一个是对象的值是否相等,用==判断"""
return self.__dict__ == other.__dict__
#学生属性随机化
def xs_random(self):
xing_list = ["王","刘","李","张"]
ming_list = ["一","二","三","四"]
address_list = ["山东","北京","上海","广东"]
self.xing = renpy.random.choice(xing_list)
self.ming = renpy.random.choice(ming_list)
self.name = self.xing+self.ming
self.age = renpy.random.randint(15,20)
self.address = renpy.random.choice(address_list)
class Yz_xs(Student_sx):
"""一中学生."""
def __init__(self):
Student_sx.xs_random(self)
class Ez_xs(Student_sx):
"""二中学生."""
def __init__(self):
Student_sx.xs_random(self)
yz_xs_store = Yz_xs_store() #一中学生商店
ez_xs_store = Ez_xs_store()
# Yz_xs1 = yz_xs_store.xs_order("一中") #一中学生商店找一中学生工厂生产一中学生1
# Ez_xs1 = ez_xs_store.xs_order("二中") #二中学生商店找二中学生工厂生产二中学生1
yz_xs_list = [] #一中学生列表
ez_xs_list = []
#学生数量,种类
def main(num,type):
if type == "一中":
while len(yz_xs_list) <= num-1:
a = yz_xs_store.xs_order("一中")
#判断创造的实例是否与原有的实例属性相同
if a not in yz_xs_list:
yz_xs_list.append(a)
# c = set(yz_xs_list) #删除重复的元素,并不适合实例对象
elif type == "二中":
while len(ez_xs_list) <= num-1:
a = ez_xs_store.xs_order("二中")
if a not in ez_xs_list:
ez_xs_list.append(a)
#全局变量
global yz_xs_list
yz_xs_list = sorted(yz_xs_list, key=attrgetter("name","age")) #先按名字,再按年龄排序,对中文似乎不太友好
global ez_xs_list
ez_xs_list = sorted(ez_xs_list, key=attrgetter("name","age"))
#界面
screen jm():
frame:
align(0.5,0.7)
hbox:
textbutton "一中学生列表" action ShowTransient("xs_jm", name = "一中学生列表", list = yz_xs_list)
textbutton "二中学生列表" action ShowTransient("xs_jm", name = "二中学生列表", list = ez_xs_list)
#一中学生界面列表
screen xs_jm(name,list):
viewport:
xsize 350
ysize 350
align(0,0)
draggable True
mousewheel True
arrowkeys True
scrollbars "vertical"
vbox:
text "[name]"
for i in list:
#textbutton "[i.name]" action ShowTransient("xs_sx", xs = i)
text "姓名:[i.name],年龄:[i.age],出生地:[i.address]"
#学生具体属性
screen xs_sx(xs):
frame:
align(0.8,0)
vbox:
xysize(200,230)
text "名字:"+ xs.name
text "年龄:" + str(xs.age)
text "出生地:" + xs.address
label start:
#$ main()
#show expression aa
"aaaaaaaa"
"bbbbbbbb"
#$ fj.main()#调用main函数启动
$ main(100,"一中") #生产10个一中学生
$ main(10,"二中")
# $ a = renpy.random.randint(10,20)
# $ main(a,"二中")
"[yz_xs_list[0].name],[yz_xs_list[0].age],[yz_xs_list[0].address]"
show screen jm()
"ccccccccccc"