podstawowe AI

This commit is contained in:
s464859 2022-03-10 00:13:14 +01:00
parent ea540d5b73
commit d095fe469e
4 changed files with 76 additions and 23 deletions

BIN
assets/music.ogg Normal file

Binary file not shown.

51
classes/ai.py Normal file
View 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)

View File

@ -55,8 +55,6 @@ class Map:
def generate(self): def generate(self):
#pygame.mixer.Channel(0).play(pygame.mixer.Sound("assets/music.ogg"), -1)
#generowanie terenu #generowanie terenu
matrix = [] matrix = []
for i in range(self.tiles_y): for i in range(self.tiles_y):
@ -116,7 +114,8 @@ class Minesweeper:
for mine in map.mines: for mine in map.mines:
if (self.position_x, self.position_y) == (mine.position_x, mine.position_y): if (self.position_x, self.position_y) == (mine.position_x, mine.position_y):
map.mines.remove(mine) 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 break
def update_offset(self, delta:float, map): def update_offset(self, delta:float, map):
@ -140,7 +139,7 @@ class Minesweeper:
finished=True finished=True
if finished: if finished:
pygame.mixer.music.stop() pygame.mixer.Channel(1).stop()
self.offset_y=0 self.offset_y=0
self.offset_x=0 self.offset_x=0
self.check_position(map) self.check_position(map)
@ -184,9 +183,8 @@ class Minesweeper:
move_legal=False move_legal=False
if move_legal: if move_legal:
pygame.mixer.music.load("assets/sounds/moving.wav") pygame.mixer.Channel(1).set_volume(0.3)
pygame.mixer.music.set_volume(0.4) pygame.mixer.Channel(1).play(pygame.mixer.Sound("assets/sounds/moving.wav"))
pygame.mixer.music.play()
if dir==0: if dir==0:
self.position_y+=1 self.position_y+=1
self.offset_y=-self.size self.offset_y=-self.size
@ -200,6 +198,5 @@ class Minesweeper:
self.position_x+=1 self.position_x+=1
self.offset_x=-self.size self.offset_x=-self.size
else: else:
pygame.mixer.music.load("assets/sounds/collision.wav") pygame.mixer.Channel(2).set_volume(0.5)
pygame.mixer.music.set_volume(0.6) pygame.mixer.Channel(2).play(pygame.mixer.Sound("assets/sounds/collision.wav"))
pygame.mixer.music.play()

29
main.py
View File

@ -2,17 +2,25 @@
import pygame import pygame
#system - klasy związane z pygame #system - klasy związane z pygame
#minesweeper - klasy związane z samym saperem #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 #ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy
TILE_SIZE = 64 TILE_SIZE = 64
TILES_X = 16 TILES_X = 16
TILES_Y = 10 TILES_Y = 10
#wł/wył muzyki
MUSIC=True
#ustalenie FPS #ustalenie FPS
FPS = 60 FPS = 60
def main(): def main():
if MUSIC:
pygame.mixer.init()
pygame.mixer.Channel(0).play(pygame.mixer.Sound("assets/music.ogg"), -1)
#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")
@ -24,6 +32,12 @@ def main():
#utworzenie sapera #utworzenie sapera
saper = minesweeper.Minesweeper(0,0, TILE_SIZE) 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 #główna pętla
game_loop = True game_loop = True
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -31,17 +45,8 @@ def main():
#wdrożenie FPS, delta - czas od ostatniej klatki #wdrożenie FPS, delta - czas od ostatniej klatki
delta = clock.tick(FPS) delta = clock.tick(FPS)
#sterowanie #wykonanie funkcji update() AI
keys = pygame.key.get_pressed() AI.update()
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)
#narysowanie terenu i obiektów #narysowanie terenu i obiektów
map.draw_tiles() map.draw_tiles()