diff --git a/source/area/__pycache__/constants.cpython-311.pyc b/source/area/__pycache__/constants.cpython-311.pyc index 170b454..5da78ea 100644 Binary files a/source/area/__pycache__/constants.cpython-311.pyc and b/source/area/__pycache__/constants.cpython-311.pyc differ diff --git a/source/area/__pycache__/field.cpython-311.pyc b/source/area/__pycache__/field.cpython-311.pyc index 3a7109f..4fef23c 100644 Binary files a/source/area/__pycache__/field.cpython-311.pyc and b/source/area/__pycache__/field.cpython-311.pyc differ diff --git a/source/area/__pycache__/tractor.cpython-311.pyc b/source/area/__pycache__/tractor.cpython-311.pyc index 36769f1..f6b2a75 100644 Binary files a/source/area/__pycache__/tractor.cpython-311.pyc and b/source/area/__pycache__/tractor.cpython-311.pyc differ diff --git a/source/area/field.py b/source/area/field.py index ac7bd20..a7082ae 100644 --- a/source/area/field.py +++ b/source/area/field.py @@ -6,7 +6,6 @@ from tile import Tile tiles = [] - fieldX = (WIDTH-FIELD_WIDTH)/2 # in center of the screen fieldY = 100 @@ -26,6 +25,7 @@ def createTiles(): tile.randomizeContent() tiles.append(tile) positionFieldElements() + return tiles def createField(win): createTiles() @@ -41,3 +41,21 @@ def drawWindow(win): createField(win) pygame.display.flip() + +def update_tiles(win, tractor): + tiles = createTiles() + for tile in tiles: + if tile.x == tractor.previous_x and tile.y == tractor.previous_y: + tile.x = tractor.x + tile.y = tractor.y + + if tile.image is not None: + new_tile = Tile(tile.x, tile.y) + new_tile.image = tile.image + + + tiles.append(new_tile) + + image = pygame.image.load(tile.image).convert() + image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE)) + win.blit(image, (tile.x + fieldX-1, tile.y + fieldY-1)) \ No newline at end of file diff --git a/source/area/tractor.py b/source/area/tractor.py index 1dab655..da5652e 100644 --- a/source/area/tractor.py +++ b/source/area/tractor.py @@ -1,5 +1,5 @@ from crop_protection_product import CropProtectionProduct -from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH +from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, GREY from area.field import fieldX,fieldY import pygame @@ -7,7 +7,7 @@ import pygame class Tractor: x = None y = None - direction = None #direction takes values in the range of 1 to 4 + direction = None #direction takes values in the range of 1 to 4 (1->North, 2->East etc...) image = None cypermetryna = CropProtectionProduct("pests", "cereal") diflufenikan = CropProtectionProduct("weeds", "cereal") @@ -20,8 +20,20 @@ class Tractor: def __init__(self, x, y, direction): self.x = x self.y = y + + self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE) + + # self.previous_x = x + # self.previous_y = y self.direction = direction - self.image = pygame.image.load('resources/images/tractor.png').convert_alpha() + self.image = pygame.image.load('resources/images/tractor_right.png').convert_alpha() + + if (self.direction==1): + self.image = pygame.image.load('resources/images/tractor_up.png').convert_alpha() + elif (self.direction==3): + self.image = pygame.image.load('resources/images/tractor_down.png').convert_alpha() + elif (self.direction==4): + self.image = pygame.image.load('resources/images/tractor_left.png').convert_alpha() def work_on_field(self, tile, ground, plant1): @@ -69,16 +81,20 @@ class Tractor: def move(self): if self.direction == DIRECTION_EAST: - self.x = self.x + TILE_SIZE + #self.x += TILE_SIZE + self.rect.x += TILE_SIZE elif self.direction == DIRECTION_WEST: - self.x = self.x - TILE_SIZE + #self.x -= TILE_SIZE + self.rect.x -= TILE_SIZE elif self.direction == DIRECTION_NORTH: - self.y = self.y - TILE_SIZE + #self.y -= TILE_SIZE + self.rect.y -= TILE_SIZE elif self.direction == DIRECTION_SOUTH: - self.y = self.y + TILE_SIZE + #self.y += TILE_SIZE + self.rect.y += TILE_SIZE def rotate_to_right(self): @@ -98,9 +114,8 @@ class Tractor: def draw_tractor(self, win): - + imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE)) - self.x += fieldX - self.y += fieldY - win.blit(imageTractor, (self.x, self.y)) - pygame.display.flip() \ No newline at end of file + win.blit(imageTractor, (self.rect.x, self.rect.y)) + pygame.display.flip() + diff --git a/source/main.py b/source/main.py index 4f62a5c..0dd2df0 100644 --- a/source/main.py +++ b/source/main.py @@ -2,16 +2,18 @@ import pygame import time import random -from area.constants import WIDTH, HEIGHT, TILE_SIZE +from area.constants import WIDTH, HEIGHT, TILE_SIZE, GREY from area.field import drawWindow from area.tractor import Tractor -from area.field import tiles +from area.field import tiles, fieldX, fieldY +from area.field import update_tiles from ground import Dirt from plant import Plant WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Intelligent tractor') - +trail = pygame.image.load("resources/images/background.jpg").convert_alpha() +trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE)) def main(): run = True window = drawWindow(WIN) @@ -24,22 +26,30 @@ def main(): run = False #small test: - time.sleep(2) + time.sleep(1) tile1 = tiles[0] p1 = Plant('wheat', 'cereal', random.randint(1,100), random.randint(1,100), random.randint(1,100)) d1 = Dirt(random.randint(1, 100), random.randint(1,100)) d1.pests_and_weeds() tile1.ground=d1 - tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2) + tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 1) tractor.rotate_to_right() - tractor.move() + tractor.rect.x += fieldX + tractor.rect.y += fieldY tractor.draw_tractor(WIN) - - pygame.display.update() - tractor.work_on_field(tile1, d1, p1) - time.sleep(3) - print("\n") + time.sleep(1) + for _ in range(5): + WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE)) + tractor.move() + + tractor.draw_tractor(WIN) + pygame.display.flip() + tractor.work_on_field(tile1, d1, p1) + time.sleep(2) + print("\n") + + # in loop move tractor: diff --git a/source/resources/images/tractor_down.png b/source/resources/images/tractor_down.png new file mode 100644 index 0000000..6b81279 Binary files /dev/null and b/source/resources/images/tractor_down.png differ diff --git a/source/resources/images/tractor_left.png b/source/resources/images/tractor_left.png new file mode 100644 index 0000000..f879336 Binary files /dev/null and b/source/resources/images/tractor_left.png differ diff --git a/source/resources/images/tractor_right.png b/source/resources/images/tractor_right.png new file mode 100644 index 0000000..42a9154 Binary files /dev/null and b/source/resources/images/tractor_right.png differ diff --git a/source/resources/images/tractor_up.png b/source/resources/images/tractor_up.png new file mode 100644 index 0000000..3f687c0 Binary files /dev/null and b/source/resources/images/tractor_up.png differ