def collided(self, obj): self.keep_colliding.add(obj) if (obj in self.collision_set): return if (obj.hostile): Player.get_instance().got_hit(obj) self.level.controller.push_screen('FadeMessage', 'Ship_Hit') v1i = self.shape.velocity v2i = None if (hasattr(obj.shape, 'fake_vel')): v2i = obj.shape.fake_vel else: v2i = obj.shape.velocity m1 = self.shape.mass m2 = obj.shape.mass e = 1. # ellastic collision n = (obj.shape.position - self.shape.position).normalizing() c = n * (v1i - v2i) v1f = v1i - n.scalar(((m2 * c) / (m1 + m2)) * (1 + e)) #v2f = v2i + n.scalar(((m1*c)/(m1+m2)) * (1 + e)) self.shape.velocity = v1f
def collided(self, obj): self.keep_colliding.add(obj) if (obj in self.collision_set): return if (obj.hostile): Player.get_instance().got_hit(obj) self.level.controller.push_screen('FadeMessage', 'Ship_Hit') v1i = self.shape.velocity v2i = None if (hasattr(obj.shape, 'fake_vel')): v2i = obj.shape.fake_vel else: v2i = obj.shape.velocity m1 = self.shape.mass m2 = obj.shape.mass e = 1. # ellastic collision n = (obj.shape.position - self.shape.position).normalizing() c = n * (v1i - v2i) v1f = v1i - n.scalar(((m2*c)/(m1+m2)) * (1 + e)) #v2f = v2i + n.scalar(((m1*c)/(m1+m2)) * (1 + e)) self.shape.velocity = v1f
def collided_with_asteroid(self, ast): l = [('remove', self)] if (ast.destructible): ast.hp -= self.damage if (ast.hp <= 0): Player.get_instance().object_destroyed(ast) l.append(('remove', ast)) return l
def collided_with_asteroid(self, ast): l = [('remove', self)] if (ast.destructible): ast.hp -= self.damage if (ast.hp <= 0): Player.get_instance().object_destroyed(ast) l.append(('remove', ast)) self.lvl.controller.push_screen('MovingMessage', 'Show_Missile_Hit') else: self.lvl.controller.push_screen('MovingMessage', 'Show_Missile_Miss') return l
def start_pyasteroids(): random.seed() import sys from OpenGL.GLUT import glutInit glutInit(sys.argv) app = QApplication(argv) # Game state Player() # Load the config files in memory ConfigManager() # Add the custom fonts to the Qt database FontManager() # Creates the window (GLWidget is created there) win = AsteroidsMainWindow() # Pop-up the window win.show() # Faster processing try: import psyco except: print 'You currently do not have the Psyco module in your PYTHONPATH.' print 'It is highly advisable to install it for a much better gaming performance.' print 'Official site: http://psyco.sourceforge.net/' # Gives control to Qt app.exec_()
def __init__(self, level_number, level): self.level_number = level_number self.info = {} self.fields = set() self.level = level self.player_state = Player.get_instance() cfg = Config('interface', 'Settings') font_name = cfg.get('field_font') font_size = cfg.get('field_font_sz') self.field_font = FontManager.getFont(font_name) self.field_font.setPointSize(font_size) self.field_color = QColor.fromRgb(*cfg.get('field_color')) for f_name in ConfigManager.getOptions('interface', 'Fields'): s = ConfigManager.getVal('interface', 'Fields', f_name) s = map(str.strip, s.split('||')) img = QImage('resources/images/'+s[0]) img_pos = QPoint(*eval(s[1])) info_rect = QRect(*eval(s[2])) scale = float(s[3]) if (len(s) >= 5): font = QFont(self.field_font) font.setPointSize(int(s[4])) else: font = self.field_font img_w, img_h = img.width(), img.height() img_rect = QRect( img_pos.x(), img_pos.y(), int(img_w*scale), int(img_h*scale) ) self.info[f_name] = '' self.fields.add(Field(f_name, img, img_rect, info_rect, font)) self.radar = Radar.from_config('E-Radar', self) self.missile = GuidedMissile.from_config('GuidedMissile', self)
def load_file(self, level_name): lvl = yaml.load(open('resources/levels/' + level_name + '.lvl')) self.title = lvl['name'] self.dimensions = lvl['scene']['dimensions'] self.time = float(lvl['time']) * 60 #models = {} for element in lvl['elements']: file = open('resources/models/' + element['model']['file'], 'r') name = element['name'] subtitle = element['subtitle'] translate = element['model'].get('translate', (0., 0., 0.)) rotate = element['model'].get('rotate', (0., 0., 0.)) scale = element['model'].get('scale', 1.) rc = element['model'].get('radius_correction', 0.) gl_model = GLModel(file, translate, rotate, scale, rc) self.models[name] = (gl_model, element) file.close() self.dimensions = Vector3d(*lvl['scene']['dimensions']) type_class = { 'planet': Planet, 'asteroid': Asteroid, 'start_portal': Portal, 'end_portal': Portal, 'enemyship': EnemyShip } id_table = {} obj_set = set() for object in lvl['scene']['objects']: element_name = object['element'] shape = None model, element = self.models[element_name] mass = float(object['mass']) rvel = object['rotation_velocity'] movement = object['movement']['type'] if (movement == 'static'): pos = Vector3d(*object['pos']) shape = Shape(mass, pos) elif (movement == 'orbit'): center_id = object['movement']['center_planet_id'] center_obj = id_table[center_id] center_pos = Vector3d(*center_obj.shape.position) shape = Shape(mass, center_pos) shape.rotation_radius = object['movement']['radius'] shape.rot_vel_xy = object['movement']['rot_velocity_xy'] shape.rot_vel_z = object['movement']['rot_velocity_z'] shape.rot_xy = object['movement']['rot_xy'] shape.rot_z = object['movement']['rot_z'] elif (movement == 'dynamic'): pos = Vector3d(*object['pos']) shape = Shape(mass, pos) shape.velocity = Vector3d(*object['movement']['velocity']) shape.velocity_angular_x = rvel[0] shape.velocity_angular_y = rvel[1] shape.velocity_angular_z = rvel[2] type = element['type'] _object = type_class[type](model, shape, element) if (type == 'asteroid'): _object.asteroid_type = object['movement']['asteroid_type'] elif (type == 'enemyship'): _object.level = self _object.gun_pos = Vector3d(*object['gun_position']) obj_set.add(_object) if ('id' in object): id_table[object['id']] = _object for obj in obj_set: self.add_object(obj) Player.get_instance().beginning_level(self) self.make_spaceship(lvl, self.models) self.make_guns(self.models)
def load_file(self, level_name): lvl = yaml.load(open('resources/levels/' + level_name + '.lvl')) self.title = lvl['name'] self.dimensions = lvl['scene']['dimensions'] self.time = float(lvl['time']) * 60 #models = {} for element in lvl['elements']: file = open('resources/models/'+element['model']['file'], 'r') name = element['name'] subtitle = element['subtitle'] translate = element['model'].get('translate', (0.,0.,0.)) rotate = element['model'].get('rotate', (0.,0.,0.)) scale = element['model'].get('scale', 1.) rc = element['model'].get('radius_correction', 0.) gl_model = GLModel(file, translate, rotate, scale, rc) self.models[name] = (gl_model, element) file.close() self.dimensions = Vector3d(*lvl['scene']['dimensions']) type_class = { 'planet' : Planet, 'asteroid' : Asteroid, 'start_portal' : Portal, 'end_portal' : Portal, 'enemyship' : EnemyShip } id_table = { } obj_set = set() for object in lvl['scene']['objects']: element_name = object['element'] shape = None model, element = self.models[element_name] mass = float(object['mass']) rvel = object['rotation_velocity'] movement = object['movement']['type'] if (movement == 'static'): pos = Vector3d(*object['pos']) shape = Shape(mass, pos) elif (movement == 'orbit'): center_id = object['movement']['center_planet_id'] center_obj = id_table[center_id] center_pos = Vector3d(*center_obj.shape.position) shape = Shape(mass, center_pos) shape.rotation_radius = object['movement']['radius'] shape.rot_vel_xy = object['movement']['rot_velocity_xy'] shape.rot_vel_z = object['movement']['rot_velocity_z'] shape.rot_xy = object['movement']['rot_xy'] shape.rot_z = object['movement']['rot_z'] elif (movement == 'dynamic'): pos = Vector3d(*object['pos']) shape = Shape(mass, pos) shape.velocity = Vector3d(*object['movement']['velocity']) shape.velocity_angular_x = rvel[0] shape.velocity_angular_y = rvel[1] shape.velocity_angular_z = rvel[2] type = element['type'] _object = type_class[type](model, shape, element) if ( type == 'asteroid' ): _object.asteroid_type = object['movement']['asteroid_type'] elif ( type == 'enemyship' ): _object.level = self _object.gun_pos = Vector3d(*object['gun_position']) obj_set.add(_object) if ('id' in object): id_table[object['id']] = _object for obj in obj_set: self.add_object(obj) Player.get_instance().beginning_level(self) self.make_spaceship(lvl, self.models) self.make_guns(self.models)