forked from easonabc-public/paia-hw5
reinit
This commit is contained in:
44
TankMan/src/game_module/SoundController.py
Normal file
44
TankMan/src/game_module/SoundController.py
Normal file
@ -0,0 +1,44 @@
|
||||
from os import path
|
||||
|
||||
import pygame.mixer
|
||||
|
||||
|
||||
def create_sounds_data(id: str, name: str):
|
||||
return {
|
||||
"_id": id
|
||||
, "_name": name
|
||||
}
|
||||
|
||||
|
||||
def create_bgm_data(name: str, volume: float):
|
||||
return {
|
||||
"_name": name
|
||||
, "_volume": volume
|
||||
}
|
||||
|
||||
|
||||
class SoundController:
|
||||
def __init__(self, sound_path: str, sounds_data_list: list):
|
||||
self._sound_path = sound_path
|
||||
if not self._sound_path:
|
||||
return
|
||||
self._sounds_obj = {}
|
||||
pygame.mixer.init()
|
||||
for sounds_data in sounds_data_list:
|
||||
sound_data = path.join(self._sound_path, sounds_data["_name"])
|
||||
self._sounds_obj[sounds_data["_id"]] = pygame.mixer.Sound(sound_data)
|
||||
|
||||
def play_music(self, bgm_data: dict) -> None:
|
||||
if not self._sound_path:
|
||||
return
|
||||
pygame.mixer.init()
|
||||
pygame.mixer.music.load(path.join(self._sound_path, bgm_data["_name"]))
|
||||
pygame.mixer.music.set_volume(bgm_data["_volume"])
|
||||
pygame.mixer.music.play(-1)
|
||||
|
||||
def play_sound(self, id: str, volume: float, maz_time: int) -> None:
|
||||
if not self._sound_path:
|
||||
return
|
||||
sound_obj = self._sounds_obj[id]
|
||||
sound_obj.set_volume(volume)
|
||||
sound_obj.play(maxtime=maz_time)
|
68
TankMan/src/game_module/TiledMap.py
Normal file
68
TankMan/src/game_module/TiledMap.py
Normal file
@ -0,0 +1,68 @@
|
||||
import pytmx
|
||||
|
||||
|
||||
def create_construction(_id: int or str, _no: int, _init_pos: tuple, _init_size: tuple):
|
||||
return {
|
||||
"_id": _id
|
||||
, "_no": _no
|
||||
, "_init_pos": _init_pos
|
||||
, "_init_size": _init_size
|
||||
}
|
||||
|
||||
|
||||
# Map 讀取地圖資料
|
||||
class TiledMap:
|
||||
def __init__(self, filepath: str):
|
||||
tm = pytmx.TiledMap(filepath)
|
||||
self.tile_width = tm.tilewidth
|
||||
self.tile_height = tm.tileheight
|
||||
self.width = tm.width
|
||||
self.height = tm.height
|
||||
self.map_width = self.tile_width * self.width
|
||||
self.map_height = self.tile_height * self.height
|
||||
self.tmx_data = tm
|
||||
self._is_record = False
|
||||
self.all_pos_list = []
|
||||
self.empty_pos_list = []
|
||||
self.empty_quadrant_pos_dict = {1: [], 2: [], 3: [], 4: []}
|
||||
self.all_obj_data_dict = {}
|
||||
# TODO refactor
|
||||
self.all_obj = {}
|
||||
|
||||
def add_init_obj_data(self, img_id: int, cls, **kwargs):
|
||||
obj_data = {img_id: {"cls": cls,
|
||||
"kwargs": kwargs
|
||||
}
|
||||
}
|
||||
self.all_obj_data_dict.update(obj_data)
|
||||
self.all_obj[img_id] = []
|
||||
|
||||
def create_init_obj_dict(self) -> dict:
|
||||
obj_no = 0
|
||||
for layer in self.tmx_data.visible_layers:
|
||||
for x, y, gid, in layer:
|
||||
if isinstance(layer, pytmx.TiledTileLayer):
|
||||
pos = (x * self.tile_width, y * self.tile_height)
|
||||
if not self._is_record:
|
||||
self.all_pos_list.append(pos)
|
||||
if not self._is_record and not gid: # 0代表空格,無圖塊
|
||||
self.empty_pos_list.append(pos)
|
||||
if pos[0] >= self.map_width // 2 and pos[1] < self.map_height // 2:
|
||||
self.empty_quadrant_pos_dict[1].append(pos)
|
||||
elif pos[0] < self.map_width // 2 and pos[1] < self.map_height // 2:
|
||||
self.empty_quadrant_pos_dict[2].append(pos)
|
||||
elif pos[0] < self.map_width // 2 and pos[1] >= self.map_height // 2:
|
||||
self.empty_quadrant_pos_dict[3].append(pos)
|
||||
else:
|
||||
self.empty_quadrant_pos_dict[4].append(pos)
|
||||
elif gid:
|
||||
img_id = layer.parent.tiledgidmap[gid]
|
||||
kwargs = self.all_obj_data_dict[img_id]["kwargs"]
|
||||
obj_no += 1
|
||||
img_info = {"_id": img_id, "_no": obj_no
|
||||
, "_init_pos": pos
|
||||
, "_init_size": (self.tile_width, self.tile_height)
|
||||
}
|
||||
self.all_obj[img_id].append(self.all_obj_data_dict[img_id]["cls"](img_info, **kwargs))
|
||||
self._is_record = True
|
||||
return self.all_obj
|
0
TankMan/src/game_module/__init__.py
Normal file
0
TankMan/src/game_module/__init__.py
Normal file
26
TankMan/src/game_module/fuctions.py
Normal file
26
TankMan/src/game_module/fuctions.py
Normal file
@ -0,0 +1,26 @@
|
||||
import pygame.sprite
|
||||
|
||||
|
||||
def get_size(sprite: pygame.sprite.Sprite):
|
||||
return sprite.rect.width, sprite.rect.height
|
||||
|
||||
|
||||
def set_topleft(sprite: pygame.sprite.Sprite, top_left: tuple):
|
||||
sprite.rect.topleft = top_left
|
||||
|
||||
|
||||
def add_score(sprite: pygame.sprite.Sprite, score: int):
|
||||
sprite.score += score
|
||||
|
||||
|
||||
def set_shoot(sprite: pygame.sprite.Sprite, is_shoot: bool):
|
||||
sprite.is_shoot = is_shoot
|
||||
|
||||
|
||||
def get_sprites_progress_data(sprites: pygame.sprite.Group):
|
||||
data_list = []
|
||||
for sprite in sprites:
|
||||
data = sprite.get_obj_progress_data()
|
||||
if data:
|
||||
data_list.append(data)
|
||||
return data_list
|
11
TankMan/src/game_module/test_case.py
Normal file
11
TankMan/src/game_module/test_case.py
Normal file
@ -0,0 +1,11 @@
|
||||
from src.template.Player import Player
|
||||
|
||||
|
||||
class TestPlayer(object):
|
||||
construction = {"_id": 0, "_no": 0, "x": 0, "y": 0, "width": 60, "height": 60}
|
||||
player = Player(construction)
|
||||
|
||||
def test_get_xy_pos(self):
|
||||
assert type(self.player.get_xy_pos()) == tuple
|
||||
assert type(self.player.get_xy_pos()[0]) == int
|
||||
assert type(self.player.get_xy_pos()[1]) == int
|
Reference in New Issue
Block a user