用Ren'Py制作恋爱模拟器:好感度系统怎么写?代码+UI全解析
1710 字
9 分钟
用Ren'Py制作恋爱模拟器:好感度系统怎么写?代码+UI全解析
本文将从变量设计、对话影响、剧情分支到UI显示,全面讲解如何构建一个完整的好感度系统。
一、角色好感度变量设计
变量定义与初始化
在Ren’Py中,好感度变量通常使用整数类型,范围从0到100,这样便于理解和处理边界值。
# 游戏初始化时定义好感度变量default hero_affection = 50 # 主角好感度,初始值为50(中等关系)default rival1_affection = 30 # 竞争者1好感度default rival2_affection = 20 # 竞争者2好感度
# 好感度等级常量定义define AFFECTION_MIN = 0define AFFECTION_MAX = 100define AFFECTION_LOW = 30 # 低好感度阈值define AFFECTION_MEDIUM = 50 # 中好感度阈值define AFFECTION_HIGH = 80 # 高好感度阈值变量命名规范与数据类型
采用统一的命名规范有助于代码维护: 角色_affection 表示好感度,使用整数类型便于数学运算和比较。
# 边界值处理函数init python: def clamp_affection(value, min_val=AFFECTION_MIN, max_val=AFFECTION_MAX): """确保好感度在有效范围内""" return max(min_val, min(value, max_val))
def modify_affection(character_affection, delta): """修改好感度并返回新值""" new_value = character_affection + delta return clamp_affection(new_value)初始值设定策略
不同角色的初始好感度应体现游戏开始时的关系状态:
# 根据角色背景设定不同的初始好感度default childhood_friend_affection = 70 # 青梅竹马,初始好感度高default new_student_affection = 30 # 转学生,初始好感度低default teacher_affection = 40 # 老师,保持专业距离二、对话选项影响好感度
基础对话分支结构
Ren’Py使用menu语句创建对话选项,每个选项可以触发不同的好感度变化。
# 基础对话选项示例label date_conversation: hero "今天天气真不错呢!"
menu: "确实如此,很适合散步": $ hero_affection = modify_affection(hero_affection, 5) hero "那就一起去公园吧!"
"我更喜欢待在家里": $ hero_affection = modify_affection(hero_affection, -3) hero "这样啊...那下次再约吧"
"(沉默不语)": $ hero_affection = modify_affection(hero_affection, -8) hero "你好像不太想说话..."复杂好感度变化逻辑
对于需要多个条件判断的场景,可以在选项内部使用if语句:
label gift_giving: "你想送什么礼物给hero?"
menu: "送她最喜欢的玫瑰": if hero_affection >= 60: $ hero_affection = modify_affection(hero_affection, 10) hero "谢谢你记得我喜欢玫瑰!" else: $ hero_affection = modify_affection(hero_affection, 5) hero "这玫瑰很漂亮,谢谢你。"
"送昂贵的项链": if hero_affection < 40: $ hero_affection = modify_affection(hero_affection, -5) hero "这太贵重了,我不能收..." else: $ hero_affection = modify_affection(hero_affection, 3) hero "你总是这么破费..."批量好感度处理
当一个选择影响多个角色好感度时:
label group_activity: "周末大家要去游乐园,你决定:"
menu: "主动组织活动": $ hero_affection = modify_affection(hero_affection, 8) $ rival1_affection = modify_affection(rival1_affection, 5) $ rival2_affection = modify_affection(rival2_affection, -3) "大家都觉得你很可靠!"
"保持低调参与": $ hero_affection = modify_affection(hero_affection, 2) $ rival1_affection = modify_affection(rival1_affection, 2) $ rival2_affection = modify_affection(rival2_affection, 2) "平淡但和谐的聚会。"三、好感度触发不同剧情分支
基础条件判断
使用if语句根据好感度阈值决定剧情走向:
label confession_scene: hero "我有话想对你说..."
if hero_affection >= AFFECTION_HIGH: jump good_ending elif hero_affection >= AFFECTION_MEDIUM: jump normal_ending else: jump bad_ending
label good_ending: "(好感度高:成功告白)" hero "其实我一直喜欢你..." "幸福的结局!" return
label normal_ending: "(好感度中等:保持朋友关系)" hero "我们还是做朋友比较好..." "平淡但真实的结局。" return
label bad_ending: "(好感度低:被拒绝)" hero "对不起,我不能接受你的感情..." "遗憾的结局。" return多分支剧情管理
对于复杂的剧情网络,可以使用标签(label)和跳转(jump)来组织:
label story_branch_point: "根据你与hero的关系,故事将走向不同方向..."
if hero_affection >= 80: jump romantic_route elif hero_affection >= 50: jump friendship_route else: jump stranger_route
label romantic_route: "浪漫路线:解锁特殊对话和事件" hero "最近我总是在想你..." jump route_ending
label friendship_route: "友情路线: focus on 共同兴趣和互助" hero "作为朋友,你总是很可靠" jump route_ending
label stranger_route: "陌生人路线:关系疏远,互动有限" hero "我们还是保持距离比较好" jump route_ending
label route_ending: "各路线的收尾剧情" return动态剧情触发
结合好感度和游戏状态创建更复杂的触发条件:
init python: def check_special_event(): """检查是否满足特殊事件触发条件""" return (hero_affection >= 70 and day_count >= 7 and not special_event_seen)
label daily_event: $ day_count += 1
if check_special_event(): $ special_event_seen = True jump special_date_event else: jump normal_daily_event
label special_date_event: "(特殊事件:好感度高且游戏时间满足条件)" hero "今天是个特别的日子..." return四、好感度数值显示UI
基础好感度界面设计
使用screen语句创建好感度显示界面:
# 好感度状态显示界面screen affection_display(): frame: xalign 0.95 yalign 0.05 xpadding 10 ypadding 10
vbox: text "角色状态" size 20 null height 10
text "Hero: [hero_affection]/100" bar value hero_affection range 100 xmaximum 150
null height 5
text "Rival1: [rival1_affection]/100" bar value rival1_affection range 100 xmaximum 150
text "Rival2: [rival2_affection]/100" bar value rival2_affection range 100 xmaximum 150实时更新好感度数值
在游戏过程中显示当前好感度状态:
# 在对话中显示好感度变化label show_affection_change: $ old_affection = hero_affection $ hero_affection = modify_affection(hero_affection, 10) $ change = hero_affection - old_affection
if change > 0: "Hero好感度 +[change]" elif change < 0: "Hero好感度 [change]"
show screen affection_display "(按任意键继续...)" hide screen affection_display可视化好感度状态
使用图标和颜色增强视觉效果:
# 增强版好感度显示界面screen affection_display_enhanced(): frame: xalign 0.95 yalign 0.05 background None
vbox: # Hero状态 hbox: text "❤️ Hero:" text "[hero_affection]" if hero_affection >= 80: text "❤️❤️❤️" color "#ff6b6b" elif hero_affection >= 50: text "❤️❤️" color "#ffaa00" else: text "❤️" color "#cccccc"
bar value hero_affection range 100 xmaximum 200 style "affection_bar"
null height 10
# Rival1状态 hbox: text "💛 Rival1:" text "[rival1_affection]"
bar value rival1_affection range 100 xmaximum 200 style "affection_bar"
null height 10
# Rival2状态 hbox: text "💙 Rival2:" text "[rival2_affection]"
bar value rival2_affection range 100 xmaximum 200 style "affection_bar"
# 好感度进度条样式style affection_bar: left_bar Frame("gui/affection_left.png", 10, 10) right_bar Frame("gui/affection_right.png", 10, 10) ysize 20好感度历史记录界面
创建专门的好感度查看界面:
# 好感度详情查看界面screen affection_details(): tag menu
use game_menu(_("角色好感度详情"), scroll="viewport"):
vbox: # Hero详细信息 frame: vbox: label "Hero 好感度详情" null height 10 text "当前数值: [hero_affection]/100" text "关系等级: [get_affection_level(hero_affection)]" null height 5 text "最近变化记录:" text "• 第[day_count-3]天: +5 (送礼物)" text "• 第[day_count-1]天: -2 (争吵)"
null height 20
# 其他角色信息...
init python: def get_affection_level(value): """根据好感度数值返回关系等级描述""" if value >= 90: return "❤️ 深爱" elif value >= 70: return "💕 喜欢你" elif value >= 50: return "😊 好朋友" elif value >= 30: return "😐 普通朋友" else: return "😔 陌生人"
# 在游戏中调用好感度详情界面label check_affection: show screen affection_details "查看角色好感度详情..." hide screen affection_details文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
用Ren'Py制作恋爱模拟器:好感度系统怎么写?代码+UI全解析
https://www.kshare.top/posts/用renpy制作恋爱模拟器好感度系统怎么写代码ui全/