前言:
周末终于有时间玩一玩原神,发现3.1上半的义卖小游戏很有意思,就寻思能不能自己实现一个丐版,因此有了这个文章.
使用的技术是 Python+PySide6
先挂仓库链接,之后的内容也会同步更新上去
NicoIer/GameStruct: 一些小游戏的简单实现案例 (github.com)
https://github.com/NicoIer/GameStruct
最终的实现效果
游戏逻辑部分
佳酿节 - 义卖店铺
游戏目标
- 赚取足够数量的资金
基本逻辑
资金 | 店铺 | 指标 | 策略 |
---|---|---|---|
本金,收入,支出 | 小吃,百货,花卉 | 质量,效率,服务 | 提升指标,增加收入 |
玩家可以进行的操作
-
进入某家店铺
-
通过花费资金增加某项指标
-
选择策略卡
-
-
结算本轮
-
应对突发事件
可视化窗口
界面基于QtDesigner实现
具体ui文件去github下载就好了
核心代码
玩家 与 店铺之间的交互
class Player:
def __init__(self, money: int = 0, strategies=None):
if strategies is None:
strategies = set()
self.money = money
self.strategies = strategies
self.used_strategies = set()
def add_strategy(self, shop, strategy):
# ToDo 判断当前策略是否已经被使用
if strategy not in self.used_strategies:
self.used_strategies.add(strategy)
return shop.add_strategy(strategy)
def remove_strategy(self, shop, strategy):
if strategy in self.used_strategies:
self.used_strategies.remove(strategy)
return shop.remove_strategy(strategy)
def improve_indicator(self, shop, indicator, num: int) -> bool:
if self.money - num >= 0:
if shop.improve_indicator(indicator, num):
self.money -= num
return True
else:
return False
else:
return False
def cancel_improve_indicator(self, shop, indicator, num: int) -> bool:
if shop.cancel_improve_indicator(indicator, num):
self.money += num
return True
else:
return False
class Shop:
def __init__(self, shop_name):
self.name = shop_name
# 基础指标
self.__indicators = {
'quality': 0,
'efficiency': 0,
'serve': 0
}
# 运行时指标
self.__added_indicators = {
'quality': 0,
'efficiency': 0,
'serve': 0
}
# 目标指标
self.__target_indicators = {
'quality': 1000,
'efficiency': 1000,
'serve': 1000
}
# 最大值
self.__max_indicator = 100000
# 策略
self.__strategy_list: Set[Strategy] = set()
self.max_strategy_len = num_shops
#
self.used_money = 0
def max_indicator(self):
return self.__max_indicator
def can_add_strategy(self) -> bool:
if len(self.__strategy_list) < self.max_strategy_len:
return True
else:
return False
def can_remove_strategy(self) -> bool:
return not self.can_add_strategy()
def add_strategy(self, strategy: 'Strategy'):
if strategy not in self.__strategy_list and self.can_add_strategy():
self.__strategy_list.add(strategy)
return True
else:
return False
def remove_strategy(self, strategy):
if strategy in self.__strategy_list and self.can_remove_strategy():
self.__strategy_list.remove(strategy)
else:
return True
def improve_indicator(self, indicator, num) -> bool:
"""
:param indicator: 需要提升的指标
:param num: 花费的money数量
:return: 提升成功与否
"""
_ = self.show_indicators() # 获取最终计算后的指标值 进行比较
if _[indicator] + num > self.__max_indicator:
return False
else:
self.__added_indicators[indicator] += num
self.used_money += num
return True
def cancel_improve_indicator(self, indicator, num) -> bool:
if self.__added_indicators[indicator] - num < self.__indicators[indicator]:
return False
else:
self.__added_indicators[indicator] -= num
self.used_money -= num
return True
def show_indicators(self):
"""
:return:返回分配时指标副本在策略作用下的副本
"""
show_indicators = self.__added_indicators.copy()
for strategy in self.__strategy_list:
# ToDo 完成 策略影响 运行时指标数量
pass
return show_indicators
def settlement(self) -> int:
"""
确认结算资金,更新基本指标
:return:
"""
self.__indicators = self.__added_indicators.copy()
# TODo 完成收益函数 --> 想要使用边际递减函数
earnings = 10000
# ToDo 优化更新下一局的目标
for indicator, target in self.__target_indicators:
self.__target_indicators[indicator] = int(target * 1.5)
return earnings