Traktor/board.py
2024-06-08 19:46:06 +02:00

115 lines
4.9 KiB
Python

import pygame
from constant import size, rows, cols
import random
import numpy as np
class Board:
def __init__(self,load_from_file=False, filename='generated_board.npy'):
self.board = []
self.vegetables = []
self.soil_features = None
self.vegetable_names = []
self.load_images()
if load_from_file:
self.load_board_from_file(filename)
def load_images(self):
self.grass = pygame.image.load("board/grass.png")
self.dirt = pygame.image.load("board/dirt.png")
self.rock = pygame.image.load("board/rock.png")
self.weeds = pygame.image.load("board/weeds.png")
self.soil = pygame.image.load("board/zyzna.png")
self.warzywa_images = {
"marchewka": [pygame.image.load(f"warzywa/Carrot/{i}.jpg") for i in range(1, 10)],
"ziemniak": [pygame.image.load(f"warzywa/Potato/{i}.jpg") for i in range(1, 10)],
"pomidor": [pygame.image.load(f"warzywa/tomato/{i}.jpg") for i in range(1, 10)],
"fasola": [pygame.image.load(f"warzywa/Bean/{i}.jpg") for i in range(1, 10)],
"dynia": [pygame.image.load(f"warzywa/Pumpkin/{i}.jpg") for i in range(1, 10)],
"rzodkiewka": [pygame.image.load(f"warzywa/Radish/{i}.jpg") for i in range(1, 10)],
"ogorek": [pygame.image.load(f"warzywa/Cucumber/{i}.jpg") for i in range(1, 10)],
"kalafior": [pygame.image.load(f"warzywa/Cauliflower/{i}.jpg") for i in range(1, 10)],
"kapusta": [pygame.image.load(f"warzywa/Cabbage/{i}.jpg") for i in range(1, 10)],
"brokul": [pygame.image.load(f"warzywa/Broccoli/{i}.jpg") for i in range(1, 10)]
}
self.vegetable_types = {
"marchewka": 2,
"ziemniak": 3,
"pomidor": 4,
"fasola": 5,
"dynia": 6,
"rzodkiewka": 7,
"ogorek": 8,
"kalafior": 9,
"kapusta": 10,
"brokul": 11
}
def load_board_from_file(self, filename):
self.board = np.load(filename)
self.vegetables = [[None for _ in range(cols)] for _ in range(rows)]
self.vegetable_names = [[None for _ in range(cols)] for _ in range(rows)]
for row in range(rows):
for col in range(cols):
if self.board[row][col] in self.vegetable_types.values():
vegetable_type = list(self.warzywa_images.keys())[self.board[row][col] - 2]
vegetable_image = random.choice(self.warzywa_images[vegetable_type])
self.vegetables[row][col] = vegetable_image
self.vegetable_names[row][col] = vegetable_type
self.soil_features = self.generate_soil_features()
print(f"Shared soil features: {self.soil_features}")
def generate_soil_features(self):
return {
"wilgotnosc_gleby": random.randint(30, 70),
"temperatura_gleby": random.randint(13, 26),
"opady_deszczu": random.randint(0, 11),
"wiek_rosliny": random.randint(1, 9),
"proc_ekspo_na_swiatlo": random.randint(10, 90),
"pora_dnia": random.randint(8, 20),
"pora_roku": random.randint(1, 4)
}
def draw_cubes(self, win):
for row in range(rows):
for col in range(cols):
cube_rect = pygame.Rect(col * size, row * size, size, size)
cube = self.board[row][col]
if cube == 0:
win.blit(self.grass, cube_rect)
elif cube == 1:
rock_scale = pygame.transform.scale(self.rock, (size, size))
win.blit(self.dirt, cube_rect)
win.blit(rock_scale, cube_rect)
elif cube == 12:
win.blit(self.soil, cube_rect)
if self.vegetables[row][col]:
vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size))
win.blit(vegetable_image, cube_rect)
elif cube == 13:
win.blit(self.dirt, cube_rect)
if self.vegetables[row][col]:
vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size))
win.blit(vegetable_image, cube_rect)
else:
win.blit(self.dirt, cube_rect)
if self.vegetables[row][col]:
vegetable_image = pygame.transform.scale(self.vegetables[row][col], (size, size))
win.blit(vegetable_image, cube_rect)
def is_rock(self, row, col):
return self.board[row][col] == 1
def is_dirt(self, row, col):
return self.board[row][col] in (2,3,4,5,6,7,8,9,10,11)
def set_soil(self, row, col):
self.board[row][col] = 12
self.vegetables[row][col] = self.vegetables[row][col]
def set_fakedirt(self, row, col):
self.board[row][col] = 13