Saper na ekranie
This commit is contained in:
commit
21936d8d91
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__/
|
BIN
assets/sprites/sand.png
Normal file
BIN
assets/sprites/sand.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
BIN
assets/sprites/saper.png
Normal file
BIN
assets/sprites/saper.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
109
classes/minesweeper.py
Normal file
109
classes/minesweeper.py
Normal file
@ -0,0 +1,109 @@
|
||||
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)
|
||||
|
||||
|
31
classes/system.py
Normal file
31
classes/system.py
Normal file
@ -0,0 +1,31 @@
|
||||
import pygame
|
||||
|
||||
class Window:
|
||||
window:None
|
||||
width:int
|
||||
height:int
|
||||
title:str
|
||||
icon_path:str
|
||||
|
||||
def __init__(self, width:int=640, height:int=480, title="", icon_path=""):
|
||||
self.set_resolution(width,height)
|
||||
self.set_title(title)
|
||||
self.set_icon(icon_path)
|
||||
self.mount()
|
||||
|
||||
def set_resolution(self, width:int, height:int):
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
def set_title(self, title:str):
|
||||
self.title=title
|
||||
|
||||
def set_icon(self, icon_path:str):
|
||||
self.icon_path= icon_path
|
||||
|
||||
def mount(self):
|
||||
self.window = pygame.display.set_mode((self.width,self.height))
|
||||
pygame.display.set_caption(self.title)
|
||||
if self.icon_path != "":
|
||||
icon = pygame.image.load(self.icon_path)
|
||||
pygame.display.set_icon(icon)
|
59
main.py
Normal file
59
main.py
Normal file
@ -0,0 +1,59 @@
|
||||
#pygame - biblioteka do symulacji graficznych
|
||||
import pygame
|
||||
#system - klasy związane z pygame
|
||||
#minesweeper - klasy związane z samym saperem
|
||||
from classes import system, minesweeper
|
||||
|
||||
#ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy
|
||||
TILE_SIZE = 64
|
||||
TILES_X = 16
|
||||
TILES_Y = 10
|
||||
|
||||
def main():
|
||||
#utworzenie okna do gry
|
||||
window = system.Window(TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y, "Intelligent Minesweeper", "icon.png")
|
||||
|
||||
#utworzenie objektu mapy, wygenerowanie jej i narysowanie na ekranie
|
||||
map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y)
|
||||
map.generate()
|
||||
map.draw_tiles()
|
||||
|
||||
#główna pętla
|
||||
game_loop = True
|
||||
clock = pygame.time.Clock()
|
||||
while game_loop:
|
||||
#ustalenie FPS
|
||||
clock.tick(60)
|
||||
|
||||
#sprawdzanie różnych interakcji użytkownika
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
game_loop = False
|
||||
pygame.quit()
|
||||
|
||||
#sterowanie
|
||||
keys = pygame.key.get_pressed()
|
||||
not_moving = map.minesweeper.offset_x==0 and map.minesweeper.offset_y==0
|
||||
if keys[pygame.K_DOWN] and not_moving:
|
||||
map.minesweeper.move(0)
|
||||
elif keys[pygame.K_UP] and not_moving:
|
||||
map.minesweeper.move(1)
|
||||
elif keys[pygame.K_LEFT] and not_moving:
|
||||
map.minesweeper.move(2)
|
||||
elif keys[pygame.K_RIGHT] and not_moving:
|
||||
map.minesweeper.move(3)
|
||||
|
||||
|
||||
|
||||
#narysowanie terenu i obiektów
|
||||
map.draw_tiles()
|
||||
map.draw_objects()
|
||||
|
||||
#odświeżenie ekranu
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
pygame
|
Loading…
Reference in New Issue
Block a user