added plants #12

Merged
s459307 merged 1 commits from plants into master 2022-03-25 06:52:48 +01:00
7 changed files with 137 additions and 7 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,7 @@
from enum import Enum
import pygame
import settings
import plant
class Type(Enum):
@ -17,6 +18,20 @@ class Type(Enum):
}[_type]
# Field Types START
class FieldType:
def __init__(self):
self.field = None
self.plowed = False
def plow(self):
self.plowed = True
def sow(self, seed: plant.Seeds):
if self.plowed:
self.field = plant.Seeds.seed_to_plant(seed)
class Object:
height = width = 0
name = 'default'
@ -29,7 +44,7 @@ class Object:
return pygame.transform.scale(img, (self.width, self.height))
class Tile(Object) :
class Tile(Object):
def __init__(self, name: str, _type: Type):
self.name = name
self.type = _type
@ -37,7 +52,7 @@ class Tile(Object) :
self.object = self.create()
class Block(Object) :
class Block(Object):
def __init__(self, name: str, _type: Type):
self.name = name
self.type = _type
@ -49,28 +64,33 @@ class Block(Object) :
class Field:
type = Type.DEFAULT
name = 'default'
def __init__(self):
self.tile = Tile(self.name, self.type)
self.block = Block(self.name + "_block", self.type)
self.field_type = FieldType()
def can_be_sown(self):
return self.type == Type.DEFAULT
# Tile Types START
class Dirt(Field):
type = Type.PLANT
name = 'dirt'
def __init__(self):
super(Dirt, self).__init__()
class Cobble(Field):
type = Type.SPECIAL
name = 'cobble'
def __init__(self):
super(Cobble, self).__init__()
class Grass(Field):
type = Type.DEFAULT
name = 'grass'
@ -85,3 +105,4 @@ class Sand(Field):
def __init__(self):
super(Sand, self).__init__()
# Tile Types END

View File

@ -44,6 +44,7 @@ def read_img(agent, fields):
window.blit(possibleFields['sand'].block.object, (0, 0))
elif current_field == possibleFields['cobble'].tile.object:
window.blit(possibleFields['cobble'].block.object, (0, 0))
pygame.display.update()
pygame.time.delay(2000)
common.set('state_imgShown', False)

108
plant.py Normal file
View File

@ -0,0 +1,108 @@
from enum import Enum
class PlantGrowthStages(Enum):
PLANTED = 0
GROWING = 1
UNRIPE = 2
RIPE = 3
@staticmethod
def can_be_harvested(_type):
return _type == PlantGrowthStages.RIPE
@staticmethod
def next_growth_type(_type):
return {
PlantGrowthStages.PLANTED: PlantGrowthStages.GROWING,
PlantGrowthStages.GROWING: PlantGrowthStages.UNRIPE,
PlantGrowthStages.UNRIPE: PlantGrowthStages.RIPE,
PlantGrowthStages.RIPE: PlantGrowthStages.RIPE,
}[_type]
class Plant:
tick_decrease_amount: 1
tick_increase_amount: 1
def __init__(self):
self.growth = PlantGrowthStages.PLANTED
self.canBeHarvested = False
self.water_level = 0
self.fertilizer_level = 0
self.growth_stage_level = 0
self.growth_stage_threshold = 10000
def grow(self):
self.growth_stage_level = 0
previous_growth = self.growth
self.growth = PlantGrowthStages.next_growth_type(self.growth)
if self.growth == previous_growth:
self.canBeHarvested = True
def is_watered(self):
return self.water_level > 0
def water(self):
self.water_level = 100
def is_fertilized(self):
return self.fertilizer_level > 0
def fertilize(self):
self.fertilizer_level = 1000
def tick(self):
self.water_level -= self.tick_decrease_amount
self.fertilizer_level -= self.tick_decrease_amount
self.growth_stage_level += self.tick_increase_amount
if self.growth_stage_threshold == self.growth_stage_level:
self.grow()
def harvest(self):
if PlantGrowthStages.can_be_harvested(self.growth):
return self
@staticmethod
def sow(seed):
return {
PlantGrowthStages.PLANTED: PlantGrowthStages.GROWING,
PlantGrowthStages.GROWING: PlantGrowthStages.UNRIPE,
PlantGrowthStages.UNRIPE: PlantGrowthStages.RIPE,
PlantGrowthStages.RIPE: PlantGrowthStages.RIPE,
}[seed]
class Carrot(Plant):
def __init__(self):
super(Carrot, self).__init__()
self.growth_stage_threshold = 5000
class Potato(Plant):
def __init__(self):
super(Potato, self).__init__()
class Wheat(Plant):
def __init__(self):
super(Wheat, self).__init__()
class Seeds(Enum):
Carrot = 0
Potato = 1
Wheat = 2
@staticmethod
def seed_to_plant(seed):
return {
Seeds.Carrot: Carrot(),
Seeds.Potato: Potato(),
Seeds.Wheat: Wheat(),
}[seed]