diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..36930ba --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/cursed_files/ diff --git a/config.py b/config.py deleted file mode 100644 index 2eb908c..0000000 --- a/config.py +++ /dev/null @@ -1,64 +0,0 @@ -import random -import numpy -import os -import pygame -############Dane początkowe############# -#Szerokość i wysokość okna gry -ILE_RUCHOW = 0 -SZER = 850 -WYS = 1000 -#Kolor okna -COL = (0,255,0) - -#Nasze pole -POLE_SZER = 700 -POLE_WYS = 700 -POLE_COL = (128,128,0) -POLE_POZ = (75, 75) -TRAKTOR_POZ = [POLE_POZ[1], POLE_POZ[0]] -text_rect = [POLE_POZ[0], POLE_WYS + POLE_POZ[1] + 100] - -#Nazwa okna -WINDOW_NAME = "SI Projekt" -POLE_STAN = numpy.random.randint(0,9,(10,10)) -print("Reprezentacja pola jako macierz:") -print("###################") -for i in POLE_STAN: - for j in i: - print(j, end=" ") - print("") -print("###################") -images_names = ['gleba_pix.png','gleba_chwasty_pix.png','sadzonka_suchi_pix.png','sadzonka_chwasty_pix.png','gleba_mokra_pix.png','gleba_mokra_chwasty.png','sadzonka_mokra_pix.png','sadzonka_mokra_chwasty_pix.png','gotowy_burak_pix.png'] -images = [] -for i in images_names: - images.append(pygame.image.load('images/'+i)) - - -def mat_val(traktor_poz,value): - POLE_STAN[traktor_poz[0],traktor_poz[1]]+=value - - -class img: - traktor = ["traktor_d.png","traktor_l.png","traktor_u.png","traktor_r.png"] - turn = 0 - def traktor_get(self): - return self.traktor[self.turn] - - def traktor_turn(self, turn): - self.turn = turn - -traktor = img() - - -class activities: - modes = ["Nawadnianie", "Odchwaszczanie", "Sadzenie", "Zbiory"] - mode = 0 - - def activity_val(self,value): - self.mode = value - print("Tryb: "+ str(self.modes[self.mode])) - - def activity_get_value(self): - return self.mode - -activity = activities() \ No newline at end of file diff --git a/field.py b/field.py new file mode 100644 index 0000000..f703b74 --- /dev/null +++ b/field.py @@ -0,0 +1,22 @@ +import numpy +import random + +class field(): + def __init__(self): + self.field_matrix = numpy.random.randint(0, 9, (10, 10)) + print("Reprezentacja pola jako macierz:") + print("###################") + for i in self.field_matrix: + for j in i: + print(j, end=" ") + print("") + print("###################") + + def get_matrix(self): + return self.field_matrix + + def change_value(self, position, value): + self.field_matrix[position[1],position[0]] += value + + def get_value(self,position): + return self.field_matrix[position[1],position[0]] \ No newline at end of file diff --git a/functions.py b/functions.py deleted file mode 100644 index 80b0e0b..0000000 --- a/functions.py +++ /dev/null @@ -1,196 +0,0 @@ -import pygame -import sys, time -from pygame.locals import * -import config -from queue import PriorityQueue - - -def quit(): - print("Zamykanie...") - pygame.quit() - sys.exit() - -def pressed(key, traktor_poz): - if key[K_d]: - return(move_right()) - if key[K_s]: - return(move_down()) - if key[K_a]: - return(move_left()) - if key[K_w]: - return(move_up()) - if key[K_SPACE]: - work(traktor_poz) - if key[K_1]: - config.activity.activity_val(0) - if key[K_2]: - config.activity.activity_val(1) - if key[K_3]: - config.activity.activity_val(2) - if key[K_4]: - config.activity.activity_val(3) - if key[K_p]: - pathfinding() - - -def move_left(): - if config.TRAKTOR_POZ[0]==config.POLE_POZ[1]: - pass - else: - config.TRAKTOR_POZ[0]-=70 - print("Ruch w lewo") - config.traktor.traktor_turn(1) - return 1 - - -def move_up(): - if config.TRAKTOR_POZ[1]==config.POLE_POZ[0]: - pass - else: - config.TRAKTOR_POZ[1]-=70 - print("Ruch w górę") - config.traktor.traktor_turn(2) - return 1 - - - -def move_right(): - if config.TRAKTOR_POZ[0]==705: - pass - else: - config.TRAKTOR_POZ[0]+=70 - config.traktor.traktor_turn(3) - print("Ruch w prawo") - return 1 -def move_down(): - if config.TRAKTOR_POZ[1]==705: - pass - else: - config.TRAKTOR_POZ[1]+=70 - config.traktor.traktor_turn(0) - print("Ruch w dół") - return 1 - - -def work(traktor_poz): - if config.activity.activity_get_value()==0: - water(traktor_poz) - print("Podlanie pola") - - if config.activity.activity_get_value()==1: - weeds(traktor_poz) - print("Odchwaszczenie pola") - - if config.activity.activity_get_value()==2: - plant(traktor_poz) - print("Zasadzenie pola") - - if config.activity.activity_get_value()==3: - harvest(traktor_poz) - print("Zbiory z pola") - -def water(position): - if config.POLE_STAN[position[0],position[1]] in [0,1,2,3]: - config.mat_val([position[0],position[1]],4) - -def weeds(position): - if config.POLE_STAN[position[0], position[1]] in [1,3,5,7]: - config.mat_val([position[0], position[1]], -1) -def plant(position): - if config.POLE_STAN[position[0], position[1]] in [0,1,4,5]: - config.mat_val([position[0], position[1]], 2) -def harvest(position): - if config.POLE_STAN[position[0], position[1]] == 8: - config.mat_val([position[0], position[1]], -8) - - -def heuristic(a, b): - (x1, y1) = a - (x2, y2) = b - return abs(x1 - x2) + abs(y1 - y2) - - -def pathfinding(): - activity = config.activity.activity_get_value() - avaiable_value = [] - if activity == 0: - avaiable_value = [0,1,2,3] - elif activity == 1: - avaiable_value = [1,3,5,7] - elif activity == 2: - avaiable_value = [0,1,4,5] - elif activity == 3: - avaiable_value = [8] - start_position = [int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)] - end_point = search(start_position,avaiable_value) - if start_position == end_point: - work([int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)]) - else: - route = a_star(start_position,end_point) - for i in route[::-1]: - poz = [int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)] - if i[0]> poz[0]: - move_down() - elif i[0]< poz[0]: - move_up() - elif i[1]> poz[1]: - move_right() - elif i[1]< poz[1]: - move_left() - pygame.display.update() - time.sleep(0.2) - work([int(((config.TRAKTOR_POZ[1]-5)/70)-1), int(((config.TRAKTOR_POZ[0]-5)/70)-1)]) - -def a_star(start, end): - a_queue = PriorityQueue() - a_queue.put(start,0) - cost = {tuple(start): 0} - path_from = {tuple(start): None} - finall_path = [tuple(end)] - found = 0 - while not a_queue.empty(): - current = tuple(a_queue.get()) - - if current == tuple(end): - break - - for next in points(current): - new_cost = cost[tuple(current)] + int(config.POLE_STAN[next[0],next[1]]) - if tuple(next) not in cost or new_cost < cost[tuple(next)]: - cost[tuple(next)] = new_cost - priority = new_cost + heuristic(end, next) - a_queue.put(next,priority) - path_from[tuple(next)] = current - if next == end: - found = 1 - break - if found: - break - - pth = path_from[tuple(end)] - while not pth==tuple(start): - finall_path.append(pth) - pth = path_from[pth] - - return finall_path - -def search(start,value): - visited = [start] - while visited: - if config.POLE_STAN[visited[0][0],visited[0][1]] in value: - print("Znaleziono pole: "+str(visited[0])) - return visited[0] - else: - for i in points(visited[0]): - visited.append(i) - del visited[0] - - -def points(point): - points = [] - for i in [[point[0],point[1]-1],[point[0]-1,point[1]],[point[0],point[1]+1],[point[0]+1,point[1]]]: - if i[0] in [-1,10] or i[1] in [-1,10]: - pass - else: - points.append(i) - return points diff --git a/main.py b/main.py index 49080f8..7b23bb3 100644 --- a/main.py +++ b/main.py @@ -1,70 +1,46 @@ -from typing import List - -import pygame -import sys +import pygame, sys +import tractor,pathfinding,field,ui from pygame.locals import * -import functions -import os -from config import * +pole = field.field() +path = pathfinding.pathfinding() +traktor = tractor.tractor(pole) +UI = ui.game_ui(traktor,pole) pygame.init() -#Załadowanie zdjęć - - -#Pole tekstowe -ILE_RUCHOW = 0 - -#Init pola gry -okno = pygame.display.set_mode((SZER, WYS), 0, 32) - -#Tytuł okna -pygame.display.set_caption(WINDOW_NAME) - -#Tworzenie powierzchni pola -pole_surf = pygame.Surface([POLE_SZER,POLE_WYS]) -pole_surf.fill(POLE_COL) -pole_surf_rect = pole_surf.get_rect() -pole_surf_rect.x = POLE_POZ[0] -pole_surf_rect.y = POLE_POZ[1] - -pole1_surf = pygame.Surface([POLE_SZER+150,POLE_WYS+150]) -pole1_surf.fill((0,0,0)) -pole1_surf_rect = pole_surf.get_rect() -pole1_surf_rect.x = POLE_POZ[0] - 75 -pole1_surf_rect.y = POLE_POZ[1] - 75 -#Tworzenie pól pola(w sensie właściwe pola do obróbki) - - -#Zezwalamy na przechwytywanie klawiszy -pygame.event.pump() - -########################################## -#główna pętla +UI.update() +UI.update() while True: for event in pygame.event.get(): - #zamknięcie okna - # print(event) if event.type == QUIT: - quit() + print("Zamykanie...") + pygame.quit() + sys.exit() elif event.type == KEYDOWN: - TRAKTOR_POZ_POLA = [int(((TRAKTOR_POZ[1]-5)/70)-1), int(((TRAKTOR_POZ[0]-5)/70)-1)] - if functions.pressed(pygame.key.get_pressed(),TRAKTOR_POZ_POLA) == 1: - ILE_RUCHOW+=1 + key = pygame.key.get_pressed() + if key[K_d]: + traktor.move_right() + if key[K_s]: + traktor.move_down() + if key[K_a]: + traktor.move_left() + if key[K_w]: + traktor.move_up() + if key[K_SPACE]: + traktor.work() + if key[K_1]: + traktor.set_mode(0) + if key[K_2]: + traktor.set_mode(1) + if key[K_3]: + traktor.set_mode(2) + if key[K_4]: + traktor.set_mode(3) + if key[K_p]: + path.pathfinding(traktor,pole,UI) + if key[K_F10]: + print(traktor.get_poz()) + if key[K_F11]: + print(traktor.get_field_value()) + UI.update() + UI.update() - #kolor okna - okno.fill(COL) - #wstawienie pola - okno.blit(pole1_surf,pole1_surf_rect) - okno.blit(pole_surf,pole_surf_rect) - text_value = "Ile ruchów: " + str(ILE_RUCHOW) + " Tryb: " + activity.modes[activity.activity_get_value()] - font = pygame.font.Font('freesansbold.ttf', 24) - text = font.render(text_value, True, (0, 0, 0), COL) - okno.blit(text, text_rect) - for i in range(0,700,70): - for j in range(0,700,70): - pole_surf.blit(images[POLE_STAN[int(i/70),int(j/70)]],(j,i)) - traktor_img = pygame.image.load('images/'+traktor.traktor_get()) - - okno.blit(traktor_img, TRAKTOR_POZ) - #aktualizacja okna i wyświetlenie - pygame.display.update() \ No newline at end of file diff --git a/pathfinding.py b/pathfinding.py new file mode 100644 index 0000000..c83ca0e --- /dev/null +++ b/pathfinding.py @@ -0,0 +1,107 @@ +from queue import PriorityQueue +import time + +class pathfinding(): + def __init__(self): + pass + + def heuristic(self,a, b): + (x1, y1) = a + (x2, y2) = b + return abs(x1 - x2) + abs(y1 - y2) + + def points(self, point): + self.point = [] + for i in [[point[0],point[1]-1],[point[0]-1,point[1]],[point[0],point[1]+1],[point[0]+1,point[1]]]: + if i[0] in [-1,10] or i[1] in [-1,10]: + pass + else: + self.point.append(i) + return self.point + + def pathfinding(self,traktor,field,ui): + self.ui = ui + self.traktor = traktor + self.activity = self.traktor.get_mode() + self.start_position = self.traktor.get_poz() + self.field = field + if self.activity == 0: + self.avaiable_value = [0,1,2,3] + elif self.activity == 1: + self.avaiable_value = [1,3,5,7] + elif self.activity == 2: + self.avaiable_value = [0,1,4,5] + elif self.activity == 3: + self.avaiable_value = [8] + self.end_point = self.search(self.start_position,self.avaiable_value) + if self.end_point == None: + print("Brak pola") + elif self.start_position == self.end_point: + self.traktor.work() + else: + self.route = self.a_star(self.start_position,self.end_point) + for i in self.route[::-1]: + self.poz = self.traktor.get_poz() + if i[1]> self.poz[1]: + self.traktor.move_down() + elif i[1]< self.poz[1]: + self.traktor.move_up() + elif i[0]> self.poz[0]: + self.traktor.move_right() + elif i[0]< self.poz[0]: + self.traktor.move_left() + self.ui.update() + time.sleep(0.1) + self.traktor.work() + + + def a_star(self,start, end): + self.a_queue = PriorityQueue() + self.a_queue.put(start,0) + self.cost = {tuple(start): 0} + self.path_from = {tuple(start): None} + self.finall_path = [tuple(end)] + self.found = 0 + while not self.a_queue.empty(): + self.current = tuple(self.a_queue.get()) + + if self.current == tuple(end): + break + + for self.next in self.points(self.current): + self.new_cost = self.cost[tuple(self.current)] + self.field.get_value(self.next) + if tuple(self.next) not in self.cost or self.new_cost < self.cost[tuple(self.next)]: + self.cost[tuple(self.next)] = self.new_cost + self.priority = self.new_cost + self.heuristic(end, self.next) + self.a_queue.put(self.next,self.priority) + self.path_from[tuple(self.next)] = self.current + if self.next == end: + self.found = 1 + break + if self.found: + break + + self.pth = self.path_from[tuple(end)] + while not self.pth==tuple(start): + self.finall_path.append(self.pth) + self.pth = self.path_from[self.pth] + + return self.finall_path + + + def search(self,start,value): + self.checked = [] + self.visited = [start] + while self.visited: + if self.field.get_value(self.visited[0]) in value: + print("Znaleziono pole: "+str(self.visited[0])) + return self.visited[0] + else: + self.p = self.points(self.visited[0]) + for i in self.p: + if i not in self.checked: + self.visited.append(i) + self.checked.append(self.visited[0]) + del self.visited[0] + + diff --git a/tractor.py b/tractor.py new file mode 100644 index 0000000..6d68a87 --- /dev/null +++ b/tractor.py @@ -0,0 +1,122 @@ +from pygame.locals import * +import pygame + +class tractor(): + def __init__(self,field): + self.field = field + self.moves = 0 + self.turn = 0 + self.modes = ["Nawadnianie", "Odchwaszczanie", "Sadzenie", "Zbiory"] + self.mode_value = 0 + self.poz = [0,0] + self.poz_on_screen = [75,75] + + def get_field_value(self): + return self.field.get_value(self.poz) + + def get_moves(self): + return self.moves + + def moves_up(self): + self.moves += 1 + + def set_mode(self,value): + self.mode_value = value + print("Tryb: " + str(self.modes[self.mode_value])) + + def get_mode(self): + return self.mode_value + + def get_mode_name(self): + return self.modes[self.mode_value] + + def get_turn(self): + return self.turn + + def set_turn(self, turn): + self.turn = turn + + def get_poz(self): + return self.poz + + def get_poz_on_screen(self): + return self.poz_on_screen + + def set_poz(self,poz): + self.poz = [self.poz[0]+poz[0],self.poz[1]+poz[1]] + + def set_poz_on_screen(self,poz): + self.poz_on_screen = [self.poz_on_screen[0]+poz[0],self.poz_on_screen[1]+poz[1]] + + def move_left(self): + if self.poz[0] == 0: + pass + else: + self.set_poz([-1,0]) + self.set_poz_on_screen([-70,0]) + print("Ruch w lewo") + self.set_turn(1) + self.moves_up() + + def move_up(self): + if self.poz[1] == 0: + pass + else: + self.set_poz([0, -1]) + self.set_poz_on_screen([0, -70]) + print("Ruch w górę") + self.set_turn(2) + self.moves_up() + + def move_right(self): + if self.poz[0] == 9: + pass + else: + self.set_poz([1, 0]) + self.set_poz_on_screen([70, 0]) + self.set_turn(3) + print("Ruch w prawo") + self.moves_up() + + def move_down(self): + if self.poz[1] == 9: + pass + else: + self.set_poz([0, 1]) + self.set_poz_on_screen([0, 70]) + self.set_turn(0) + print("Ruch w dół") + self.moves_up() + + def work(self): + if self.get_mode() == 0: + self.water() + print("Podlanie pola") + + if self.get_mode() == 1: + self.weeds() + print("Odchwaszczenie pola") + + if self.get_mode() == 2: + self.plant() + print("Zasadzenie pola") + + if self.get_mode() == 3: + self.harvest() + print("Zbiory z pola") + + def water(self): + if self.field.get_value(self.poz) in [0, 1, 2, 3]: + self.field.change_value(self.poz,4) + + def weeds(self): + if self.field.get_value(self.poz) in [1, 3, 5, 7]: + self.field.change_value(self.poz,-1) + + def plant(self): + if self.field.get_value(self.poz) in [0, 1, 4, 5]: + self.field.change_value(self.poz,2) + + def harvest(self): + if self.field.get_value(self.poz) == 8: + self.field.change_value(self.poz,-8) \ No newline at end of file diff --git a/ui.py b/ui.py new file mode 100644 index 0000000..6fa34e9 --- /dev/null +++ b/ui.py @@ -0,0 +1,59 @@ +import pygame + +class game_ui(): + + def __init__(self,traktor,field): + self.traktor = traktor + self.field = field + self.window_name = "SI_Projekt" + self.szer = 850 + self.wys = 1000 + self.col =(0,255,0) + self.POLE_SZER = 700 + self.POLE_WYS = 700 + self.POLE_COL = (128, 128, 0) + self.POLE_POZ = (75, 75) + self.text_rect = [self.POLE_POZ[0], self.POLE_WYS + self.POLE_POZ[1] + 100] + + # Init pola gry + self.okno = pygame.display.set_mode((self.szer, self.wys), 0, 32) + + # Tytuł okna + pygame.display.set_caption(self.window_name) + + # Tworzenie powierzchni pola + self.pole_surf = pygame.Surface([self.POLE_SZER, self.POLE_WYS]) + self.pole_surf.fill(self.POLE_COL) + self.pole_surf_rect = self.pole_surf.get_rect() + self.pole_surf_rect.x = self.POLE_POZ[0] + self.pole_surf_rect.y = self.POLE_POZ[1] + self.pole1_surf = pygame.Surface([self.POLE_SZER + 150, self.POLE_WYS + 150]) + self.pole1_surf.fill((0, 0, 0)) + self.pole1_surf_rect = self.pole_surf.get_rect() + self.pole1_surf_rect.x = self.POLE_POZ[0] - 75 + self.pole1_surf_rect.y = self.POLE_POZ[1] - 75 + self.traktor_img = ["traktor_d.png", "traktor_l.png", "traktor_u.png", "traktor_r.png"] + self.field_images = [] + for i in ['gleba_pix.png','gleba_chwasty_pix.png','sadzonka_suchi_pix.png','sadzonka_chwasty_pix.png','gleba_mokra_pix.png','gleba_mokra_chwasty.png','sadzonka_mokra_pix.png','sadzonka_mokra_chwasty_pix.png','gotowy_burak_pix.png']: + self.field_images.append(pygame.image.load('images/'+i)) + + # Zezwalamy na przechwytywanie klawiszy + pygame.event.pump() + + def update(self): + # kolor okna + self.okno.fill(self.col) + # wstawienie pola + self.okno.blit(self.pole1_surf, self.pole1_surf_rect) + self.okno.blit(self.pole_surf, self.pole_surf_rect) + self.text_value = "Ile ruchów: " + str(self.traktor.get_moves()) + " Tryb: " + self.traktor.get_mode_name() + self.font = pygame.font.Font('freesansbold.ttf', 24) + self.text = self.font.render(self.text_value, True, (0, 0, 0), self.col) + self.okno.blit(self.text, self.text_rect) + for i in range(0, 10): + for j in range(0, 10): + self.pole_surf.blit(self.field_images[self.field.get_value([i,j])], (i*70, j*70)) + self.traktor_actual_img = pygame.image.load('images/' + self.traktor_img[self.traktor.get_turn()]) + self.okno.blit(self.traktor_actual_img,self.traktor.get_poz_on_screen()) + # aktualizacja okna i wyświetlenie + pygame.display.update() \ No newline at end of file