completely reworked tractor movement

This commit is contained in:
MarRac 2024-04-10 01:52:13 +02:00
parent 53603120bc
commit 03a1b0c906
10 changed files with 67 additions and 24 deletions

View File

@ -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))

View File

@ -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()
win.blit(imageTractor, (self.rect.x, self.rect.y))
pygame.display.flip()

View File

@ -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:

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB