def read_loader_config ( name ): ''' reads the loader config to a tuple Arguments: - name: the name of the file to read, according to "Objects" configuration name ''' path = "{0}/{1}/{2}{3}".format(get("Objects"), get("Objects Configs"), name, get("Objects Suffix")) r = [] # variable returned to the caller try: cfg = io.open( path ) lines = cfg.readlines( ) # Some pretty sketchy code to read the files for line in lines: if line[0] != '#': #do not read the comments try: line = line.rstrip(" "+os.linesep) line_parts = line.split('=') r+=[[line_parts[0], line_parts[1]]] except: pass except Exception as e: print (e) sys.exit ( "!!! Modules.Config.LoaderConfigReader.read_loader_config Could not load the file: '{0}'".format( path ) ) return r
def font(name): # return pygame.font.Font(pygame.font.match_font(get("Font {0}".format(name))), int(get("Font {0} Size".format(name)))) n,s = get("Font {0}".format(name)), int(get("Font {0} Size".format(name))) print('iii Loading Font {}, Size={}'.format(n,s)) r = pygame.font.Font(pygame.font.match_font(n), s) #r = None #r = pygame.font.Font(pygame.font.match_font(None, s)) print('iii Loaded Font {}, Size={}'.format(n,s)) return r
def load_images(imglist): r = dict() for image in imglist: print ( "Loading image {0}".format( "{0}/{1}/{2}".format(get("Objects"),get ( "Objects Images" ), image[1] ) )) img = pygame.image.load( "{0}/{1}/{2}".format(get("Objects"),get ( "Objects Images" ), image[1] ) ).convert_alpha() r[image[0]] = img return r
def init_screen(self): pygame.init() dims = self.dimensions opts = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.FULLSCREEN opts = pygame.DOUBLEBUF | pygame.HWSURFACE self.screen = pygame.display.set_mode((dims[0], int(dims[1])), opts) pygame.display.set_caption( get("Game Name") )
def init_modules(self): modules_list = get("Physics Modules").split(" ") self.class_list = dict() i = 0 for module in modules_list: self.load_module(module) self.class_list[module] = i i+=1
def load_images(imglist): r = dict() for image in imglist: p = "/Users/apple/Projects/Personal/Asteroids/{0}/{1}/{2}".format(get("Objects"),get ( "Objects Images" ), image[1] ) print ( "Loading image {0}".format(p)) img = pygame.image.load(p).convert_alpha() r[image[0]] = img return r
def load_config(self, name): cfg = ConfigReader2("{0}/{1}/{2}{3}".format(get("Objects"), get("Sprite Config Path"), name, get("Sprite Config Suffix"))) path="{0}/{1}{2}".format(get("Sprite Image Path"), name, get("Sprite Image Suffix")) print (path) image = Modules.Object.Loader.load_images([['default',path]])['default'] images = dict() surname = "" for sett in cfg.settings: s = sett[0] if s == "Sprite": surname = sett[1] images[surname] = [] elif s == "Picture": c = sett[1].split(" ") r = pygame.Rect ( int(c[0]), int(c[1]), int(c[2]), int(c[3]) ) surface = pygame.Surface( (r.w, r.h), pygame.SRCALPHA ).convert_alpha() surface.blit( image, (-r.x, -r.y) ) images[surname] += [surface] return images
def __init__(self): self.dimensions = ( int(get("Window Width")), int(get("Window Height")) ) pygame.init() self.init_screen() self.init_background() self.init_physics() self.init_explosions() self.init_fonts() # 1 Objects under the standart objects self.fast_objects = [] # 2 Standart obejcts self.objects = [] # 3 Objects over the standart objects self.over_objects = [] # 4 Texts which we want to show self.texts = [] # 5 Interface objects we're blitting to the screen self.interfaces = [] self.clock = pygame.time.Clock() self.fps = int(get("FPS")) self.tickcount = 0 self.input = Input() self.stop = False self.global_mappings = GlobalKeyMappings(self)
''' 02.12.2010 Andrej Cizov Engine class ''' from Modules.Object.CustomObjects.GenericObject import GenericObject import Modules.World from Modules.Config import get from Modules.World import World from Modules.Vector25D import Vector25D DIST_MULTIPLIER=float(get("Bomb Distance Multiplier"))**2 class GenericBomb(GenericObject): ''' Bomb definitions ''' def init(self): self.set_radius(100) self.set_power(100000000) self.set_counter(300) def set_counter(self, counter): self.counter = 300 def set_power(self, p): self.power = p def set_radius(self, r): self.explode_in = r**2
def __init__(self,world): Register.__init__(self) self.max = Vector25D ( float(get("Window Width")), float(get("Window Height")) ) print ("iii Modules.Physics.Modules.ScreenLoop loaded")
def font(name): return pygame.font.Font(pygame.font.match_font(get("Font {0}".format(name))), int(get("Font {0} Size".format(name))))
from Modules.Object.CustomObjects.GenericObject import GenericObject from Modules.Vector25D import Vector25D from Modules.Config import get import random import Modules.Object.Loader import Modules.World import math MAX_SMALL = int(get("Asteroids Children Max")) VELOCITY_FACTOR = float(get("Asteroids Velocity Factor")) COLLISION_DAMAGE = float(get("Asteroids Collision Damage Factor")) class Asteroid(GenericObject): def init(self): self.large = self.parts["root"].large def create_small(self): if self.large: # asteroid calculation is quadratic. we have more chances to get a larger ammount of children, than small n = random.random() n = n ** 2 n = int(math.ceil((1 - n) * MAX_SMALL)) for i in range(0, n): asteroid = Modules.Object.Loader.load("SmallAsteroid", Asteroid) asteroid.P.v[0] = self.P[0] - self.rect.w / 2 + random.random() * self.rect.w asteroid.P.v[1] = self.P[1] - self.rect.h / 2 + random.random() * self.rect.h asteroid.P.v[2] = random.random() * 360 asteroid.V = self.V + ((asteroid.P - self.V).norm() * VELOCITY_FACTOR * random.random())
from Modules.Object.CustomObjects.GenericObject import GenericObject from Modules.Vector25D import Vector25D from Modules.Config import get import Modules.World import pygame DAMAGEMULT=float(get("Bullet Damage Multiplier")) BULLETLIFESPAN=int(get("Bullet Lifespan")) class Bullet(GenericObject): def __init__(self, P, V, r, color, mass): global BULLETLIFESPAN self.HP = 100 self.init_vectors() self.V = V self.P = P self.mass = mass self.r = 4 self.color = color self.rect = pygame.Rect( (P[0]-r/2, P[1]-r/2), (r/2, r/2) ) self.lifespan = BULLETLIFESPAN self.mask = pygame.mask.Mask((1,1)) self.mask.set_at((0,0), True) def static_blit(self): pass def draw(self, to): self.rect.x = self.P[0]-self.r/2 self.rect.y = self.P[1]-self.r/2 try:
def init_config(self): self.pps = float(get("pps"))
def get_all_indexes(self, name): module_names = get("Physics Modules "+name).split(" ") r = [] for module in module_names: r+=[self.physics.get_module_index(module)] return r
Physics operation module - detects collisions - does force application to the main object ''' from __future__ import absolute_import from Modules.Register import Register from Modules.Config import get from Modules.Vector25D import Vector25D import math import random BLAST_FORCE_MULT=float(get("Blast Force Multiplier")) BLAST_DAMAGE_MULT=float(get("Blast Damage Multiplier")) class Physics(Register): def __init__(self, world): Register.__init__(self) self.world = world self.init_modules () print ("iii Modules.Physics loaded modules: {0}".format(self.items())) self.init_config() def init_modules(self): modules_list = get("Physics Modules").split(" ") self.class_list = dict() i = 0 for module in modules_list:
def init_screen(self): pygame.init() self.screen = pygame.display.set_mode(self.dimensions, pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.FULLSCREEN) pygame.display.set_caption( get("Game Name") )