podstawowe AI
This commit is contained in:
parent
ea540d5b73
commit
d095fe469e
BIN
assets/music.ogg
Normal file
BIN
assets/music.ogg
Normal file
Binary file not shown.
51
classes/ai.py
Normal file
51
classes/ai.py
Normal file
@ -0,0 +1,51 @@
|
||||
import pygame
|
||||
from classes import minesweeper, system
|
||||
from random import randrange
|
||||
|
||||
class AI:
|
||||
window:system.Window
|
||||
map:minesweeper.Map
|
||||
saper:minesweeper.Minesweeper
|
||||
|
||||
#jak True to można się poruszać strzałkami, jak False sam się porusza
|
||||
user_controlled=False
|
||||
|
||||
def __init__(self, window, map, saper):
|
||||
self.window = window
|
||||
self.map = map
|
||||
self.saper = saper
|
||||
|
||||
#co ma zrobić tylko na początku
|
||||
def ready(self):
|
||||
pass
|
||||
|
||||
#co ma robić przy każdym FPS'ie
|
||||
def update(self):
|
||||
if self.user_controlled:
|
||||
self.minesweeper_controls()
|
||||
return
|
||||
|
||||
self.chaos_controls()
|
||||
|
||||
|
||||
def minesweeper_controls(self):
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_DOWN]:
|
||||
self.saper.move(self.map, 0)
|
||||
elif keys[pygame.K_UP]:
|
||||
self.saper.move(self.map, 180)
|
||||
elif keys[pygame.K_LEFT]:
|
||||
self.saper.move(self.map, 270)
|
||||
elif keys[pygame.K_RIGHT]:
|
||||
self.saper.move(self.map, 90)
|
||||
|
||||
def chaos_controls(self):
|
||||
dir = randrange(4)
|
||||
if dir==0:
|
||||
self.saper.move(self.map, 0)
|
||||
elif dir==1:
|
||||
self.saper.move(self.map, 180)
|
||||
elif dir==2:
|
||||
self.saper.move(self.map, 270)
|
||||
elif dir==3:
|
||||
self.saper.move(self.map, 90)
|
@ -55,8 +55,6 @@ class Map:
|
||||
|
||||
|
||||
def generate(self):
|
||||
#pygame.mixer.Channel(0).play(pygame.mixer.Sound("assets/music.ogg"), -1)
|
||||
|
||||
#generowanie terenu
|
||||
matrix = []
|
||||
for i in range(self.tiles_y):
|
||||
@ -116,7 +114,8 @@ class Minesweeper:
|
||||
for mine in map.mines:
|
||||
if (self.position_x, self.position_y) == (mine.position_x, mine.position_y):
|
||||
map.mines.remove(mine)
|
||||
pygame.mixer.Channel(1).play(pygame.mixer.Sound("assets/sounds/pickup.wav"))
|
||||
pygame.mixer.Channel(3).set_volume(0.7)
|
||||
pygame.mixer.Channel(3).play(pygame.mixer.Sound("assets/sounds/pickup.wav"))
|
||||
break
|
||||
|
||||
def update_offset(self, delta:float, map):
|
||||
@ -140,7 +139,7 @@ class Minesweeper:
|
||||
finished=True
|
||||
|
||||
if finished:
|
||||
pygame.mixer.music.stop()
|
||||
pygame.mixer.Channel(1).stop()
|
||||
self.offset_y=0
|
||||
self.offset_x=0
|
||||
self.check_position(map)
|
||||
@ -184,9 +183,8 @@ class Minesweeper:
|
||||
move_legal=False
|
||||
|
||||
if move_legal:
|
||||
pygame.mixer.music.load("assets/sounds/moving.wav")
|
||||
pygame.mixer.music.set_volume(0.4)
|
||||
pygame.mixer.music.play()
|
||||
pygame.mixer.Channel(1).set_volume(0.3)
|
||||
pygame.mixer.Channel(1).play(pygame.mixer.Sound("assets/sounds/moving.wav"))
|
||||
if dir==0:
|
||||
self.position_y+=1
|
||||
self.offset_y=-self.size
|
||||
@ -200,6 +198,5 @@ class Minesweeper:
|
||||
self.position_x+=1
|
||||
self.offset_x=-self.size
|
||||
else:
|
||||
pygame.mixer.music.load("assets/sounds/collision.wav")
|
||||
pygame.mixer.music.set_volume(0.6)
|
||||
pygame.mixer.music.play()
|
||||
pygame.mixer.Channel(2).set_volume(0.5)
|
||||
pygame.mixer.Channel(2).play(pygame.mixer.Sound("assets/sounds/collision.wav"))
|
31
main.py
31
main.py
@ -2,17 +2,25 @@
|
||||
import pygame
|
||||
#system - klasy związane z pygame
|
||||
#minesweeper - klasy związane z samym saperem
|
||||
from classes import system, minesweeper
|
||||
#ai - klasa wykonująca ruchy sapera
|
||||
from classes import system, minesweeper, ai
|
||||
|
||||
#ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy
|
||||
TILE_SIZE = 64
|
||||
TILES_X = 16
|
||||
TILES_Y = 10
|
||||
|
||||
#wł/wył muzyki
|
||||
MUSIC=True
|
||||
|
||||
#ustalenie FPS
|
||||
FPS = 60
|
||||
|
||||
def main():
|
||||
if MUSIC:
|
||||
pygame.mixer.init()
|
||||
pygame.mixer.Channel(0).play(pygame.mixer.Sound("assets/music.ogg"), -1)
|
||||
|
||||
#utworzenie okna do gry
|
||||
window = system.Window(TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y, "Intelligent Minesweeper", "icon.png")
|
||||
|
||||
@ -23,7 +31,13 @@ def main():
|
||||
|
||||
#utworzenie sapera
|
||||
saper = minesweeper.Minesweeper(0,0, TILE_SIZE)
|
||||
|
||||
|
||||
|
||||
#utworzenie objektu klasy AI
|
||||
AI = ai.AI(window, map, saper)
|
||||
#wykonanie funkcji ready() AI
|
||||
AI.ready()
|
||||
|
||||
#główna pętla
|
||||
game_loop = True
|
||||
clock = pygame.time.Clock()
|
||||
@ -31,17 +45,8 @@ def main():
|
||||
#wdrożenie FPS, delta - czas od ostatniej klatki
|
||||
delta = clock.tick(FPS)
|
||||
|
||||
#sterowanie
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_DOWN]:
|
||||
saper.move(map, 0)
|
||||
elif keys[pygame.K_UP]:
|
||||
saper.move(map, 180)
|
||||
elif keys[pygame.K_LEFT]:
|
||||
saper.move(map, 270)
|
||||
elif keys[pygame.K_RIGHT]:
|
||||
saper.move(map, 90)
|
||||
|
||||
#wykonanie funkcji update() AI
|
||||
AI.update()
|
||||
|
||||
#narysowanie terenu i obiektów
|
||||
map.draw_tiles()
|
||||
|
Loading…
Reference in New Issue
Block a user