import random import pygame plansza = [[' ' for i in range(8)] for j in range(8)] odkryte = [[False for i in range(8)] for j in range(8)] flagi = [[False for i in range(8)] for j in range(8)] def wyswietl_plansze(): for rzad in plansza: print(rzad) def generuj_bomby(bomby): ustawione = 0 while ustawione < bomby: x = random.randint(0, 7) y = random.randint(0, 7) if plansza[x][y] != '*': plansza[x][y] = '*' ustawione += 1 def wypelnij_cyferki(): for y in range(8): for x in range(8): if plansza[x][y] == '*': continue numerek = 0 if y-1 >= 0 and plansza[x][y-1] == '*': numerek += 1 if y+1 < 8 and plansza[x][y+1] == '*': numerek += 1 if x-1 >= 0 and plansza[x-1][y] == '*': numerek += 1 if x+1 < 8 and plansza[x+1][y] == '*': numerek += 1 if x-1 >= 0 and y-1 >= 0 and plansza[x-1][y-1] == '*': numerek += 1 if x+1 < 8 and y-1 >= 0 and plansza[x+1][y-1] == '*': numerek += 1 if x-1 >= 0 and y+1 < 8 and plansza[x-1][y+1] == '*': numerek += 1 if x+1 < 8 and y+1 < 8 and plansza[x+1][y+1] == '*': numerek += 1 plansza[x][y] = numerek def odkrywaj_zera(x, y): if odkryte[y][x]: return odkryte[y][x] = True if plansza[y][x] != 0: return if y-1 >= 0: odkrywaj_zera(x, y-1) if y+1 < 8: odkrywaj_zera(x, y+1) if x-1 >= 0: odkrywaj_zera(x-1, y) if x+1 < 8: odkrywaj_zera(x+1, y) if x-1 >= 0 and y-1 >= 0 and plansza[y-1][x-1] != 0: odkryte[y-1][x-1] = True if x+1 < 8 and y-1 >= 0 and plansza[y-1][x+1] != 0: odkryte[y-1][x+1] = True if x-1 >= 0 and y+1 < 8 and plansza[y+1][x-1] != 0: odkryte[y+1][x-1] = True if x+1 < 8 and y+1 < 8 and plansza[y+1][x+1] != 0: odkryte[y+1][x+1] = True bomby = int(input('Ile bomb wygenerować? ')) generuj_bomby(bomby) wypelnij_cyferki() wyswietl_plansze() pygame.init() ekran = pygame.display.set_mode((400, 400)) pygame.display.set_caption("Smoleńsk") pygame.mouse.set_visible(False) czcionka = pygame.font.SysFont("comicsansms", 40) brzoza = pygame.image.load("brzoza.png") samolot = pygame.image.load("samolot.png") rozbity_samolot = pygame.image.load("rozbity_samolot.png") flaga = pygame.image.load("flaga.png") bum = pygame.mixer.Sound("bum.wav") zegar = pygame.time.Clock() kliknieto_x = False koniec_gry = False ustawiono_flage = False while not kliknieto_x: for event in pygame.event.get(): if event.type == pygame.QUIT: kliknieto_x = True pressed = pygame.mouse.get_pressed() pos = pygame.mouse.get_pos() x = pos[0]//50 y = pos[1]//50 if pressed[0] and not koniec_gry: if not odkryte[y][x] and not flagi[y][x]: odkrywaj_zera(x, y) odkryte[y][x] = True if plansza[y][x] == '*': koniec_gry = True bum.play() print(x, y) if pressed[2] and not koniec_gry: if not odkryte[y][x] and not ustawiono_flage: flagi[y][x] = not flagi[y][x] ustawiono_flage = True else: ustawiono_flage = False ekran.fill(0xFFFFFF) for y in range(8): for x in range(8): if odkryte[y][x]: pygame.draw.rect(ekran, (192, 192, 192, 192), pygame.Rect(x*50, y*50, 50, 50)) if plansza[y][x] == '*': #pygame.draw.circle(ekran, 0xFF0000, (25+x*50, 25+y*50), 15) ekran.blit(brzoza, (25+x*50-100, 25+y*50-205)) elif plansza[y][x] != 0: ekran.blit(czcionka.render(str(plansza[y][x]), True, (0, 255, 0)), (x*50+15, y*50)) else: pass if flagi[y][x]: ekran.blit(flaga, (x*50, y*50)) for x in range(9): pygame.draw.line(ekran, 0x000000, (x*50, 0), (x*50, 400), 3) for x in range(9): pygame.draw.line(ekran, 0x000000, (0, x*50), (400, x*50), 3) if not koniec_gry: ekran.blit(samolot, (pos[0], pos[1]-20)) else: ekran.blit(rozbity_samolot, (pos[0]-20, pos[1]-110)) pygame.display.flip() zegar.tick(60) pygame.quit()