You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import os, pygame, glob, logging
  2. from collections import namedtuple
  3. from random import randint
  4. class Textures():
  5. def __init__(self):
  6. self.images = {
  7. "PLAYER":"player",
  8. "BULLET":"bullet",
  9. "TARGETS":[],
  10. "SHOOTER":"shooter",
  11. "TARGET_BULLET":"bullet_target",
  12. "POWERUP": "powerup"
  13. }
  14. self.path=os.path.dirname(os.path.realpath(__file__)) + "\\resources\\texture_packs\\"
  15. self.pack = "default"
  16. def load_texture_pack(self, pack_name):
  17. if os.path.exists(self.path + pack_name):
  18. self.images["TARGETS"] = []
  19. targets = glob.glob(self.path+pack_name+"\\target*.png")
  20. logging.debug("Found {0} target files.".format(len(targets)))
  21. for file in targets:
  22. filename = file.split("\\")[-1].replace(".png", "")
  23. self.images["TARGETS"].append(filename)
  24. self.pack = pack_name
  25. else: logging.warn("Cannot find texture pack '{}'".format(ppack_name))
  26. def get_texture(self, objectName):
  27. filename = self.path + self.pack + "\\{0}.png".format(self.images[objectName.upper()])
  28. return pygame.image.load(filename)
  29. def get_target_texture(self, ID=False):
  30. if not ID:
  31. index = randint(0,len(self.images["TARGETS"])-1) if len(self.images["TARGETS"]) >=1 else 0
  32. filename = self.path + self.pack + "\\{}.png".format(self.images["TARGETS"][index])
  33. return [pygame.image.load(filename), index]
  34. else:
  35. filename = self.path + self.pack + "\\target{}.png".format(int(ID))
  36. return [pygame.image.load(filename), ID]
  37. def list_packs(self):
  38. return [x[0].replace(self.path, "") for x in os.walk(self.path)]
  39. Level_Template = namedtuple('Level_Template', ("rows", "padding", "firebacks", "powerups"))
  40. Levels = [
  41. Level_Template(2, 20, 0, 0),
  42. Level_Template(3, 15, 1, 0),
  43. Level_Template(4, 15, 3, 1),
  44. Level_Template(3, 7, 2, 2),
  45. Level_Template(5, 15, 2, 1),
  46. Level_Template(2, 20, 100, 2), #All the enemies!
  47. Level_Template(3, 15, 2, 1),
  48. Level_Template(3, 7, 2, 4),
  49. Level_Template(4, 15, 4, 3),
  50. Level_Template(5, 35, 4, 6),
  51. ]
  52. def generate_random_level():
  53. logging.info("Generating a random level!")
  54. rows = randint(1, 12)
  55. padding = randint(5, 30)
  56. firebacks = randint(0, 15)
  57. powerups = randint(0, 15)
  58. return Level_Template(rows, padding, firebacks, powerups)
  59. Sounds = {}
  60. def init_sounds():
  61. music_files = ["main.ogg", "OP.ogg", "shot.ogg"]
  62. for file in music_files:
  63. path = os.path.dirname(os.path.realpath(__file__)) + "\\resources\\sounds\\" + file
  64. if file == "main.ogg":
  65. mixer = pygame.mixer.music
  66. mixer.load(path)
  67. else: mixer = pygame.mixer.Sound(path)
  68. mixer.set_volume(1.0)
  69. Sounds[file.replace(".ogg", "")] = mixer