dodanie min i kamieni

This commit is contained in:
s464859 2022-03-09 23:37:42 +01:00
parent 3eac875cc4
commit ea540d5b73
9 changed files with 174 additions and 80 deletions

BIN
assets/sounds/blowup.wav Normal file

Binary file not shown.

BIN
assets/sounds/collision.wav Normal file

Binary file not shown.

BIN
assets/sounds/ledge.wav Normal file

Binary file not shown.

BIN
assets/sounds/moving.wav Normal file

Binary file not shown.

BIN
assets/sounds/pickup.wav Normal file

Binary file not shown.

BIN
assets/sprites/mine.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

BIN
assets/sprites/stone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 B

View File

@ -1,6 +1,93 @@
import pygame
from classes import system
from random import randrange
pygame.mixer.init()
#mina
class Mine:
position_x:int
position_y:int
size:int
difficulty:int = 1
weight:float = 1.0
image:pygame.surface.Surface
def __init__(self,position_x, position_y, size):
self.position_x=position_x
self.position_y=position_y
self.size=size
self.image = pygame.image.load("assets/sprites/mine.png")
self.image = pygame.transform.scale(self.image, (self.size, self.size))
def draw(self, window):
position_on_screen = (self.size*self.position_x, self.size*self.position_y)
window.blit(self.image, position_on_screen)
#mapa
class Map:
window:system.Window
tile_size:int
tiles_x:int
tiles_y:int
tile_palette:list
terrain_matrix:list
mines=[]
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=[None]*20
image = pygame.image.load("assets/sprites/sand.png")
image = pygame.transform.scale(image, (tile_size,tile_size))
self.tile_palette[0]=image
image = pygame.image.load("assets/sprites/stone.png")
image = pygame.transform.scale(image, (tile_size,tile_size))
self.tile_palette[10]=image
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):
matrix.append([])
for j in range(self.tiles_x):
#od liczby zależy jaki teren, np. 0 - piasek
if randrange(10)==0:
matrix[i].append(10)
else:
matrix[i].append(0)
self.terrain_matrix = matrix
for i in range(self.tiles_y):
for j in range(self.tiles_x):
if matrix[i][j]<10:
if randrange(10)==0:
mine = Mine(j, i, self.tile_size)
self.mines.append(mine)
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):
for mine in self.mines:
mine.draw(self.window.window)
#saper
class Minesweeper:
size:int
position_x:int
@ -10,7 +97,12 @@ class Minesweeper:
offset_x:int=0
offset_y:int=0
carried_objects=[]
speed=1
ability=1
max_carried_weight=5.0
def __init__(self, position_x=0, position_y=0, size=64):
self.position_x=position_x
@ -20,95 +112,94 @@ class Minesweeper:
self.image = pygame.transform.scale(self.image, (self.size, self.size))
self.rotated_image = self.image
def update_offset(self, delta:float):
def check_position(self, map:Map):
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"))
break
def update_offset(self, delta:float, map):
dist=round(self.speed*delta/8)
finished=False
if self.offset_x>0:
self.offset_x-=dist
if self.offset_x<0:
self.offset_x=0
if self.offset_x<=0:
finished=True
elif self.offset_x<0:
self.offset_x+=dist
if self.offset_x>0:
self.offset_x=0
if self.offset_x>=0:
finished=True
if self.offset_y>0:
self.offset_y-=dist
if self.offset_y<0:
self.offset_y=0
if self.offset_y<=0:
finished=True
elif self.offset_y<0:
self.offset_y+=dist
if self.offset_y>0:
self.offset_y=0
if self.offset_y>=0:
finished=True
if finished:
pygame.mixer.music.stop()
self.offset_y=0
self.offset_x=0
self.check_position(map)
def draw(self, window, delta:float):
def draw(self, window, delta:float, map):
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(delta)
self.update_offset(delta, map)
def move(self, dir:int):
def move(self, map:Map, dir:int):
#południe - 0
#wschód - 90
#północ - 180
#zachód - 270
if self.offset_x!=0 or self.offset_y!=0:
return
self.rotated_image = pygame.transform.rotate(self.image, dir)
move_legal=True
if dir==0:
self.position_y+=1
self.offset_y=-self.size
if self.position_y+1 == map.tiles_y:
move_legal=False
elif map.terrain_matrix[self.position_y+1][self.position_x]>9:
move_legal=False
elif dir==180:
self.position_y-=1
self.offset_y=self.size
if self.position_y-1 == -1:
move_legal=False
elif map.terrain_matrix[self.position_y-1][self.position_x]>9:
move_legal=False
elif dir==270:
self.position_x-=1
self.offset_x=self.size
if self.position_x-1 == -1:
move_legal=False
elif map.terrain_matrix[self.position_y][self.position_x-1]>9:
move_legal=False
elif dir==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
if self.position_x+1 == map.tiles_x:
move_legal=False
elif map.terrain_matrix[self.position_y][self.position_x+1]>9:
move_legal=False
#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, delta:float):
self.minesweeper.draw(self.window.window, delta)
if move_legal:
pygame.mixer.music.load("assets/sounds/moving.wav")
pygame.mixer.music.set_volume(0.4)
pygame.mixer.music.play()
if dir==0:
self.position_y+=1
self.offset_y=-self.size
elif dir==180:
self.position_y-=1
self.offset_y=self.size
elif dir==270:
self.position_x-=1
self.offset_x=self.size
elif dir==90:
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()

25
main.py
View File

@ -20,30 +20,33 @@ def main():
map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y)
map.generate()
map.draw_tiles()
#utworzenie sapera
saper = minesweeper.Minesweeper(0,0, TILE_SIZE)
#główna pętla
game_loop = True
clock = pygame.time.Clock()
while game_loop:
#ustalenie FPS, delta - czas od ostatniej klatki
#wdrożenie FPS, delta - czas od ostatniej klatki
delta = clock.tick(FPS)
#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(180)
elif keys[pygame.K_LEFT] and not_moving:
map.minesweeper.move(270)
elif keys[pygame.K_RIGHT] and not_moving:
map.minesweeper.move(90)
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
map.draw_tiles()
map.draw_objects(delta)
map.draw_objects()
saper.draw(window.window, delta, map)
#odświeżenie ekranu
pygame.display.update()