This commit is contained in:
Szymon Szczubkowski 2023-03-27 15:33:18 +02:00
commit e2e87dca79
49 changed files with 352 additions and 120 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.DS_Store
.idea
__pycache__

79
blocks.py Normal file
View File

@ -0,0 +1,79 @@
import pygame
import random
from pygame.math import Vector2
import soil
class Blocks:
def __init__(self, parent_screen,cell_size):
self.parent_screen = parent_screen
self.flower_image = pygame.image.load(r'resources/flower.png').convert_alpha()
self.flower_image = pygame.transform.scale(self.flower_image, (cell_size, cell_size))
self.stone_image = pygame.image.load(r'resources/stone.png').convert_alpha()
self.stone_image = pygame.transform.scale(self.stone_image, (cell_size, cell_size))
self.leaf_image = pygame.image.load(r'resources/dead.png').convert_alpha()
self.leaf_image = pygame.transform.scale(self.leaf_image, (cell_size, cell_size))
self.alive_leaf_image = pygame.image.load(r'resources/alive.png').convert_alpha()
self.alive_leaf_image = pygame.transform.scale(self.alive_leaf_image, (cell_size, cell_size))
self.fawn_seed_image = pygame.image.load(r'resources/fawn_seed.png').convert_alpha()
self.fawn_seed_image = pygame.transform.scale(self.fawn_seed_image, (cell_size, cell_size))
self.fawn_wheat_image = pygame.image.load(r'resources/fawn_wheat.png').convert_alpha()
self.fawn_wheat_image = pygame.transform.scale(self.fawn_wheat_image, (cell_size, cell_size))
self.soil = soil.Soil()
def locate_blocks(self, blocks_number, cell_number, body):
for i in range(blocks_number):
self.x = random.randint(0, cell_number-1)
self.y = random.randint(0, cell_number-1)
self.pos = [self.x,self.y]
body.append(self.pos)
#entire_block.update({self.x : 1}) # for now it may lay on each other,
#print(entire_block)
def place_blocks(self, parent_screen, cell_size, body, color): #drawing blocks
for block in body:
x = int(block[0] * cell_size)
y = int(block[1] * cell_size)
if color == 'leaf':
self.parent_screen.blit(self.leaf_image, (x, y))
if color == 'alive':
self.parent_screen.blit(self.alive_leaf_image, (x, y))
if color == 'stone':
self.parent_screen.blit(self.stone_image, (x, y))
if color == 'flower':
self.parent_screen.blit(self.flower_image, (x, y))
if color == 'fawn_seed':
self.parent_screen.blit(self.fawn_seed_image, (x, y))
if color == 'fawn_wheat':
self.parent_screen.blit(self.fawn_wheat_image, (x, y))
# if color == 'potato':
# pass
def locate_soil(self, name, acidity, irrigation, blocks_number):
# for block in blocks_number:
self.soil.set_name(name)
self.soil.set_irrigation(irrigation)
self.soil.set_acidity(acidity)
def get_soil_info(self):
return self.soil
def draw_lines(self, parent_screen, cell_size): # background lines
for i in range(1, 10):
pygame.draw.line(self.parent_screen, (228, 253, 227), (cell_size * i, 0), (cell_size * i, parent_screen), 1)
pygame.draw.line(self.parent_screen, (228, 253, 227), (0, cell_size * i), (parent_screen, cell_size * i), 1)

69
land.py Normal file
View File

