1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
| import tkinter as tk from tkinter import ttk, scrolledtext import random import threading import time from dataclasses import dataclass from typing import List, Dict import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure import numpy as np from datetime import datetime
@dataclass class Civilization: id: int alive: bool initial_development: int current_development: int def __str__(self): status = "存活" if self.alive else "灭绝" return f"文明{self.id}: {status}, 初始发达程度: {self.initial_development}, 当前发达程度: {self.current_development}"
@dataclass class SimulationEvent: timestamp: str event_type: str description: str civilizations_involved: List[int] def __str__(self): return f"[{self.timestamp}] {self.description}"
class CivilizationSimulator: def __init__(self, root): self.root = root self.root.title("文明模拟器") self.root.geometry("1200x800") self.civilizations: List[Civilization] = [] self.events: List[SimulationEvent] = [] self.running = False self.simulation_thread = None self.step_counter = 0 self.num_civilizations = tk.IntVar(value=10) self.time_interval = tk.DoubleVar(value=1.0) self.development_change_range = tk.IntVar(value=5) self.discovery_probability = tk.DoubleVar(value=0.1) self.advanced_destroy_probability = tk.DoubleVar(value=0.3) self.primitive_destroy_probability = tk.DoubleVar(value=0.05) self.merge_probability = tk.DoubleVar(value=0.2) self.merge_change_range = tk.IntVar(value=3) self.setup_ui() def setup_ui(self): main_frame = ttk.Frame(self.root) main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) control_frame = ttk.LabelFrame(main_frame, text="控制面板", padding=10) control_frame.pack(side=tk.LEFT, fill=tk.Y, padx=(0, 10)) ttk.Label(control_frame, text="文明总数:").pack(anchor=tk.W) ttk.Spinbox(control_frame, from_=2, to=100, textvariable=self.num_civilizations, width=15).pack(pady=2) ttk.Label(control_frame, text="时间间隔(秒):").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=0.1, to=10.0, increment=0.1, textvariable=self.time_interval, width=15).pack(pady=2) ttk.Label(control_frame, text="发达程度变化范围:").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=1, to=50, textvariable=self.development_change_range, width=15).pack(pady=2) ttk.Label(control_frame, text="发现概率:").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=0.01, to=1.0, increment=0.01, textvariable=self.discovery_probability, width=15).pack(pady=2) ttk.Label(control_frame, text="高级文明消灭概率:").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=0.01, to=1.0, increment=0.01, textvariable=self.advanced_destroy_probability, width=15).pack(pady=2) ttk.Label(control_frame, text="低级文明消灭概率:").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=0.01, to=1.0, increment=0.01, textvariable=self.primitive_destroy_probability, width=15).pack(pady=2) ttk.Label(control_frame, text="融合概率:").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=0.01, to=1.0, increment=0.01, textvariable=self.merge_probability, width=15).pack(pady=2) ttk.Label(control_frame, text="融合变化范围:").pack(anchor=tk.W, pady=(10,0)) ttk.Spinbox(control_frame, from_=1, to=20, textvariable=self.merge_change_range, width=15).pack(pady=2) ttk.Button(control_frame, text="初始化文明", command=self.initialize_civilizations).pack(pady=(20,5), fill=tk.X) ttk.Button(control_frame, text="开始模拟", command=self.start_simulation).pack(pady=5, fill=tk.X) ttk.Button(control_frame, text="停止模拟", command=self.stop_simulation).pack(pady=5, fill=tk.X) ttk.Button(control_frame, text="重置", command=self.reset_simulation).pack(pady=5, fill=tk.X) display_frame = ttk.Frame(main_frame) display_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True) self.notebook = ttk.Notebook(display_frame) self.notebook.pack(fill=tk.BOTH, expand=True) text_frame = ttk.Frame(self.notebook) self.notebook.add(text_frame, text="文明状态") self.text_output = scrolledtext.ScrolledText(text_frame, wrap=tk.WORD, width=60, height=30) self.text_output.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) events_frame = ttk.Frame(self.notebook) self.notebook.add(events_frame, text="事件记录") events_toolbar = ttk.Frame(events_frame) events_toolbar.pack(fill=tk.X, padx=10, pady=(10,5)) ttk.Button(events_toolbar, text="清空记录", command=self.clear_events).pack(side=tk.LEFT) ttk.Button(events_toolbar, text="导出记录", command=self.export_events).pack(side=tk.LEFT, padx=(10,0)) ttk.Label(events_toolbar, text="筛选事件:").pack(side=tk.LEFT, padx=(20,5)) self.event_filter = ttk.Combobox(events_toolbar, values=["全部", "灭绝事件", "融合事件", "发展变化"], width=12) self.event_filter.set("全部") self.event_filter.bind("<<ComboboxSelected>>", self.filter_events) self.event_filter.pack(side=tk.LEFT) self.events_output = scrolledtext.ScrolledText(events_frame, wrap=tk.WORD, width=60, height=25) self.events_output.pack(fill=tk.BOTH, expand=True, padx=10, pady=5) stats_frame = ttk.Frame(self.notebook) self.notebook.add(stats_frame, text="统计信息") self.stats_output = scrolledtext.ScrolledText(stats_frame, wrap=tk.WORD, width=60, height=30) self.stats_output.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) chart_frame = ttk.Frame(self.notebook) self.notebook.add(chart_frame, text="发达程度图表") self.fig = Figure(figsize=(8, 6), dpi=100) self.ax = self.fig.add_subplot(111) self.canvas = FigureCanvasTkAgg(self.fig, chart_frame) self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True) self.status_var = tk.StringVar(value="准备就绪") ttk.Label(main_frame, textvariable=self.status_var, relief=tk.SUNKEN).pack(side=tk.BOTTOM, fill=tk.X, pady=(10,0)) def initialize_civilizations(self): """初始化文明""" self.civilizations.clear() self.events.clear() self.step_counter = 0 num_civs = self.num_civilizations.get() for i in range(num_civs): initial_dev = random.randint(0, num_civs) civ = Civilization( id=i, alive=True, initial_development=initial_dev, current_development=initial_dev ) self.civilizations.append(civ) self.add_event("initialization", f"初始化了 {num_civs} 个文明", list(range(num_civs))) self.update_display() self.status_var.set(f"已初始化 {num_civs} 个文明") def start_simulation(self): """开始模拟""" if not self.civilizations: self.status_var.set("请先初始化文明") return if not self.running: self.running = True self.simulation_thread = threading.Thread(target=self.simulation_loop, daemon=True) self.simulation_thread.start() self.status_var.set("模拟运行中...") def stop_simulation(self): """停止模拟""" self.running = False self.status_var.set("模拟已停止") def reset_simulation(self): """重置模拟""" self.stop_simulation() self.civilizations.clear() self.events.clear() self.step_counter = 0 self.text_output.delete(1.0, tk.END) self.events_output.delete(1.0, tk.END) self.stats_output.delete(1.0, tk.END) self.ax.clear() self.canvas.draw() self.status_var.set("已重置") def simulation_loop(self): """模拟主循环""" while self.running: self.simulate_step() time.sleep(self.time_interval.get()) def simulate_step(self): """执行一步模拟""" self.step_counter += 1 alive_civs = [civ for civ in self.civilizations if civ.alive] if len(alive_civs) <= 1: self.running = False winner = alive_civs[0] if alive_civs else None if winner: self.add_event("simulation_end", f"模拟结束 - 文明{winner.id} 成为最后的幸存者!", [winner.id]) else: self.add_event("simulation_end", "模拟结束 - 所有文明都已灭绝", []) self.root.after(0, lambda: self.status_var.set("模拟结束")) return if self.step_counter % 10 == 1: alive_count = len(alive_civs) self.add_event("development", f"第{self.step_counter}步: {alive_count}个文明仍在发展", [civ.id for civ in alive_civs]) development_changes = [] for civ in alive_civs: old_dev = civ.current_development change = random.randint(-self.development_change_range.get(), self.development_change_range.get()) civ.current_development = max(0, civ.current_development + change) if abs(change) >= self.development_change_range.get() // 2: direction = "大幅提升" if change > 0 else "急剧衰退" development_changes.append(f"文明{civ.id}{direction}(变化{change:+d})") if development_changes: self.add_event("development", f"发展变化: {', '.join(development_changes)}", [civ.id for civ in alive_civs if abs(civ.current_development - civ.initial_development) >= self.development_change_range.get() // 2]) for i, civ1 in enumerate(alive_civs): for j, civ2 in enumerate(alive_civs): if i >= j or not civ1.alive or not civ2.alive: continue if random.random() < self.discovery_probability.get(): self.handle_civilization_encounter(civ1, civ2) self.root.after(0, self.update_display) def handle_civilization_encounter(self, civ1: Civilization, civ2: Civilization): """处理文明相遇""" if not civ1.alive or not civ2.alive: return if civ1.current_development > civ2.current_development: advanced, primitive = civ1, civ2 elif civ2.current_development > civ1.current_development: advanced, primitive = civ2, civ1 else: advanced, primitive = random.choice([(civ1, civ2), (civ2, civ1)]) encounter_roll = random.random() if encounter_roll < self.merge_probability.get(): self.merge_civilizations(advanced, primitive) elif encounter_roll < self.merge_probability.get() + self.advanced_destroy_probability.get(): primitive.alive = False self.add_event("extinction", f"高级文明{advanced.id}(发达程度{advanced.current_development}) 消灭了低级文明{primitive.id}(发达程度{primitive.current_development})", [advanced.id, primitive.id]) elif encounter_roll < (self.merge_probability.get() + self.advanced_destroy_probability.get() + self.primitive_destroy_probability.get()): advanced.alive = False self.add_event("extinction", f"低级文明{primitive.id}(发达程度{primitive.current_development}) 奇迹般地消灭了高级文明{advanced.id}(发达程度{advanced.current_development})", [primitive.id, advanced.id]) def merge_civilizations(self, civ1: Civilization, civ2: Civilization): """融合两个文明""" old_dev1, old_dev2 = civ1.current_development, civ2.current_development base_development = civ1.current_development + civ2.current_development change = random.randint(-self.merge_change_range.get(), self.merge_change_range.get()) new_development = max(0, base_development + change) if civ1.id < civ2.id: survivor, absorbed = civ1, civ2 else: survivor, absorbed = civ2, civ1 survivor.current_development = new_development absorbed.alive = False self.add_event("merge", f"文明{survivor.id}(发达程度{old_dev1}) 和文明{absorbed.id}(发达程度{old_dev2}) 融合成新文明{survivor.id}(发达程度{new_development}, 变化{change:+d})", [survivor.id, absorbed.id]) def add_event(self, event_type: str, description: str, civilizations_involved: List[int]): """添加事件记录""" timestamp = datetime.now().strftime("%H:%M:%S") event = SimulationEvent( timestamp=timestamp, event_type=event_type, description=description, civilizations_involved=civilizations_involved ) self.events.append(event) def update_events(): self.events_output.insert(tk.END, str(event) + "\n") self.events_output.see(tk.END) self.root.after(0, update_events) def clear_events(self): """清空事件记录""" self.events.clear() self.events_output.delete(1.0, tk.END) def export_events(self): """导出事件记录""" filename = f"civilization_events_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt" try: with open(filename, 'w', encoding='utf-8') as f: f.write("文明模拟器事件记录\n") f.write("=" * 50 + "\n\n") for event in self.events: f.write(str(event) + "\n") self.status_var.set(f"事件记录已导出到 {filename}") except Exception as e: self.status_var.set(f"导出失败: {str(e)}") def filter_events(self, event=None): """筛选事件显示""" filter_type = self.event_filter.get() self.events_output.delete(1.0, tk.END) for event in self.events: show_event = False if filter_type == "全部": show_event = True elif filter_type == "灭绝事件" and event.event_type == "extinction": show_event = True elif filter_type == "融合事件" and event.event_type == "merge": show_event = True elif filter_type == "发展变化" and event.event_type == "development": show_event = True if show_event: self.events_output.insert(tk.END, str(event) + "\n") def update_statistics(self): """更新统计信息""" self.stats_output.delete(1.0, tk.END) total_civs = len(self.civilizations) alive_civs = [civ for civ in self.civilizations if civ.alive] dead_civs = [civ for civ in self.civilizations if not civ.alive] self.stats_output.insert(tk.END, "=== 文明统计 ===\n") self.stats_output.insert(tk.END, f"总文明数: {total_civs}\n") self.stats_output.insert(tk.END, f"存活文明数: {len(alive_civs)}\n") self.stats_output.insert(tk.END, f"灭绝文明数: {len(dead_civs)}\n") self.stats_output.insert(tk.END, f"存活率: {len(alive_civs)/total_civs*100:.1f}%\n\n") if alive_civs: developments = [civ.current_development for civ in alive_civs] self.stats_output.insert(tk.END, "=== 存活文明发达程度统计 ===\n") self.stats_output.insert(tk.END, f"平均发达程度: {sum(developments)/len(developments):.1f}\n") self.stats_output.insert(tk.END, f"最高发达程度: {max(developments)} (文明{alive_civs[developments.index(max(developments))].id})\n") self.stats_output.insert(tk.END, f"最低发达程度: {min(developments)} (文明{alive_civs[developments.index(min(developments))].id})\n\n") extinction_events = [e for e in self.events if e.event_type == "extinction"] merge_events = [e for e in self.events if e.event_type == "merge"] self.stats_output.insert(tk.END, "=== 事件统计 ===\n") self.stats_output.insert(tk.END, f"总事件数: {len(self.events)}\n") self.stats_output.insert(tk.END, f"灭绝事件: {len(extinction_events)}\n") self.stats_output.insert(tk.END, f"融合事件: {len(merge_events)}\n") self.stats_output.insert(tk.END, f"模拟步数: {self.step_counter}\n\n") if self.events: civ_activity = {} for event in self.events: for civ_id in event.civilizations_involved: civ_activity[civ_id] = civ_activity.get(civ_id, 0) + 1 if civ_activity: most_active = max(civ_activity.items(), key=lambda x: x[1]) self.stats_output.insert(tk.END, f"最活跃文明: 文明{most_active[0]} (参与{most_active[1]}次事件)\n") if alive_civs: self.stats_output.insert(tk.END, "\n=== 存活文明详情 ===\n") for civ in sorted(alive_civs, key=lambda x: x.current_development, reverse=True): growth = civ.current_development - civ.initial_development growth_str = f"(成长{growth:+d})" if growth != 0 else "(无变化)" self.stats_output.insert(tk.END, f"文明{civ.id}: 当前{civ.current_development}, 初始{civ.initial_development} {growth_str}\n") def update_display(self): """更新显示""" self.text_output.delete(1.0, tk.END) alive_count = 0 for civ in self.civilizations: self.text_output.insert(tk.END, str(civ) + "\n") if civ.alive: alive_count += 1 self.text_output.insert(tk.END, f"\n存活文明数量: {alive_count}\n") self.update_chart() self.update_statistics() def update_chart(self): """更新图表显示""" self.ax.clear() alive_civs = [civ for civ in self.civilizations if civ.alive] dead_civs = [civ for civ in self.civilizations if not civ.alive] if alive_civs: alive_ids = [civ.id for civ in alive_civs] alive_developments = [civ.current_development for civ in alive_civs] self.ax.bar(alive_ids, alive_developments, color='green', alpha=0.7, label='存活文明') if dead_civs: dead_ids = [civ.id for civ in dead_civs] dead_developments = [civ.current_development for civ in dead_civs] self.ax.bar(dead_ids, dead_developments, color='red', alpha=0.7, label='灭绝文明') self.ax.set_xlabel('文明编号') self.ax.set_ylabel('发达程度') self.ax.set_title('文明发达程度分布') self.ax.legend() self.ax.grid(True, alpha=0.3) self.canvas.draw()
def main(): root = tk.Tk() app = CivilizationSimulator(root) root.mainloop()
if __name__ == "__main__": main()
|
Comments