Prześlij pliki do ''

This commit is contained in:
Cezary Adamczak 2021-03-28 23:50:42 +02:00
parent 3e11cd8831
commit e18112aed2
5 changed files with 166 additions and 0 deletions

8
colors.py Normal file
View File

@ -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)

43
field.py Normal file
View File

@ -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

44
plant.py Normal file
View File

@ -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

11
sprites.py Normal file
View File

@ -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'))

60
tractor.py Normal file
View File

@ -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()