@ -0,0 +1,69 @@
import pygame
import random
class Land:
def __init__(self, parent_screen, cell_size, cell_number, all_soil_body, irrigation):
self.parent_screen = parent_screen
self.cell_number = cell_number
self.cell_size = cell_size
self._irrigation = irrigation
self.all_soil_body = all_soil_body
self.grass_image = pygame.image.load(r'resources/grass.png').convert_alpha()
self.grass_image = pygame.transform.scale(self.grass_image, (cell_size, cell_size))
# self.bad_grass_image = pygame.image.load(r'resources/bad_grass.png').convert()
# self.bad_grass_image = pygame.transform.scale(self.bad_grass_image, (cell_size, cell_size))
self.black_earth_image = pygame.image.load(r'resources/grass.png').convert()
self.black_earth_image = pygame.transform.scale(self.black_earth_image, (cell_size, cell_size))
self.green_earth_image = pygame.image.load(r'resources/grass.png').convert()
self.green_earth_image = pygame.transform.scale(self.green_earth_image, (cell_size, cell_size))
self.fawn_soil_image = pygame.image.load(r'resources/fawn_soil.png').convert()
self.fawn_soil_image = pygame.transform.scale(self.fawn_soil_image, (cell_size, cell_size))
self.fen_soil_image = pygame.image.load(r'resources/grass.png').convert()
self.fen_soil_image = pygame.transform.scale(self.fen_soil_image, (cell_size, cell_size))
def locate_soil(self, soil_body): # finds free places(coordinates) for soil and adds them to soil_body[]
number_of_blocs_for_each_soil = 50
if number_of_blocs_for_each_soil > (self.cell_number * self.cell_number) // 4:
number_of_blocs_for_each_soil = (self.cell_number * self.cell_number) // 4
print('Number of soil blocks exceeds the number of fields!')
for i in range(number_of_blocs_for_each_soil): # can't be more than: (cell_number * cell_number) // soil_types
while True:
rand_x = random.randint(0, self.cell_number - 1) # to-check
rand_y = random.randint(0, self.cell_number - 1)
if [rand_x, rand_y] not in self.all_soil_body:
self.all_soil_body.append([rand_x, rand_y])
soil_body.append([rand_x, rand_y])
break
def place_soil(self, soil_body, soil_name):
for body in soil_body:
x = int(body[0] * self.cell_size)
y = int(body[1] * self.cell_size)
if soil_name == 'black_earth':
self.parent_screen.blit(self.black_earth_image, (x, y))
if soil_name == 'green_earth':
self.parent_screen.blit(self.green_earth_image, (x, y))
if soil_name == 'fawn_soil':
self.parent_screen.blit(self.fawn_soil_image, (x, y))
if soil_name == 'fen_soil':
self.parent_screen.blit(self.fen_soil_image, (x, y))
def set_and_place_block_of_grass(self, name):
for i in range(0, self.cell_number):
for k in range(0, self.cell_number):
if [k, i] not in self.all_soil_body:
x = int(k * self.cell_size)
y = int(i * self.cell_size)
if name == 'good':
self.parent_screen.blit(self.grass_image, (x, y))
if name == 'bad':
self.parent_screen.blit(self.bad_grass_image, (x, y))

205
main.py
View File

