diff --git a/AI/decision_tree.py b/AI/decision_tree.py new file mode 100644 index 0000000..aa0af9e --- /dev/null +++ b/AI/decision_tree.py @@ -0,0 +1,87 @@ +# used in Plant + +def decision_tree(plant): + if plant.field.hydration == 4: + if plant.is_healthy == 1: + if plant.field.tractor_there == 0: + if plant.ticks == 0: + return 0 + elif plant.ticks == 1: + return 1 + elif plant.field.tractor_there == 1: + return 0 + elif plant.is_healthy == 0: + return 0 + elif plant.field.hydration == 2: + if plant.species == "sorrel": + if plant.ticks == 1: + if plant.is_healthy == 1: + return 1 + elif plant.is_healthy == 0: + return 0 + elif plant.ticks == 0: + return 0 + elif plant.species == "potato": + return 0 + elif plant.species == "wheat": + return 0 + elif plant.species == "strawberry": + return 0 + elif plant.field.hydration == 1: + if plant.species == "potato": + return 0 + elif plant.species == "strawberry": + if plant.ticks == 1: + return -1 + elif plant.ticks == 0: + return 0 + elif plant.species == "wheat": + return 0 + elif plant.species == "sorrel": + if plant.is_healthy == 0: + return 0 + elif plant.is_healthy == 1: + if plant.field.tractor_there == 0: + if plant.ticks == 0: + return 0 + elif plant.ticks == 1: + return 1 + elif plant.field.tractor_there == 1: + return 0 + elif plant.field.hydration == 3: + if plant.ticks == 1: + if plant.field.tractor_there == 0: + if plant.is_healthy == 1: + if plant.species == "potato": + if plant.field.fertility == 1: + return 1 + elif plant.field.fertility == 0: + return 0 + elif plant.species == "strawberry": + return 1 + elif plant.species == "sorrel": + return 1 + elif plant.species == "wheat": + return 1 + elif plant.is_healthy == 0: + return 0 + elif plant.field.tractor_there == 1: + return 0 + elif plant.ticks == 0: + return 0 + elif plant.field.hydration == 5: + if plant.field.tractor_there == 1: + return 0 + elif plant.field.tractor_there == 0: + if plant.is_healthy == 0: + return 0 + elif plant.is_healthy == 1: + if plant.ticks == 1: + return 1 + elif plant.ticks == 0: + return 0 + elif plant.field.hydration == 0: + if plant.ticks == 0: + return 0 + elif plant.ticks == 1: + return -1 diff --git a/grader.py b/AI/grader.py similarity index 70% rename from grader.py rename to AI/grader.py index f027e18..7b0057a 100644 --- a/grader.py +++ b/AI/grader.py @@ -1,16 +1,7 @@ -import torch -import torchvision -import torchvision.transforms as transforms -import torch.nn as nn -import torch.nn.functional as f -import torch.optim as optim -import numpy as np -from matplotlib.pyplot import imshow -import os import PIL -import numpy as np -import neural_network -from matplotlib.pyplot import imshow +import torchvision.transforms as transforms + +from AI import neural_network # Create the model model = neural_network.Net() @@ -22,10 +13,10 @@ neural_network.load_network_from_structure(model) transform = transforms.Compose([neural_network.Negative(), transforms.ToTensor()]) # load your image(s) -img = PIL.Image.open('test\\0_100.jpg') -img2 = PIL.Image.open('test\\1_100.jpg') -img3 = PIL.Image.open('test\\4_100.jpg') -img4 = PIL.Image.open('test\\5_100.jpg') +img = PIL.Image.open('../src/test/0_100.jpg') +img2 = PIL.Image.open('../src/test/1_100.jpg') +img3 = PIL.Image.open('../src/test/4_100.jpg') +img4 = PIL.Image.open('../src/test/5_100.jpg') # Transform input = transform(img) @@ -62,4 +53,4 @@ print(index) print(output4) index = output4.cpu().data.numpy().argmax() -print(index) \ No newline at end of file +print(index) diff --git a/id3.py b/AI/id3.py similarity index 86% rename from id3.py rename to AI/id3.py index c554803..68b9348 100644 --- a/id3.py +++ b/AI/id3.py @@ -1,30 +1,35 @@ -from cases import * -from collections import Counter -import operator -from types import prepare_class -import numpy as np import copy +import operator +from collections import Counter + +import numpy as np + +from src.cases import * + class Node: def __init__(self, Class, tag=None): self.Class = Class self.childs = [] -def classes_of_cases (cases): + +def classes_of_cases(cases): classes = [] for case in cases: if case.Class not in classes: classes.append(case.Class) return classes -def count_classes (cases): + +def count_classes(cases): classes = [] for case in cases: classes.append(case.Class) c = Counter(classes) return max(c.items(), key=operator.itemgetter(1))[0] -def chose_attribute (cases, attributes): + +def chose_attribute(cases, attributes): a = "" max = float("-inf") for attribute in attributes: @@ -33,7 +38,8 @@ def chose_attribute (cases, attributes): a = attribute return a -def I (cases): + +def I(cases): i = 0 all = len(cases) classes = classes_of_cases(cases) @@ -42,9 +48,10 @@ def I (cases): for case in cases: if case.Class == Class: noc += 1 - i -= (noc/all)*np.log2(noc/all) + i -= (noc / all) * np.log2(noc / all) return i + def E(cases, attribute): e = 0 values = [] @@ -57,9 +64,9 @@ def E(cases, attribute): for case in cases: if case.values[index] == value: ei.append(case) - e += (len(ei)/len(cases))*I(ei) + e += (len(ei) / len(cases)) * I(ei) return e - + def treelearn(cases, attributes, default_class): if cases == []: @@ -86,15 +93,16 @@ def treelearn(cases, attributes, default_class): for case in cases: if case.values[index] == value: new_case = copy.deepcopy(case) - new_case.values = case.values[:index] + case.values[index+1:] - new_case.attributes = case.attributes[:index] + case.attributes[index+1:] + new_case.values = case.values[:index] + case.values[index + 1:] + new_case.attributes = case.attributes[:index] + case.attributes[index + 1:] new_cases.append(new_case) - new_attributes = attributes[:index] + attributes[index+1 :] + new_attributes = attributes[:index] + attributes[index + 1:] child = treelearn(new_cases, new_attributes, new_default_class) t.childs.append([child, value]) return t + def pretty_print(root, n): if len(root.childs) == 0: for _ in range(n): @@ -104,19 +112,13 @@ def pretty_print(root, n): for _ in range(n): print(" ", end="") if child != root.childs[0]: - print("el", end= "") + print("el", end="") if len(str(child[1])) > 1: print("if self." + str(root.Class) + " == \"" + str(child[1]) + "\":") else: print("if self." + str(root.Class) + " == " + str(child[1]) + ":") - pretty_print(child[0], n+1) + pretty_print(child[0], n + 1) tree = treelearn(cases, attributes, 0) pretty_print(tree, 0) - - - - - - \ No newline at end of file diff --git a/neural_network.py b/AI/neural_network.py similarity index 93% rename from neural_network.py rename to AI/neural_network.py index b94dbeb..0910476 100644 --- a/neural_network.py +++ b/AI/neural_network.py @@ -1,31 +1,31 @@ -import torch -import torchvision -import torchvision.transforms as transforms -import torch.nn as nn -import torch.nn.functional as f -import torch.optim as optim -import numpy as np -from matplotlib.pyplot import imshow -import os import PIL import numpy as np +import torch +import torch.nn as nn +import torch.optim as optim +import torchvision +import torchvision.transforms as transforms from matplotlib.pyplot import imshow + def to_negative(img): img = PIL.ImageOps.invert(img) return img + class Negative(object): def __init__(self): pass - + def __call__(self, img): return to_negative(img) + def plotdigit(image): img = np.reshape(image, (-1, 100)) imshow(img, cmap='Greys') + transform = transforms.Compose([Negative(), transforms.ToTensor()]) train_set = torchvision.datasets.ImageFolder(root='train', transform=transform) classes = ("apple", "potato") @@ -35,12 +35,13 @@ train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE, shu device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") + class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.flatten = nn.Flatten() self.linear_relu_stack = nn.Sequential( - nn.Linear(3*100*100, 512), + nn.Linear(3 * 100 * 100, 512), nn.ReLU(), nn.Linear(512, 512), nn.ReLU(), @@ -53,14 +54,15 @@ class Net(nn.Module): x = self.flatten(x).to(device) logits = self.linear_relu_stack(x).to(device) return logits - + + def training_network(): net = Net() net = net.to(device) - + criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) - + for epoch in range(4): running_loss = 0.0 for i, data in enumerate(train_loader, 0): @@ -70,34 +72,33 @@ def training_network(): loss = criterion(outputs, labels) loss.backward() optimizer.step() - + running_loss += loss.item() if i % 2000 == 1999: print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss)) running_loss = 0.0 - + print("Finished training") save_network_to_file(net) - - + + def result_from_network(net, loaded_image): image = PIL.Image.open(loaded_image) pil_to_tensor = transforms.ToTensor()(image.convert("RGB")).unsqueeze_(0) outputs = net(pil_to_tensor.to(device)) - + return classes[torch.max(outputs, 1)[1]] - - + + def save_network_to_file(network): torch.save(network.state_dict(), 'network_model.pth') print("Network saved to file") - - + + def load_network_from_structure(network): network.load_state_dict(torch.load('network_model.pth')) - - + + if __name__ == "__main__": print(torch.cuda.is_available()) training_network() - \ No newline at end of file diff --git a/basic_grid.py b/basic_grid.py deleted file mode 100644 index 90e6327..0000000 --- a/basic_grid.py +++ /dev/null @@ -1,156 +0,0 @@ -# Import the pygame module -import pygame - -# Import pygame.locals for easier access to key coordinates -from pygame.locals import ( - K_UP, - K_LEFT, - K_RIGHT, - K_ESCAPE, - KEYDOWN, - QUIT -) - -# Import other files from project -import field as F -import tractor as T -import plant as P -import colors as C -import dimensions as D -import node as N -import mapschema as maps - -# Initialize pygame -pygame.init() - -# Name the window -pygame.display.set_caption("Inteligentny Traktor") - -# Create the screen object -# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT -screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT)) - -# Define the map of the field -mapschema = maps.createField() - -# Create field array -field = [] - -# Populate the field array -for row in range(D.GSIZE): - field.append([]) - for column in range(D.GSIZE): - fieldbit = F.Field(row, column, mapschema[column][row]) - field[row].append(fieldbit) - -# Create Tractor object -tractor = T.Tractor(field, [0,0]) - -# Define the map of plants -mapschema = maps.createPlants() - -# Createt plants array -plants = [] - -# Populate the plants array -for row in range(D.GSIZE): - plants.append([]) - for column in range(D.GSIZE): - if mapschema[column][row] != 0: - plantbit = P.Plant(field[row][column], mapschema[column][row]) - plants[row].append(plantbit) - -# Create list for tractor instructions -path = [] - -# Variable to keep the main loop running -RUNNING = True - -# Variable conroling timed eventes -TICKER = 0 - -# Initialize clock -clock = pygame.time.Clock() - - -# Main loop -while RUNNING: - - # Look at every event in the queue - for event in pygame.event.get(): - # Did the user hit a key? - if event.type == KEYDOWN: - # Was it the Escape key? If so, stop the loop. - if event.key == K_ESCAPE: - RUNNING = False - # Did the user click the window close button? If so, stop the loop. - elif event.type == QUIT: - RUNNING = False - - # Create key Node that will be used to calculate tractor instructions - processor = N.Node(field, tractor.position, tractor.direction) - - # If path is empty or nonexistent, create new one - if path is None or len(path) == 0: - path = processor.findPathToPlant() - - # control tractor by poping instructions from path list - if path is not None: - if path[0] == "move": - tractor.move() - path.pop(0) - elif path[0] =="left": - tractor.rotate_left() - path.pop(0) - elif path[0] == "right": - tractor.rotate_right() - path.pop(0) - elif path[0] == "hydrate": - tractor.hydrate(field) - path.pop(0) - else: - path.pop(0) - - # Get all keys pressed at a time CURRENTLY UNUSED - pressed_keys = pygame.key.get_pressed() - - # control tractor with pressed keys CURRENTLY UNUSED - if pressed_keys[K_UP]: - tractor.move() - elif pressed_keys[K_LEFT]: - tractor.rotate_left() - elif pressed_keys[K_RIGHT]: - tractor.rotate_right() - - # Set the screen background - screen.fill(C.DBROWN) - - # Draw the field - for row in range(D.GSIZE): - for column in range(D.GSIZE): - screen.blit(field[row][column].surf, field[row][column].rect) - - # Draw the tactor - screen.blit(tractor.surf, tractor.rect) - - # Plants grow with every 10th tick, then they are drawn - for row in plants: - for plant in row: - plant.tick() - plant.grow() - screen.blit(plant.surf, plant.rect) - - # Field are drying with every 100th tick - if TICKER == 0: - for row in range(D.GSIZE): - for column in range(D.GSIZE): - field[row][column].dehydrate() - - # Increment ticker - TICKER = (TICKER + 1)%100 - - # Update the screen - pygame.display.flip() - - # Ensure program maintains a stable framerate - clock.tick(8) diff --git a/cases.py b/cases.py deleted file mode 100644 index 0c3574b..0000000 --- a/cases.py +++ /dev/null @@ -1,259 +0,0 @@ -class Case: - def __init__(self, values, attributes, Class): - self.values = values - self.attributes = attributes - self.Class = Class - -attributes = ["field.hydration", "field.fertility", "species", "ticks", "is_healthy", "field.tractor_there"] - -cases = [Case([4, 0, "potato", 0, 1, 0], attributes, 0), -Case([2, 0, "sorrel", 1, 1, 0], attributes, 1), -Case([4, 1, "wheat", 0, 0, 1], attributes, 0), -Case([1, 1, "potato", 1, 0, 1], attributes, 0), -Case([2, 1, "potato", 0, 0, 1], attributes, 0), -Case([2, 0, "potato", 0, 1, 0], attributes, 0), -Case([1, 1, "strawberry", 1, 0, 1], attributes, -1), -Case([1, 0, "wheat", 1, 1, 1], attributes, 0), -Case([2, 0, "wheat", 1, 0, 1], attributes, 0), -Case([1, 1, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 1, "potato", 1, 1, 0], attributes, 1), -Case([2, 1, "strawberry", 1, 0, 0], attributes, 0), -Case([4, 0, "wheat", 1, 1, 1], attributes, 0), -Case([4, 1, "wheat", 1, 0, 1], attributes, 0), -Case([5, 1, "potato", 1, 1, 1], attributes, 0), -Case([4, 0, "strawberry", 1, 0, 0], attributes, 0), -Case([1, 1, "sorrel", 1, 0, 0], attributes, 0), -Case([0, 0, "sorrel", 0, 0, 1], attributes, 0), -Case([2, 0, "sorrel", 1, 1, 0], attributes, 1), -Case([0, 1, "sorrel", 0, 1, 1], attributes, 0), -Case([0, 1, "strawberry", 1, 1, 0], attributes, -1), -Case([2, 0, "sorrel", 0, 1, 1], attributes, 0), -Case([4, 0, "wheat", 1, 0, 1], attributes, 0), -Case([5, 0, "wheat", 0, 0, 0], attributes, 0), -Case([0, 0, "strawberry", 1, 0, 0], attributes, -1), -Case([4, 0, "sorrel", 1, 0, 0], attributes, 0), -Case([3, 0, "sorrel", 0, 0, 1], attributes, 0), -Case([3, 1, "potato", 1, 0, 1], attributes, 0), -Case([4, 1, "potato", 0, 0, 1], attributes, 0), -Case([1, 1, "wheat", 0, 1, 1], attributes, 0), -Case([3, 0, "wheat", 0, 1, 1], attributes, 0), -Case([2, 0, "wheat", 0, 0, 0], attributes, 0), -Case([5, 1, "potato", 1, 1, 1], attributes, 0), -Case([4, 1, "strawberry", 0, 0, 1], attributes, 0), -Case([1, 0, "potato", 1, 1, 0], attributes, 0), -Case([4, 1, "sorrel", 1, 1, 1], attributes, 0), -Case([0, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([4, 0, "strawberry", 1, 0, 0], attributes, 0), -Case([4, 0, "sorrel", 1, 0, 0], attributes, 0), -Case([5, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 1, "wheat", 1, 1, 1], attributes, 0), -Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([4, 0, "potato", 0, 0, 1], attributes, 0), -Case([5, 1, "wheat", 1, 1, 1], attributes, 0), -Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 0, "sorrel", 0, 0, 1], attributes, 0), -Case([0, 0, "potato", 1, 1, 1], attributes, -1), -Case([4, 0, "strawberry", 1, 1, 1], attributes, 0), -Case([2, 1, "strawberry", 1, 0, 1], attributes, 0), -Case([2, 1, "wheat", 0, 0, 1], attributes, 0), -Case([2, 1, "sorrel", 1, 1, 0], attributes, 1), -Case([1, 0, "potato", 1, 0, 1], attributes, 0), -Case([4, 0, "strawberry", 1, 0, 0], attributes, 0), -Case([4, 1, "potato", 1, 1, 1], attributes, 0), -Case([0, 0, "strawberry", 1, 0, 1], attributes, -1), -Case([0, 1, "wheat", 0, 0, 0], attributes, 0), -Case([1, 1, "wheat", 0, 1, 0], attributes, 0), -Case([0, 0, "sorrel", 1, 1, 0], attributes, -1), -Case([2, 0, "sorrel", 0, 0, 1], attributes, 0), -Case([5, 1, "wheat", 1, 1, 1], attributes, 0), -Case([2, 0, "strawberry", 0, 1, 0], attributes, 0), -Case([2, 1, "wheat", 0, 0, 1], attributes, 0), -Case([3, 0, "potato", 1, 0, 1], attributes, 0), -Case([5, 0, "wheat", 1, 1, 1], attributes, 0), -Case([0, 1, "strawberry", 1, 0, 0], attributes, -1), -Case([0, 0, "wheat", 0, 0, 0], attributes, 0), -Case([5, 0, "potato", 1, 1, 0], attributes, 1), -Case([2, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 1, "sorrel", 0, 1, 0], attributes, 0), -Case([2, 1, "potato", 1, 1, 1], attributes, 0), -Case([5, 0, "strawberry", 1, 1, 0], attributes, 1), -Case([5, 0, "wheat", 0, 0, 0], attributes, 0), -Case([5, 0, "wheat", 1, 1, 0], attributes, 1), -Case([2, 0, "potato", 1, 0, 0], attributes, 0), -Case([3, 1, "wheat", 0, 1, 0], attributes, 0), -Case([3, 0, "potato", 1, 1, 1], attributes, 0), -Case([0, 1, "sorrel", 1, 1, 1], attributes, -1), -Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), -Case([2, 1, "strawberry", 0, 1, 0], attributes, 0), -Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), -Case([3, 0, "wheat", 0, 1, 1], attributes, 0), -Case([4, 0, "strawberry", 1, 0, 1], attributes, 0), -Case([3, 1, "potato", 0, 0, 1], attributes, 0), -Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([5, 1, "wheat", 1, 0, 1], attributes, 0), -Case([5, 0, "potato", 1, 0, 1], attributes, 0), -Case([3, 0, "potato", 0, 0, 1], attributes, 0), -Case([1, 0, "wheat", 0, 1, 0], attributes, 0), -Case([5, 0, "sorrel", 0, 1, 1], attributes, 0), -Case([4, 0, "potato", 0, 1, 0], attributes, 0), -Case([0, 0, "strawberry", 0, 0, 0], attributes, 0), -Case([5, 0, "sorrel", 1, 1, 0], attributes, 1), -Case([4, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([1, 1, "strawberry", 1, 1, 0], attributes, -1), -Case([5, 0, "strawberry", 1, 0, 0], attributes, 0), -Case([5, 1, "wheat", 0, 0, 0], attributes, 0), -Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), -Case([3, 1, "potato", 1, 0, 1], attributes, 0), -Case([1, 0, "sorrel", 0, 0, 0], attributes, 0), -Case([3, 0, "wheat", 0, 1, 1], attributes, 0), -Case([0, 0, "wheat", 0, 0, 1], attributes, 0), -Case([1, 0, "potato", 0, 0, 0], attributes, 0), -Case([1, 1, "sorrel", 0, 1, 0], attributes, 0), -Case([0, 1, "strawberry", 0, 1, 0], attributes, 0), -Case([5, 1, "potato", 1, 0, 1], attributes, 0), -Case([2, 0, "strawberry", 1, 1, 1], attributes, 0), -Case([4, 1, "wheat", 1, 0, 0], attributes, 0), -Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), -Case([1, 0, "strawberry", 1, 1, 1], attributes, -1), -Case([4, 0, "wheat", 0, 0, 0], attributes, 0), -Case([4, 0, "strawberry", 0, 1, 0], attributes, 0), -Case([0, 0, "sorrel", 1, 0, 1], attributes, -1), -Case([1, 0, "strawberry", 0, 1, 0], attributes, 0), -Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), -Case([2, 1, "potato", 0, 0, 0], attributes, 0), -Case([3, 1, "strawberry", 1, 1, 0], attributes, 1), -Case([1, 0, "sorrel", 1, 1, 1], attributes, 0), -Case([5, 1, "strawberry", 1, 1, 0], attributes, 1), -Case([2, 0, "wheat", 1, 1, 1], attributes, 0), -Case([5, 1, "strawberry", 0, 1, 0], attributes, 0), -Case([1, 1, "wheat", 0, 1, 1], attributes, 0), -Case([1, 1, "potato", 0, 1, 0], attributes, 0), -Case([4, 0, "potato", 1, 1, 0], attributes, 1), -Case([2, 1, "strawberry", 0, 0, 1], attributes, 0), -Case([0, 1, "potato", 0, 0, 1], attributes, 0), -Case([3, 0, "sorrel", 1, 0, 1], attributes, 0), -Case([4, 0, "wheat", 1, 0, 1], attributes, 0), -Case([5, 1, "sorrel", 1, 0, 0], attributes, 0), -Case([1, 0, "sorrel", 1, 0, 0], attributes, 0), -Case([5, 0, "sorrel", 1, 0, 1], attributes, 0), -Case([2, 1, "potato", 1, 0, 1], attributes, 0), -Case([1, 0, "potato", 1, 1, 1], attributes, 0), -Case([4, 0, "wheat", 1, 1, 0], attributes, 1), -Case([0, 0, "sorrel", 0, 1, 0], attributes, 0), -Case([2, 0, "wheat", 0, 0, 1], attributes, 0), -Case([0, 1, "potato", 1, 0, 1], attributes, -1), -Case([0, 1, "sorrel", 1, 0, 1], attributes, -1), -Case([1, 1, "potato", 0, 0, 1], attributes, 0), -Case([3, 0, "sorrel", 1, 1, 1], attributes, 0), -Case([0, 0, "potato", 1, 0, 1], attributes, -1), -Case([4, 0, "potato", 0, 0, 1], attributes, 0), -Case([0, 0, "strawberry", 1, 0, 0], attributes, -1), -Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), -Case([5, 0, "potato", 0, 1, 1], attributes, 0), -Case([2, 0, "potato", 0, 0, 0], attributes, 0), -Case([0, 1, "potato", 1, 0, 0], attributes, -1), -Case([1, 0, "potato", 0, 0, 1], attributes, 0), -Case([4, 0, "sorrel", 1, 0, 1], attributes, 0), -Case([1, 0, "potato", 1, 0, 0], attributes, 0), -Case([5, 1, "potato", 1, 0, 1], attributes, 0), -Case([2, 1, "wheat", 1, 0, 0], attributes, 0), -Case([0, 1, "potato", 1, 1, 1], attributes, -1), -Case([5, 1, "strawberry", 0, 1, 0], attributes, 0), -Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 0, "strawberry", 1, 0, 0], attributes, 0), -Case([0, 1, "strawberry", 0, 0, 1], attributes, 0), -Case([0, 1, "wheat", 0, 0, 0], attributes, 0), -Case([0, 1, "strawberry", 1, 0, 0], attributes, -1), -Case([1, 0, "potato", 0, 0, 1], attributes, 0), -Case([1, 1, "wheat", 0, 0, 0], attributes, 0), -Case([0, 1, "strawberry", 1, 0, 1], attributes, -1), -Case([1, 1, "potato", 0, 0, 1], attributes, 0), -Case([0, 0, "wheat", 0, 0, 1], attributes, 0), -Case([4, 1, "sorrel", 1, 1, 1], attributes, 0), -Case([5, 1, "wheat", 0, 0, 1], attributes, 0), -Case([5, 1, "strawberry", 0, 0, 0], attributes, 0), -Case([4, 1, "wheat", 0, 0, 1], attributes, 0), -Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([1, 1, "potato", 1, 1, 0], attributes, 0), -Case([0, 1, "sorrel", 1, 0, 0], attributes, -1), -Case([5, 0, "sorrel", 1, 0, 0], attributes, 0), -Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), -Case([0, 0, "potato", 0, 0, 0], attributes, 0), -Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), -Case([4, 0, "strawberry", 0, 0, 1], attributes, 0), -Case([2, 0, "potato", 1, 0, 0], attributes, 0), -Case([4, 0, "strawberry", 0, 0, 0], attributes, 0), -Case([0, 1, "strawberry", 0, 1, 1], attributes, 0), -Case([1, 1, "wheat", 1, 1, 0], attributes, 0), -Case([3, 0, "potato", 0, 1, 0], attributes, 0), -Case([1, 1, "wheat", 0, 1, 0], attributes, 0), -Case([1, 1, "sorrel", 0, 0, 1], attributes, 0), -Case([3, 1, "wheat", 0, 0, 0], attributes, 0), -Case([3, 1, "wheat", 0, 0, 0], attributes, 0), -Case([1, 0, "wheat", 0, 1, 1], attributes, 0), -Case([5, 1, "potato", 1, 0, 1], attributes, 0), -Case([5, 0, "wheat", 0, 0, 1], attributes, 0), -Case([2, 1, "sorrel", 0, 1, 0], attributes, 0), -Case([5, 0, "sorrel", 1, 0, 0], attributes, 0), -Case([1, 0, "potato", 1, 1, 1], attributes, 0), -Case([5, 1, "wheat", 1, 1, 0], attributes, 1), -Case([3, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([5, 0, "wheat", 0, 0, 1], attributes, 0), -Case([2, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([1, 0, "potato", 1, 1, 1], attributes, 0), -Case([0, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([5, 1, "potato", 1, 1, 0], attributes, 1), -Case([2, 0, "wheat", 0, 1, 0], attributes, 0), -Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 0, "potato", 1, 1, 0], attributes, 0), -Case([1, 1, "potato", 0, 0, 0], attributes, 0), -Case([2, 1, "potato", 0, 1, 0], attributes, 0), -Case([2, 1, "wheat", 0, 1, 0], attributes, 0), -Case([2, 0, "sorrel", 1, 1, 0], attributes, 1), -Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([2, 0, "wheat", 1, 1, 1], attributes, 0), -Case([5, 0, "wheat", 1, 1, 0], attributes, 1), -Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([0, 1, "potato", 0, 0, 0], attributes, 0), -Case([5, 1, "strawberry", 1, 1, 1], attributes, 0), -Case([4, 1, "wheat", 1, 0, 0], attributes, 0), -Case([5, 1, "sorrel", 0, 0, 1], attributes, 0), -Case([1, 1, "wheat", 1, 0, 0], attributes, 0), -Case([5, 0, "strawberry", 1, 0, 1], attributes, 0), -Case([5, 0, "wheat", 1, 0, 1], attributes, 0), -Case([2, 0, "potato", 1, 0, 1], attributes, 0), -Case([3, 1, "wheat", 1, 0, 0], attributes, 0), -Case([0, 1, "strawberry", 1, 0, 0], attributes, -1), -Case([0, 1, "strawberry", 0, 1, 1], attributes, 0), -Case([3, 0, "wheat", 0, 1, 0], attributes, 0), -Case([4, 1, "potato", 1, 1, 1], attributes, 0), -Case([3, 0, "potato", 0, 0, 1], attributes, 0), -Case([2, 1, "strawberry", 1, 1, 0], attributes, 0), -Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), -Case([4, 1, "wheat", 1, 0, 1], attributes, 0), -Case([2, 0, "potato", 0, 1, 0], attributes, 0), -Case([5, 0, "sorrel", 0, 1, 1], attributes, 0), -Case([0, 1, "wheat", 1, 1, 0], attributes, -1), -Case([5, 1, "wheat", 1, 0, 0], attributes, 0), -Case([2, 0, "potato", 0, 0, 0], attributes, 0), -Case([2, 0, "strawberry", 0, 1, 1], attributes, 0), -Case([4, 1, "potato", 0, 1, 1], attributes, 0), -Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), -Case([1, 1, "strawberry", 1, 0, 1], attributes, -1), -Case([3, 0, "sorrel", 1, 1, 0], attributes, 1), -Case([5, 1, "wheat", 1, 0, 0], attributes, 0), -Case([4, 0, "sorrel", 1, 1, 0], attributes, 1), -Case([2, 1, "sorrel", 1, 0, 0], attributes, 0), -Case([0, 1, "wheat", 0, 1, 0], attributes, 0), -Case([5, 0, "potato", 1, 1, 0], attributes, 1), -Case([3, 1, "strawberry", 0, 1, 0], attributes, 0), -Case([5, 1, "strawberry", 0, 0, 0], attributes, 0), -Case([4, 1, "potato", 1, 1, 1], attributes, 0), -Case([5, 1, "potato", 1, 0, 1], attributes, 0), -Case([5, 1, "potato", 1, 1, 1], attributes, 0), -Case([0, 0, "sorrel", 0, 0, 0], attributes, 0), -Case([1, 1, "sorrel", 1, 1, 0], attributes, 1), -Case([0, 1, "potato", 0, 1, 0], attributes, 0), -Case([4, 1, "strawberry", 1, 1, 1], attributes, 0), -Case([0, 0, "wheat", 0, 1, 1], attributes, 0), -Case([3, 0, "wheat", 1, 1, 0], attributes, 1)] \ No newline at end of file diff --git a/field.py b/field.py index b4b19b9..6a8aa81 100644 --- a/field.py +++ b/field.py @@ -1,6 +1,8 @@ import pygame -from colors import * -from dimensions import * + +from src.colors import * +from src.dimensions import * + class Field(pygame.sprite.Sprite): def __init__(self, row, column, field_type): @@ -30,30 +32,16 @@ class Field(pygame.sprite.Sprite): def hydrate(self): if self.field_type == "soil" and self.hydration <= 5: self.hydration += 1 - if self.hydration == 0: - self.surf.fill(BROWN0) - if self.hydration == 1: - self.surf.fill(BROWN1) - if self.hydration == 2: - self.surf.fill(BROWN2) - if self.hydration == 3: - self.surf.fill(BROWN3) - if self.hydration == 4 or self.hydration == 5: - self.surf.fill(BROWN4) - + + # color field to it's hydration value + self.surf.fill(eval('BROWN' + str(self.hydration))) + def dehydrate(self): if self.field_type == "soil" and self.hydration > 0: self.hydration -= 1 - if self.hydration == 0: - self.surf.fill(BROWN0) - if self.hydration == 1: - self.surf.fill(BROWN1) - if self.hydration == 2: - self.surf.fill(BROWN2) - if self.hydration == 3: - self.surf.fill(BROWN3) - if self.hydration == 4 or self.hydration == 5: - self.surf.fill(BROWN4) - + + # color field to it's hydration value + self.surf.fill(eval('BROWN' + str(self.hydration))) + def free(self): self.planted = 0 diff --git a/main.py b/main.py new file mode 100644 index 0000000..c9dc836 --- /dev/null +++ b/main.py @@ -0,0 +1,151 @@ +# Import the pygame module +import pygame +# Import pygame.locals for easier access to key coordinates +from pygame.locals import ( + K_UP, + K_LEFT, + K_RIGHT, + K_ESCAPE, + KEYDOWN, + QUIT +) + +# Import other files from project +import field as F +import node as N +import plant as P +import src.colors as C +import src.dimensions as D +import tractor as T +from src import mapschema as maps + +if __name__ == "__main__": + # Initialize pygame + pygame.init() + + # Name the window + pygame.display.set_caption("Inteligentny Traktor") + + # Create the screen object + # The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT + screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT)) + + # Define the map of the field + mapschema = maps.createField() + + # Create field array + field = [] + + # Populate the field array + for row in range(D.GSIZE): + field.append([]) + for column in range(D.GSIZE): + fieldbit = F.Field(row, column, mapschema[column][row]) + field[row].append(fieldbit) + + # Create Tractor object + tractor = T.Tractor(field, [0, 0]) + + # Define the map of plants + mapschema = maps.createPlants() + + # Createt plants array + plants = [] + + # Populate the plants array + for row in range(D.GSIZE): + plants.append([]) + for column in range(D.GSIZE): + if mapschema[column][row] != 0: + plantbit = P.Plant(field[row][column], mapschema[column][row]) + plants[row].append(plantbit) + + # Create list for tractor instructions + path = [] + + # Variable to keep the main loop running + RUNNING = True + + # Variable conroling timed eventes + TICKER = 0 + + # Initialize clock + clock = pygame.time.Clock() + + # Main loop + while RUNNING: + + # Look at every event in the queue + for event in pygame.event.get(): + # Did the user hit a key? + if event.type == KEYDOWN: + # Was it the Escape key? If so, stop the loop. + if event.key == K_ESCAPE: + RUNNING = False + # Did the user click the window close button? If so, stop the loop. + elif event.type == QUIT: + RUNNING = False + + # Create key Node that will be used to calculate tractor instructions + processor = N.Node(field, tractor.position, tractor.direction) + + # If path is empty or nonexistent, create new one + if path is None or len(path) == 0: + path = processor.findPathToPlant() + + # control tractor by poping instructions from path list + if path is not None: + if path[0] == "move": + tractor.move() + elif path[0] == "left": + tractor.rotate_left() + elif path[0] == "right": + tractor.rotate_right() + elif path[0] == "hydrate": + tractor.hydrate(field) + + path.pop(0) + + # Get all keys pressed at a time CURRENTLY UNUSED + pressed_keys = pygame.key.get_pressed() + + # control tractor with pressed keys CURRENTLY UNUSED + if pressed_keys[K_UP]: + tractor.move() + elif pressed_keys[K_LEFT]: + tractor.rotate_left() + elif pressed_keys[K_RIGHT]: + tractor.rotate_right() + + # Set the screen background + screen.fill(C.DBROWN) + + # Draw the field + for row in range(D.GSIZE): + for column in range(D.GSIZE): + screen.blit(field[row][column].surf, field[row][column].rect) + + # Draw the tactor + screen.blit(tractor.surf, tractor.rect) + + # Plants grow with every 10th tick, then they are drawn + for row in plants: + for plant in row: + plant.tick() + plant.grow() + screen.blit(plant.surf, plant.rect) + + # Field are drying with every 100th tick + if TICKER == 0: + for row in range(D.GSIZE): + for column in range(D.GSIZE): + field[row][column].dehydrate() + + # Increment ticker + TICKER = (TICKER + 1) % 100 + + # Update the screen + pygame.display.flip() + + # Ensure program maintains a stable framerate + clock.tick(8) diff --git a/mapschema.py b/mapschema.py deleted file mode 100644 index 4cc5963..0000000 --- a/mapschema.py +++ /dev/null @@ -1,27 +0,0 @@ -def createField(): - field = [["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"], - ["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"], - ["soil", "soil", "soil", "soil", "soil", "road", "road", "road", "road", "road"], - ["rocks", "rocks", "rocks", "rocks", "soil", "road", "soil", "soil", "rocks", "soil"], - ["soil", "soil", "soil", "soil", "soil", "road", "rocks", "rocks", "soil", "soil"], - ["soil", "soil", "soil", "pond", "rocks", "road", "rocks", "soil", "soil", "rocks"], - ["rocks", "pond", "pond", "pond", "pond", "road", "rocks", "soil", "soil", "rocks"], - ["road", "road", "road", "road", "road", "road", "rocks", "soil", "soil", "soil"], - ["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks"], - ["soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks", "soil"] - ] - return field - -def createPlants(): - field = [["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"], - ["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"], - ["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - ["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0], - ["wheat", "wheat", "wheat", 0, 0, 0, 0, "potato", "potato", 0], - [0, 0, 0, 0, 0, 0, 0, "potato", "potato", 0], - [0, 0, 0, 0, 0, 0, 0, "potato", "potato", "potato"], - ["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, "potato", 0, 0], - ["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, 0, 0, 0] - ] - return field diff --git a/node.py b/node.py index c8a8c52..3c1ee8b 100644 --- a/node.py +++ b/node.py @@ -1,10 +1,12 @@ -from dimensions import * import heapq +from src.dimensions import * + def getTotalCost(x): return x.totalCost + def showPath(node, goal): path = node.findPath(goal) for x in path: @@ -12,36 +14,38 @@ def showPath(node, goal): print(x.rotation, end=" ") print(x.action) print("***") - -def succesor (node): + +def succesor(node): succesors = [] - if node.position[0]+node.rotation[0] in range(0,GSIZE) and node.position[1]+node.rotation[1] in range(0,GSIZE): - child = Node(node.field, [node.position[0]+node.rotation[0], node.position[1]+node.rotation[1]], node.rotation) + if node.position[0] + node.rotation[0] in range(0, GSIZE) and node.position[1] + node.rotation[1] in range(0, + GSIZE): + child = Node(node.field, [node.position[0] + node.rotation[0], node.position[1] + node.rotation[1]], + node.rotation) child.action = "move" succesors.append(child) - if node.rotation == [1,0]: - child = Node(node.field, node.position, [0,-1]) + if node.rotation == [1, 0]: + child = Node(node.field, node.position, [0, -1]) child.action = "left" succesors.append(child) - child = Node(node.field, node.position, [0,1]) + child = Node(node.field, node.position, [0, 1]) child.action = "right" succesors.append(child) - if node.rotation == [0,1]: + if node.rotation == [0, 1]: child = Node(node.field, node.position, [-1, 0]) succesors.append(child) child.action = "right" child = Node(node.field, node.position, [1, 0]) child.action = "left" succesors.append(child) - if node.rotation == [-1,0]: - child = Node(node.field, node.position, [0,-1]) + if node.rotation == [-1, 0]: + child = Node(node.field, node.position, [0, -1]) succesors.append(child) child.action = "right" - child = Node(node.field, node.position, [0,1]) + child = Node(node.field, node.position, [0, 1]) child.action = "left" succesors.append(child) - if node.rotation == [0,-1]: + if node.rotation == [0, -1]: child = Node(node.field, node.position, [-1, 0]) child.action = "left" succesors.append(child) @@ -50,7 +54,8 @@ def succesor (node): succesors.append(child) return succesors -class Node(): + +class Node: def __init__(self, field, position, rotation): self.parent = 0 self.startCost = 0 @@ -88,7 +93,7 @@ class Node(): path.append(current) current = current.parent return path[::-1] - + children = succesor(currentNode) perm = 0 @@ -102,8 +107,8 @@ class Node(): continue child.parent = currentNode child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost - child.heuristic = abs(goal[0]-child.position[0]) + abs(goal[1]-child.position[1]) - child.totalCost = child.startCost+child.heuristic + child.heuristic = abs(goal[0] - child.position[0]) + abs(goal[1] - child.position[1]) + child.totalCost = child.startCost + child.heuristic for openNode in openList: if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost > openNode.startCost: @@ -131,7 +136,8 @@ class Node(): closedList.append(currentNode) - if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2: + if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and \ + currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2: path = [] for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4): path.append("hydrate") @@ -140,7 +146,7 @@ class Node(): path.append(current.action) current = current.parent return path[::-1] - + children = succesor(currentNode) perm = 0 @@ -154,8 +160,9 @@ class Node(): continue child.parent = currentNode child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost - child.heuristic = abs(startNode.position[0]-child.position[0]) + abs(startNode.position[1]-child.position[1]) - child.totalCost = child.startCost+child.heuristic + child.heuristic = abs(startNode.position[0] - child.position[0]) + abs( + startNode.position[1] - child.position[1]) + child.totalCost = child.startCost + child.heuristic for openNode in openList: if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost >= openNode.startCost: @@ -166,4 +173,4 @@ class Node(): perm = 0 continue - heapq.heappush(openList, child) + heapq.heappush(openList, child) diff --git a/plant.py b/plant.py index 0e24e0d..8fd28b3 100644 --- a/plant.py +++ b/plant.py @@ -1,7 +1,7 @@ -import pygame -from colors import * -from dimensions import * -from sprites import * +from AI.decision_tree import * +from src.dimensions import * +from src.sprites import * + class Plant(pygame.sprite.Sprite): def __init__(self, field, species): @@ -40,90 +40,7 @@ class Plant(pygame.sprite.Sprite): self.ticks = 0 def dtree(self): - if self.field.hydration == 4: - if self.is_healthy == 1: - if self.field.tractor_there == 0: - if self.ticks == 0: - return 0 - elif self.ticks == 1: - return 1 - elif self.field.tractor_there == 1: - return 0 - elif self.is_healthy == 0: - return 0 - elif self.field.hydration == 2: - if self.species == "sorrel": - if self.ticks == 1: - if self.is_healthy == 1: - return 1 - elif self.is_healthy == 0: - return 0 - elif self.ticks == 0: - return 0 - elif self.species == "potato": - return 0 - elif self.species == "wheat": - return 0 - elif self.species == "strawberry": - return 0 - elif self.field.hydration == 1: - if self.species == "potato": - return 0 - elif self.species == "strawberry": - if self.ticks == 1: - return -1 - elif self.ticks == 0: - return 0 - elif self.species == "wheat": - return 0 - elif self.species == "sorrel": - if self.is_healthy == 0: - return 0 - elif self.is_healthy == 1: - if self.field.tractor_there == 0: - if self.ticks == 0: - return 0 - elif self.ticks == 1: - return 1 - elif self.field.tractor_there == 1: - return 0 - elif self.field.hydration == 3: - if self.ticks == 1: - if self.field.tractor_there == 0: - if self.is_healthy == 1: - if self.species == "potato": - if self.field.fertility == 1: - return 1 - elif self.field.fertility == 0: - return 0 - elif self.species == "strawberry": - return 1 - elif self.species == "sorrel": - return 1 - elif self.species == "wheat": - return 1 - elif self.is_healthy == 0: - return 0 - elif self.field.tractor_there == 1: - return 0 - elif self.ticks == 0: - return 0 - elif self.field.hydration == 5: - if self.field.tractor_there == 1: - return 0 - elif self.field.tractor_there == 0: - if self.is_healthy == 0: - return 0 - elif self.is_healthy == 1: - if self.ticks == 1: - return 1 - elif self.ticks == 0: - return 0 - elif self.field.hydration == 0: - if self.ticks == 0: - return 0 - elif self.ticks == 1: - return -1 + decision_tree(self) def update(self): if self.growth == 0: diff --git a/src/cases.py b/src/cases.py new file mode 100644 index 0000000..0e1a0f5 --- /dev/null +++ b/src/cases.py @@ -0,0 +1,260 @@ +class Case: + def __init__(self, values, attributes, Class): + self.values = values + self.attributes = attributes + self.Class = Class + + +attributes = ["field.hydration", "field.fertility", "species", "ticks", "is_healthy", "field.tractor_there"] + +cases = [Case([4, 0, "potato", 0, 1, 0], attributes, 0), + Case([2, 0, "sorrel", 1, 1, 0], attributes, 1), + Case([4, 1, "wheat", 0, 0, 1], attributes, 0), + Case([1, 1, "potato", 1, 0, 1], attributes, 0), + Case([2, 1, "potato", 0, 0, 1], attributes, 0), + Case([2, 0, "potato", 0, 1, 0], attributes, 0), + Case([1, 1, "strawberry", 1, 0, 1], attributes, -1), + Case([1, 0, "wheat", 1, 1, 1], attributes, 0), + Case([2, 0, "wheat", 1, 0, 1], attributes, 0), + Case([1, 1, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 1, "potato", 1, 1, 0], attributes, 1), + Case([2, 1, "strawberry", 1, 0, 0], attributes, 0), + Case([4, 0, "wheat", 1, 1, 1], attributes, 0), + Case([4, 1, "wheat", 1, 0, 1], attributes, 0), + Case([5, 1, "potato", 1, 1, 1], attributes, 0), + Case([4, 0, "strawberry", 1, 0, 0], attributes, 0), + Case([1, 1, "sorrel", 1, 0, 0], attributes, 0), + Case([0, 0, "sorrel", 0, 0, 1], attributes, 0), + Case([2, 0, "sorrel", 1, 1, 0], attributes, 1), + Case([0, 1, "sorrel", 0, 1, 1], attributes, 0), + Case([0, 1, "strawberry", 1, 1, 0], attributes, -1), + Case([2, 0, "sorrel", 0, 1, 1], attributes, 0), + Case([4, 0, "wheat", 1, 0, 1], attributes, 0), + Case([5, 0, "wheat", 0, 0, 0], attributes, 0), + Case([0, 0, "strawberry", 1, 0, 0], attributes, -1), + Case([4, 0, "sorrel", 1, 0, 0], attributes, 0), + Case([3, 0, "sorrel", 0, 0, 1], attributes, 0), + Case([3, 1, "potato", 1, 0, 1], attributes, 0), + Case([4, 1, "potato", 0, 0, 1], attributes, 0), + Case([1, 1, "wheat", 0, 1, 1], attributes, 0), + Case([3, 0, "wheat", 0, 1, 1], attributes, 0), + Case([2, 0, "wheat", 0, 0, 0], attributes, 0), + Case([5, 1, "potato", 1, 1, 1], attributes, 0), + Case([4, 1, "strawberry", 0, 0, 1], attributes, 0), + Case([1, 0, "potato", 1, 1, 0], attributes, 0), + Case([4, 1, "sorrel", 1, 1, 1], attributes, 0), + Case([0, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([4, 0, "strawberry", 1, 0, 0], attributes, 0), + Case([4, 0, "sorrel", 1, 0, 0], attributes, 0), + Case([5, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 1, "wheat", 1, 1, 1], attributes, 0), + Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([4, 0, "potato", 0, 0, 1], attributes, 0), + Case([5, 1, "wheat", 1, 1, 1], attributes, 0), + Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 0, "sorrel", 0, 0, 1], attributes, 0), + Case([0, 0, "potato", 1, 1, 1], attributes, -1), + Case([4, 0, "strawberry", 1, 1, 1], attributes, 0), + Case([2, 1, "strawberry", 1, 0, 1], attributes, 0), + Case([2, 1, "wheat", 0, 0, 1], attributes, 0), + Case([2, 1, "sorrel", 1, 1, 0], attributes, 1), + Case([1, 0, "potato", 1, 0, 1], attributes, 0), + Case([4, 0, "strawberry", 1, 0, 0], attributes, 0), + Case([4, 1, "potato", 1, 1, 1], attributes, 0), + Case([0, 0, "strawberry", 1, 0, 1], attributes, -1), + Case([0, 1, "wheat", 0, 0, 0], attributes, 0), + Case([1, 1, "wheat", 0, 1, 0], attributes, 0), + Case([0, 0, "sorrel", 1, 1, 0], attributes, -1), + Case([2, 0, "sorrel", 0, 0, 1], attributes, 0), + Case([5, 1, "wheat", 1, 1, 1], attributes, 0), + Case([2, 0, "strawberry", 0, 1, 0], attributes, 0), + Case([2, 1, "wheat", 0, 0, 1], attributes, 0), + Case([3, 0, "potato", 1, 0, 1], attributes, 0), + Case([5, 0, "wheat", 1, 1, 1], attributes, 0), + Case([0, 1, "strawberry", 1, 0, 0], attributes, -1), + Case([0, 0, "wheat", 0, 0, 0], attributes, 0), + Case([5, 0, "potato", 1, 1, 0], attributes, 1), + Case([2, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 1, "sorrel", 0, 1, 0], attributes, 0), + Case([2, 1, "potato", 1, 1, 1], attributes, 0), + Case([5, 0, "strawberry", 1, 1, 0], attributes, 1), + Case([5, 0, "wheat", 0, 0, 0], attributes, 0), + Case([5, 0, "wheat", 1, 1, 0], attributes, 1), + Case([2, 0, "potato", 1, 0, 0], attributes, 0), + Case([3, 1, "wheat", 0, 1, 0], attributes, 0), + Case([3, 0, "potato", 1, 1, 1], attributes, 0), + Case([0, 1, "sorrel", 1, 1, 1], attributes, -1), + Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), + Case([2, 1, "strawberry", 0, 1, 0], attributes, 0), + Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), + Case([3, 0, "wheat", 0, 1, 1], attributes, 0), + Case([4, 0, "strawberry", 1, 0, 1], attributes, 0), + Case([3, 1, "potato", 0, 0, 1], attributes, 0), + Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([5, 1, "wheat", 1, 0, 1], attributes, 0), + Case([5, 0, "potato", 1, 0, 1], attributes, 0), + Case([3, 0, "potato", 0, 0, 1], attributes, 0), + Case([1, 0, "wheat", 0, 1, 0], attributes, 0), + Case([5, 0, "sorrel", 0, 1, 1], attributes, 0), + Case([4, 0, "potato", 0, 1, 0], attributes, 0), + Case([0, 0, "strawberry", 0, 0, 0], attributes, 0), + Case([5, 0, "sorrel", 1, 1, 0], attributes, 1), + Case([4, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([1, 1, "strawberry", 1, 1, 0], attributes, -1), + Case([5, 0, "strawberry", 1, 0, 0], attributes, 0), + Case([5, 1, "wheat", 0, 0, 0], attributes, 0), + Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), + Case([3, 1, "potato", 1, 0, 1], attributes, 0), + Case([1, 0, "sorrel", 0, 0, 0], attributes, 0), + Case([3, 0, "wheat", 0, 1, 1], attributes, 0), + Case([0, 0, "wheat", 0, 0, 1], attributes, 0), + Case([1, 0, "potato", 0, 0, 0], attributes, 0), + Case([1, 1, "sorrel", 0, 1, 0], attributes, 0), + Case([0, 1, "strawberry", 0, 1, 0], attributes, 0), + Case([5, 1, "potato", 1, 0, 1], attributes, 0), + Case([2, 0, "strawberry", 1, 1, 1], attributes, 0), + Case([4, 1, "wheat", 1, 0, 0], attributes, 0), + Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), + Case([1, 0, "strawberry", 1, 1, 1], attributes, -1), + Case([4, 0, "wheat", 0, 0, 0], attributes, 0), + Case([4, 0, "strawberry", 0, 1, 0], attributes, 0), + Case([0, 0, "sorrel", 1, 0, 1], attributes, -1), + Case([1, 0, "strawberry", 0, 1, 0], attributes, 0), + Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), + Case([2, 1, "potato", 0, 0, 0], attributes, 0), + Case([3, 1, "strawberry", 1, 1, 0], attributes, 1), + Case([1, 0, "sorrel", 1, 1, 1], attributes, 0), + Case([5, 1, "strawberry", 1, 1, 0], attributes, 1), + Case([2, 0, "wheat", 1, 1, 1], attributes, 0), + Case([5, 1, "strawberry", 0, 1, 0], attributes, 0), + Case([1, 1, "wheat", 0, 1, 1], attributes, 0), + Case([1, 1, "potato", 0, 1, 0], attributes, 0), + Case([4, 0, "potato", 1, 1, 0], attributes, 1), + Case([2, 1, "strawberry", 0, 0, 1], attributes, 0), + Case([0, 1, "potato", 0, 0, 1], attributes, 0), + Case([3, 0, "sorrel", 1, 0, 1], attributes, 0), + Case([4, 0, "wheat", 1, 0, 1], attributes, 0), + Case([5, 1, "sorrel", 1, 0, 0], attributes, 0), + Case([1, 0, "sorrel", 1, 0, 0], attributes, 0), + Case([5, 0, "sorrel", 1, 0, 1], attributes, 0), + Case([2, 1, "potato", 1, 0, 1], attributes, 0), + Case([1, 0, "potato", 1, 1, 1], attributes, 0), + Case([4, 0, "wheat", 1, 1, 0], attributes, 1), + Case([0, 0, "sorrel", 0, 1, 0], attributes, 0), + Case([2, 0, "wheat", 0, 0, 1], attributes, 0), + Case([0, 1, "potato", 1, 0, 1], attributes, -1), + Case([0, 1, "sorrel", 1, 0, 1], attributes, -1), + Case([1, 1, "potato", 0, 0, 1], attributes, 0), + Case([3, 0, "sorrel", 1, 1, 1], attributes, 0), + Case([0, 0, "potato", 1, 0, 1], attributes, -1), + Case([4, 0, "potato", 0, 0, 1], attributes, 0), + Case([0, 0, "strawberry", 1, 0, 0], attributes, -1), + Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), + Case([5, 0, "potato", 0, 1, 1], attributes, 0), + Case([2, 0, "potato", 0, 0, 0], attributes, 0), + Case([0, 1, "potato", 1, 0, 0], attributes, -1), + Case([1, 0, "potato", 0, 0, 1], attributes, 0), + Case([4, 0, "sorrel", 1, 0, 1], attributes, 0), + Case([1, 0, "potato", 1, 0, 0], attributes, 0), + Case([5, 1, "potato", 1, 0, 1], attributes, 0), + Case([2, 1, "wheat", 1, 0, 0], attributes, 0), + Case([0, 1, "potato", 1, 1, 1], attributes, -1), + Case([5, 1, "strawberry", 0, 1, 0], attributes, 0), + Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 0, "strawberry", 1, 0, 0], attributes, 0), + Case([0, 1, "strawberry", 0, 0, 1], attributes, 0), + Case([0, 1, "wheat", 0, 0, 0], attributes, 0), + Case([0, 1, "strawberry", 1, 0, 0], attributes, -1), + Case([1, 0, "potato", 0, 0, 1], attributes, 0), + Case([1, 1, "wheat", 0, 0, 0], attributes, 0), + Case([0, 1, "strawberry", 1, 0, 1], attributes, -1), + Case([1, 1, "potato", 0, 0, 1], attributes, 0), + Case([0, 0, "wheat", 0, 0, 1], attributes, 0), + Case([4, 1, "sorrel", 1, 1, 1], attributes, 0), + Case([5, 1, "wheat", 0, 0, 1], attributes, 0), + Case([5, 1, "strawberry", 0, 0, 0], attributes, 0), + Case([4, 1, "wheat", 0, 0, 1], attributes, 0), + Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([1, 1, "potato", 1, 1, 0], attributes, 0), + Case([0, 1, "sorrel", 1, 0, 0], attributes, -1), + Case([5, 0, "sorrel", 1, 0, 0], attributes, 0), + Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), + Case([0, 0, "potato", 0, 0, 0], attributes, 0), + Case([0, 0, "strawberry", 1, 1, 1], attributes, -1), + Case([4, 0, "strawberry", 0, 0, 1], attributes, 0), + Case([2, 0, "potato", 1, 0, 0], attributes, 0), + Case([4, 0, "strawberry", 0, 0, 0], attributes, 0), + Case([0, 1, "strawberry", 0, 1, 1], attributes, 0), + Case([1, 1, "wheat", 1, 1, 0], attributes, 0), + Case([3, 0, "potato", 0, 1, 0], attributes, 0), + Case([1, 1, "wheat", 0, 1, 0], attributes, 0), + Case([1, 1, "sorrel", 0, 0, 1], attributes, 0), + Case([3, 1, "wheat", 0, 0, 0], attributes, 0), + Case([3, 1, "wheat", 0, 0, 0], attributes, 0), + Case([1, 0, "wheat", 0, 1, 1], attributes, 0), + Case([5, 1, "potato", 1, 0, 1], attributes, 0), + Case([5, 0, "wheat", 0, 0, 1], attributes, 0), + Case([2, 1, "sorrel", 0, 1, 0], attributes, 0), + Case([5, 0, "sorrel", 1, 0, 0], attributes, 0), + Case([1, 0, "potato", 1, 1, 1], attributes, 0), + Case([5, 1, "wheat", 1, 1, 0], attributes, 1), + Case([3, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([5, 0, "wheat", 0, 0, 1], attributes, 0), + Case([2, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([1, 0, "potato", 1, 1, 1], attributes, 0), + Case([0, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([5, 1, "potato", 1, 1, 0], attributes, 1), + Case([2, 0, "wheat", 0, 1, 0], attributes, 0), + Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 0, "potato", 1, 1, 0], attributes, 0), + Case([1, 1, "potato", 0, 0, 0], attributes, 0), + Case([2, 1, "potato", 0, 1, 0], attributes, 0), + Case([2, 1, "wheat", 0, 1, 0], attributes, 0), + Case([2, 0, "sorrel", 1, 1, 0], attributes, 1), + Case([3, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([2, 0, "wheat", 1, 1, 1], attributes, 0), + Case([5, 0, "wheat", 1, 1, 0], attributes, 1), + Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([0, 1, "potato", 0, 0, 0], attributes, 0), + Case([5, 1, "strawberry", 1, 1, 1], attributes, 0), + Case([4, 1, "wheat", 1, 0, 0], attributes, 0), + Case([5, 1, "sorrel", 0, 0, 1], attributes, 0), + Case([1, 1, "wheat", 1, 0, 0], attributes, 0), + Case([5, 0, "strawberry", 1, 0, 1], attributes, 0), + Case([5, 0, "wheat", 1, 0, 1], attributes, 0), + Case([2, 0, "potato", 1, 0, 1], attributes, 0), + Case([3, 1, "wheat", 1, 0, 0], attributes, 0), + Case([0, 1, "strawberry", 1, 0, 0], attributes, -1), + Case([0, 1, "strawberry", 0, 1, 1], attributes, 0), + Case([3, 0, "wheat", 0, 1, 0], attributes, 0), + Case([4, 1, "potato", 1, 1, 1], attributes, 0), + Case([3, 0, "potato", 0, 0, 1], attributes, 0), + Case([2, 1, "strawberry", 1, 1, 0], attributes, 0), + Case([1, 1, "sorrel", 0, 0, 0], attributes, 0), + Case([4, 1, "wheat", 1, 0, 1], attributes, 0), + Case([2, 0, "potato", 0, 1, 0], attributes, 0), + Case([5, 0, "sorrel", 0, 1, 1], attributes, 0), + Case([0, 1, "wheat", 1, 1, 0], attributes, -1), + Case([5, 1, "wheat", 1, 0, 0], attributes, 0), + Case([2, 0, "potato", 0, 0, 0], attributes, 0), + Case([2, 0, "strawberry", 0, 1, 1], attributes, 0), + Case([4, 1, "potato", 0, 1, 1], attributes, 0), + Case([0, 1, "sorrel", 1, 1, 0], attributes, -1), + Case([1, 1, "strawberry", 1, 0, 1], attributes, -1), + Case([3, 0, "sorrel", 1, 1, 0], attributes, 1), + Case([5, 1, "wheat", 1, 0, 0], attributes, 0), + Case([4, 0, "sorrel", 1, 1, 0], attributes, 1), + Case([2, 1, "sorrel", 1, 0, 0], attributes, 0), + Case([0, 1, "wheat", 0, 1, 0], attributes, 0), + Case([5, 0, "potato", 1, 1, 0], attributes, 1), + Case([3, 1, "strawberry", 0, 1, 0], attributes, 0), + Case([5, 1, "strawberry", 0, 0, 0], attributes, 0), + Case([4, 1, "potato", 1, 1, 1], attributes, 0), + Case([5, 1, "potato", 1, 0, 1], attributes, 0), + Case([5, 1, "potato", 1, 1, 1], attributes, 0), + Case([0, 0, "sorrel", 0, 0, 0], attributes, 0), + Case([1, 1, "sorrel", 1, 1, 0], attributes, 1), + Case([0, 1, "potato", 0, 1, 0], attributes, 0), + Case([4, 1, "strawberry", 1, 1, 1], attributes, 0), + Case([0, 0, "wheat", 0, 1, 1], attributes, 0), + Case([3, 0, "wheat", 1, 1, 0], attributes, 1)] diff --git a/colors.py b/src/colors.py similarity index 92% rename from colors.py rename to src/colors.py index 9d96429..603711b 100644 --- a/colors.py +++ b/src/colors.py @@ -6,6 +6,7 @@ BROWN1 = (160, 130, 70) BROWN2 = (140, 110, 55) BROWN3 = (110, 85, 40) BROWN4 = (80, 60, 20) +BROWN5 = (80, 60, 20) DBROWN = (65, 50, 20) LBROWN = (108, 97, 62) BLUE = (18, 93, 156) diff --git a/dimensions.py b/src/dimensions.py similarity index 95% rename from dimensions.py rename to src/dimensions.py index 8983dab..833dfb4 100644 --- a/dimensions.py +++ b/src/dimensions.py @@ -1,13 +1,13 @@ -# Grid size -GSIZE = 10 - -# This sets the WIDTH and HEIGHT of each grid location -WIDTH = 35 -HEIGHT = 35 - -# This sets the margin between each cell -MARGIN = 5 - -# Window size -SCREEN_WIDTH = GSIZE * (WIDTH + MARGIN) + MARGIN -SCREEN_HEIGHT = GSIZE * (HEIGHT + MARGIN) + MARGIN +# Grid size +GSIZE = 10 + +# This sets the WIDTH and HEIGHT of each grid location +WIDTH = 35 +HEIGHT = 35 + +# This sets the margin between each cell +MARGIN = 5 + +# Window size +SCREEN_WIDTH = GSIZE * (WIDTH + MARGIN) + MARGIN +SCREEN_HEIGHT = GSIZE * (HEIGHT + MARGIN) + MARGIN diff --git a/src/mapschema.py b/src/mapschema.py new file mode 100644 index 0000000..0403d45 --- /dev/null +++ b/src/mapschema.py @@ -0,0 +1,28 @@ +def createField(): + field = [["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"], + ["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"], + ["soil", "soil", "soil", "soil", "soil", "road", "road", "road", "road", "road"], + ["rocks", "rocks", "rocks", "rocks", "soil", "road", "soil", "soil", "rocks", "soil"], + ["soil", "soil", "soil", "soil", "soil", "road", "rocks", "rocks", "soil", "soil"], + ["soil", "soil", "soil", "pond", "rocks", "road", "rocks", "soil", "soil", "rocks"], + ["rocks", "pond", "pond", "pond", "pond", "road", "rocks", "soil", "soil", "rocks"], + ["road", "road", "road", "road", "road", "road", "rocks", "soil", "soil", "soil"], + ["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks"], + ["soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks", "soil"] + ] + return field + + +def createPlants(): + field = [["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"], + ["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"], + ["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + ["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0], + ["wheat", "wheat", "wheat", 0, 0, 0, 0, "potato", "potato", 0], + [0, 0, 0, 0, 0, 0, 0, "potato", "potato", 0], + [0, 0, 0, 0, 0, 0, 0, "potato", "potato", "potato"], + ["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, "potato", 0, 0], + ["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, 0, 0, 0] + ] + return field diff --git a/sprites.py b/src/sprites.py similarity index 99% rename from sprites.py rename to src/sprites.py index 2aafd8b..2d211a0 100644 --- a/sprites.py +++ b/src/sprites.py @@ -1,4 +1,5 @@ import os + import pygame # set up asset folders diff --git a/sprites/plant0.png b/src/sprites/plant0.png similarity index 100% rename from sprites/plant0.png rename to src/sprites/plant0.png diff --git a/sprites/plant1.png b/src/sprites/plant1.png similarity index 100% rename from sprites/plant1.png rename to src/sprites/plant1.png diff --git a/sprites/plant2.png b/src/sprites/plant2.png similarity index 100% rename from sprites/plant2.png rename to src/sprites/plant2.png diff --git a/sprites/plant3.png b/src/sprites/plant3.png similarity index 100% rename from sprites/plant3.png rename to src/sprites/plant3.png diff --git a/sprites/potato0.png b/src/sprites/potato0.png similarity index 100% rename from sprites/potato0.png rename to src/sprites/potato0.png diff --git a/sprites/potato1.png b/src/sprites/potato1.png similarity index 100% rename from sprites/potato1.png rename to src/sprites/potato1.png diff --git a/sprites/potato2.png b/src/sprites/potato2.png similarity index 100% rename from sprites/potato2.png rename to src/sprites/potato2.png diff --git a/sprites/potato3.png b/src/sprites/potato3.png similarity index 100% rename from sprites/potato3.png rename to src/sprites/potato3.png diff --git a/sprites/strawberry0.png b/src/sprites/strawberry0.png similarity index 100% rename from sprites/strawberry0.png rename to src/sprites/strawberry0.png diff --git a/sprites/strawberry1.png b/src/sprites/strawberry1.png similarity index 100% rename from sprites/strawberry1.png rename to src/sprites/strawberry1.png diff --git a/sprites/strawberry2.png b/src/sprites/strawberry2.png similarity index 100% rename from sprites/strawberry2.png rename to src/sprites/strawberry2.png diff --git a/sprites/strawberry3.png b/src/sprites/strawberry3.png similarity index 100% rename from sprites/strawberry3.png rename to src/sprites/strawberry3.png diff --git a/sprites/tractor0.png b/src/sprites/tractor0.png similarity index 100% rename from sprites/tractor0.png rename to src/sprites/tractor0.png diff --git a/sprites/tractor1.png b/src/sprites/tractor1.png similarity index 100% rename from sprites/tractor1.png rename to src/sprites/tractor1.png diff --git a/sprites/tractor2.png b/src/sprites/tractor2.png similarity index 100% rename from sprites/tractor2.png rename to src/sprites/tractor2.png diff --git a/sprites/tractor3.png b/src/sprites/tractor3.png similarity index 100% rename from sprites/tractor3.png rename to src/sprites/tractor3.png diff --git a/sprites/wheat0.png b/src/sprites/wheat0.png similarity index 100% rename from sprites/wheat0.png rename to src/sprites/wheat0.png diff --git a/sprites/wheat1.png b/src/sprites/wheat1.png similarity index 100% rename from sprites/wheat1.png rename to src/sprites/wheat1.png diff --git a/sprites/wheat2.png b/src/sprites/wheat2.png similarity index 100% rename from sprites/wheat2.png rename to src/sprites/wheat2.png diff --git a/sprites/wheat3.png b/src/sprites/wheat3.png similarity index 100% rename from sprites/wheat3.png rename to src/sprites/wheat3.png diff --git a/test/0_100.jpg b/src/test/0_100.jpg similarity index 100% rename from test/0_100.jpg rename to src/test/0_100.jpg diff --git a/test/1_100.jpg b/src/test/1_100.jpg similarity index 100% rename from test/1_100.jpg rename to src/test/1_100.jpg diff --git a/test/4_100.jpg b/src/test/4_100.jpg similarity index 100% rename from test/4_100.jpg rename to src/test/4_100.jpg diff --git a/test/5_100.jpg b/src/test/5_100.jpg similarity index 100% rename from test/5_100.jpg rename to src/test/5_100.jpg diff --git a/train/apple/156_100.jpg b/src/train/apple/156_100.jpg similarity index 100% rename from train/apple/156_100.jpg rename to src/train/apple/156_100.jpg diff --git a/train/apple/165_100.jpg b/src/train/apple/165_100.jpg similarity index 100% rename from train/apple/165_100.jpg rename to src/train/apple/165_100.jpg diff --git a/train/apple/18_100.jpg b/src/train/apple/18_100.jpg similarity index 100% rename from train/apple/18_100.jpg rename to src/train/apple/18_100.jpg diff --git a/train/apple/31_100.jpg b/src/train/apple/31_100.jpg similarity index 100% rename from train/apple/31_100.jpg rename to src/train/apple/31_100.jpg diff --git a/train/apple/r_105_100.jpg b/src/train/apple/r_105_100.jpg similarity index 100% rename from train/apple/r_105_100.jpg rename to src/train/apple/r_105_100.jpg diff --git a/train/apple/r_118_100.jpg b/src/train/apple/r_118_100.jpg similarity index 100% rename from train/apple/r_118_100.jpg rename to src/train/apple/r_118_100.jpg diff --git a/train/apple/r_120_100.jpg b/src/train/apple/r_120_100.jpg similarity index 100% rename from train/apple/r_120_100.jpg rename to src/train/apple/r_120_100.jpg diff --git a/train/apple/r_140_100.jpg b/src/train/apple/r_140_100.jpg similarity index 100% rename from train/apple/r_140_100.jpg rename to src/train/apple/r_140_100.jpg diff --git a/train/apple/r_157_100.jpg b/src/train/apple/r_157_100.jpg similarity index 100% rename from train/apple/r_157_100.jpg rename to src/train/apple/r_157_100.jpg diff --git a/train/apple/r_15_100.jpg b/src/train/apple/r_15_100.jpg similarity index 100% rename from train/apple/r_15_100.jpg rename to src/train/apple/r_15_100.jpg diff --git a/train/potato/143_100.jpg b/src/train/potato/143_100.jpg similarity index 100% rename from train/potato/143_100.jpg rename to src/train/potato/143_100.jpg diff --git a/train/potato/145_100.jpg b/src/train/potato/145_100.jpg similarity index 100% rename from train/potato/145_100.jpg rename to src/train/potato/145_100.jpg diff --git a/train/potato/156_100.jpg b/src/train/potato/156_100.jpg similarity index 100% rename from train/potato/156_100.jpg rename to src/train/potato/156_100.jpg diff --git a/train/potato/21_100.jpg b/src/train/potato/21_100.jpg similarity index 100% rename from train/potato/21_100.jpg rename to src/train/potato/21_100.jpg diff --git a/train/potato/r2_146_100.jpg b/src/train/potato/r2_146_100.jpg similarity index 100% rename from train/potato/r2_146_100.jpg rename to src/train/potato/r2_146_100.jpg diff --git a/train/potato/r2_175_100.jpg b/src/train/potato/r2_175_100.jpg similarity index 100% rename from train/potato/r2_175_100.jpg rename to src/train/potato/r2_175_100.jpg diff --git a/train/potato/r2_9_100.jpg b/src/train/potato/r2_9_100.jpg similarity index 100% rename from train/potato/r2_9_100.jpg rename to src/train/potato/r2_9_100.jpg diff --git a/train/potato/r_133_100.jpg b/src/train/potato/r_133_100.jpg similarity index 100% rename from train/potato/r_133_100.jpg rename to src/train/potato/r_133_100.jpg diff --git a/train/potato/r_191_100.jpg b/src/train/potato/r_191_100.jpg similarity index 100% rename from train/potato/r_191_100.jpg rename to src/train/potato/r_191_100.jpg diff --git a/train/potato/r_77_100.jpg b/src/train/potato/r_77_100.jpg similarity index 100% rename from train/potato/r_77_100.jpg rename to src/train/potato/r_77_100.jpg diff --git a/tractor.py b/tractor.py index 8ba6bc4..e090c10 100644 --- a/tractor.py +++ b/tractor.py @@ -1,20 +1,8 @@ -import pygame +from pygame.locals import (K_c) -from pygame.locals import ( - K_UP, - K_DOWN, - K_LEFT, - K_RIGHT, - K_ESCAPE, - K_SPACE, - K_c, - KEYDOWN, - QUIT -) +from src.dimensions import * +from src.sprites import * -from dimensions import * -from colors import * -from sprites import * class Tractor(pygame.sprite.Sprite): def __init__(self, field, position): @@ -27,33 +15,32 @@ class Tractor(pygame.sprite.Sprite): topleft=((MARGIN + WIDTH) * self.position[0] + MARGIN, (MARGIN + HEIGHT) * self.position[1] + MARGIN)) self.direction = [1, 0] self.field[self.position[0]][self.position[1]].tractor_there = True - + def move(self): self.field[self.position[0]][self.position[1]].tractor_there = False - self.rect.move_ip(self.direction[0]*(WIDTH + MARGIN), self.direction[1]*(HEIGHT + MARGIN)) + self.rect.move_ip(self.direction[0] * (WIDTH + MARGIN), self.direction[1] * (HEIGHT + MARGIN)) self.position[0] += self.direction[0] self.position[1] += self.direction[1] if self.position[0] >= GSIZE: - self.position[0] = GSIZE-1 + self.position[0] = GSIZE - 1 if self.position[1] >= GSIZE: - self.position[1] = GSIZE-1 + self.position[1] = GSIZE - 1 if self.position[0] < 0: self.position[0] = 0 if self.position[1] < 0: self.position[1] = 0 if self.rect.top <= MARGIN: - self.rect.top = MARGIN - if self.rect.bottom >= SCREEN_HEIGHT-MARGIN: - self.rect.bottom = SCREEN_HEIGHT-MARGIN + self.rect.top = MARGIN + if self.rect.bottom >= SCREEN_HEIGHT - MARGIN: + self.rect.bottom = SCREEN_HEIGHT - MARGIN if self.rect.left < MARGIN: - self.rect.left = MARGIN - if self.rect.right > SCREEN_WIDTH-MARGIN: - self.rect.right = SCREEN_WIDTH-MARGIN - + self.rect.left = MARGIN + if self.rect.right > SCREEN_WIDTH - MARGIN: + self.rect.right = SCREEN_WIDTH - MARGIN + self.field[self.position[0]][self.position[1]].tractor_there = True - def rotate_right(self): if self.direction == [1, 0]: self.direction = [0, 1]