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

68 lines
2.0 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.png",
"BULLET":"bullet.png",
2015-05-11 22:15:40 +01:00
"TARGETS":[],
"SHOOTER":"shooter.png",
"TARGET_BULLET":"target_bullet.png"
2015-04-16 10:00:39 +01:00
}
self.path=os.path.dirname(os.path.realpath(__file__)) + "\\resources\\texture_packs\\"
self.pack = "default"
2015-05-11 22:16:33 +01:00
def load_texture_pack(self, packName):
if os.path.exists(self.path + packName):
2015-04-18 16:33:22 +01:00
self.images["TARGETS"] = []
targets = glob.glob(self.path+packName+"\\target*.png")
2015-04-18 16:33:22 +01:00
logging.debug("Found {0} target files.".format(len(targets)))
for file in targets:
fileName = file.split("\\")[-1]
2015-04-18 16:33:22 +01:00
self.images["TARGETS"].append(filename)
self.pack = packName
2015-05-11 22:17:04 +01:00
else: logging.warn("Cannot find texture pack '{}'".format(packName))
2015-05-11 22:16:33 +01:00
def get_texture(self, objectName):
filename = self.path + self.pack + "\\{0}.png".format(self.images[objectName.upper()])
return pygame.image.load(filename)
2015-05-11 22:15:40 +01:00
def get_target_texture(self):
filename = self.path + self.pack + "\\{}.png".format(self.images["TARGETS"][randint(0,len(self.images["TARGETS"]))])
return pygame.image.load(filename)
2015-05-11 22:17:04 +01:00
def list_packs(self):
return [x[0] for x in os.walk(self.path)]
Level_Template = namedtuple('Level_Template', ("rows", "padding", "firebacks", "powerups"))
Levels = [
2015-05-10 13:12:11 +01:00
Level_Template(2, 20, 0, 0),
Level_Template(3, 15, 2, 1),
Level_Template(4, 25, 7, 1)
]
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():
2015-05-09 23:54:31 +01:00
logging.info("Generating a random level!")
2015-05-08 10:11:32 +01:00
rows = randint(0, 12)
2015-05-10 13:12:11 +01:00
padding = randint(0, 30)
2015-05-08 10:11:32 +01:00
firebacks = randint(0, 15)
powerups = randint(0, 15)
2015-05-10 13:12:11 +01:00
return Level_Template(rows, padding, firebacks, powerups)
2015-05-11 22:17:04 +01:00
2015-05-10 13:12:11 +01:00
Sounds = {}
def init_sounds():
music_files = ["main.mp3", "fire.mp3"]
for file in music_files:
path = os.path.dirname(os.path.realpath(__file__)) + "\\resources\\sounds\\" + file
if file == "main.mp3": mixer = pygame.mixer.music.load(path)
else: mixer = pygame.mixer.Sound(file)
Sounds.update(file.split(".")[0], mixer)