Merge pull request 'zaktualizowane' (#1) from updated into master

Reviewed-on: #1
This commit is contained in:
Wiktor Borowski 2022-04-06 20:13:37 +02:00
commit 4fb37467b9
4 changed files with 100 additions and 17 deletions

View File

@ -20,13 +20,26 @@ class AI:
self.saper.set_map(self.current_map) self.saper.set_map(self.current_map)
#co ma robić przy każdym FPS'ie #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: if self.user_controlled:
self.minesweeper_controls() self.minesweeper_controls()
return return
self.chaos_controls() # <--------------------------zamiast tego trzeba wstawić jakiś algorytm
self.chaos_controls()
#TU pisać resztę
def minesweeper_controls(self): def minesweeper_controls(self):
@ -43,10 +56,11 @@ class AI:
def chaos_controls(self): def chaos_controls(self):
dir = randrange(4) dir = randrange(4)
if dir==0: if dir==0:
self.saper.move(0) self.saper.rotate("N")
elif dir==1: elif dir==1:
self.saper.move(180) self.saper.rotate("S")
elif dir==2: elif dir==2:
self.saper.move(270) self.saper.rotate("W")
elif dir==3: elif dir==3:
self.saper.move(90) self.saper.rotate("E")
self.saper.move()

View File

@ -201,6 +201,7 @@ class NPC:
#saper #saper
class Minesweeper: class Minesweeper:
size:int size:int
rotation_degrees:int
position_x:int position_x:int
position_y:int position_y:int
image:pygame.surface.Surface image:pygame.surface.Surface
@ -223,6 +224,7 @@ class Minesweeper:
self.image = pygame.image.load("assets/sprites/saper_fun_sized.png") self.image = pygame.image.load("assets/sprites/saper_fun_sized.png")
self.image = pygame.transform.scale(self.image, (self.size, self.size)) self.image = pygame.transform.scale(self.image, (self.size, self.size))
self.rotated_image = self.image self.rotated_image = self.image
self.rotation_degrees=0
def set_map(self, map:Map): def set_map(self, map:Map):
self.current_map = map self.current_map = map
@ -244,6 +246,8 @@ class Minesweeper:
finished=True finished=True
elif self.offset_y<0: elif self.offset_y<0:
self.offset_y+=dist 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: if self.offset_y>=0:
finished=True finished=True
@ -258,7 +262,24 @@ class Minesweeper:
window.blit(self.rotated_image, position_on_screen) window.blit(self.rotated_image, position_on_screen)
self.update_offset(delta) 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 #południe - 0
#wschód - 90 #wschód - 90
#północ - 180 #północ - 180
@ -266,6 +287,10 @@ class Minesweeper:
if self.offset_x!=0 or self.offset_y!=0: if self.offset_x!=0 or self.offset_y!=0:
return return
if dir==-1:
dir = self.rotation_degrees
else:
self.rotation_degrees=dir
self.rotated_image = pygame.transform.rotate(self.image, dir) self.rotated_image = pygame.transform.rotate(self.image, dir)
move_legal=True move_legal=True
@ -283,11 +308,9 @@ class Minesweeper:
if next_x == self.current_map.tiles_x or next_x == -1: if next_x == self.current_map.tiles_x or next_x == -1:
move_legal=False 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 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
if self.current_map.terrain_matrix[next_y][next_x]>9:
move_legal=False move_legal=False
for cliff in self.current_map.cliffs: for cliff in self.current_map.cliffs:
@ -335,6 +358,38 @@ class Minesweeper:
def drop_civilians(self): def drop_civilians(self):
pass pass
def sensor(self): def sensor(self, x:int=-1, y:int=-1):
sensor_list = [[],[],[]] 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 return sensor_list

View File

@ -6,12 +6,14 @@ class Window:
height:int height:int
title:str title:str
icon_path:str icon_path:str
paused:bool
def __init__(self, width:int=640, height:int=480, title="", icon_path=""): def __init__(self, width:int=640, height:int=480, title="", icon_path=""):
self.set_resolution(width,height) self.set_resolution(width,height)
self.set_title(title) self.set_title(title)
self.set_icon(icon_path) self.set_icon(icon_path)
self.mount() self.mount()
self.paused=False
def set_resolution(self, width:int, height:int): def set_resolution(self, width:int, height:int):
self.width = width self.width = width

14
main.py
View File

@ -24,6 +24,11 @@ def main():
#utworzenie okna do gry #utworzenie okna do gry
window = system.Window(TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y, "Intelligent Minesweeper", "icon.png") 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 #utworzenie objektu mapy, wygenerowanie jej i narysowanie na ekranie
map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y) map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y)
map.generate() map.generate()
@ -46,13 +51,20 @@ def main():
delta = clock.tick(FPS) delta = clock.tick(FPS)
#wykonanie funkcji update() AI #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 #narysowanie terenu i obiektów
map.draw_tiles() map.draw_tiles()
map.draw_objects() map.draw_objects()
saper.draw(window.window, delta) saper.draw(window.window, delta)
#pauza
if window.paused:
window.window.blit(pause_menu, (0,0))
#odświeżenie ekranu #odświeżenie ekranu
pygame.display.update() pygame.display.update()