diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..1f6e2ef --- /dev/null +++ b/colors.py @@ -0,0 +1,8 @@ +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +GREEN = (0, 255, 0) +RED = (255, 0, 0) +BROWN = (140, 95, 65) +DBROWN = (65, 50, 20) +YELLOW = (255, 255, 0) +BLUE = (0, 0, 255) diff --git a/field.py b/field.py new file mode 100644 index 0000000..977a16b --- /dev/null +++ b/field.py @@ -0,0 +1,43 @@ +import pygame +from colors import * +from dimensions import * + +class Field(pygame.sprite.Sprite): + def __init__(self, row, column): + super(Field, self).__init__() + self.surf = pygame.Surface((WIDTH, HEIGHT)) + self.surf.fill(BROWN) + self.rect = self.surf.get_rect( + topleft=((MARGIN + WIDTH) * row + MARGIN, (MARGIN + HEIGHT) * column + MARGIN)) + self.position = [row, column] + self.hydration = 0 + self.isRocky = False + self.planted = 0 + self.ferility = 1 + + def hydrate(self): + if self.hydration <= 3: + self.hydration += 1 + if self.hydration == 0: + self.surf.fill(BROWN) + if self.hydration == 1: + self.surf.fill(YELLOW) + if self.hydration == 2: + self.surf.fill(GREEN) + if self.hydration == 3: + self.surf.fill(BLUE) + + def dehydrate(self): + if self.hydration > 0: + self.hydration -= 1 + if self.hydration == 0: + self.surf.fill(BROWN) + if self.hydration == 1: + self.surf.fill(YELLOW) + if self.hydration == 2: + self.surf.fill(GREEN) + if self.hydration == 3: + self.surf.fill(BLUE) + + def free(self): + self.planted = 0 diff --git a/plant.py b/plant.py new file mode 100644 index 0000000..d297b5d --- /dev/null +++ b/plant.py @@ -0,0 +1,44 @@ +import pygame +from colors import * +from dimensions import * +from sprites import * + +class Plant(pygame.sprite.Sprite): + def __init__(self, field, species): + super(Plant, self).__init__() + self.surf = plant_img_0 + self.position = field.position + self.field = field + self.rect = self.surf.get_rect( + topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN)) + self.growth = 0 + self.isHealthy = True + self.species = species + if self.species == "beetroot": + self.growth_speed = 1.5 + self.humidity_needed = 2 + elif self.species == "wheat": + self.growth_speed = 1 + self.humidity_needed = 1 + elif self.species == "cotton": + self.growth_speed = 0.8 + self.humidity_needed = 1 + + + def grow(self): + if self.field.hydration >= self.humidity_needed: + self.growth += self.growth_speed + if self.field.hydration == 0: + self.growth -= self.growth_speed + if self.growth > 4: + self.growth = 4 + if self.growth < 0: + self.growth = 0 + if self.growth == 0: + self.surf = plant_img_0 + elif self.growth == 1: + self.surf = plant_img_1 + elif self.growth == 2: + self.surf = plant_img_2 + elif self.growth == 3: + self.surf = plant_img_3 diff --git a/sprites.py b/sprites.py new file mode 100644 index 0000000..fd86f3e --- /dev/null +++ b/sprites.py @@ -0,0 +1,11 @@ +import pygame +import random +import os + +# set up asset folders +project_folder = os.path.dirname(__file__) +sprites_folder = os.path.join(project_folder, 'sprites') +plant_img_0 = pygame.image.load(os.path.join(sprites_folder, 'plant0.png')) +plant_img_1 = pygame.image.load(os.path.join(sprites_folder, 'plant1.png')) +plant_img_2 = pygame.image.load(os.path.join(sprites_folder, 'plant2.png')) +plant_img_3 = pygame.image.load(os.path.join(sprites_folder, 'plant3.png')) \ No newline at end of file diff --git a/tractor.py b/tractor.py new file mode 100644 index 0000000..3b6b1bf --- /dev/null +++ b/tractor.py @@ -0,0 +1,60 @@ +import pygame + +from pygame.locals import ( + K_UP, + K_DOWN, + K_LEFT, + K_RIGHT, + K_ESCAPE, + K_SPACE, + K_c, + KEYDOWN, + QUIT +) + +from dimensions import * +from colors import * + +class Tractor(pygame.sprite.Sprite): + def __init__(self, field): + super(Tractor, self).__init__() + self.surf = pygame.Surface((WIDTH, HEIGHT)) + self.surf.fill(RED) + self.position = field.position + self.rect = self.surf.get_rect( + topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN)) + self.field = field + + def update(self, pressed_keys): + if pressed_keys[K_UP]: + self.rect.move_ip(0, -(HEIGHT + MARGIN)) + self.position[1] -= 1 + if self.rect.top <= MARGIN: + self.rect.top = MARGIN + self.position[1] = 0 + if pressed_keys[K_DOWN]: + self.rect.move_ip(0, HEIGHT + MARGIN) + self.position[1] += 1 + if self.rect.bottom >= SCREEN_HEIGHT-MARGIN: + self.rect.bottom = SCREEN_HEIGHT-MARGIN + self.position[1] = GSIZE-1 + if pressed_keys[K_LEFT]: + self.rect.move_ip(-(WIDTH + MARGIN), 0) + self.position[0] -= 1 + if self.rect.left < MARGIN: + self.rect.left = MARGIN + self.position[0] = 0 + if pressed_keys[K_RIGHT]: + self.rect.move_ip(WIDTH + MARGIN, 0) + self.position[0] += 1 + if self.rect.right > SCREEN_WIDTH-MARGIN: + self.rect.right = SCREEN_WIDTH-MARGIN + self.position[0] = GSIZE-1 + + def hydrate(self, field, pressed_keys): + if pressed_keys[K_SPACE]: + field[self.position[0]][self.position[1]].hydrate() + + def cut(self, field, pressed_keys): + if pressed_keys[K_c]: + field[self.position[0]][self.position[1]].free()