@ -1,158 +1,123 @@
import os
import pygame
import random
import land
import tractor
import blocks
from pygame.locals import *
from datetime import datetime
#main variables
class Configurations:
cell_size = 50
screen_size = 500
# def _init_(self):
class Lines:
def __init__(self, parent_scr):
self.parent_scr = parent_scr
def draw_lines(self): # background lines
conf = Configurations()
for i in range(1, 10):
pygame.draw.line(self.parent_scr, (228, 253, 227), (conf.cell_size * i, 0), (conf.cell_size * i, conf.screen_size), 1)
pygame.draw.line(self.parent_scr, (228, 253, 227), (0, conf.cell_size * i), (conf.screen_size, conf.cell_size * i), 1)
#pygame.display.flip()
class Tractor:
def __init__(self, parent_screen):
conf = Configurations()
self.parent_screen = parent_screen
self.image = pygame.image.load(r'resources/tractor.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (conf.cell_size, conf.cell_size+5))
#self.block = pygame.Rect(int(conf.cell_size), int(conf.cell_size), conf.cell_size, conf.cell_size)
self.x = conf.cell_size*2
self.y = conf.cell_size*2
self.angle = 0
self.direction = 'up'
def draw(self):
#self.parent_screen.fill((120, 120, 0)) # background color
#self.parent_screen.blit(self.block, (self.x, self.y))
self.parent_screen.blit(pygame.transform.rotate(self.image, self.angle), (self.x, self.y)) # rotate tractor
#pygame.display.flip() # updating screen
def move(self, direction):
conf = Configurations()
if direction == 'up':
self.y -= conf.cell_size
self.angle = 0
if direction == 'down':
self.y += conf.cell_size
self.angle = 180
if direction == 'left':
self.x -= conf.cell_size
self.angle = 90
if direction == 'right':
self.x += conf.cell_size
self.angle = 270
#self.draw()
def walk(self):
choice = ['up', 'down', 'left', 'right']
if self.x == 450:
choice.pop(3)
if self.x == 0:
choice.pop(2)
if self.y == 0:
choice.pop(0)
if self.y == 450:
choice.pop(1)
self.direction = random.choice(choice)
self.move(self.direction)
class Field:
def __init__(self, parent_screen):
self.parent_screen = parent_screen
self.block = pygame.image.load(r'resources/field.png').convert()
def place_field(self, field_matrix):
conf = Configurations()
for m, posY in enumerate(field_matrix):
for n, posX in enumerate(posY):
if field_matrix[m][n] == 1:
self.parent_screen.blit(self.block, (n * conf.cell_size, m * conf.cell_size))
#pygame.display.flip()
class Game:
field_matrix = [[0 for m in range(10)] for n in range(10)]
for i in range(10):
while True:
field_posX = random.randint(0, 9)
field_posY = random.randint(0, 9)
if field_matrix[field_posY][field_posX] == 0:
field_matrix[field_posY][field_posX] = 1
break
cell_size = 50
cell_number = 15 # horizontally
blocks_number = 15
def __init__(self):
self.dead_leaf_body = []
self.green_leaf_body = []
self.stone_body = []
self.flower_body = []
self.dead_grass_body = []
self.grass_body = []
self.fawn_seed_body = []
self.fawn_wheat_body = []
self.black_earth_body = []
self.green_earth_body = []
self.fawn_soil_body = []
self.fen_soil_body = []
self.allBodyPos = []
self.entire_block = {}
# initialize a window
pygame.init()
self.conf = Configurations()
# self.screenWidth = 500
# self.screenHeight = 500
self.surface = pygame.display.set_mode((self.conf.screen_size, self.conf.screen_size)) # initialize a window
# self.surface.fill((255, 255, 255)) # background color (overwritten by tractor)
self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number))
# finds places for every type soil and grass
self.black_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.black_earth.locate_soil(self.black_earth_body)
self.green_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.green_earth.locate_soil(self.green_earth_body)
self.fawn_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.fawn_soil.locate_soil(self.fawn_soil_body)
self.fen_soil = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.fen_soil.locate_soil(self.fen_soil_body)
self.grass = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.lines = Lines(self.surface)
self.field = Field(self.surface)
self.field.place_field(self.field_matrix)
self.blocks = blocks.Blocks(self.surface, self.cell_size)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.dead_leaf_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.stone_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body)
self.tractor = Tractor(self.surface)
# self.potato = blocks.Blocks(self.surface, self.cell_size)
# self.potato.locate_soil('black earth', 6, 1, [])
self.tractor = tractor.Tractor(self.surface, self.cell_size)
self.tractor.draw()
def run(self):
# print(self.potato.get_soil_info().get_name())
# print(self.potato.get_soil_info().get_acidity())
# print(self.potato.get_soil_info().get_irrigation())
running = True
clock = pygame.time.Clock()
last_time = datetime.now()
#self.lines.draw_lines()
# last_time = datetime.now()
while running:
clock.tick(60) # manual fps control not to overwork the computer
time_now = datetime.now()
# time_now = datetime.now()
for event in pygame.event.get():
if event.type == KEYDOWN:
if pygame.key.get_pressed()[K_ESCAPE]:
running = False
# in case we want to use keyboard
if pygame.key.get_pressed()[K_UP]:
self.tractor.move('up')
self.tractor.move('up', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_DOWN]:
self.tractor.move('down')
self.tractor.move('down', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_LEFT]:
self.tractor.move('left')
self.tractor.move('left', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_RIGHT]:
self.tractor.move('right')
self.tractor.move('right', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_SPACE]:
self.tractor.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size)
# self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size)
if pygame.key.get_pressed()[K_q]:
self.tractor.harvest(self.fawn_seed_body, self.fawn_wheat_body, self.cell_size)
self.tractor.put_seed(self.fawn_soil_body, self.fawn_seed_body, self.cell_size)
elif event.type == QUIT:
running = False
self.surface.fill((140, 203, 97)) # background color
self.field.place_field(self.field_matrix)
self.lines.draw_lines()
self.surface.fill((123, 56, 51)) # background color
self.grass.set_and_place_block_of_grass('good')
self.black_earth.place_soil(self.black_earth_body, 'black_earth')
self.green_earth.place_soil(self.green_earth_body, 'green_earth')
self.fawn_soil.place_soil(self.fawn_soil_body, 'fawn_soil')
self.fen_soil.place_soil(self.fen_soil_body, 'fen_soil')
#plants examples
self.blocks.place_blocks(self.surface, self.cell_size, self.dead_leaf_body, 'leaf')
self.blocks.place_blocks(self.surface, self.cell_size, self.green_leaf_body, 'alive')
self.blocks.place_blocks(self.surface, self.cell_size, self.stone_body, 'stone')
self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower')
#seeds
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_seed_body, 'fawn_seed')
#wheat
self.blocks.place_blocks(self.surface, self.cell_size, self.fawn_wheat_body, 'fawn_wheat')
self.tractor.draw()
pygame.display.update()
# if (time_now - last_time).total_seconds() > 1: # tractor moves every 1 sec
# last_time = datetime.now()
#self.tractor.walk()
#print(f'x, y = ({int(self.tractor.x / 50)}, {int(self.tractor.y / 50)})')
if __name__ == '__main__':

