tractor rotation

This commit is contained in:
s481904 2024-04-28 21:17:39 +02:00
parent 9eb5029b38
commit 5094ee732c
3 changed files with 28 additions and 15 deletions

View File

@ -99,21 +99,19 @@ def manhattan(pos0, pos1):
def main(): def main():
rotation = ["left", "up", "right", "down"]
run = True run = True
clock = pygame.time.Clock() clock = pygame.time.Clock()
board = Board() board = Board()
board.load_images() board.load_images()
start_row, start_col = 1, 1 start_row, start_col = 0,0
end_row, end_col = 5, 0 end_row, end_col = 9,9
tractor = Tractor(start_row, start_col) tractor = Tractor(start_row, start_col)
board.set_grass(start_row, start_col) board.set_grass(start_row, start_col)
board.set_grass(end_row, end_col) board.set_grass(end_row, end_col)
grid = [[Node(x, y) for y in range(rows)] for x in range(cols)] grid = [[Node(x, y) for y in range(rows)] for x in range(cols)]
start = grid[start_row][start_col] start = grid[start_row][start_col]
end = grid[end_row][end_col] end = grid[end_row][end_col]
@ -121,8 +119,6 @@ def main():
while run: while run:
clock.tick(fps) clock.tick(fps)
for event in pygame.event.get(): for event in pygame.event.get():
@ -134,13 +130,29 @@ def main():
continue continue
next_node = path.pop(0) if path else start_node next_node = path.pop(0) if path else start_node
dx = next_node.x - tractor.col
dy = next_node.y - tractor.row
tractor.row, tractor.col = next_node.y, next_node.x tractor.row, tractor.col = next_node.y, next_node.x
#rotation
if dx > 0:
tractor.direction = "right"
elif dx < 0:
tractor.direction = "left"
elif dy > 0:
tractor.direction = "down"
elif dy < 0:
tractor.direction = "up"
if board.is_weed(tractor.col, tractor.row ):
board.set_grass(tractor.col, tractor.row )
elif board.is_dirt(tractor.col, tractor.row ):
board.set_soil(tractor.col, tractor.row )
elif board.is_soil(tractor.col, tractor.row ):
board.set_carrot(tractor.col, tractor.row )
board.draw_cubes(WIN) board.draw_cubes(WIN)
tractor.draw(WIN) tractor.draw(WIN)

View File

@ -1,7 +1,7 @@
import pygame import pygame
from constant import size, rows, cols from constant import size, rows, cols
import random import random
from tractor import Tractor
class Board: class Board:
def __init__(self): def __init__(self):
@ -55,7 +55,7 @@ class Board:
def load_costs(self): def load_costs(self):
self.costs = { self.costs = {
0: 100, #kamien 0: 100, #kamien
1: 2, #chwasty 1:2, #chwast
2: 1, #po trawie 2: 1, #po trawie
3: 1, #po trawie 3: 1, #po trawie
4: 1, #po trawie 4: 1, #po trawie
@ -73,7 +73,8 @@ class Board:
return self.costs.get(tile_type, 1) return self.costs.get(tile_type, 1)
def is_rock(self, row, col): def is_rock(self, row, col):
return self.board[row][col] == 0 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): def is_weed(self,row,col):
return self.board[row][col] == 1 return self.board[row][col] == 1

View File

@ -1,6 +1,6 @@
import pygame import pygame
width, height = 640, 640 width, height = 640, 640
rows, cols = 8, 8 rows, cols = 10, 10
size = width//cols size = width//cols
yellow = (216,178,0) yellow = (216,178,0)
green= (103,178,0) green= (103,178,0)