Traktor/board.py
2024-04-28 21:17:39 +02:00

95 lines
3.2 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):
self.board = [[random.choice([0,1,2,3,4,5,6,7,8,9]) for _ in range(rows)] for _ in range(cols)]
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