InteligentnySaper/classes/minesweeper.py
2022-03-09 01:36:46 +01:00

109 lines
3.3 KiB
Python

from numpy import size
import pygame
from classes import system
class Minesweeper:
size:int
position_x:int
position_y:int
image:pygame.surface.Surface
rotated_image:pygame.surface.Surface
offset_x:int=0
offset_y:int=0
speed=2
def __init__(self, position_x=0, position_y=0, size=64):
self.position_x=position_x
self.position_y=position_y
self.size = size
self.image = pygame.image.load("assets/sprites/saper.png")
self.image = pygame.transform.scale(self.image, (self.size, self.size))
self.rotated_image = self.image
def update_offset(self):
if self.offset_x>0:
self.offset_x-=self.speed
elif self.offset_x<0:
self.offset_x+=self.speed
if self.offset_y>0:
self.offset_y-=self.speed
elif self.offset_y<0:
self.offset_y+=self.speed
def draw(self, window):
position_on_screen = (self.size*self.position_x + self.offset_x, self.size*self.position_y + self.offset_y)
window.blit(self.rotated_image, position_on_screen)
self.update_offset()
def move(self, dir:int):
#południe
if dir==0:
self.rotated_image = pygame.transform.rotate(self.image, 0)
self.position_y+=1
self.offset_y=-self.size
#północ
elif dir==1:
self.rotated_image = pygame.transform.rotate(self.image, 180)
self.position_y-=1
self.offset_y=self.size
#zachód
elif dir==2:
self.rotated_image = pygame.transform.rotate(self.image, 270)
self.position_x-=1
self.offset_x=self.size
#wschód
elif dir==3:
self.rotated_image = pygame.transform.rotate(self.image, 90)
self.position_x+=1
self.offset_x=-self.size
class Map:
window:system.Window
tile_size:int
tiles_x:int
tiles_y:int
terrain_matrix:list
tile_palette:list
minesweeper:Minesweeper
def __init__(self, window:system.Window, tile_size:int=64, tiles_x:int=8, tiles_y:int=8):
self.window = window
self.tile_size = tile_size
self.tiles_x = tiles_x
self.tiles_y = tiles_y
#dodanie grafik wszystkich typów terenu do jednej listy
self.tile_palette=[]
image = pygame.image.load("assets/sprites/sand.png")
image = pygame.transform.scale(image, (tile_size,tile_size))
self.tile_palette.append(image)
def generate(self):
#generowanie terenu
matrix = []
for i in range(self.tiles_y):
matrix.append([])
for j in range(self.tiles_x):
#od liczby zależy jaki teren, np. 0 - piasek
matrix[i].append(0)
self.terrain_matrix = matrix
#generowanie objektów
self.minesweeper = Minesweeper(0,0, self.tile_size)
def draw_tiles(self):
#narysowanie na ekranie terenu
for i in range(len(self.terrain_matrix)):
for j in range(len(self.terrain_matrix[i])):
self.window.window.blit(self.tile_palette[self.terrain_matrix[i][j]], (self.tile_size*j, self.tile_size*i))
def draw_objects(self):
self.minesweeper.draw(self.window.window)