Traktor/board.py

103 lines
3.3 KiB
Python
Raw Normal View History

2024-03-10 02:46:14 +01:00
import pygame
from constant import size, rows, cols
2024-03-24 19:28:14 +01:00
import random
2024-04-28 21:17:39 +02:00
from tractor import Tractor
2024-03-10 02:46:14 +01:00
class Board:
def __init__(self):
self.board = []
2024-03-24 19:28:14 +01:00
self.load_images()
self.generate_board()
2024-04-27 00:34:03 +02:00
self.load_costs()
2024-03-10 02:46:14 +01:00
def load_images(self):
2024-03-24 19:28:14 +01:00
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")
2024-03-24 20:54:05 +01:00
self.soil = pygame.image.load("board/zyzna.png")
2024-04-27 00:34:03 +02:00
self.carrot = pygame.image.load("board/carrot.png")
2024-03-24 19:28:14 +01:00
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)]
2024-03-10 02:46:14 +01:00
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)
2024-03-24 19:28:14 +01:00
cube=self.board[row][col]
2024-03-10 02:46:14 +01:00
2024-03-24 19:28:14 +01:00
if row==4 and col==4:
2024-03-10 02:46:14 +01:00
win.blit(self.grass, cube_rect)
2024-03-24 19:28:14 +01:00
elif cube == 0:
rock_scale = pygame.transform.scale(self.rock, (size, size))
2024-03-10 02:46:14 +01:00
win.blit(self.dirt, cube_rect)
2024-03-24 19:28:14 +01:00
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)
2024-03-24 20:54:05 +01:00
elif cube == 10:
win.blit(self.soil, cube_rect)
2024-04-27 00:34:03 +02:00
elif cube == 11:
carrot_scale = pygame.transform.scale(self.carrot, (size,size))
2024-06-07 12:56:00 +02:00
win.blit(self.carrot, cube_rect)
2024-04-27 00:34:03 +02:00
win.blit(carrot_scale, cube_rect)
2024-06-07 12:56:00 +02:00
2024-03-24 19:28:14 +01:00
else:
win.blit(self.dirt, cube_rect)
2024-06-07 12:56:00 +02:00
2024-04-27 00:34:03 +02:00
def load_costs(self):
self.costs = {
0: 100, #kamien
2024-04-28 21:17:39 +02:00
1:2, #chwast
2024-04-27 00:34:03 +02:00
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
2024-06-07 12:56:00 +02:00
9: 3, #ziemia
2024-04-27 00:34:03 +02:00
10: 4, #zyzna
11: 10 #marchewka
}
2024-03-10 02:46:14 +01:00
2024-04-27 00:34:03 +02:00
def get_cost(self, row, col):
tile_type = self.board[row][col]
2024-06-07 12:56:00 +02:00
return self.costs.get(tile_type, 1)
2024-03-24 19:49:08 +01:00
def is_rock(self, row, col):
2024-04-28 21:17:39 +02:00
tractor = Tractor(row, col)
return self.board[row][col] == 0 and not (row == tractor.row and col == tractor.col)
2024-03-24 19:49:08 +01:00
def is_weed(self,row,col):
return self.board[row][col] == 1
def set_grass(self,row,col):
2024-03-24 20:54:05 +01:00
self.board[row][col]=2
def is_dirt(self,row,col):
return self.board[row][col] in (6,7,8,9)
2024-06-07 12:56:00 +02:00
2024-04-27 00:34:03 +02:00
def is_soil(self, row, col):
return self.board[row][col] == 10
2024-06-07 12:56:00 +02:00
2024-03-24 20:54:05 +01:00
def set_soil(self, row, col):
2024-04-27 00:34:03 +02:00
self.board[row][col] = 10
2024-06-07 12:56:00 +02:00
2024-04-27 00:34:03 +02:00
def set_carrot(self, row, col):
2024-06-07 12:56:00 +02:00
self.board[row][col] = 11
def get_dirt_positions(self):
dirt_positions = []
for row in range(rows):
for col in range(cols):
if self.is_dirt(row, col):
dirt_positions.append([row, col])
return dirt_positions