From 7e59d2ba833a3b07c5d9474562a4675588245895 Mon Sep 17 00:00:00 2001 From: s464859 <@> Date: Wed, 6 Apr 2022 19:57:42 +0200 Subject: [PATCH] zaktualizowane --- classes/ai.py | 30 +++++++++++++----- classes/minesweeper.py | 71 +++++++++++++++++++++++++++++++++++++----- classes/system.py | 2 ++ main.py | 14 ++++++++- 4 files changed, 100 insertions(+), 17 deletions(-) diff --git a/classes/ai.py b/classes/ai.py index b25f94e..3eac856 100644 --- a/classes/ai.py +++ b/classes/ai.py @@ -20,13 +20,26 @@ class AI: self.saper.set_map(self.current_map) #co ma robić przy każdym FPS'ie - def update(self): + def updateFPS(self): + pass + + #co ma zrobić przy każdym ruchu <------------------------- najważniejsze + def updateTile(self): + #aktualne pola (do debugu) + sensor = self.saper.sensor() + print(sensor[0]) + print(sensor[1]) + print(sensor[2]) + print("-------") + + #podniesienie bomby jeśli jest jakaś na tym polu + self.saper.pick_up() + + #poruszenie się if self.user_controlled: self.minesweeper_controls() return - - self.chaos_controls() - #TU pisać resztę + self.chaos_controls() # <--------------------------zamiast tego trzeba wstawić jakiś algorytm def minesweeper_controls(self): @@ -43,10 +56,11 @@ class AI: def chaos_controls(self): dir = randrange(4) if dir==0: - self.saper.move(0) + self.saper.rotate("N") elif dir==1: - self.saper.move(180) + self.saper.rotate("S") elif dir==2: - self.saper.move(270) + self.saper.rotate("W") elif dir==3: - self.saper.move(90) \ No newline at end of file + self.saper.rotate("E") + self.saper.move() \ No newline at end of file diff --git a/classes/minesweeper.py b/classes/minesweeper.py index 241a4b4..e591683 100644 --- a/classes/minesweeper.py +++ b/classes/minesweeper.py @@ -201,6 +201,7 @@ class NPC: #saper class Minesweeper: size:int + rotation_degrees:int position_x:int position_y:int image:pygame.surface.Surface @@ -223,6 +224,7 @@ class Minesweeper: self.image = pygame.image.load("assets/sprites/saper_fun_sized.png") self.image = pygame.transform.scale(self.image, (self.size, self.size)) self.rotated_image = self.image + self.rotation_degrees=0 def set_map(self, map:Map): self.current_map = map @@ -244,6 +246,8 @@ class Minesweeper: finished=True elif self.offset_y<0: self.offset_y+=dist + if self.offset_y<-self.size and self.offset_y>-1.2*self.size: + pygame.mixer.Channel(1).play(pygame.mixer.Sound("assets/sounds/ledge.wav")) if self.offset_y>=0: finished=True @@ -258,7 +262,24 @@ class Minesweeper: window.blit(self.rotated_image, position_on_screen) self.update_offset(delta) - def move(self, dir:int): + def rotate(self, dir:str): + dirr=0 + if dir=="N": + dirr=180 + elif dir=="S": + dirr=0 + elif dir=="W": + dirr=270 + elif dir=="E": + dirr=90 + else: + return + + self.rotation_degrees=dirr + self.rotated_image = pygame.transform.rotate(self.image, dirr) + + + def move(self, dir:int=-1): #południe - 0 #wschód - 90 #północ - 180 @@ -266,7 +287,11 @@ class Minesweeper: if self.offset_x!=0 or self.offset_y!=0: return - self.rotated_image = pygame.transform.rotate(self.image, dir) + if dir==-1: + dir = self.rotation_degrees + else: + self.rotation_degrees=dir + self.rotated_image = pygame.transform.rotate(self.image, dir) move_legal=True cliff_jump=False @@ -283,11 +308,9 @@ class Minesweeper: if next_x == self.current_map.tiles_x or next_x == -1: move_legal=False - if next_y == self.current_map.tiles_y or next_y == -1: + elif next_y == self.current_map.tiles_y or next_y == -1: move_legal=False - if self.current_map.terrain_matrix[next_y][next_x]>9: - move_legal=False - if self.current_map.terrain_matrix[next_y][next_x]>9: + elif self.current_map.terrain_matrix[next_y][next_x]>9: move_legal=False for cliff in self.current_map.cliffs: @@ -335,6 +358,38 @@ class Minesweeper: def drop_civilians(self): pass - def sensor(self): - sensor_list = [[],[],[]] + def sensor(self, x:int=-1, y:int=-1): + if x==-1: + x = self.position_x + if y==-1: + y = self.position_y + sensor_list = [["","",""],["","",""],["","",""]] + for i in range(3): + for j in range(3): + posx = x-1+j + posy = y-1+i + if posx >= self.current_map.tiles_x or posx <= -1: + sensor_list[i][j]="wall" + elif posy >= self.current_map.tiles_y or posy <= -1: + sensor_list[i][j]="wall" + elif self.current_map.terrain_matrix[posy][posx]>9: + sensor_list[i][j]="wall" + else: + sensor_list[i][j]="sand" + for cliff in self.current_map.cliffs: + if (posx, posy) == (cliff.position_x, cliff.position_y): + if cliff.rotation==0: + sensor_list[i][j]="cliff_south" + elif cliff.rotation==90: + sensor_list[i][j]="cliff_east" + elif cliff.rotation==180: + sensor_list[i][j]="cliff_north" + elif cliff.rotation==270: + sensor_list[i][j]="cliff_west" + break + for mine in self.current_map.mines: + if (posx, posy) == (mine.position_x, mine.position_y): + sensor_list[i][j]="mine" + break + return sensor_list \ No newline at end of file diff --git a/classes/system.py b/classes/system.py index e2faba0..52b67db 100644 --- a/classes/system.py +++ b/classes/system.py @@ -6,12 +6,14 @@ class Window: height:int title:str icon_path:str + paused:bool def __init__(self, width:int=640, height:int=480, title="", icon_path=""): self.set_resolution(width,height) self.set_title(title) self.set_icon(icon_path) self.mount() + self.paused=False def set_resolution(self, width:int, height:int): self.width = width diff --git a/main.py b/main.py index 883b076..ba392f3 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,11 @@ def main(): #utworzenie okna do gry window = system.Window(TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y, "Intelligent Minesweeper", "icon.png") + #utworzenie ekranu pauzy + pause_menu = pygame.Surface((TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y)) + pause_menu.set_alpha(128) + pause_menu.fill((0,0,0)) + #utworzenie objektu mapy, wygenerowanie jej i narysowanie na ekranie map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y) map.generate() @@ -46,13 +51,20 @@ def main(): delta = clock.tick(FPS) #wykonanie funkcji update() AI - AI.update() + AI.updateFPS() + + if saper.offset_x==0 and saper.offset_y==0: + AI.updateTile() #narysowanie terenu i obiektów map.draw_tiles() map.draw_objects() saper.draw(window.window, delta) + #pauza + if window.paused: + window.window.blit(pause_menu, (0,0)) + #odświeżenie ekranu pygame.display.update()