1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
attack-on-blocks/assets.py

83 lines
2.9 KiB
Python
Raw Normal View History

2015-04-18 16:32:14 +01:00
import os, pygame, glob, logging
from collections import namedtuple
2015-05-08 10:11:32 +01:00
from random import randint
2015-04-18 16:32:14 +01:00
2015-04-16 10:00:39 +01:00
class Textures():
def __init__(self):
self.images = {
"PLAYER":"player",
"BULLET":"bullet",
"TARGETS":[],
"SHOOTER":"shooter",
2015-05-19 12:13:44 +01:00
"TARGET_BULLET":"bullet_target",
"POWERUP": "powerup"
}
self.path=os.path.dirname(os.path.realpath(__file__)) + "\\resources\\texture_packs\\"
self.pack = "default"
def load_texture_pack(self, pack_name):
if os.path.exists(self.path + pack_name):
self.images["TARGETS"] = []
2015-04-18 16:33:22 +01:00
targets = glob.glob(self.path+pack_name+"\\target*.png")
logging.debug("Found {0} target files.".format(len(targets)))
for file in targets:
filename = file.split("\\")[-1].replace(".png", "")
self.images["TARGETS"].append(filename)
2015-04-18 16:33:22 +01:00
self.pack = pack_name
else: logging.warn("Cannot find texture pack '{}'".format(ppack_name))
def get_texture(self, objectName):
filename = self.path + self.pack + "\\{0}.png".format(self.images[objectName.upper()])
return pygame.image.load(filename)
def get_target_texture(self, ID=False):
if not ID:
index = randint(0,len(self.images["TARGETS"])-1) if len(self.images["TARGETS"]) >=1 else 0
filename = self.path + self.pack + "\\{}.png".format(self.images["TARGETS"][index])
return [pygame.image.load(filename), index]
else:
filename = self.path + self.pack + "\\target{}.png".format(int(ID))
return [pygame.image.load(filename), ID]
2015-05-11 22:15:40 +01:00
def list_packs(self):
return [x[0].replace(self.path, "") for x in os.walk(self.path)]
2015-05-11 22:17:04 +01:00
Level_Template = namedtuple('Level_Template', ("rows", "padding", "firebacks", "powerups"))
Levels = [
Level_Template(2, 20, 0, 0),
2015-05-12 13:15:53 +01:00
Level_Template(3, 15, 1, 0),
Level_Template(4, 15, 3, 1),
Level_Template(3, 7, 2, 2),
Level_Template(5, 15, 2, 1),
2015-05-19 12:19:06 +01:00
Level_Template(2, 20, 100, 2), #All the enemies!
Level_Template(3, 15, 2, 1),
2015-05-19 12:19:06 +01:00
Level_Template(3, 7, 2, 4),
Level_Template(4, 15, 4, 3),
Level_Template(5, 35, 4, 6),
]
2015-05-08 10:11:32 +01:00
2015-05-10 13:12:11 +01:00
2015-05-08 10:11:32 +01:00
def generate_random_level():
logging.info("Generating a random level!")
2015-05-12 13:07:57 +01:00
rows = randint(1, 12)
2015-05-12 15:00:50 +01:00
padding = randint(5, 30)
firebacks = randint(0, 15)
powerups = randint(0, 15)
return Level_Template(rows, padding, firebacks, powerups)
2015-05-10 13:12:11 +01:00
2015-05-11 22:17:04 +01:00
2015-05-10 13:12:11 +01:00
Sounds = {}
def init_sounds():
2015-05-21 13:21:37 +01:00
music_files = ["main.ogg", "OP.ogg", "shot.ogg"]
for file in music_files:
path = os.path.dirname(os.path.realpath(__file__)) + "\\resources\\sounds\\" + file
2015-05-21 13:21:37 +01:00
if file == "main.ogg":
mixer = pygame.mixer.music
mixer.load(path)
else: mixer = pygame.mixer.Sound(path)
mixer.set_volume(1.0)
2015-05-21 13:21:37 +01:00
Sounds[file.replace(".ogg", "")] = mixer