From 5cd050a0db1d57b8c0176cc05fab02f9e6719454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kinga=20Jagodzi=C5=84ska?= Date: Tue, 28 Apr 2020 19:06:42 +0000 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tractor.py | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ran.py | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 325 insertions(+) create mode 100644 Tractor.py create mode 100644 ran.py diff --git a/Tractor.py b/Tractor.py new file mode 100644 index 0000000..855e4c7 --- /dev/null +++ b/Tractor.py @@ -0,0 +1,169 @@ +import pygame +from pygame.math import Vector2 +import math as m + + +class Tractor(object): + + def __init__(self, game): + self.game = game + + size = self.game.screen.get_size() + + self.pos = Vector2(22, 22) + self.oy = True + self.oz = False + + # A* + self.g_score = [] + self.f_score = [] + self.h_score = [] + self.came_from = [] + self.neighbours() + self.score() + + #ruszanie się + self.road = self.algo(0, 24) + # obecne pole + + + def tick(self): + + # input + # pressed = pygame.key.get_pressed() + + # if pressed[pygame.K_d]: + # self.add_force(Vector2(self.speed,0)) + # if pressed[pygame.K_s]: + # self.add_force(Vector2(0,self.speed)) + # if pressed[pygame.K_a]: + # self.add_force(Vector2(-self.speed,0)) + # if pressed[pygame.K_w]: + # self.add_force(Vector2(0,-self.speed)) + + if pygame.key.get_pressed()[pygame.K_SPACE]: + self.pole = self.pos.y // 144 * 5 + self.pos.x // 144 + if self.road[0] == self.pole + 1: + self.pos.x = self.pos.x + 144 + elif self.road[0] == self.pole - 1: + self.pos.x = self.pos.x - 144 + elif self.road[0] == self.pole + 5: + self.pos.y = self.pos.y + 144 + elif self.road[0] == self.pole - 5: + self.pos.y = self.pos.y - 144 + self.road.pop(0) + + + + # + # if (self.pos.x >= 576) and (self.pos.y >= 576) and (self.oz == False): + # self.oz = True + # elif (self.pos.x < 576) and (self.oy == True) and (self.oz == False): + # self.pos.x = self.pos.x + 144 + # elif (self.pos.x >= 576) and (self.oy == True) and (self.oz == False): + # self.pos.y = self.pos.y + 144 + # self.oy = False + # elif (self.pos.x > 144) and (self.oy == False) and (self.oz == False): + # self.pos.x = self.pos.x - 144 + # elif (self.pos.x <= 144) and (self.oy == False) and (self.oz == False): + # self.pos.y = self.pos.y + 144 + # self.oy = True + # elif (self.pos.x <= 144) and (self.pos.y <= 144) and (self.oz == True): + # self.oz = False + # elif (self.pos.x < 576) and (self.oy == False) and (self.oz == True): + # self.pos.x = self.pos.x + 144 + # elif (self.pos.x >= 576) and (self.oy == False) and (self.oz == True): + # self.pos.y = self.pos.y - 144 + # self.oy = True + # elif (self.pos.x > 144) and (self.oy == True) and (self.oz == True): + # self.pos.x = self.pos.x - 144 + # elif (self.pos.x <= 144) and (self.oy == True) and (self.oz == True): + # self.pos.y = self.pos.y - 144 + # self.oy = False + + def draw(self): + # drawing + rect = pygame.Rect(self.pos.x, self.pos.y, 100, 100) + pygame.draw.rect(self.game.screen, (255, 255, 0), rect) + + # Sąsiedztwo poszczególnych pól + def neighbours(self): + self.neighbours = list(range(25)) + self.neighbours[0] = [1, 5] + self.neighbours[4] = [3, 9] + self.neighbours[20] = [15, 21] + self.neighbours[24] = [19, 23] + for x in range(1, 4): + self.neighbours[x] = [x - 1, x + 5, x + 1] + for x in range(5, 16, 5): + self.neighbours[x] = [x - 5, x + 1, x + 5] + for x in range(9, 20, 5): + self.neighbours[x] = [x - 5, x - 1, x + 5] + for x in range(21, 24): + self.neighbours[x] = [x - 1, x - 5, x + 1] + for x in [6, 7, 8, 11, 12, 13, 16, 17, 18]: + self.neighbours[x] = [x - 5, x - 1, x + 1, x + 5] + + # Tworzenie list dla g, h, f oraz came_from + def score(self): + for x in range(25): + self.g_score.append(0) + self.f_score.append(0) + self.h_score.append(0) + self.came_from.append(0) + + # Obliczanie h (założenie - odległość pomiędzy sąsiednimi polami wynosi 2, tak jak koszt wjazdu na puste pole) + # s to pole na którym jesteśmy + # f to pole końcowe + def hscore(self, s, f): + if f >= s: + a_h = (f - s) // 5 + else: + a_h = (s - f) // 5 + if f % 5 >= s % 5: + b_h = f % 5 - s % 5 + else: + b_h = s % 5 - f % 5 + 1 + return 2 * m.sqrt(a_h ** 2 + b_h ** 2) + + # A* + def algo(self, start, koniec): + # definiowanie setów + closed_set = [] + open_set = [start] + + while open_set: + # Szukanie pola w open_set z najniższym f + temp1 = max(self.f_score) + 1 + x = 0 + for i in range(len(open_set)): + if self.f_score[open_set[i]] <= temp1: + x = open_set[i] + temp1 = self.f_score[open_set[i]] + + if x == koniec: + return self.reconstruct_path(self.came_from, koniec) + + open_set.remove(x) + closed_set.append(x) + for y in self.neighbours[x]: + if y in closed_set: + continue + tentative_g_score = self.g_score[x] + self.game.fields[y][3] + if y not in open_set: + open_set.append(y) + tentative_is_better = True + elif tentative_g_score < self.g_score[y]: + tentative_is_better = True + if tentative_is_better == True: + self.came_from[y] = x + self.g_score[y] = tentative_g_score + self.f_score[y] = self.g_score[y] + self.hscore(y, koniec) + print("failure") + + def reconstruct_path(self, came_from, current): + total_path = [current] + while came_from[current] != 0: + current = came_from[current] + total_path.insert(0, current) + return total_path diff --git a/ran.py b/ran.py new file mode 100644 index 0000000..0891bb7 --- /dev/null +++ b/ran.py @@ -0,0 +1,156 @@ +import pygame +import sys +import random +from Tractor import Tractor + +# dodać growth rate +# Dodać co jeśli pusta +# Pamiętać o zmianie algorytmu po dodaniu growth rate + + +class Game(object): + def __init__(self): + # Config + self.max_tps = 2.0 + self.res = (720, 720) + + self.fields = [] + self.randomize_field() + + # Initialization + pygame.init() + self.screen = pygame.display.set_mode(self.res) + pygame.display.set_caption('Traktorek') + self.clock = pygame.time.Clock() + self.dt = 0.0 + + self.player = Tractor(self) + + while True: + # Handle events + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit(0) + elif event.type == pygame.K_ESCAPE: + sys.exit(0) + + # Ticking + self.dt += self.clock.tick() / 1000.0 + while self.dt > 1 / self.max_tps: + self.tick() + self.dt -= 1 / self.max_tps + + # Rendering + self.screen.fill((0, 0, 0)) + self.draw() + pygame.display.flip() + + def tick(self): + self.player.tick() + + def draw(self): + self.screen.fill((0, 0, 0)) + self.draw_field() + self.draw_net() + self.player.draw() + pygame.display.update() + + def draw_net(self): + color = (255, 255, 255) + for i in range(1, 5): + krat = int(720 / 5 * i) + # linia pozioma + pygame.draw.line(self.screen, color, (0, krat), (720, krat), 1) + # linia pionowa + pygame.draw.line(self.screen, color, (krat, 0), (krat, 720), 1) + + def randomize_field(self): + for x in range(25): + temp = [] + # nasiona + temp.append(random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka", "puste"])) + # gleba + temp.append(random.choice([True, False])) + # woda + temp.append(random.choice([True, False])) + # # growth rate + # temp.append(random) + # # cost + if temp[0] == "żyto": + temp.append(10) + elif temp[0] == "jęczmień": + temp.append(12) + elif temp[0] == "owies": + temp.append(8) + elif temp[0] == "marchew": + temp.append(14) + elif temp[0] == "rzodkiew": + temp.append(7) + elif temp[0] == "pietruszka": + temp.append(6) + elif temp[0] == "puste": + temp.append(2) + else: + temp.append(0) + + self.fields.append(temp) + + def draw_field(self): + for x in range(25): + self.screen.fill(self.color(x), (144 * (x % 5), 144 * (x // 5), 144 * (x % 5 + 1), 144 * (x // 5 + 1))) + + def color(self, z): + if self.fields[z][0] == 'owies': + return (255, 200, 55) + # if self.fields[z][1] == True: + # if self.fields[z][2] == True: + # return (255, 200, 55) + # elif self.fields[z][2] == False: + # return (255, 200, 100) + # elif self.fields[z][1] == False: + # if self.fields[z][2] == True: + # return (255, 170, 55) + # elif self.fields[z][2] == False: + # return (255, 170, 100) + elif self.fields[z][0] == 'jęczmień': + return (170, 150, 40) + # if self.fields[z][1] == True: + # if self.fields[z][2] == True: + # return (170, 150, 40) + # elif self.fields[z][2] == False: + # return (170, 150, 85) + # elif self.fields[z][1] == False: + # if self.fields[z][2] == True: + # return (170, 120, 40) + # elif self.fields[z][2] == False: + # return (170, 120, 85) + elif self.fields[z][0] == 'żyto': + return (100, 215, 80) + # if self.fields[z][1] == True: + # if self.fields[z][2] == True: + # return (100, 215, 80) + # elif self.fields[z][2] == False: + # return (100, 215, 125) + # elif self.fields[z][1] == False: + # if self.fields[z][2] == True: + # return (100, 185, 80) + # elif self.fields[z][2] == False: + # return (100, 185, 125) + elif self.fields[z][0] == 'marchew': + return (224, 60, 14) + elif self.fields[z][0] == 'rzodkiew': + return (142, 24, 104) + elif self.fields[z][0] == 'pietruszka': + return (254, 247, 246) + else: + return (0,0,0) + + + + + + + + +if __name__ == "__main__": + Game()