Улучшил баланс. Корова идет к одному из ближайщих 3х растений. Учитывается так же размер растения. Если последняя корова умирает, то новая случайным образом спавнится.

This commit is contained in:
2026-06-17 17:46:47 +03:00
parent 1127cd9cb1
commit dc9a8d52ff
+15 -11
View File
@@ -19,6 +19,8 @@ class Cow(arcade.SpriteCircle):
self.age_to_bride = 3 self.age_to_bride = 3
self.health = 30 self.health = 30
self.speed = 5 self.speed = 5
self.move_target = 0
self.target_timer = 0
Cow.ls.append(self) Cow.ls.append(self)
@classmethod @classmethod
def update(cls): def update(cls):
@@ -26,6 +28,8 @@ class Cow(arcade.SpriteCircle):
c.age += 1 c.age += 1
if c.age >= 30: if c.age >= 30:
cls.ls.remove(c) cls.ls.remove(c)
if len(cls.ls) == 0:
Cow()
continue continue
hit_plant = arcade.check_for_collision_with_list(c,Plant.ls) hit_plant = arcade.check_for_collision_with_list(c,Plant.ls)
for p in hit_plant: for p in hit_plant:
@@ -35,10 +39,12 @@ class Cow(arcade.SpriteCircle):
c.health -= 30 c.health -= 30
Plant.ls.remove(p) Plant.ls.remove(p)
else: else:
near_plant = Plant.get_near(c.center_x,c.center_y) if c.target_timer <= 0:
if near_plant: c.move_target = Plant.get_near(c.center_x,c.center_y)
vec = cls.get_vector(c.center_x,near_plant.center_x, c.target_timer = 5
c.center_y,near_plant.center_y) if c.move_target:
vec = cls.get_vector(c.center_x,c.move_target.center_x,
c.center_y,c.move_target.center_y)
new_x = c.center_x + vec[0]*randint(0,c.speed) new_x = c.center_x + vec[0]*randint(0,c.speed)
new_y = c.center_y + vec[1]*randint(0,c.speed) new_y = c.center_y + vec[1]*randint(0,c.speed)
if 0 < new_x < SCREEN_SIZE[0] and 0 < new_y < SCREEN_SIZE[1]: if 0 < new_x < SCREEN_SIZE[0] and 0 < new_y < SCREEN_SIZE[1]:
@@ -79,14 +85,11 @@ class Plant(arcade.SpriteCircle):
Plant.ls.append(self) Plant.ls.append(self)
@classmethod @classmethod
def get_near(cls,x,y): def get_near(cls,x,y):
min_dist = SCREEN_SIZE[0] dist_list = list()
near_plant = None
for p in cls.ls: for p in cls.ls:
dist = ((p.center_x - x)**2 + (p.center_y - y)**2)**0.5 dist_list.append((p,((p.center_x - x)**2 + (p.center_y - y)**2)**0.5 - p.width ))
if dist < min_dist: dist_list.sort(key=lambda x: x[1])
near_plant = p return dist_list[randint(0,3)][0]
min_dist = dist
return near_plant
@classmethod @classmethod
def update(cls): def update(cls):
for p in cls.ls: for p in cls.ls:
@@ -121,6 +124,7 @@ class MainView(arcade.View):
def on_update(self, delta_time): def on_update(self, delta_time):
self.cur_time += delta_time self.cur_time += delta_time
if self.cur_time >= TICK_TIME: if self.cur_time >= TICK_TIME:
print(f'Total plants: {len(Plant.ls)}, total cows: {len(Cow.ls)}')
Plant.update() Plant.update()
Cow.update() Cow.update()
self.time += 1 self.time += 1