From 11ae86c5309fd8bef9b20f26b737c6400228698d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Wed, 19 Feb 2025 22:25:32 -0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++++++- "\345\237\272\346\234\254.ul" | 42 ++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ff4f538..55a18fb 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,13 @@ $ 木兰 基本.ul 第15步:狼:11;羊:73;草:11909; 第16步:狼:11;羊:99;草:23766; 第17步:狼:12;羊:117;草:23746; -``` \ No newline at end of file +``` + +## 待做 + +- 暂停运行后观察个体 +- 根据条件暂停运行 + +## 参考 + +https://stackoverflow.com/questions/11552320/correct-way-to-pause-a-python-program diff --git "a/\345\237\272\346\234\254.ul" "b/\345\237\272\346\234\254.ul" index 8494af5..a030334 100644 --- "a/\345\237\272\346\234\254.ul" +++ "b/\345\237\272\346\234\254.ul" @@ -8,15 +8,6 @@ random.seed(datetime.now().timestamp()) type 生物 { - func $繁衍 { - if $能量 >= $繁衍需能 { - $能量 = 初始能量 - 后代 = $__new__($__class__) - 后代.__init__($x + random.randint(-20, 20), $y + random.randint(-20, 20)) - return 后代 - } - } - func $生物(x=nil, y=nil) { super() $速度上限 = 0.0 @@ -32,6 +23,24 @@ type 生物 { $食物种类 = nil } + func $观察 { + 情况 = {:} + 情况['种类'] = $__class__.__name__ + 情况['活着'] = $活着 + 情况['能量'] = $能量 + 情况['目标'] = $目标.观察() if $目标 + return 情况 + } + + func $繁衍 { + if $能量 >= $繁衍需能 { + $能量 = 初始能量 + 后代 = $__new__($__class__) + 后代.__init__($x + random.randint(-20, 20), $y + random.randint(-20, 20)) + return 后代 + } + } + func $代谢(所有生物表) { 食物 = 所有生物表.__contains__($食物种类) ? 所有生物表[$食物种类] : [] if ($__class__ == 草) { @@ -139,7 +148,9 @@ func 按类整理(所有生物) { } func main() { - 所有生物 = list(map(i -> 狼(), range(10))) + list(map(i -> 羊(), range(40))) + list(map(i -> 草(), range(100))) + 所有生物 = list(map(i -> 狼(), range(10))) + + list(map(i -> 羊(), range(40))) + + list(map(i -> 草(), range(100))) 时间 = 0 while (时间 < 终了) { 所有生物表 = 按类整理(所有生物) @@ -147,6 +158,17 @@ func main() { a.代谢(所有生物表) for a in 所有生物 + while true { + 输入 = input("请问:") + if 输入 == "能狼" { + for 某狼 in 所有生物表[狼] { + println(某狼.观察()) if 某狼.能量 > 0 + } + } elif 输入 == "" { + break + } + } + 所有生物 = list(filter(某物 -> 某物.活着, 所有生物)) for p in 所有生物 { 后代 = p.繁衍() -- Gitee From 2d8efb0d27841be4f37d9baf2d552c6237341fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Thu, 20 Feb 2025 17:11:22 -0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BE=8B=E5=AD=90=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + "\345\237\272\346\234\254.ul" | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 55a18fb..4695a11 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ $ 木兰 基本.ul ## 待做 - 暂停运行后观察个体 + - 控制台快速切换输入 上下历史 - 根据条件暂停运行 ## 参考 diff --git "a/\345\237\272\346\234\254.ul" "b/\345\237\272\346\234\254.ul" index a030334..eb9c94a 100644 --- "a/\345\237\272\346\234\254.ul" +++ "b/\345\237\272\346\234\254.ul" @@ -27,6 +27,7 @@ type 生物 { 情况 = {:} 情况['种类'] = $__class__.__name__ 情况['活着'] = $活着 + 情况['速度'] = math.sqrt($dx ^ 2 + $dy ^ 2) 情况['能量'] = $能量 情况['目标'] = $目标.观察() if $目标 return 情况 @@ -109,7 +110,7 @@ type 狼 : 生物 { func $狼(x=nil, y=nil) { super() $食物种类 = 羊 - $速度上限 = 2.5 + $速度上限 = 10 $繁衍需能 = 1 } } @@ -147,6 +148,10 @@ func 按类整理(所有生物) { return 所有生物表 } +func 距离(甲: 生物, 乙: 生物) { + return (甲.x - 乙.x) ^ 2 + (甲.y - 乙.y) ^ 2 +} + func main() { 所有生物 = list(map(i -> 狼(), range(10))) + list(map(i -> 羊(), range(40))) + @@ -158,12 +163,23 @@ func main() { a.代谢(所有生物表) for a in 所有生物 + 某狼 = nil while true { 输入 = input("请问:") if 输入 == "能狼" { for 某狼 in 所有生物表[狼] { println(某狼.观察()) if 某狼.能量 > 0 } + } elif 输入 == "某狼" { + println(某狼.观察()) if 某狼 + } elif 输入 == "狼距目标" { + 某狼 = 所有生物表[狼][0] if #所有生物表[狼] > 0 and !某狼 + println(距离(某狼, 某狼.目标)) + } elif 输入 == "狼速加倍" { + if 某狼 { + 某狼.dx = 某狼.dx * 2 + 某狼.dy = 某狼.dy * 2 + } } elif 输入 == "" { break } -- Gitee From 1b630d6828af3e03e8fb8bfb1845e796ed2ddbd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Thu, 20 Feb 2025 17:36:07 -0800 Subject: [PATCH 3/6] =?UTF-8?q?=E9=87=8D=E6=9E=84.=20=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=AF=BC=E8=87=B4=E4=BB=A5=E7=B1=BB=E4=B8=BA?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E7=B4=A2=E5=BC=95=E4=B8=8D=E5=8F=AF=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\272\244\344\272\222.ul" | 32 ++++ "\345\237\272\346\234\254.ul" | 162 +----------------- ...37\347\211\251\347\247\215\347\261\273.ul" | 136 +++++++++++++++ "\351\227\256\351\242\230.md" | 18 ++ 4 files changed, 191 insertions(+), 157 deletions(-) create mode 100644 "\344\272\244\344\272\222.ul" create mode 100644 "\347\224\237\347\211\251\347\247\215\347\261\273.ul" create mode 100644 "\351\227\256\351\242\230.md" diff --git "a/\344\272\244\344\272\222.ul" "b/\344\272\244\344\272\222.ul" new file mode 100644 index 0000000..5dd4ce7 --- /dev/null +++ "b/\344\272\244\344\272\222.ul" @@ -0,0 +1,32 @@ +using * in 生物种类 + +type 交互 { + + func $交互() { + super() + $某狼 = nil + } + + func $进行(所有生物表) { + while true { + 输入 = input("请问:") + if 输入 == "能狼" { + for $某狼 in 所有生物表[狼] { + println($某狼.观察()) if $某狼.能量 > 0 + } + } elif 输入 == "某狼" { + println($某狼.观察()) if $某狼 + } elif 输入 == "狼距目标" { + $某狼 = 所有生物表[狼][0] if #所有生物表[狼] > 0 and !$某狼 + println(距离($某狼, $某狼.目标)) + } elif 输入 == "狼速加倍" { + if $某狼 { + $某狼.dx = $某狼.dx * 2 + $某狼.dy = $某狼.dy * 2 + } + } elif 输入 == "" { + break + } + } + } +} \ No newline at end of file diff --git "a/\345\237\272\346\234\254.ul" "b/\345\237\272\346\234\254.ul" index eb9c94a..4fef514 100644 --- "a/\345\237\272\346\234\254.ul" +++ "b/\345\237\272\346\234\254.ul" @@ -1,139 +1,7 @@ -using math -using random -using datetime in datetime -random.seed(datetime.now().timestamp()) -地图宽度, 地图高度 = 1440, 900 -终了 = 18 -初始能量 = 0 - -type 生物 { - - func $生物(x=nil, y=nil) { - super() - $速度上限 = 0.0 - // 位置 - $x = x ? x : random.randint(0, 地图宽度) - $y = y ? y : random.randint(0, 地图高度) - // 两方向速度 - $dx, $dy = 0, 0 - $活着 = true - $目标 = nil - $能量 = 初始能量 - $繁衍需能 = 0 - $食物种类 = nil - } - - func $观察 { - 情况 = {:} - 情况['种类'] = $__class__.__name__ - 情况['活着'] = $活着 - 情况['速度'] = math.sqrt($dx ^ 2 + $dy ^ 2) - 情况['能量'] = $能量 - 情况['目标'] = $目标.观察() if $目标 - return 情况 - } - - func $繁衍 { - if $能量 >= $繁衍需能 { - $能量 = 初始能量 - 后代 = $__new__($__class__) - 后代.__init__($x + random.randint(-20, 20), $y + random.randint(-20, 20)) - return 后代 - } - } - - func $代谢(所有生物表) { - 食物 = 所有生物表.__contains__($食物种类) ? 所有生物表[$食物种类] : [] - if ($__class__ == 草) { - $能量 = $能量 + 1 - return - } - if ($目标 and (!$目标.活着)) { - $目标 = nil - } - if $目标 { - 直线距离 = ($x - $目标.x) ^ 2 + ($y - $目标.y) ^ 2 - if (直线距离 < 400) { - $目标.活着 = false - $能量 = $能量 + 1 - } - } else { - $选取目标(食物) - } - $更新位置() - } - - func $选取目标(食物) { - 最小距离 = 9999999 - 最近目标 = nil - for 目标 in 食物 { - if ((目标 !== self) and 目标.活着) { - 目标距离 = ($x - 目标.x) ^ 2 + ($y - 目标.y) ^ 2 - if (目标距离 < 最小距离) { - 最小距离 = 目标距离 - 最近目标 = 目标 - } - } - } - if (最小距离 < 100000) { - $目标 = 最近目标 - } - } - - func $更新位置 { - fx = 0 - fy = 0 - if $目标 { - fx += 0.1 * ($目标.x - $x) - fy += 0.1 * ($目标.y - $y) - } - $dx = $dx + 0.05 * fx - $dy = $dy + 0.05 * fy - 速度 = math.sqrt($dx ^ 2 + $dy ^ 2) - if (速度 > $速度上限) { - $dx = $dx / 速度 * $速度上限 - $dy = $dy / 速度 * $速度上限 - } - $x = $x + $dx - $y = $y + $dy - - $x = max($x, 0) - $x = min($x, 地图宽度) - $y = max($y, 0) - $y = min($y, 地图高度) - } -} - - -type 狼 : 生物 { - - func $狼(x=nil, y=nil) { - super() - $食物种类 = 羊 - $速度上限 = 10 - $繁衍需能 = 1 - } -} - - -type 羊 : 生物 { +using 交互 in 交互 +using * in 生物种类 - func $羊(x=nil, y=nil) { - super() - $食物种类 = 草 - $速度上限 = 2 - $繁衍需能 = 1 - } -} - - -type 草 : 生物 { - - func $草(x=nil, y=nil) { - super() - $繁衍需能 = 2 - } -} +终了 = 18 func 按类整理(所有生物) { 所有生物表 = {:} @@ -156,6 +24,7 @@ func main() { 所有生物 = list(map(i -> 狼(), range(10))) + list(map(i -> 羊(), range(40))) + list(map(i -> 草(), range(100))) + 交互平台 = 交互() 时间 = 0 while (时间 < 终了) { 所有生物表 = 按类整理(所有生物) @@ -163,28 +32,7 @@ func main() { a.代谢(所有生物表) for a in 所有生物 - 某狼 = nil - while true { - 输入 = input("请问:") - if 输入 == "能狼" { - for 某狼 in 所有生物表[狼] { - println(某狼.观察()) if 某狼.能量 > 0 - } - } elif 输入 == "某狼" { - println(某狼.观察()) if 某狼 - } elif 输入 == "狼距目标" { - 某狼 = 所有生物表[狼][0] if #所有生物表[狼] > 0 and !某狼 - println(距离(某狼, 某狼.目标)) - } elif 输入 == "狼速加倍" { - if 某狼 { - 某狼.dx = 某狼.dx * 2 - 某狼.dy = 某狼.dy * 2 - } - } elif 输入 == "" { - break - } - } - + 交互平台.进行(所有生物表) 所有生物 = list(filter(某物 -> 某物.活着, 所有生物)) for p in 所有生物 { 后代 = p.繁衍() diff --git "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" new file mode 100644 index 0000000..0cad759 --- /dev/null +++ "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" @@ -0,0 +1,136 @@ +using datetime in datetime +using math +using random +random.seed(datetime.now().timestamp()) + +地图宽度, 地图高度 = 1440, 900 +初始能量 = 0 + +type 生物 { + + func $生物(x=nil, y=nil) { + super() + $速度上限 = 0.0 + // 位置 + $x = x ? x : random.randint(0, 地图宽度) + $y = y ? y : random.randint(0, 地图高度) + // 两方向速度 + $dx, $dy = 0, 0 + $活着 = true + $目标 = nil + $能量 = 初始能量 + $繁衍需能 = 0 + $食物种类 = nil + } + + func $观察 { + 情况 = {:} + 情况['种类'] = $__class__.__name__ + 情况['活着'] = $活着 + 情况['速度'] = math.sqrt($dx ^ 2 + $dy ^ 2) + 情况['能量'] = $能量 + 情况['目标'] = $目标.观察() if $目标 + return 情况 + } + + func $繁衍 { + if $能量 >= $繁衍需能 { + $能量 = 初始能量 + 后代 = $__new__($__class__) + 后代.__init__($x + random.randint(-20, 20), $y + random.randint(-20, 20)) + return 后代 + } + } + + func $代谢(所有生物表) { + 食物 = 所有生物表.__contains__($食物种类) ? 所有生物表[$食物种类] : [] + if ($__class__ == 草) { + $能量 = $能量 + 1 + return + } + if ($目标 and (!$目标.活着)) { + $目标 = nil + } + if $目标 { + 直线距离 = ($x - $目标.x) ^ 2 + ($y - $目标.y) ^ 2 + if (直线距离 < 400) { + $目标.活着 = false + $能量 = $能量 + 1 + } + } else { + $选取目标(食物) + } + $更新位置() + } + + func $选取目标(食物) { + 最小距离 = 9999999 + 最近目标 = nil + for 目标 in 食物 { + if ((目标 !== self) and 目标.活着) { + 目标距离 = ($x - 目标.x) ^ 2 + ($y - 目标.y) ^ 2 + if (目标距离 < 最小距离) { + 最小距离 = 目标距离 + 最近目标 = 目标 + } + } + } + if (最小距离 < 100000) { + $目标 = 最近目标 + } + } + + func $更新位置 { + fx = 0 + fy = 0 + if $目标 { + fx += 0.1 * ($目标.x - $x) + fy += 0.1 * ($目标.y - $y) + } + $dx = $dx + 0.05 * fx + $dy = $dy + 0.05 * fy + 速度 = math.sqrt($dx ^ 2 + $dy ^ 2) + if (速度 > $速度上限) { + $dx = $dx / 速度 * $速度上限 + $dy = $dy / 速度 * $速度上限 + } + $x = $x + $dx + $y = $y + $dy + + $x = max($x, 0) + $x = min($x, 地图宽度) + $y = max($y, 0) + $y = min($y, 地图高度) + } +} + + +type 狼 : 生物 { + + func $狼(x=nil, y=nil) { + super() + $食物种类 = 羊 + $速度上限 = 10 + $繁衍需能 = 1 + } +} + + +type 羊 : 生物 { + + func $羊(x=nil, y=nil) { + super() + $食物种类 = 草 + $速度上限 = 2 + $繁衍需能 = 1 + } +} + + +type 草 : 生物 { + + func $草(x=nil, y=nil) { + super() + $繁衍需能 = 2 + } +} \ No newline at end of file diff --git "a/\351\227\256\351\242\230.md" "b/\351\227\256\351\242\230.md" new file mode 100644 index 0000000..b648a2e --- /dev/null +++ "b/\351\227\256\351\242\230.md" @@ -0,0 +1,18 @@ +## 语言工具局限 + +` 😰 字典中不存在此键:` +应该由此导致:https://zhuanlan.zhihu.com/p/350337192 + +## 报错信息改进 + + func $进行(所有生物表) { + + + 交互平台 = 交互() +。。。 + 交互.进行(所有生物表) + +类调用 - 应该实例调用 + + 😰 类型错误:进行() missing 1 required positional argument: '所有生物表' + -- Gitee From f28d69ac193c4c0608123f47f215dd9e554adab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Thu, 20 Feb 2025 18:01:38 -0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=BE=BE=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\272\244\344\272\222.ul" | 8 ++-- "\345\237\272\346\234\254.ul" | 8 +--- ...37\347\211\251\347\247\215\347\261\273.ul" | 16 +++++-- "\351\227\256\351\242\230.md" | 45 +++++++++++++++++++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git "a/\344\272\244\344\272\222.ul" "b/\344\272\244\344\272\222.ul" index 5dd4ce7..aeee94e 100644 --- "a/\344\272\244\344\272\222.ul" +++ "b/\344\272\244\344\272\222.ul" @@ -11,14 +11,14 @@ type 交互 { while true { 输入 = input("请问:") if 输入 == "能狼" { - for $某狼 in 所有生物表[狼] { + for $某狼 in 所有生物表[狼.__name__] { println($某狼.观察()) if $某狼.能量 > 0 } } elif 输入 == "某狼" { - println($某狼.观察()) if $某狼 + $某狼 = 所有生物表[狼.__name__][0] if #所有生物表[狼.__name__] > 0 and !$某狼 + println($某狼.观察()) } elif 输入 == "狼距目标" { - $某狼 = 所有生物表[狼][0] if #所有生物表[狼] > 0 and !$某狼 - println(距离($某狼, $某狼.目标)) + println($某狼.目标 ? $某狼.距离($某狼.目标) : "无目标") if $某狼 } elif 输入 == "狼速加倍" { if $某狼 { $某狼.dx = $某狼.dx * 2 diff --git "a/\345\237\272\346\234\254.ul" "b/\345\237\272\346\234\254.ul" index 4fef514..1dddedf 100644 --- "a/\345\237\272\346\234\254.ul" +++ "b/\345\237\272\346\234\254.ul" @@ -6,7 +6,7 @@ using * in 生物种类 func 按类整理(所有生物) { 所有生物表 = {:} for 生物 in 所有生物 { - 类型 = 生物.__class__ + 类型 = 生物.__class__.__name__ if 所有生物表.__contains__(类型) { 所有生物表[类型].append(生物) } else { @@ -16,10 +16,6 @@ func 按类整理(所有生物) { return 所有生物表 } -func 距离(甲: 生物, 乙: 生物) { - return (甲.x - 乙.x) ^ 2 + (甲.y - 乙.y) ^ 2 -} - func main() { 所有生物 = list(map(i -> 狼(), range(10))) + list(map(i -> 羊(), range(40))) + @@ -28,7 +24,7 @@ func main() { 时间 = 0 while (时间 < 终了) { 所有生物表 = 按类整理(所有生物) - print("第`时间`步:"); print("`类型.__name__`:`#所有生物表[类型]`;") for 类型 in 所有生物表; println() + print("第`时间`步:"); print("`类型`:`#所有生物表[类型]`;") for 类型 in 所有生物表; println() a.代谢(所有生物表) for a in 所有生物 diff --git "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" index 0cad759..c9178f5 100644 --- "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" +++ "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" @@ -42,8 +42,14 @@ type 生物 { } } + func $所有潜在食物(所有生物表) { + return [] if !$食物种类 + 食物种类名 = $食物种类.__name__ + return 所有生物表.__contains__(食物种类名) ? 所有生物表[食物种类名] : [] + } + func $代谢(所有生物表) { - 食物 = 所有生物表.__contains__($食物种类) ? 所有生物表[$食物种类] : [] + 食物 = $所有潜在食物(所有生物表) if ($__class__ == 草) { $能量 = $能量 + 1 return @@ -52,7 +58,7 @@ type 生物 { $目标 = nil } if $目标 { - 直线距离 = ($x - $目标.x) ^ 2 + ($y - $目标.y) ^ 2 + 直线距离 = $距离($目标) if (直线距离 < 400) { $目标.活着 = false $能量 = $能量 + 1 @@ -68,7 +74,7 @@ type 生物 { 最近目标 = nil for 目标 in 食物 { if ((目标 !== self) and 目标.活着) { - 目标距离 = ($x - 目标.x) ^ 2 + ($y - 目标.y) ^ 2 + 目标距离 = $距离(目标) if (目标距离 < 最小距离) { 最小距离 = 目标距离 最近目标 = 目标 @@ -80,6 +86,10 @@ type 生物 { } } + func $距离(目标) { + return ($x - 目标.x) ^ 2 + ($y - 目标.y) ^ 2 + } + func $更新位置 { fx = 0 fy = 0 diff --git "a/\351\227\256\351\242\230.md" "b/\351\227\256\351\242\230.md" index b648a2e..67c98d8 100644 --- "a/\351\227\256\351\242\230.md" +++ "b/\351\227\256\351\242\230.md" @@ -5,6 +5,8 @@ ## 报错信息改进 +### 错误的类方法调用 + func $进行(所有生物表) { @@ -16,3 +18,46 @@ 😰 类型错误:进行() missing 1 required positional argument: '所有生物表' +### + +```python +print("`类型.__name__`:`#所有生物表[类型]`;") for 类型 in 所有生物表 +``` + +`类型` 已为字符串: + +``` +第0步:Traceback (most recent call last): + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/中.py", line 190, in 中 + exec(可执行码, 环境变量) + File "基本.ul", line 45, in + main() + File "基本.ul", line 1, in main + using 交互 in 交互 +AttributeError: 'str' object has no attribute '__name__' + +During handling of the above exception, another exception occurred: + +Traceback (most recent call last): + File "/Users/xuanwu/opt/anaconda3/bin/木兰", line 8, in + sys.exit(中()) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/中.py", line 199, in 中 + sys.stderr.write('%s\n' % 反馈信息(e, 源码文件)) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 28, in 反馈信息 + return 提神符 + "\n".join(中文化(例外, 源码文件)) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 40, in 中文化 + 关键 = 取关键信息(提示(类型, 原信息), 行信息[0]) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 81, in 取关键信息 + 问题变量 = 诊断无属性问题(节点, 问题属性) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 130, in 诊断无属性问题 + 所有变量 += 诊断无属性问题(属性[1], 问题属性) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 115, in 诊断无属性问题 + 所有变量 += 诊断无属性问题(子节点, 问题属性) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 130, in 诊断无属性问题 + 所有变量 += 诊断无属性问题(属性[1], 问题属性) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 115, in 诊断无属性问题 + 所有变量 += 诊断无属性问题(子节点, 问题属性) + File "/Users/xuanwu/opt/anaconda3/lib/python3.8/site-packages/木兰/功用/反馈信息.py", line 130, in 诊断无属性问题 + 所有变量 += 诊断无属性问题(属性[1], 问题属性) +TypeError: 'NoneType' object is not iterable +``` \ No newline at end of file -- Gitee From 94eb42783841aab069dc4a121ef1bc1406eca553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Thu, 20 Feb 2025 22:52:10 -0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\345\237\272\346\234\254.ul" | 35 ++++++++++++++++-- ...37\347\211\251\347\247\215\347\261\273.ul" | 36 +++++++++++++++---- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git "a/\345\237\272\346\234\254.ul" "b/\345\237\272\346\234\254.ul" index 1dddedf..f5f3f0c 100644 --- "a/\345\237\272\346\234\254.ul" +++ "b/\345\237\272\346\234\254.ul" @@ -1,3 +1,6 @@ +using pygame +using K_ESCAPE, KEYDOWN in pygame.locals + using 交互 in 交互 using * in 生物种类 @@ -16,26 +19,54 @@ func 按类整理(所有生物) { return 所有生物表 } +显示 = true + func main() { + if 显示 { + pygame.init() + clock = pygame.time.Clock() + screen = pygame.display.set_mode(tuple(地图宽度, 地图高度)) + } 所有生物 = list(map(i -> 狼(), range(10))) + list(map(i -> 羊(), range(40))) + list(map(i -> 草(), range(100))) 交互平台 = 交互() 时间 = 0 - while (时间 < 终了) { + 跑着 = true + while 时间 < 终了 and 跑着 { 所有生物表 = 按类整理(所有生物) print("第`时间`步:"); print("`类型`:`#所有生物表[类型]`;") for 类型 in 所有生物表; println() a.代谢(所有生物表) for a in 所有生物 - 交互平台.进行(所有生物表) + if 显示 { + for 事件 in pygame.event.get() { + if (事件.__getattribute__('type') == pygame.QUIT) { + 跑着 = false + } + if (事件.__getattribute__('type') == KEYDOWN) { + 跑着 = false if 事件.key == K_ESCAPE + } + } + screen.fill(tuple(11, 11, 11)) + a.显示(screen) for a in 所有生物 + } else { + 交互平台.进行(所有生物表) + } + 所有生物 = list(filter(某物 -> 某物.活着, 所有生物)) for p in 所有生物 { 后代 = p.繁衍() 所有生物.append(后代) if 后代 } + + if 显示 { + pygame.display.flip() + clock.tick(24) + } 时间 = 时间 + 1 } + pygame.quit() if 显示 } if (__name__ == '__main__') { main() diff --git "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" index c9178f5..2704ce9 100644 --- "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" +++ "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" @@ -1,14 +1,15 @@ using datetime in datetime using math +using pygame using random random.seed(datetime.now().timestamp()) 地图宽度, 地图高度 = 1440, 900 初始能量 = 0 -type 生物 { +type 生物 : pygame.sprite.Sprite { - func $生物(x=nil, y=nil) { + func $生物(个头, 颜色, x=nil, y=nil) { super() $速度上限 = 0.0 // 位置 @@ -21,6 +22,13 @@ type 生物 { $能量 = 初始能量 $繁衍需能 = 0 $食物种类 = nil + + // 待做:不需显示时可否略过 + $surf = pygame.Surface(tuple(2 * 个头, 2 * 个头), pygame.SRCALPHA, 32) + pygame.draw.circle($surf, 颜色, tuple(个头, 个头), 个头) + $rect = $surf.get_rect() + $rect.centerx = int($x) + $rect.centery = int($y) } func $观察 { @@ -48,6 +56,16 @@ type 生物 { return 所有生物表.__contains__(食物种类名) ? 所有生物表[食物种类名] : [] } + func $显示(screen) { + if ($__class__ == 草) { + screen.blit($surf, $rect) + } else { + $rect.centerx = int($x) + $rect.centery = int($y) + screen.blit($surf, $rect) + } + } + func $代谢(所有生物表) { 食物 = $所有潜在食物(所有生物表) if ($__class__ == 草) { @@ -118,7 +136,9 @@ type 生物 { type 狼 : 生物 { func $狼(x=nil, y=nil) { - super() + 个头 = 4 + 颜色 = tuple(255, 0, 0) + super(个头, 颜色) $食物种类 = 羊 $速度上限 = 10 $繁衍需能 = 1 @@ -129,7 +149,9 @@ type 狼 : 生物 { type 羊 : 生物 { func $羊(x=nil, y=nil) { - super() + 个头 = 3 + 颜色 = tuple(255, 255, 255) + super(个头, 颜色) $食物种类 = 草 $速度上限 = 2 $繁衍需能 = 1 @@ -140,7 +162,9 @@ type 羊 : 生物 { type 草 : 生物 { func $草(x=nil, y=nil) { - super() - $繁衍需能 = 2 + 个头 = 2 + 颜色 = tuple(0, 128, 0) + super(个头, 颜色) + $繁衍需能 = 3 } } \ No newline at end of file -- Gitee From 279c3595014675af3b77cb50523cd87d48e73c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Fri, 21 Feb 2025 10:05:30 -0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=8F=90=E5=87=BApygame=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\345\237\272\346\234\254.ul" | 35 +++------- "\346\230\276\347\244\272.ul" | 67 +++++++++++++++++++ ...37\347\211\251\347\247\215\347\261\273.ul" | 34 ++-------- 3 files changed, 82 insertions(+), 54 deletions(-) create mode 100644 "\346\230\276\347\244\272.ul" diff --git "a/\345\237\272\346\234\254.ul" "b/\345\237\272\346\234\254.ul" index f5f3f0c..12e95d2 100644 --- "a/\345\237\272\346\234\254.ul" +++ "b/\345\237\272\346\234\254.ul" @@ -1,8 +1,6 @@ -using pygame -using K_ESCAPE, KEYDOWN in pygame.locals - using 交互 in 交互 using * in 生物种类 +using * in 显示 终了 = 18 @@ -22,34 +20,25 @@ func 按类整理(所有生物) { 显示 = true func main() { - if 显示 { - pygame.init() - clock = pygame.time.Clock() - screen = pygame.display.set_mode(tuple(地图宽度, 地图高度)) - } + 屏幕 = 生物显示.初始化(地图宽度, 地图高度) if 显示 所有生物 = list(map(i -> 狼(), range(10))) + list(map(i -> 羊(), range(40))) + list(map(i -> 草(), range(100))) 交互平台 = 交互() 时间 = 0 - 跑着 = true - while 时间 < 终了 and 跑着 { + 运行 = true + while 时间 < 终了 and 运行 { 所有生物表 = 按类整理(所有生物) print("第`时间`步:"); print("`类型`:`#所有生物表[类型]`;") for 类型 in 所有生物表; println() a.代谢(所有生物表) for a in 所有生物 if 显示 { - for 事件 in pygame.event.get() { - if (事件.__getattribute__('type') == pygame.QUIT) { - 跑着 = false - } - if (事件.__getattribute__('type') == KEYDOWN) { - 跑着 = false if 事件.key == K_ESCAPE - } - } - screen.fill(tuple(11, 11, 11)) - a.显示(screen) for a in 所有生物 + 运行 = 生物显示.探测退出事件() + 屏幕.fill(tuple(11, 11, 11)) + + 生物显示(a).显示(屏幕) for a in 所有生物 + 生物显示.刷新() } else { 交互平台.进行(所有生物表) } @@ -60,13 +49,9 @@ func main() { 所有生物.append(后代) if 后代 } - if 显示 { - pygame.display.flip() - clock.tick(24) - } 时间 = 时间 + 1 } - pygame.quit() if 显示 + 生物显示.结束() if 显示 } if (__name__ == '__main__') { main() diff --git "a/\346\230\276\347\244\272.ul" "b/\346\230\276\347\244\272.ul" new file mode 100644 index 0000000..5d45889 --- /dev/null +++ "b/\346\230\276\347\244\272.ul" @@ -0,0 +1,67 @@ +using pygame +using K_ESCAPE, KEYDOWN in pygame.locals + +type 生物显示 : pygame.sprite.Sprite { + + { + pygame.init() + clock = pygame.time.Clock() + } + + func 初始化(地图宽度, 地图高度) { + return pygame.display.set_mode(tuple(地图宽度, 地图高度)) + } + + func 探测退出事件 { + for 事件 in pygame.event.get() { + return false if (事件.__getattribute__('type') == pygame.QUIT) + + if (事件.__getattribute__('type') == KEYDOWN) { + return false if 事件.key == K_ESCAPE + } + } + return true + } + + func 刷新 { + pygame.display.flip() + 生物显示.clock.tick(24) + } + + func 结束 { + pygame.quit() + } + + func $生物显示(生物) { + super() + $生物 = 生物 + 种类 = 生物.__class__.__name__ + if 种类 == '狼' { + 个头 = 4 + 颜色 = tuple(255, 0, 0) + } elif 种类 == '羊' { + 个头 = 3 + 颜色 = tuple(255, 255, 255) + } elif 种类 == '草' { + 个头 = 2 + 颜色 = tuple(0, 128, 0) + } + + $surf = pygame.Surface(tuple(2 * 个头, 2 * 个头), pygame.SRCALPHA, 32) + pygame.draw.circle($surf, 颜色, tuple(个头, 个头), 个头) + $rect = $surf.get_rect() + $rect.centerx = int(生物.x) + $rect.centery = int(生物.y) + } + + func $显示(screen) { + if ($生物.__class__.__name__ == '草') { + screen.blit($surf, $rect) + } else { + $rect.centerx = int($生物.x) + $rect.centery = int($生物.y) + screen.blit($surf, $rect) + } + } + +} \ No newline at end of file diff --git "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" index 2704ce9..d5e0958 100644 --- "a/\347\224\237\347\211\251\347\247\215\347\261\273.ul" +++ "b/\347\224\237\347\211\251\347\247\215\347\261\273.ul" @@ -1,15 +1,14 @@ using datetime in datetime using math -using pygame using random random.seed(datetime.now().timestamp()) 地图宽度, 地图高度 = 1440, 900 初始能量 = 0 -type 生物 : pygame.sprite.Sprite { +type 生物 { - func $生物(个头, 颜色, x=nil, y=nil) { + func $生物(x=nil, y=nil) { super() $速度上限 = 0.0 // 位置 @@ -22,13 +21,6 @@ type 生物 : pygame.sprite.Sprite { $能量 = 初始能量 $繁衍需能 = 0 $食物种类 = nil - - // 待做:不需显示时可否略过 - $surf = pygame.Surface(tuple(2 * 个头, 2 * 个头), pygame.SRCALPHA, 32) - pygame.draw.circle($surf, 颜色, tuple(个头, 个头), 个头) - $rect = $surf.get_rect() - $rect.centerx = int($x) - $rect.centery = int($y) } func $观察 { @@ -56,16 +48,6 @@ type 生物 : pygame.sprite.Sprite { return 所有生物表.__contains__(食物种类名) ? 所有生物表[食物种类名] : [] } - func $显示(screen) { - if ($__class__ == 草) { - screen.blit($surf, $rect) - } else { - $rect.centerx = int($x) - $rect.centery = int($y) - screen.blit($surf, $rect) - } - } - func $代谢(所有生物表) { 食物 = $所有潜在食物(所有生物表) if ($__class__ == 草) { @@ -136,9 +118,7 @@ type 生物 : pygame.sprite.Sprite { type 狼 : 生物 { func $狼(x=nil, y=nil) { - 个头 = 4 - 颜色 = tuple(255, 0, 0) - super(个头, 颜色) + super() $食物种类 = 羊 $速度上限 = 10 $繁衍需能 = 1 @@ -149,9 +129,7 @@ type 狼 : 生物 { type 羊 : 生物 { func $羊(x=nil, y=nil) { - 个头 = 3 - 颜色 = tuple(255, 255, 255) - super(个头, 颜色) + super() $食物种类 = 草 $速度上限 = 2 $繁衍需能 = 1 @@ -162,9 +140,7 @@ type 羊 : 生物 { type 草 : 生物 { func $草(x=nil, y=nil) { - 个头 = 2 - 颜色 = tuple(0, 128, 0) - super(个头, 颜色) + super() $繁衍需能 = 3 } } \ No newline at end of file -- Gitee