119 lines
4.0 KiB
Python
119 lines
4.0 KiB
Python
import pygame
|
|
from constant import size, rows, cols
|
|
import random
|
|
from tractor import Tractor
|
|
class Board:
|
|
def __init__(self):
|
|
self.board = []
|
|
self.load_images()
|
|
self.generate_board()
|
|
self.load_costs()
|
|
|
|
|
|
|
|
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.carrot = pygame.image.load("board/carrot.png")
|
|
|
|
def generate_board(self):
|
|
# Najpierw wypełniamy całą planszę trawą (kod 2)
|
|
self.board = [[2 for _ in range(rows)] for _ in range(cols)]
|
|
|
|
# Losowo wybieramy 5 unikalnych pozycji dla chwastów
|
|
weed_positions = random.sample([(row, col) for row in range(rows) for col in range(cols)], 5)
|
|
|
|
# Umieszczamy chwasty na wylosowanych pozycjach
|
|
for row, col in weed_positions:
|
|
self.board[row][col] = 1 # 1 oznacza chwast
|
|
|
|
# Teraz losowo umieszczamy inne elementy, omijając pozycje chwastów
|
|
for row in range(rows):
|
|
for col in range(cols):
|
|
if (row, col) not in weed_positions:
|
|
# Losujemy typ terenu, ale pomijamy kod 1 (chwast)
|
|
self.board[row][col] = random.choice([0, 2, 3, 4, 5, 6, 7, 8, 9])
|
|
|
|
def draw_cubes(self, win):
|
|
|
|
|
|
for row in range(rows):
|
|
for col in range(cols):
|
|
cube_rect = pygame.Rect(row * size, col * size, size, size)
|
|
cube = self.board[row][col]
|
|
|
|
if row == 4 and col == 4:
|
|
win.blit(self.grass, cube_rect)
|
|
elif cube == 0:
|
|
rock_scale = pygame.transform.scale(self.rock, (size, size))
|
|
win.blit(self.dirt, cube_rect)
|
|
#win.blit(rock_scale, cube_rect)
|
|
elif cube == 1:
|
|
weed_scale = pygame.transform.scale(self.weeds, (size, size))
|
|
win.blit(self.grass, cube_rect)
|
|
win.blit(weed_scale, cube_rect)
|
|
elif cube in (2, 3, 4, 5):
|
|
win.blit(self.grass, cube_rect)
|
|
elif cube == 10:
|
|
win.blit(self.soil, cube_rect)
|
|
elif cube == 11:
|
|
carrot_scale = pygame.transform.scale(self.carrot, (size, size))
|
|
win.blit(self.carrot, cube_rect)
|
|
win.blit(carrot_scale, cube_rect)
|
|
|
|
else:
|
|
win.blit(self.dirt, cube_rect)
|
|
|
|
def load_costs(self):
|
|
self.costs = {
|
|
0: 100, #kamien
|
|
1:2, #chwast
|
|
2: 1, #po trawie
|
|
3: 1, #po trawie
|
|
4: 1, #po trawie
|
|
5: 1, #po trawie
|
|
6: 3, #ziemia
|
|
7: 3, #ziemia
|
|
8: 3, #ziemia
|
|
9: 3, #ziemia
|
|
10: 4, #zyzna
|
|
11: 10 #marchewka
|
|
}
|
|
|
|
def get_cost(self, row, col):
|
|
tile_type = self.board[row][col]
|
|
return self.costs.get(tile_type, 1)
|
|
|
|
def is_rock(self, row, col):
|
|
tractor = Tractor(row, col)
|
|
return self.board[row][col] == 0 and not (row == tractor.row and col == tractor.col)
|
|
|
|
def is_weed(self,row,col):
|
|
return self.board[row][col] == 1
|
|
|
|
|
|
def set_grass(self,row,col):
|
|
self.board[row][col]=2
|
|
|
|
def is_dirt(self,row,col):
|
|
return self.board[row][col] in (6,7,8,9)
|
|
|
|
def is_soil(self, row, col):
|
|
return self.board[row][col] == 10
|
|
|
|
def set_soil(self, row, col):
|
|
self.board[row][col] = 10
|
|
|
|
def set_carrot(self, row, col):
|
|
self.board[row][col] = 11
|
|
|
|
def get_weed_positions(self):
|
|
weed_positions = []
|
|
for row in range(rows):
|
|
for col in range(cols):
|
|
if self.is_weed(row, col):
|
|
weed_positions.append([row, col])
|
|
return weed_positions |