Add land methods and soil pictures

This commit is contained in:
unknown 2023-03-27 11:43:13 +02:00
parent 0793deb2cd
commit 25348047e8
16 changed files with 100 additions and 44 deletions

View File

@ -21,7 +21,6 @@ class Blocks:
self.soil = soil.Soil() self.soil = soil.Soil()
def locate_blocks(self, blocks_number, cell_number, body): def locate_blocks(self, blocks_number, cell_number, body):
for i in range(blocks_number): for i in range(blocks_number):
self.x = random.randint(0, cell_number-1) self.x = random.randint(0, cell_number-1)
@ -31,7 +30,6 @@ class Blocks:
#entire_block.update({self.x : 1}) # for now it may lay on each other, #entire_block.update({self.x : 1}) # for now it may lay on each other,
#print(entire_block) #print(entire_block)
def place_blocks(self, parent_screen, cell_size, body, color): #drawing blocks def place_blocks(self, parent_screen, cell_size, body, color): #drawing blocks
for block in body: for block in body:
x = int(block[0] * cell_size) x = int(block[0] * cell_size)

71
land.py
View File

@ -2,24 +2,71 @@ import pygame
import random import random
class Land: class Land:
def __init__(self, cell_size, cell_number, grass_body): 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() self.grass_image = pygame.image.load(r'resources/grass.png').convert()
self.grass_image = pygame.transform.scale(self.grass_image, (cell_size, cell_size)) 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.image.load(r'resources/bad_grass.png').convert()
self.bad_grass_image = pygame.transform.scale(self.bad_grass_image, (cell_size, cell_size)) self.bad_grass_image = pygame.transform.scale(self.bad_grass_image, (cell_size, cell_size))
for i in range(0, cell_number): self.black_earth_image = pygame.image.load(r'resources/black_earth.png').convert()
for k in range(0, cell_number): self.black_earth_image = pygame.transform.scale(self.black_earth_image, (cell_size, cell_size))
grass_body.append([i,k])
self.brown_earth_image = pygame.image.load(r'resources/brown_earth.png').convert()
self.brown_earth_image = pygame.transform.scale(self.brown_earth_image, (cell_size, cell_size))
def place_grass(self, parent_screen, cell_number, cell_size, grass_body, name): self.fawn_soil_image = pygame.image.load(r'resources/fawn_soil.png').convert()
for body in grass_body: self.fawn_soil_image = pygame.transform.scale(self.fawn_soil_image, (cell_size, cell_size))
x = int(body[0] * cell_size)
y = int(body[1] * cell_size)
if(name == 'good'):
parent_screen.blit(self.grass_image, (x, y)) # to-check: () redundant parentheses
if(name == 'bad'):
parent_screen.blit(self.bad_grass_image, (x, y))
self.fen_soil_image = pygame.image.load(r'resources/fen_soil.png').convert()
self.fen_soil_image = pygame.transform.scale(self.fen_soil_image, (cell_size, cell_size))
# for i in range(0, cell_number):
# for k in range(0, cell_number):
# all_soil_body.append([i, k])
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 == 'brown_earth':
self.parent_screen.blit(self.brown_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))

62
main.py
View File