BIN
resources/.DS_Store vendored

Binary file not shown.

BIN
resources/1black_earth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

BIN
resources/alive.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 313 B

BIN
resources/bad_grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
resources/black_seed.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
resources/blossom.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 B

BIN
resources/bush.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 B

BIN
resources/dead.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 455 B

BIN
resources/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

BIN
resources/earth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
resources/earth2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
resources/fawn_seed.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
resources/fawn_soil.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
resources/fawn_wheat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
resources/fen_soil.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
resources/fen_soil2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
resources/fen_wheat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 577 B

BIN
resources/flower.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 448 B

BIN
resources/garden.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 B

BIN
resources/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 B

BIN
resources/grass4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 482 B

BIN
resources/grass5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

BIN
resources/grassTwo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
resources/ivy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

BIN
resources/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

BIN
resources/mashroom.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 385 B

BIN
resources/plant.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 B

BIN
resources/red.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 B

BIN
resources/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

BIN
resources/rip.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 139 KiB

BIN
resources/robot2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
resources/seed1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

BIN
resources/statue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

BIN
resources/stone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

BIN
resources/stone2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 B

BIN
resources/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 B

BIN
resources/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
resources/wheat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
resources/wheatFour.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
resources/wheatTwo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

23
soil.py Normal file
View File

@ -0,0 +1,23 @@
class Soil:
def __init__(self, name='', irrigation=-1, acidity=-1):
self._name = name
self._irrigation = irrigation
self._acidity = acidity
def get_name(self):
return self._name
def set_name(self, name):
self._name = name
def get_acidity(self):
return self._acidity
def set_acidity(self, acidity):
self._acidity = acidity
def get_irrigation(self):
return self._irrigation
def set_irrigation(self, irrigation):
self._irrigation = irrigation

93
tractor.py Normal file
View File

@ -0,0 +1,93 @@
import pygame
import random
class Tractor:
def __init__(self, parent_screen, cell_size):
self.parent_screen = parent_screen
self.lastVisitedBlocks = [] # tractor stores last 3 visited blocks
self.up = pygame.image.load(r'resources/up.png').convert_alpha()
self.down = pygame.image.load(r'resources/down.png').convert_alpha()
self.left = pygame.image.load(r'resources/left.png').convert_alpha()
self.right = pygame.image.load(r'resources/right.png').convert_alpha()
self.up = pygame.transform.scale(self.up, (cell_size+2, cell_size))
self.down = pygame.transform.scale(self.down, (cell_size, cell_size+2))
self.left = pygame.transform.scale(self.left, (cell_size+2, cell_size+2))
self.right = pygame.transform.scale(self.right, (cell_size+4, cell_size+1))
self.x = cell_size*2 # to-check: start pos may be written explicit
self.y = cell_size*2
#self.pos = Vector2(self.x, self.y)
self.angle = 0
self.direction = 'up'
self.image = self.down
self.step = 0
self.lastVisitedBlocks = [] # as tractor moves it stores last 3 coordinates
def draw(self):
self.parent_screen.blit(self.image, (self.x, self.y)) # rotate tractor
def move(self, direction, cell_size, cell_number):
if direction == 'up':
if self.y != 0:
self.y -= cell_size
self.image = self.up
if direction == 'down':
if self.y != (cell_number-1)*cell_size:
self.y += cell_size
self.image = self.down
if direction == 'left':
if self.x != 0:
self.x -= cell_size
self.image = self.left
if direction == 'right':
if self.x != (cell_number-1)*cell_size:
self.x += cell_size
self.image = self.right
self.step = self.step + 1
print(self.x, self.y)
print(self.step)
def water(self, body_before, body_after, cell_size):
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in body_before:
body_before.remove(self.pos)
body_after.append(self.pos)
print('HERE!')
#print(body)
def put_seed(self, body, seed_body, cell_size):
#self.step = 0
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in body:
#body.remove(self.pos)
seed_body.append(self.pos)
print('HERE IS THE SEED!')
def harvest(self, seed_body, wheat_body, cell_size):
self.pos = [self.x/cell_size, self.y/cell_size]
if self.pos in seed_body:
seed_body.remove(self.pos)
wheat_body.append(self.pos)
print('HERE IS THE WHEAT!')
def walk(self):
choice = ['up', 'down', 'left', 'right']
if self.x == 450:
choice.pop(3)
if self.x == 0:
choice.pop(2)
if self.y == 0:
choice.pop(0)
if self.y == 450:
choice.pop(1)
self.direction = random.choice(choice)
self.move(self.direction)