diff --git a/Tractor.py b/Tractor.py index d8bf2f4..84710b9 100644 --- a/Tractor.py +++ b/Tractor.py @@ -1,3 +1,4 @@ +import time import pygame import random import displayControler as dCon @@ -5,36 +6,85 @@ import Slot import Osprzet class Tractor: + DIRECTION_NORTH = 'N' + DIRECTION_SOUTH = 'S' + DIRECTION_WEST = 'W' + DIRECTION_EAST = 'E' def __init__(self,slot,screen, osprzet): - self.tractor_image = pygame.image.load('images/traktor.png') - self.tractor_image = pygame.transform.scale(self.tractor_image, (dCon.CUBE_SIZE, dCon.CUBE_SIZE)) + self.tractor_images = { + Tractor.DIRECTION_NORTH: pygame.transform.scale(pygame.image.load('images/traktorN.png'), + (dCon.CUBE_SIZE, dCon.CUBE_SIZE)), + Tractor.DIRECTION_SOUTH: pygame.transform.scale(pygame.image.load('images/traktorS.png'), + (dCon.CUBE_SIZE, dCon.CUBE_SIZE)), + Tractor.DIRECTION_WEST: pygame.transform.scale(pygame.image.load('images/traktorW.png'), + (dCon.CUBE_SIZE, dCon.CUBE_SIZE)), + Tractor.DIRECTION_EAST: pygame.transform.scale(pygame.image.load('images/traktor.png'), + (dCon.CUBE_SIZE, dCon.CUBE_SIZE)) + } + self.direction = Tractor.DIRECTION_EAST # początkowy kierunek wschód + self.current_tractor_image = self.tractor_images[self.direction] self.screen=screen self.slot=slot self.osprzet = osprzet + def draw_tractor(self): - self.screen.blit(self.tractor_image, (self.slot.x_axis*dCon.CUBE_SIZE,self.slot.y_axis*dCon.CUBE_SIZE)) + self.screen.blit(self.current_tractor_image, (self.slot.x_axis * dCon.CUBE_SIZE, self.slot.y_axis * dCon.CUBE_SIZE)) pygame.display.update() - def move_tractor(self, pole, direction): - next_slot = None - if direction == "right" and pole.is_valid_move((self.slot.x_axis + 1, self.slot.y_axis)): - next_slot = pole.get_neighbor(self.slot, 1, 0) - elif direction == "left" and pole.is_valid_move((self.slot.x_axis - 1, self.slot.y_axis)): - next_slot = pole.get_neighbor(self.slot, -1, 0) - elif direction == "down" and pole.is_valid_move((self.slot.x_axis, self.slot.y_axis + 1)): - next_slot = pole.get_neighbor(self.slot, 0, 1) - elif direction == "up" and pole.is_valid_move((self.slot.x_axis, self.slot.y_axis - 1)): - next_slot = pole.get_neighbor(self.slot, 0, -1) + def turn_left(self): + # zmiana kierunku w lewo + direction_map = { + Tractor.DIRECTION_EAST: Tractor.DIRECTION_NORTH, + Tractor.DIRECTION_NORTH: Tractor.DIRECTION_WEST, + Tractor.DIRECTION_WEST: Tractor.DIRECTION_SOUTH, + Tractor.DIRECTION_SOUTH: Tractor.DIRECTION_EAST + } + self.direction = direction_map[self.direction] + self.current_tractor_image = self.tractor_images[self.direction] + self.draw_tractor() - if next_slot: + def turn_right(self): + # zmiana kierunku w prawo + direction_map = { + Tractor.DIRECTION_EAST: Tractor.DIRECTION_SOUTH, + Tractor.DIRECTION_SOUTH: Tractor.DIRECTION_WEST, + Tractor.DIRECTION_WEST: Tractor.DIRECTION_NORTH, + Tractor.DIRECTION_NORTH: Tractor.DIRECTION_EAST + } + self.direction = direction_map[self.direction] + self.current_tractor_image = self.tractor_images[self.direction] + self.draw_tractor() + + def move_forward(self, pole): + next_slot_coordinates = None + if self.direction == Tractor.DIRECTION_EAST: + next_slot_coordinates = (self.slot.x_axis + 1, self.slot.y_axis) + self.current_tractor_image = self.tractor_images[self.direction] + elif self.direction == Tractor.DIRECTION_WEST: + next_slot_coordinates = (self.slot.x_axis - 1, self.slot.y_axis) + self.current_tractor_image = self.tractor_images[self.direction] + elif self.direction == Tractor.DIRECTION_SOUTH: + next_slot_coordinates = (self.slot.x_axis, self.slot.y_axis + 1) + self.current_tractor_image = self.tractor_images[self.direction] + elif self.direction == Tractor.DIRECTION_NORTH: + next_slot_coordinates = (self.slot.x_axis, self.slot.y_axis - 1) + self.current_tractor_image = self.tractor_images[self.direction] + + # sprawdzenie czy następny slot jest dobry + if next_slot_coordinates and pole.is_valid_move(next_slot_coordinates): + next_slot = pole.get_slot_from_cord(next_slot_coordinates) + self.slot.redraw_image() self.slot = next_slot self.draw_tractor() def random_move(self, pole): - directions = ["right", "left", "down", "up"] - direction = random.choice(directions) - self.move_tractor(pole, direction) + # losowanie skrętu + turn_direction = random.choice([self.turn_left, self.turn_right]) + turn_direction() + time.sleep(0.5) + # wykonanie ruchu do przodu z uwzględnieniem aktualnej orientacji + self.move_forward(pole) #to tak zrobiłam już na później, może się przyda diff --git a/images/traktorN.png b/images/traktorN.png new file mode 100644 index 0000000..940e09a Binary files /dev/null and b/images/traktorN.png differ diff --git a/images/traktorS.png b/images/traktorS.png new file mode 100644 index 0000000..0020c3d Binary files /dev/null and b/images/traktorS.png differ diff --git a/images/traktorW.png b/images/traktorW.png new file mode 100644 index 0000000..4120a16 Binary files /dev/null and b/images/traktorW.png differ