-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucky_farmer_GUI.py
528 lines (465 loc) · 17.6 KB
/
lucky_farmer_GUI.py
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import flet as ft
import random
# 定义植物
class Plant:
name = ""
temperature = (0, 0)
light = (0, 0)
water = (0, 0)
def __init__(self, name, temperature, light, water) -> None:
self.name = name
self.temperature = temperature
self.light = light
self.water = water
plants = [
Plant(name="Corn", temperature=(20, 30), light=(40, 999), water=(60, 999)),
Plant(name="Wheat", temperature=(15, 30), light=(70, 999), water=(50, 999)),
Plant(name="Banana", temperature=(22, 35), light=(60, 999), water=(50, 999)),
Plant(name="Carrot", temperature=(0, 20), light=(40, 999), water=(60, 999)),
Plant(name="Potato", temperature=(5, 25), light=(70, 999), water=(40, 999)),
Plant(name="Apple", temperature=(10, 20), light=(80, 999), water=(60, 999)),
Plant(name="Sugar Cane", temperature=(20, 30), light=(60, 999), water=(80, 999)),
Plant(name="Mushroom", temperature=(15, 25), light=(10, 30), water=(80, 999)),
]
# 定义场景
class Season:
name = ""
temperature = (0, 0)
weathers = {}
def __init__(self, name, temperature, weathers) -> None:
self.name = name
self.temperature = temperature
self.weathers = weathers
day = 0
class Weather:
name = ""
light = (0, 0)
water = (0, 0)
def __init__(self, name, light, water) -> None:
self.name = name
self.light = light
self.water = water
seasons = [
Season(
name="spring",
temperature=(10, 25),
weathers={
Weather(name="Sunny", light=(80, 80), water=(10, 10)),
Weather(name="Cloudy", light=(50, 50), water=(20, 20)),
Weather(name="Light Rain", light=(40, 40), water=(40, 40)),
Weather(name="Heavy Rain", light=(40, 40), water=(70, 70)),
},
),
Season(
name="summer",
temperature=(25, 38),
weathers={
Weather(name="Sunny", light=(100, 100), water=(0, 0)),
Weather(name="Cloudy", light=(70, 70), water=(10, 10)),
Weather(name="Light Rain", light=(50, 50), water=(30, 30)),
Weather(name="Heavy Rain", light=(50, 50), water=(90, 90)),
},
),
Season(
name="autumn",
temperature=(10, 25),
weathers={
Weather(name="Sunny", light=(70, 70), water=(10, 10)),
Weather(name="Cloudy", light=(40, 40), water=(10, 10)),
Weather(name="Light Rain", light=(30, 30), water=(30, 30)),
Weather(name="Heavy Rain", light=(30, 30), water=(60, 60)),
},
),
Season(
name="winter",
temperature=(-5, 10),
weathers={
Weather(name="Sunny", light=(50, 50), water=(0, 0)),
Weather(name="Cloudy", light=(40, 40), water=(20, 20)),
Weather(name="Light Rain", light=(20, 20), water=(40, 40)),
Weather(name="Heavy Rain", light=(20, 20), water=(50, 50)),
},
),
]
class Land:
plant = None
plant_points = 0
plant_status = 0
display = None
# 更改所有作物数值的值都在这里,只能改数值,不能重新创建组件
def __init__(self, plant) -> None:
self.plant = plant
self.plant_points = 0
if plant != None:
self.display = ft.Row(
[
ft.Text(self.plant.name),
ft.Text(self.plant_points),
ft.ProgressBar(width=100, value=0),
]
)
else:
self.display = ft.Row(
[
ft.Text("❌ Empty"),
ft.Text(self.plant_points),
ft.ProgressBar(width=100, value=0),
]
)
# 种植作物
def grow_crops(self, plant):
self.plant = plant
self.display.controls[0].value = self.plant.name
# 收获
def harvest(self):
self.plant_status = 1
self.display.controls[0].value = "✅ " + self.plant.name
print("✅Harvest")
# 清除
def clear(self):
self.plant = None
self.plant_points = 0
self.plant_status = 0
self.display.controls[1].value = 0
self.display.controls[0].value = "❌ Empty"
self.display.controls[2].value = 0
# 成长
def grow(self, temperature, water, light, player):
# 计算玩家道具效果
for buff in player.buffs:
if buff.name == "Grow light":
light += 20
if buff.name == "Water Pump":
water += 20
if buff.name == "Greenhouse":
temperature += 5
if buff.name == "Cold wave":
temperature -= 5
if buff.name == "Drought":
water -= 10
if buff.name == "Cloudy":
water -= 10
self.plant_points += calc_grow(self.plant, temperature, water, light)
self.display.controls[1].value = self.plant_points
self.display.controls[2].value = self.plant_points / 10
# 定义道具
class Prop:
name = ""
duration = 0
def __init__(self, name, duration) -> None:
self.name = name
self.duration = duration
self.display = ft.Row(
[
ft.Text(self.name),
ft.Text(self.duration),
]
)
def use(self, player):
self.duration -= 1
self.display.controls[1].value = self.duration
print(self.name, self.duration)
# 结束清除道具
if self.duration == 0:
player.buffs.remove(self)
player.buffs_column.controls.remove(self.display)
def apply(self, player):
player.buffs.append(self)
self.display.controls[1].value = self.duration
@classmethod
def create_copy(cls, original_prop):
# Create a new instance of Prop with the same attributes
return cls(name=original_prop.name, duration=original_prop.duration)
props = [
Prop(
name="Greenhouse",
duration=15,
),
Prop(
name="Water Pump",
duration=15,
),
Prop(
name="Grow light",
duration=15,
),
Prop(
name="Cold wave",
duration=15,
),
Prop(
name="Drought",
duration=15,
),
Prop(
name="Cloudy",
duration=15,
),
]
def calc_grow(plant, temperature, water, light):
point = 0
# print(plant.name)
if temperature in range(plant.temperature[0], plant.temperature[1] + 1):
point += 2
print("T2",end="")
elif temperature in range(plant.temperature[0]-3, plant.temperature[1] +4):
point += 1
print("T1",end="")
if water in range(plant.water[0], plant.water[1] + 1):
point += 1
print("W1",end="")
if light in range(plant.light[0], plant.light[1] + 1):
point += 1
print("L1",end="")
return point
def main(page: ft.Page):
# 主要的计算逻辑(需要与全部事情关联)
def new_day(*e):
global season, weather, temperature, water, light, day
day += 1
season = seasons[(day - 1) // 10 % 4]
weather = random.choice(list(season.weathers))
# 试图改进温度算法,波动更小
if day % 10 == 1:
temperature = random.randint(season.temperature[0], season.temperature[1])
while 1:
temperature1 = random.randint(temperature-4, temperature+4)
if temperature1 in range(season.temperature[0], season.temperature[1]):
temperature = temperature1
break
# temperature = random.randint(season.temperature[0], season.temperature[1])
water = random.randint(weather.water[0], weather.water[1])
light = random.randint(weather.light[0], weather.light[1])
# 输出数值
# card container column
info_card.content.content.controls = [
ft.Text("📅Day: {}".format(day) ,size=30),
ft.Text("🌏Season: {}".format(season.name)),
ft.Text("📆Date: {}".format(10 if day % 10 == 0 else day % 10)),
ft.Text("🌈Weather: {}".format(weather.name)),
ft.Text("🌡️Temperature: {}".format(temperature)),
ft.Text("💡Light: {}".format(light)),
ft.Text("💧Water: {}".format(water)),
ft.Divider(),
]
print("Day: {}".format(day))
# 植物计算与输出
for plant in plants:
point = calc_grow(plant, temperature, water, light)
info_card.content.content.controls.append(
ft.Text("{}: {} points".format(plant.name, point))
)
print(" {}: {} points".format(plant.name, point))
# 道具计算与输出
# 下一天按钮
info_card.content.content.controls.append(
ft.ElevatedButton(
text="Next Day", on_click=new_day, icon=ft.icons.BEDTIME_ROUNDED
),
)
# 计算玩家土地生长情况
for player in players:
for land in player.lands:
if land.plant == None:
continue
# 生长
if land.plant_status == 0:
land.grow(temperature, water, light, player)
# 收获
if land.plant_status == 1:
land.clear()
# 更改植物状态
if land.plant_points >= 10:
land.harvest()
for buff in player.buffs:
buff.use(player)
page.update()
page.update()
# 场景信息卡
info_card = ft.Card(
content=ft.Container(
content=ft.Column(
[
ft.ElevatedButton(
text="Start", on_click=new_day, icon=ft.icons.PLAY_ARROW_ROUNDED
),
],
),
width=400,
height=600,
padding=15,
)
)
# 定义玩家
# 基本上全部操作都在这里
# 函数有: 打开种子弹窗、使用种子、购买土地
class Player:
playername = ""
lands = []
buffs = []
player_card = None
def __init__(self, playername, land) -> None:
self.playername = playername
self.lands = [land]
self.buffs = []
# 定义地皮展示
self.lands_column = ft.Column(spacing=0)
for l in self.lands:
self.lands_column.controls.append(l.display)
# 定义道具显示
self.buffs_column = ft.Column(spacing=0,wrap=True,height=60)
for b in self.buffs:
self.buffs_column.controls.append(b.display)
# 玩家界面展示(静态)
self.player_card = ft.Card(
content=ft.Container(
content=ft.Row(
[
ft.Column(
[
ft.Text(playername),
# 显示土地
ft.Row([ft.Text("Lands: "),self.lands_column,]),
# 显示道具
ft.Row([ft.Text("Buffs: "),self.buffs_column]),
],
width=300,
spacing=0,
),
ft.Column(
[
ft.OutlinedButton(
"Plant", on_click=self.open_seed_popups
),
ft.OutlinedButton("Use prop",on_click=self.open_prop_popups),
ft.OutlinedButton("Buy land",on_click=self.menuitem_buy_land),
],
),
],
),
padding=10,
),
width=500,
height=140,
)
# 种植弹窗
self.grow_popups = ft.AlertDialog(
title=ft.Text("Select Land & Seed "),
content=ft.Text("Please select your land and seed to grow."),
actions=[
ft.Dropdown(
width=50,
options=[ft.dropdown.Option("1"), ft.dropdown.Option("2")],
),
ft.Dropdown(
width=150,
options=[],
),
ft.TextButton("Grow", on_click=self.menuitem_plant),
],
actions_alignment=ft.MainAxisAlignment.END,
)
# 种植弹窗添加植物
for plant in plants:
self.grow_popups.actions[1].options.append(ft.dropdown.Option(plant.name))
self.player_card.content.content.controls.append(self.grow_popups)
# 选择道具弹窗
self.prop_popups = ft.AlertDialog(
title=ft.Text("Select Player & Prop "),
content=ft.Text("Please select your prop player to use."),
actions=[
ft.Dropdown(
width=50,
options=[ft.dropdown.Option("1"), ft.dropdown.Option("2"),ft.dropdown.Option("3"),ft.dropdown.Option("4")],
),
ft.Dropdown(
width=150,
options=[],
),
ft.TextButton("Use", on_click=self.menuitem_use),
],
actions_alignment=ft.MainAxisAlignment.END,
)
for prop in props:
self.prop_popups.actions[1].options.append(ft.dropdown.Option(prop.name))
self.player_card.content.content.controls.append(self.prop_popups)
# 打开选择种子窗口
def open_seed_popups(self, e):
self.grow_popups.open = True
page.update()
def open_prop_popups(self, e):
self.prop_popups.open = True
page.update()
# 种植
def menuitem_plant(self, e):
# land_num = int(input("Input land number: (1-{}): ".format(len(self.lands))))
land_num = int(self.grow_popups.actions[0].value)
for i in range(len(plants)):
if plants[i].name == self.grow_popups.actions[1].value:
plant_num = i
break
if land_num > len(self.lands):
print("🚫 Invalid land number!")
return
if self.lands[land_num - 1].plant == None:
# plant_num = int(input("Input plant: (1-{}): ".format(len(plants))))
# self.lands[land_num - 1].plant = plants[plant_num - 1]
self.lands[land_num - 1].grow_crops(plants[plant_num])
self.grow_popups.open = False
page.update()
else:
print("🚫 Land is not empty!")
return
# 使用道具
def menuitem_use(self, e):
# prop_num = int(input("Input prop number (1-{}): ".format(len(props))))
for i in range(len(props)):
if props[i].name == self.prop_popups.actions[1].value:
prop_num = i
break
# print("🚫 Invalid prop number!")
# return
player_num = int(self.prop_popups.actions[0].value)
players[player_num - 1].get_buff(Prop.create_copy(props[prop_num]))
self.prop_popups.open = False
page.update()
def get_buff(self, prop):
self.buffs.append(prop)
self.buffs_column.controls.append(self.buffs[-1].display)
page.update()
# 购买土地
def menuitem_buy_land(self, e):
print("===Buy Land===")
if len(self.lands) >= 2:
print("Land is full!")
return
else:
# confirm = input("Buy land? Have enough money? (y/n): ")
# if confirm == "y":
self.lands.append(Land(None))
self.lands_column.controls.append(self.lands[-1].display)
page.update()
print("✅ Buy Land successfully!")
# 添加玩家
players = []
players.append(Player("Player 1", Land(None)))
players.append(Player("Player 2", Land(None)))
players.append(Player("Player 3", Land(None)))
players.append(Player("Player 4", Land(None)))
players_card = ft.Column([])
for player in players:
players_card.controls.append(player.player_card)
# page.add(color_dropdown, submit_btn, output_text)
# 主界面布局
page.title = "Lucky Farmer"
page.add(
ft.Row(
[
info_card,
ft.VerticalDivider(),
players_card,
]
)
)
page.update()
ft.app(target=main)