2023-03-07 12:28:13 +01:00
|
|
|
import pygame
|
2023-03-19 21:00:49 +01:00
|
|
|
import sys
|
2023-03-25 12:33:24 +01:00
|
|
|
import random
|
2023-04-22 21:31:19 +02:00
|
|
|
from settings import screen_height, screen_width, SIZE, SPECIES, block_size, tile, road_coords, directions
|
2023-05-25 18:11:06 +02:00
|
|
|
from src.map import drawRoads, seedForFirstTime, return_fields_list, WORLD_MATRIX
|
2023-03-25 12:33:24 +01:00
|
|
|
from src.Tractor import Tractor
|
2023-05-14 18:24:15 +02:00
|
|
|
from src.bfs import Astar
|
2023-05-28 10:23:30 +02:00
|
|
|
from src.Plant import Plant
|
2023-05-25 18:11:06 +02:00
|
|
|
from src.Field import Field
|
2023-05-25 20:17:48 +02:00
|
|
|
import pickle
|
|
|
|
import os
|
2023-05-28 10:23:30 +02:00
|
|
|
from src.ID3 import make_decision
|
2023-05-25 18:11:06 +02:00
|
|
|
|
2023-03-07 12:28:13 +01:00
|
|
|
|
2023-03-15 19:58:49 +01:00
|
|
|
# pygame initialization
|
2023-03-07 12:28:13 +01:00
|
|
|
pygame.init()
|
2023-03-25 12:33:24 +01:00
|
|
|
clock = pygame.time.Clock()
|
2023-03-26 12:00:34 +02:00
|
|
|
#pygame.mouse.set_visible(False)
|
2023-03-19 21:00:49 +01:00
|
|
|
|
2023-03-25 12:33:24 +01:00
|
|
|
#GAME SCREEN
|
|
|
|
screen = pygame.display.set_mode(SIZE)
|
|
|
|
pygame.display.set_caption("Traktor_interaktor")
|
|
|
|
background = pygame.image.load("assets/farmland.jpg")
|
|
|
|
background = pygame.transform.scale(background,SIZE)
|
|
|
|
screen.fill((90,50,20))
|
|
|
|
background.fill((90,50,20))
|
|
|
|
background = drawRoads(background)
|
2023-03-07 12:28:13 +01:00
|
|
|
|
2023-03-25 12:33:24 +01:00
|
|
|
for line in range(26):
|
2023-04-22 21:31:19 +02:00
|
|
|
pygame.draw.line(background, (0, 0, 0), (0, line * block_size), (screen_width, line * block_size))
|
|
|
|
pygame.draw.line(background, (0, 0, 0), (line * block_size, 0), (line * block_size, screen_height))
|
2023-03-25 12:33:24 +01:00
|
|
|
|
|
|
|
#TRACTOR
|
2023-04-22 21:31:19 +02:00
|
|
|
tractor = Tractor('oil','manual', 'fuel', 'fertilizer1', 20)
|
2023-03-25 12:33:24 +01:00
|
|
|
tractor_group = pygame.sprite.Group()
|
|
|
|
tractor_group.add(tractor)
|
|
|
|
|
2023-05-28 10:23:30 +02:00
|
|
|
tractor.setCapacity(90)
|
|
|
|
tractor.setFuel(100)
|
|
|
|
|
2023-03-25 12:33:24 +01:00
|
|
|
#PLANTS
|
|
|
|
plant_group = pygame.sprite.Group()
|
|
|
|
plant_group = seedForFirstTime()
|
2023-05-14 18:24:15 +02:00
|
|
|
fields = return_fields_list()
|
2023-03-07 12:28:13 +01:00
|
|
|
|
2023-05-28 10:23:30 +02:00
|
|
|
|
2023-05-25 20:17:48 +02:00
|
|
|
|
2023-04-22 21:31:19 +02:00
|
|
|
#
|
|
|
|
tractor_move = pygame.USEREVENT + 1
|
|
|
|
pygame.time.set_timer(tractor_move, 800)
|
|
|
|
moves = []
|
2023-05-14 18:24:15 +02:00
|
|
|
goal_astar = Astar()
|
2023-05-25 18:11:06 +02:00
|
|
|
mx=random.randrange(0, 936, 36)
|
|
|
|
my=random.randrange(0, 936, 36)
|
|
|
|
destination = (mx, my)
|
2023-04-22 21:31:19 +02:00
|
|
|
print("Destination: ", destination)
|
2023-05-25 18:11:06 +02:00
|
|
|
mx=int((mx+18)/36)
|
|
|
|
my=int((my+18)/36)
|
|
|
|
print("Destination: ", mx,my)
|
2023-05-28 10:23:30 +02:00
|
|
|
|
2023-05-28 12:20:54 +02:00
|
|
|
#ID3 TREE LOADING
|
2023-05-28 10:23:30 +02:00
|
|
|
dtree = pickle.load(open(os.path.join('src','tree.plk'),'rb'))
|
|
|
|
|
|
|
|
# pobierz dane o polu field i czy ma na sobie roslinke, zadecyduj czy zebrac
|
|
|
|
this_field = WORLD_MATRIX[mx][my]
|
|
|
|
this_contain = Field.getContain(this_field)
|
|
|
|
if isinstance(this_contain, Plant):
|
|
|
|
this_plant = this_contain
|
|
|
|
params=Plant.getParameters(this_plant)
|
2023-05-28 12:20:54 +02:00
|
|
|
#ID3 decision
|
2023-05-28 10:23:30 +02:00
|
|
|
decision=make_decision(params[0],params[1],params[2],params[3],params[4],tractor.fuel,tractor.capacity,params[5],dtree)
|
|
|
|
print('wzorst',params[0],'wilgotnosc',params[1],'dni_od_nawiezienia',params[2],'pogoda',params[3],'zdrowa',params[4],'paliwo',tractor.fuel,'pojemnosc eq',tractor.capacity,'cena sprzedazy',params[5])
|
|
|
|
print(decision)
|
|
|
|
if decision == 1:
|
|
|
|
# Tractor.collect(self=tractor, plant_group=plant_group)
|
|
|
|
print('Gotowe do zbioru')
|
|
|
|
else:
|
|
|
|
print('nie zbieramy')
|
|
|
|
|
|
|
|
else:
|
|
|
|
print('Road, no plant growing')
|
|
|
|
|
2023-05-25 18:11:06 +02:00
|
|
|
|
2023-05-14 18:24:15 +02:00
|
|
|
moves = goal_astar.search(
|
2023-04-22 21:31:19 +02:00
|
|
|
[tractor.rect.x, tractor.rect.y, directions[tractor.rotation]], destination)
|
|
|
|
|
|
|
|
|
2023-05-14 18:24:15 +02:00
|
|
|
|
2023-03-25 12:33:24 +01:00
|
|
|
if __name__ == "__main__":
|
2023-05-14 18:24:15 +02:00
|
|
|
running = True
|
2023-03-25 12:33:24 +01:00
|
|
|
|
|
|
|
while running:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
running = False
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2023-03-26 12:00:34 +02:00
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key==pygame.K_RETURN:
|
|
|
|
tractor.collect(plant_group)
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
running = False
|
2023-04-22 21:31:19 +02:00
|
|
|
if event.type == tractor_move:
|
|
|
|
if len(moves) != 0:
|
2023-05-14 18:24:15 +02:00
|
|
|
moves_list = list(moves) # convert to list
|
|
|
|
step = moves_list.pop() # pop the last element
|
|
|
|
moves = tuple(moves_list) # convert back to tuple
|
2023-04-22 21:31:19 +02:00
|
|
|
tractor.movement(step[0])
|
|
|
|
|
|
|
|
|
|
|
|
Tractor.movement_using_keys(tractor)
|
2023-03-25 12:33:24 +01:00
|
|
|
screen.blit(background,(0,0))
|
|
|
|
plant_group.draw(screen)
|
2023-04-22 21:31:19 +02:00
|
|
|
tractor_group.draw((screen))
|
2023-03-25 12:33:24 +01:00
|
|
|
tractor_group.update()
|
2023-03-26 12:00:34 +02:00
|
|
|
pygame.display.flip()
|
|
|
|
clock.tick(60)
|