def construct_ship(self): self.center = self.get_center() normVect = vector2(0, 1) tanVect = vector2(1, 0) newVect = vector2(0, 3) self.line = [ (self.center - (normVect * self.scale * 3)).to_np2(), (self.center + (newVect + tanVect * self.scale)).to_np2(), (self.center - (-newVect + tanVect * self.scale)).to_np2() ]
def render_ship(self, screen): self.construct_ship() newLines = [] # Rotates all the lines accordingly (need to figure out the angles) for point in self.line: anotherVertice = vector2.from_np(point) - self.center finalVertice = vector2(0, 0) finalVertice.x = anotherVertice.x * math.cos( self.currentRotation) - anotherVertice.y * math.sin( self.currentRotation) finalVertice.y = anotherVertice.y * math.cos( self.currentRotation) + anotherVertice.x * math.sin( self.currentRotation) self.normalVect.x = -math.sin(self.currentRotation) self.normalVect.y = math.cos(self.currentRotation) point = (finalVertice + self.center).to_np2() newLines.append(point) self.line = newLines pygame.draw.polygon(screen, (200, 200, 0), self.line, 2)
def new_metagrid_cell(self, new_cell): #add terrain and stuff on it choice = 'none' for x in range(GRID_SIZE): for y in range(GRID_SIZE): p = vector2(new_cell.pos[0]+x, new_cell.pos[1]+y) #log ('new cell at %s ' % p) Terrain(pos=p)
def add_part(self, x, y, height, tex): if (x,y,height) in self.parts: part = self.parts[(x,y,height)] part.tex = tex part.layer = LAYER_BLOCKS+height else: part = SolidBlock(vector2(x,y), tex=tex, layer=LAYER_BLOCKS+height) self.parts[(x,y,height)] = part
def circle_visitor(x,y): ents = engine.metagrid.get_entities(x,y) if LAYER_BLOCKS in ents: dude = ents[LAYER_BLOCKS] if isinstance(ents, Flammable): Fire.burn(self,other) return else: FirePuff(pos=vector2(x,y))
def circle_visitor(x, y): ents = engine.metagrid.get_entities(x, y) if LAYER_BLOCKS in ents: dude = ents[LAYER_BLOCKS] if isinstance(ents, Flammable): Fire.burn(self, other) return else: FirePuff(pos=vector2(x, y))
def get_free_position(self, layer, tries=10): "GameGrid at %s getting free position for layer %s" % (self.pos, layer) placed = False while not placed and tries: tries = tries - 1 x = self.pos[0] + random.randint(0, GRID_SIZE - 1) y = self.pos[1] + random.randint(0, GRID_SIZE - 1) if not layer in self.get_entities(x, y): return vector2(x, y)
def add_part(self, x, y, height, tex): if (x, y, height) in self.parts: part = self.parts[(x, y, height)] part.tex = tex part.layer = LAYER_BLOCKS + height else: part = SolidBlock(vector2(x, y), tex=tex, layer=LAYER_BLOCKS + height) self.parts[(x, y, height)] = part
def __init__(self, center): self.center = center self.previousPos = center self.mass = 200 self.acceleration = vector2(0,0) self.angularAcceleration = 0 self.prevVelocity = vector2(0,0) self.currentVelocity = vector2(0,0) self.prevAngVelocity = 0 self.currentAngVelocity = 0 self.prevRotation = 0 self.currentRotation = 0 self.allForcesVect = [] self.currentRotation = 0 self.totalForce = vector2(0,0) self.inertiaMoment = 2
def __init__(self, pos_x, pos_y, width, height, edge=2): halfwidth = width * 0.5 halfheight = height * 0.5 d_max = sqrt(halfwidth*halfwidth+\ halfheight*halfheight) for x in range(width): for y in range(height): if random.random() < 0.025: try: Tree(vector2(pos_x + x, pos_y + y)) except CellFull: pass
def __init__(self, pos_x, pos_y, width, height,edge=2): halfwidth = width*0.5 halfheight = height*0.5 d_max = sqrt(halfwidth*halfwidth+\ halfheight*halfheight) for x in range(width): for y in range(height): if random.random() < 0.025: try: Fruit(vector2(pos_x+x, pos_y+y)) except CellFull: pass
def __init__(self, center, scale): super().__init__(center) self.center = center self.scale = scale self.line = [vector2(0, 0), vector2(0, 0), vector2(0, 0)] self.normalVect = vector2(0, 1) self.tangentVect = vector2(1, 0) self.velocity = vector2(0, 0) self.acceleration = vector2(0, 0)
def update_current_velocity(self, deltaTime): # Initializes the 'totalAcceleration' variable as a vector2 totalAcceleration = vector2(0,0) # Add all forces together for force in self.allForcesVect: totalAcceleration += force self.allForcesVect.remove(force) # Equalizes the current acceleration to the total acceleration with # all forces in consideration self.acceleration = totalAcceleration # Sets the previous position as the center self.previousPos = self.center # Updates current velocity self.currentVelocity = self.prevVelocity + (self.acceleration * deltaTime) # Sets previous velocity equal to the current one in order to make # other updates possible self.prevVelocity = self.currentVelocity # Utilizes the position equation to calculate the updated position self.center = self.previousPos + self.currentVelocity * deltaTime # Resets the acceleration self.acceleration = vector2(0,0)
def get_real_item(item): if isinstance(item, unicode): return str(item) if isinstance(item, dict): if "_type" in item: if item["_type"] == "vector2": return vector2(item["vec"][0], item["vec"][1]) if item["_type"] == "ent": log("making entity wrapper.") return PersistedWrapper(item["ent_id"]) elif item["_type"] == "dict": res = {} for kvp in item["pairs"]: res[get_real_item(kvp[0])] = get_real_item(kvp[1]) return res return item
import random from vector2 import * from metagrid import GRID_SIZE, METAGRID_SIZE ENTITY_SIZE = vector2(8,8) entity_class_registry = {} def RegisterPersisted(f): entity_class_registry[f.__name__] = f return f @RegisterPersisted class PersistedWrapper: def __init__(self, target_id): self.id = target_id self.target = None def __getattr__(self, name): if name == 'id' : return self.id if not self.target: #see if you can get the entity from the database print 'getting entity', self.id self.target = engine.get_entity(self.id) if not self.target: raise NoSuchEntity("Empty json wrapper for '%s'" % self.id) return getattr(self.target, name)
def move(self, new_pos): from engine import engine x,y = engine.metagrid.wrap_coords(new_pos.x, new_pos.y) new_pos = vector2(x,y) engine.metagrid.move_entity(self,new_pos)
import random from vector2 import * from metagrid import GRID_SIZE, METAGRID_SIZE ENTITY_SIZE = vector2(8, 8) entity_class_registry = {} def RegisterPersisted(f): entity_class_registry[f.__name__] = f return f @RegisterPersisted class PersistedWrapper: def __init__(self, target_id): self.id = target_id self.target = None def __getattr__(self, name): if name == 'id': return self.id if not self.target: #see if you can get the entity from the database print 'getting entity', self.id self.target = engine.get_entity(self.id) if not self.target: raise NoSuchEntity("Empty json wrapper for '%s'" % self.id)
def get_pos(self): return vector2(self.pos[0], self.pos[1])
def move(self, new_pos): from engine import engine x, y = engine.metagrid.wrap_coords(new_pos.x, new_pos.y) new_pos = vector2(x, y) engine.metagrid.move_entity(self, new_pos)
def add_vert_block_line(self,block_class, x, start_y, stop_y): for y in xrange(start_y, stop_y+1): self.add_block(block_class, vector2(x,y))
def main(): # Initializes pygame pygame.init() # Sets a predefined screen resolution res = (1280, 720) screen = pygame.display.set_mode(res) # Loads the background image bg = pygame.image.load("background.png") # Defines what will be the center of the ship center = vector2(res[0] / 2, res[1] / 2) # Instatiate all entities aship = ship(center, 8) dumbwell = gravity_well(vector2(300, 360), 105, 10000) well_2 = gravity_well(vector2(1000, 360), 170, 10000) well_3 = gravity_well(vector2(640, 200), 80, 10000) # Initialize delta time deltaTime = 0 lastTime = time.time() while (True): keypressed = pygame.key.get_pressed() for event in pygame.event.get(): if (event.type == pygame.QUIT): return if (keypressed[pygame.K_w]): aship.move_ship(-5000) # Valor em Newton if (keypressed[pygame.K_s]): aship.move_ship(5000) # Valor em Newton if (keypressed[pygame.K_a]): aship.rotate_ship(-2, deltaTime) # Valor em Newton if (keypressed[pygame.K_d]): aship.rotate_ship(2, deltaTime) # Valor em Newton # Fills the screen screen.fill((0, 0, 59)) # Draws the background screen.blit(bg, (0, 0)) # Update Ship aship.render_ship(screen) aship.update_current_velocity(deltaTime) aship.update_angular_velocity(deltaTime) # Update Gravity Wells dumbwell.check_if_in_radius(aship, screen) dumbwell.render_well(screen) well_2.check_if_in_radius(aship, screen) well_2.render_well(screen) well_3.check_if_in_radius(aship, screen) well_3.render_well(screen) # Flips the display pygame.display.flip() # Updates the deltaTime deltaTime = time.time() - lastTime lastTime = time.time()
def add_horiz_block_line(self, block_class, start_x, stop_x, y): for x in xrange(start_x, stop_x + 1): self.add_block(block_class,vector2(x,y))