@ -11,8 +11,8 @@ from datetime import datetime
class Game: class Game:
cell_size = 50 cell_size = 50
cell_number = 15 #horizontally cell_number = 15 # horizontally
blocks_number = 15 # to-check sth is wrong: duplicate(?) and overlap blocks_number = 15
def __init__(self): def __init__(self):
self.dead_leaf_body = [] self.dead_leaf_body = []
@ -22,18 +22,30 @@ class Game:
self.dead_grass_body = [] self.dead_grass_body = []
self.grass_body = [] self.grass_body = []
self.potato_field = [] self.black_earth_body = []
self.brown_earth_body = []
self.fawn_soil_body = []
self.fen_soil_body = []
self.allBodyPos = []
self.entire_block = {} self.entire_block = {}
# initialize a window
pygame.init() pygame.init()
self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number)) # initialize a window self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number))
self.land = land.Land(self.cell_size, self.cell_number, self.grass_body) # 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.blocks = blocks.Blocks(self.surface,self.cell_size) self.black_earth.locate_soil(self.black_earth_body)
self.brown_earth = land.Land(self.surface, self.cell_size, self.cell_number, self.allBodyPos, 100)
self.brown_earth.locate_soil(self.brown_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.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.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.stone_body)
self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body) self.blocks.locate_blocks(self.blocks_number, self.cell_number, self.flower_body)
@ -45,16 +57,16 @@ class Game:
self.tractor.draw() self.tractor.draw()
def run(self): def run(self):
print(self.potato.get_soil_info().get_name()) # print(self.potato.get_soil_info().get_name())
print(self.potato.get_soil_info().get_acidity()) # print(self.potato.get_soil_info().get_acidity())
print(self.potato.get_soil_info().get_irrigation()) # print(self.potato.get_soil_info().get_irrigation())
running = True running = True
clock = pygame.time.Clock() clock = pygame.time.Clock()
last_time = datetime.now() # last_time = datetime.now()
while running: while running:
clock.tick(60) # manual fps control not to overwork the computer 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(): for event in pygame.event.get():
if event.type == KEYDOWN: if event.type == KEYDOWN:
@ -62,41 +74,37 @@ class Game:
running = False running = False
# in case we want to use keyboard # in case we want to use keyboard
if pygame.key.get_pressed()[K_UP]: if pygame.key.get_pressed()[K_UP]:
self.tractor.move('up',self.cell_size, self.cell_number) self.tractor.move('up', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_DOWN]: if pygame.key.get_pressed()[K_DOWN]:
self.tractor.move('down',self.cell_size, self.cell_number) self.tractor.move('down', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_LEFT]: if pygame.key.get_pressed()[K_LEFT]:
self.tractor.move('left',self.cell_size, self.cell_number) self.tractor.move('left', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_RIGHT]: if pygame.key.get_pressed()[K_RIGHT]:
self.tractor.move('right',self.cell_size, self.cell_number) self.tractor.move('right', self.cell_size, self.cell_number)
if pygame.key.get_pressed()[K_SPACE]: 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.dead_leaf_body, self.green_leaf_body, self.cell_size)
#self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size) # self.tractor.water(self.grass_body, self.dead_grass_body, self.cell_size)
elif event.type == QUIT: elif event.type == QUIT:
running = False running = False
self.surface.fill((140, 203, 97)) # background color self.surface.fill((140, 203, 97)) # background color
self.land.place_grass(self.surface, self.cell_number, self.cell_size, self.grass_body, 'good') self.grass.set_and_place_block_of_grass('good')
#self.land.place_grass(self.surface, self.cell_number, self.cell_size, self.dead_grass_body, 'bad') self.black_earth.place_soil(self.black_earth_body, 'black_earth')
self.brown_earth.place_soil(self.brown_earth_body, 'brown_earth')
self.fawn_soil.place_soil(self.fawn_soil_body, 'fawn_soil')
self.fen_soil.place_soil(self.fen_soil_body, 'fen_soil')
self.blocks.place_blocks(self.surface, self.cell_size, self.dead_leaf_body, 'leaf') 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.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.stone_body, 'stone')
self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower') self.blocks.place_blocks(self.surface, self.cell_size, self.flower_body, 'flower')
self.tractor.draw() self.tractor.draw()
pygame.display.update() 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__': if __name__ == '__main__':
game = Game() game = Game()
game.run() game.run()

BIN
resources/black_earth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

BIN
resources/earth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
resources/fawn_soil.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
resources/fen_soil.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
resources/grass copy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
resources/grassTwo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
resources/seed.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 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/wheatOne.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
resources/wheatThree.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

View File

@ -4,6 +4,7 @@ import random
class Tractor: class Tractor:
def __init__(self, parent_screen, cell_size): def __init__(self, parent_screen, cell_size):
self.parent_screen = parent_screen 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.up = pygame.image.load(r'resources/up.png').convert_alpha()
self.down = pygame.image.load(r'resources/down.png').convert_alpha() self.down = pygame.image.load(r'resources/down.png').convert_alpha()
@ -22,6 +23,8 @@ class Tractor:
self.direction = 'up' self.direction = 'up'
self.image = self.down self.image = self.down
self.lastVisitedBlocks = [] # as tractor moves it stores last 3 coordinates
def draw(self): def draw(self):
self.parent_screen.blit(self.image, (self.x, self.y)) # rotate tractor self.parent_screen.blit(self.image, (self.x, self.y)) # rotate tractor