implemented new mine types and corresponding classes

This commit is contained in:
s452645 2021-03-26 15:46:00 +01:00
parent 0d30245e20
commit 37b4107aa4
8 changed files with 84 additions and 11 deletions

View File

@ -1,7 +0,0 @@
class Mine:
def __init__(self, position, mine_type, active=True):
self.position = position
self.mine_type = mine_type
self.active = active
# more to be added later (e.g. disarming)

View File

@ -1,7 +1,8 @@
import json
import project_constants as const
import tile as tl
import mine as mn
from mines_models import standard_mine as sm
tile_asset_options = {
"BLUE": const.ASSET_TILE_BLUE,
@ -53,8 +54,10 @@ class Minefield:
# create and add mine if there is one
if tile_data["mine"] is not None:
mine_type = tile_data["mine"]["mine_type"].upper()
mine = mn.Mine((x, y), mine_type)
# TODO: check mine type and create suitable one
# JSON file doesn't represent new mine types yet, so every mine is a standard one at the moment
mine = sm.StandardMine((x, y))
self.matrix[x][y].mine = mine
self.matrix[x][y].color = tile_data["color"].upper()
@ -77,7 +80,9 @@ class Minefield:
# draw a mine on top if there is one
if tile.mine is not None:
window.blit(mine_asset_options.get(tile.mine.mine_type), tile_screen_coords)
# TODO: blit appropriate mine type
# current icons don't represent actual types, thus every mine has the same icon (temporary solution)
window.blit(mine_asset_options['A'], tile_screen_coords)
# draw the sapper
sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_position[1])

0
mines_models/__init__.py Normal file
View File

View File

@ -0,0 +1,14 @@
# for type hints (predecessor)
from __future__ import annotations
from .mine import Mine
class ChainedMine(Mine):
def __init__(self, position, predecessor: ChainedMine = None, active=True):
self.predecessor = predecessor
self.type = "chained"
super().__init__(position, active)
def disarm(self):
pass

19
mines_models/mine.py Normal file
View File

@ -0,0 +1,19 @@
# module for abstarct classes
from abc import ABC, abstractmethod
# type hints
from typing import Tuple
# Mine cannot be instatinated
# all abstarct methods must be implemented in derived classes
class Mine(ABC):
@abstractmethod
def __init__(self, position: Tuple[int, int], active=True):
self.position = position
self.active = active
@abstractmethod
def disarm(self):
pass

View File

@ -0,0 +1,10 @@
from .mine import Mine
class StandardMine(Mine):
def __init__(self, position, active=True):
self.mine_type = "standard"
super().__init__(position, active)
def disarm(self):
pass

21
mines_models/tests.py Normal file
View File

@ -0,0 +1,21 @@
import time_mine as tm
import chained_mine as cm
import standard_mine as sm
mine1 = tm.TimeMine((1, 2), "time", 3)
mine2 = cm.ChainedMine((3, 4))
mine3 = cm.ChainedMine((0, 6), "chained", mine2)
mine4 = sm.StandardMine((1, 2), "standard")
mines = [mine1, mine2, mine3, mine4]
for mine in mines:
print(mine.position)
print(mine.active)
print(mine2.predecessor)
print(mine3.predecessor)
mine4.disarm()
mine5 = cm.ChainedMine()

11
mines_models/time_mine.py Normal file
View File

@ -0,0 +1,11 @@
from .mine import Mine
class TimeMine(Mine):
def __init__(self, position, timer, active=True):
self.type = "time"
self.timer = timer
super().__init__(position, active)
def disarm(self):
pass