我最近为游戏Apple Clicker写成就系统,加上后整个程序的代码竟然超过2200行,窗口启动时间也很慢,主要是if...else...使用的太多。于是查了查缩短代码的方法,总结如下:
示例一
if get > 10 :
num = 1
else:
num = 0
可以简化为:
num = 1 if param > 10 else 0
示例二
if param > 10 :
num = 1
elif param == 10:
num = 2
else:
num = 0
可以简化为:
num = 1 if param > 10 else 2 if param == 10 else 0
多行语句可以用分号“; ”隔开。
示例三
这是成就系统的一小部分:
if apple_amount >= 1 and achievement_1_get == 0:
- achievement_1_on = 1
- else:
- achievement_1_on = 0
- if apple_amount >=