def update(self,timepassed): self.fire_acctime += timepassed self.flash_acctime += timepassed self.blink_acctime += timepassed super(Boneman,self).update(timepassed) player = self.good_guys[0] if self.blink_acctime > self.blink_updatetime: self.blink_acctime = 0 self.is_blend_visible = not(self.is_blend_visible) if self.controller.current_steering_behaviour.vel.x > 0.1: self.id = 2 self.animator.play(self.id,timepassed) self.frame = self.animator.getFrame()[1] elif self.controller.current_steering_behaviour.vel.x < -0.05: self.id = 1 self.animator.play(self.id,timepassed) self.frame = self.animator.getFrame()[1] self.image = self.image_master[self.id][self.frame] self.handleFlashing() self.fire() # check against walls self.updateBullets(timepassed) for bullet in self.bullets_list: for wall in self.tileList: if bullet.col_rect.colliderect(wall.col_rect): bullet.alive = False # check against items for bullet in self.bullets_list: for item in self.itemList: if item.kind == "FireCrate": if bullet.col_rect.colliderect(item.col_rect): bullet.alive = False for bullet in self.bullets_list: if not player.is_flashing: if bullet.col_rect.colliderect(player.rect): bullet.alive = False player.flash() player.health -= self.damage player.sound_manager.play("lose.ogg") for bullet in self.bullets_list: if not bullet.alive: self.bullets_list.remove(bullet) # add collision effect params = [self.images_dict["hit"][0],1,10] new_effect = gobs.AnimObject(params,bullet.rect.centerx,bullet.rect.centery,0,0,1) self.effects.append(new_effect)
def __init__(self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h self.hx = self.w / 2 self.hy = self.h / 2 self.collidable = True self.x_coord = self.x / w self.y_coord = self.y / h self.kind = "FireCrate" image_path = "images\\fire_crate.png" self.master_images = gutils.sliceSheetColKey(w, h, image_path) self.anim_object = gobs.AnimObject([self.master_images, -1, 6], x, y, 0, 0, 1) self.health = 10 self.alive = True self.image = self.anim_object.image self.rect = self.image.get_rect() self.rect.topleft = (self.x, self.y) self.col_rect = pygame.Rect(0, 0, 14, 14) self.col_rect.center = self.rect.center
def __init__(self, image_path, x, y, w, h, direction, speed=0.8, fps=12, friction=1.0, rotate=False, trail=False): self.x = x self.y = y self.rotate = rotate self.kind = "AnimatedBullet" self.trail = trail self.trail_list = [] self.max_traillen = 6 self.master_images = gutils.sliceSheetColKey(w, h, image_path) self.main_image = self.master_images[0].copy() self.rect = self.main_image.get_rect() self.rect.center = (self.x, self.y) self.col_rect = pygame.Rect(self.x, self.y, 4, 4) self.dir = direction self.rot_rect = self.main_image.get_rect() self.speed = speed self.friction = friction self.fps = fps self.dx = math.cos(direction * (math.pi / 180)) * self.speed self.dy = math.sin(direction * (math.pi / 180)) * self.speed self.anim_object = gobs.AnimObject([self.master_images, -1, self.fps], x, y, self.dx, self.dy, self.friction) self.alive = True if self.rotate: self.image = pygame.transform.rotate(self.main_image, 360 - self.dir) self.rot_rect = self.image.get_rect(center=self.rect.center) else: self.image = self.main_image
def __init__(self, image_path, x, y, w, h, target, speed=0.3, fps=15, friction=0.97, trail=True): self.kind = "RandomBullet" self.trail_list = [] self.max_traillen = 6 self.x, self.y = (x, y) self.yvel = self.xvel = 0 self.fps = fps self.master_images = gutils.sliceSheetColKey(w, h, image_path) self.anim_object = gobs.AnimObject([self.master_images, -1, self.fps], x, y, 0, 0, 1) self.main_image = self.master_images[0].copy() self.image = self.main_image.copy() self.rect = self.image.get_rect() self.rot_rect = self.image.get_rect() self.col_rect = pygame.Rect(self.x, self.y, 4, 4) self.missileOrientation = 0 self.speed = speed self.maxVel = 3.0 self.vectorToTargetN = (0, 0) self.angularVelocity = 0 self.friction = friction self.alive = True self.target = target
def changeDir(self, x, y, dx, dy): self.anim_object = gobs.AnimObject([self.master_images, -1, self.fps], x, y, self.dx, self.dy, self.friction) self.dir = math.atan2(self.dy, self.dx) * 180 / math.pi
def update(self,timepassed): if self.health > 100: self.health = 100 if self.health <= 0 and self.alive: #self.run_flag[0] = False #self.gamestate.exit_status = 0 self.alive = False params = [self.images_dict["p_kill"][0],1,12] killed_effect = gobs.AnimObject(params,self.rect.x-24,self.rect.y-24,0,0,1) emitter1 = po.ParticleEmitter(self.rect.centerx,self.rect.centery, strength = 0.4 ,mode = ("stream",120),direction=270,friction=1.01) self.effects.append(killed_effect) self.effects.append(emitter1) self.time_since_killed = 0 # tweaks self.can_draw_orb = False self.is_flashing = False if not self.alive: self.time_since_killed += timepassed if self.time_since_killed > self.end_duration: self.run_flag[0] = False self.gamestate.exit_status = 0 if self.last_dir == 0: self.image = self.dead_list[0] else: self.image = self.dead_list[1] return self.cur_state.runBehaviour(timepassed) self.time_since_dash += timepassed self.flash_acctime += timepassed self.blink_acctime += timepassed if self.blink_acctime > self.blink_updatetime: self.blink_acctime = 0 self.is_blend_visible = not(self.is_blend_visible) self.cur_weapon = self.weapons_list[self.weapon_id] if self.up_is_down: self.dy -= self.speed if not self.left_is_down or not self.right_is_down: self.cur_state = self.states[3] elif self.down_is_down: self.dy += self.speed if not self.left_is_down or not self.right_is_down: self.cur_state = self.states[4] if self.left_is_down: self.cur_state = self.states[1] elif self.right_is_down: self.cur_state = self.states[2] else: if not self.up_is_down and not self.down_is_down: #print "this shouldnt run" self.cur_state = self.states[0] if self.last_dir == 0: self.target.position.x = self.rect.centerx - 4 self.target.position.y = self.rect.centery else: self.target.position.x = self.rect.centerx - 4 self.target.position.y = self.rect.centery self.weapon_target.position.x = self.rect.centerx self.weapon_target.position.y = self.rect.centery self.magic_orb.update(timepassed) if self.animator.anim_started: self.id,self.frame,a_s = self.animator.getFrame() self.animator.setFps(12) else: self.animator.setFps(8) for effect in self.effects_list: effect.update() for effect in self.effects_list: if not effect.alive: self.effects_list.remove(effect) # get firing_angle and vector self.calculateFireAngle() self.firing_angle = self.fire_angle xvec = self.rect.centerx - self.shooter_rect.centerx yvec = self.rect.centery - self.shooter_rect.centery magnitude = math.sqrt(xvec*xvec + yvec*yvec) if magnitude != 0: xvec /= magnitude yvec /= magnitude self.firing_vector = (xvec,yvec) # calculate pointer image position p_xpos = self.magic_orb.rect.centerx + (self.pointer_radius * math.cos(self.firing_angle*(DEGTORAD))) p_ypos = self.magic_orb.rect.centery + (self.pointer_radius * math.sin(self.firing_angle*(DEGTORAD))) self.pointer_image_rect.center = (p_xpos,p_ypos) self.pointer_image = pygame.transform.rotate(self.pointer_surf,180-self.firing_angle) #self.pointer_image.set_alpha(100) self.pointer_image_rot_rect = self.pointer_image.get_rect(center=self.pointer_image_rect.center) # update gun self.cur_weapon.update(timepassed) self.updateBullets(timepassed) for bullet in self.bullets_list: for enemy in self.enemyList: if bullet.col_rect.colliderect(enemy.rect): if bullet.kind != "FlameBullet": bullet.alive = False # make enemy blink here enemy.flash() # damage enemy enemy.health -= self.cur_weapon.damage self.sound_manager.play("hurt_hit.ogg") for bullet in self.bullets_list: for wall in self.tileList: if bullet.kind == "BounceBullet": if bullet.can_collide: if bullet.col_rect.colliderect(wall.col_rect): if bullet.acc_rebounds < bullet.max_rebounds: bullet.resolveCollisions(wall) bullet.cool_acctime = 0 bullet.acc_rebounds += 1 self.sound_manager.play("bounce_w.ogg") break else: # kill bullet bullet.alive = False if bullet.kind == "PiercerBullet": if bullet.rect.right <0 or bullet.rect.right > PLAY_W or\ bullet.rect.bottom <0 or bullet.rect.top > PLAY_H: bullet.alive = False else: if bullet.col_rect.colliderect(wall.col_rect): bullet.alive = False for item in self.itemList: if item.kind == "FireCrate": if bullet.kind == "BounceBullet": if bullet.can_collide: if bullet.col_rect.colliderect(item.col_rect): if bullet.acc_rebounds < bullet.max_rebounds: bullet.resolveCollisions(item) bullet.cool_acctime = 0 bullet.acc_rebounds += 1 self.sound_manager.play("bounce_w.ogg") break else: # kill bullet bullet.alive = False else: if bullet.col_rect.colliderect(item.col_rect): bullet.alive = False item.health -= self.cur_weapon.damage for bullet in self.bullets_list: if not bullet.alive: self.bullets_list.remove(bullet) if bullet.kind != "FlameBullet": # add collision effect params = [self.images_dict["hit"][0],1,10] new_effect = gobs.AnimObject(params,bullet.rect.centerx,bullet.rect.centery,0,0,1) self.effects.append(new_effect) #print self.effects self.dy *= self.friction self.dx *= self.friction self.x += self.dx self.y += self.dy self.rect.topleft = (self.x,self.y) # update line colliders topline = ((float(self.rect.left),float(self.rect.top)),(float(self.rect.right),float(self.rect.top))) bottomline = ((float(self.rect.left),float(self.rect.bottom)),(float(self.rect.right),float(self.rect.bottom))) leftline = ((float(self.rect.left),float(self.rect.top)),(float(self.rect.left),float(self.rect.bottom))) rightline = ((float(self.rect.right),float(self.rect.top)),(float(self.rect.right),float(self.rect.bottom))) self.lines = [rightline,topline,leftline,bottomline] # update camera target ''' if self.last_dir == 0: self.cam_target.position.x = self.rect.centerx - 64 else: self.cam_target.position.x = self.rect.centerx + 64 if self.last_dir_v == 0: self.cam_target.position.y = self.rect.centery - 64 else: self.cam_target.position.y = self.rect.centery + 64 ''' self.cam_target.position.x = self.rect.centerx self.cam_target.position.y = self.rect.centery self.trail_positions.append((self.x,self.y)) if len(self.trail_positions)> self.trail_len: self.trail_positions.pop(0) if self.rect.left < self.shooter_rect.left: self.shooter_rect.left = self.rect.left if self.rect.right > self.shooter_rect.right: self.shooter_rect.right = self.rect.right if self.rect.top < self.shooter_rect.top: self.shooter_rect.top = self.rect.top if self.rect.bottom > self.shooter_rect.bottom: self.shooter_rect.bottom = self.rect.bottom self.col_rect.x = self.x + self.dx self.col_rect.y = self.y + self.dy # resolve collisions self.checkCollisions() self.animator.update(timepassed) self.image = self.image_master[self.id][self.frame] self.handleFlashing()
def update(self,timepassed): self.fire_acctime += timepassed self.flash_acctime += timepassed self.blink_acctime += timepassed player = self.good_guys[0] if self.blink_acctime > self.blink_updatetime: self.blink_acctime = 0 self.is_blend_visible = not(self.is_blend_visible) if self.health <= 0: self.alive = False self.checkCollisions() if self.move_dir == "left": self.xvel = -self.speed else: self.xvel = self.speed self.handleFlashing() self.fire() self.updateBullets(timepassed) for bullet in self.bullets_list: for wall in self.tileList: if bullet.rect.colliderect(wall.rect): bullet.alive = False # check against items for bullet in self.bullets_list: for item in self.itemList: if item.kind == "FireCrate": if bullet.col_rect.colliderect(item.col_rect): bullet.alive = False for bullet in self.bullets_list: if not player.is_flashing: if bullet.rect.colliderect(player.rect): bullet.alive = False player.flash() player.health -= self.damage player.sound_manager.play("lose.ogg") if self.rect.colliderect(player.col_rect): player.flash() player.health -= self.damage/2 player.sound_manager.play("lose.ogg") for bullet in self.bullets_list: if not bullet.alive: self.bullets_list.remove(bullet) # add collision effect params = [self.images_dict["hit"][0],1,10] new_effect = gobs.AnimObject(params,bullet.rect.centerx,bullet.rect.centery,0,0,1) self.effects.append(new_effect) #print self.effects self.x += self.xvel self.rect.topleft = (self.x,self.y) self.col_rect.topleft = (self.x,self.y)
def update(self,timepassed): if self.health <= 0: self.alive = False self.fire_acctime += timepassed self.flash_acctime += timepassed self.blink_acctime += timepassed self.ray_draw_acctime += timepassed self.acctime_to_move += timepassed self.moveTo() if self.ray_draw_acctime > self.ray_draw_duration: self.controller.update(timepassed) self.x,self.y = (self.controller.x,self.controller.y) self.checkCollisions() self.rect.topleft = (self.x,self.y) self.col_rect.topleft = (self.x,self.y) if self.ray_draw_acctime > 1000 and self.ray_draw_acctime < self.ray_draw_duration: self.can_draw_ray = True else: self.can_draw_ray = False if self.blink_acctime > self.blink_updatetime: self.blink_acctime = 0 self.is_blend_visible = not(self.is_blend_visible) if self.controller.current_steering_behaviour.vel.x > 0.1: self.id = 2 self.animator.play(self.id,timepassed) self.frame = self.animator.getFrame()[1] elif self.controller.current_steering_behaviour.vel.x < -0.05: self.id = 1 self.animator.play(self.id,timepassed) self.frame = self.animator.getFrame()[1] self.image = self.image_master[self.id][self.frame] self.handleFlashing() self.fire() self.updateBullets(timepassed) for bullet in self.bullets_list: for wall in self.tileList: if bullet.rect.colliderect(wall.rect): bullet.alive = False # test ray collision with player player = self.good_guys[0] if self.can_draw_ray: ray_point1 = (float(self.hit_tile.rect.centerx),float(self.hit_tile.rect.centery)) ray_point2 = (float(self.rect.centerx),float(self.rect.centery)) if not player.is_flashing: for line in player.lines: result = player.line_collider.calculateIntersectPoint(line[0],line[1],ray_point1,ray_point2) if result is not None: player.health -= self.damage player.flash() player.sound_manager.play("lose.ogg") break for bullet in self.bullets_list: if not bullet.alive: self.bullets_list.remove(bullet) # add collision effect params = [self.images_dict["hit"][0],1,10] new_effect = gobs.AnimObject(params,bullet.rect.centerx,bullet.rect.centery,0,0,1) self.effects.append(new_effect)
def runGameLoop(): is_running = [True] timepassed = 0 fps = 0 enemies = [] items = [] tiles = [] drawables = [] good_guys = [] effects = [] room = Room(0, 0, LEVEL_W * TILE_W, LEVEL_H * TILE_H) level_generator = level_gen.LevelGenerator(LEVEL_W, LEVEL_H) level_properties = level_generator.generateLevel() # generate room grid for pathfinding level_grid = pathfind.Grid(level_properties[0], level_properties[1]) for row in range(level_properties[1]): for col in range(level_properties[0]): surface = level_properties[2] pixel_array = level_properties[3] if pixel_array[col][row] == surface.map_rgb((255,0,0)) or\ pixel_array[col][row] == surface.map_rgb((0,255,0)): # this is a wall; set square to blocked level_grid.set((col, row), True) level_gen.createLevelFrom(level_properties[0], level_properties[1], level_properties[2], level_properties[3], enemy_list=enemies, tile_list=tiles, item_list=items, draw_list=drawables, fx=effects, pcs=good_guys, images=images_dict, grid=level_grid) p = player.Player(is_running, 40, 40, enemies, tiles, effects, items, images_dict, sound_manager) p.health = state.player_health p.mana_amount = state.player_mana p.weapon_id = state.weapon_id p.attachGameState(state) room_number = state.room_number cam_ob_to_track = steer.Steerable("KArriveMovement", (p.x, p.x), 10, 10) cam_ob_to_track.current_steering_behaviour.setTimeToTarget(45.0) cam_ob_to_track.setTarget(p.cam_target) p.setState(cam_ob_to_track) good_guys.append(p) # set starting state for entities enemy_to_hold_key = random.choice(enemies) enemy_to_hold_key.setKey() grad_colors = [ ((40, 10, 0), (0, 0, 40)), ((140, 130, 0), (0, 100, 130)), ((10, 100, 0), (100, 100, 0)), ((40, 10, 100), (0, 100, 60)), ((10, 10, 90), (100, 0, 40)), ((4, 80, 10), (60, 130, 40)), ((19, 140, 149), (120, 0, 40)), ((100, 10, 50), (0, 29, 120)), ((40, 30, 90), (20, 120, 10)), ((0, 30, 130), (80, 10, 60)), ((140, 0, 90), (20, 120, 110)) ] pg = random.choice(grad_colors) # create ground surface ground_image = pygame.Surface( (LEVEL_W * TILE_W, LEVEL_H * TILE_H)).convert() ground_image_size = ground_image.get_size() grad_image1 = pygame.Surface( ((LEVEL_W * TILE_W) / 2, LEVEL_H * TILE_H)).convert() grad_image2 = pygame.Surface( ((LEVEL_W * TILE_W) / 2, LEVEL_H * TILE_H)).convert() gutils.fillGradient(grad_image1, pg[0], pg[1], vertical=False) gutils.fillGradient(grad_image2, pg[1], pg[0], vertical=False) grad_image1.set_alpha(160) grad_image2.set_alpha(160) ground_image.fill((0, 0, 0)) health_bar = generic.HealthBar(20, 26, 100) dash_bar = generic.DashBar(20, 34, p.dash_waittime) for tile in drawables: tile.draw(ground_image) for wall in tiles: wall.draw(ground_image) # draw gradient ground_image.blit(grad_image1, (0, 0)) ground_image.blit(grad_image2, (ground_image_size[0] / 2, 0)) camera = g_obs.CameraHandler((320, 240), room, cam_ob_to_track, delimit=False) camera.setState("lock", [(W / 2, H / 2)]) while is_running[0]: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: showPauseScreen(is_running) if event.key == state.controls["left"]: p.left_is_down = True if event.key == state.controls["right"]: p.right_is_down = True if event.key == state.controls["up"]: p.up_is_down = True p.last_dir_v = 0 if event.key == state.controls["down"]: p.down_is_down = True p.last_dir_v = 1 if event.key == state.controls["shoot"]: p.firePrimary() if event.key == state.controls["swap_left"]: p.swapWeapon(-1) if event.key == state.controls["swap_right"]: p.swapWeapon(1) if event.key == state.controls["use"]: p.use() if event.key == state.controls["dash"]: p.dash() if event.type == KEYUP: if event.key == state.controls["left"]: p.left_is_down = False if event.key == state.controls["right"]: p.right_is_down = False if event.key == state.controls["up"]: p.up_is_down = False if event.key == state.controls["down"]: p.down_is_down = False p.update(timepassed) for baddie in enemies: baddie.update(timepassed) for item in items: item.update(timepassed) for item in items: if item.kind == "Mana": if item.rect.colliderect(p.rect): p.mana_amount += item.mana_value items.remove(item) sound_manager.play("mana.ogg") elif item.kind == "RoomKey": if item.rect.colliderect(p.rect): p.room_key = item items.remove(item) params = [images_dict["key"][0], 1, 15] new_effect = g_obs.AnimObject(params, p.x, p.y, 0, 0, 1) effects.append(new_effect) sound_manager.play("key.ogg") elif item.kind == "DoorTile": if item.rect.colliderect(p.rect): if p.room_key is not None: item.is_open = True p.room_key = None sound_manager.play("door.ogg") elif item.kind == "FireCrate": if not item.alive: # add break animation params = [images_dict["break"][0], 1, 15] new_effect = g_obs.AnimObject(params, item.x, item.y, 0, 0, 1) effects.append(new_effect) sound_manager.play("crate.ogg") items.remove(item) for baddie in enemies: if not baddie.alive: # add die animation params = [images_dict["kill"][0], 1, 15] new_effect = g_obs.AnimObject(params, baddie.rect.x - 3, baddie.rect.y - 3, 0, 0, 1) effects.append(new_effect) sound_manager.play("ded.ogg") # if it has key ; drop a key object in item_list if baddie.holds_key: room_key = generic.RoomKey(*baddie.rect.topleft) items.append(room_key) # try particle emmiter if not too slow emitter = po.ParticleEmitter(baddie.rect.centerx, baddie.rect.centery, direction="random") emitter.burst() effects.append(emitter) # drop mana for i in range(random.randint(1, 3)): xpos = random.randint(baddie.rect.left, baddie.rect.right) ypos = random.randint(baddie.rect.top, baddie.rect.bottom) mana = generic.Mana(xpos, ypos, p.cam_target) items.append(mana) enemies.remove(baddie) for effect in effects: effect.update(timepassed) health_bar.setCurHealth(p.health) health_bar.update() dash_bar.setCurTime(p.time_since_dash) dash_bar.update() cam_ob_to_track.update(timepassed) camera.track() room.update() screen.fill((0, 0, 0)) room.clear() room.surface.blit(ground_image, (0, 0)) for item in items: item.draw(room.surface) p.draw(room.surface) for baddie in enemies: baddie.draw(room.surface) for effect in effects: effect.draw(room.surface) for effect in effects: if not effect.alive: effects.remove(effect) screen.blit(room.surface, room.surface_rect) # draw hud stuff main_hud_image.fill((0, 0, 0)) main_hud_image.blit(hud_item_image, hud_item_rect1) main_hud_image.blit(hud_item_image, hud_item_rect2) current_weapon_image = hud_images_dict[p.cur_weapon.kind] main_hud_image.blit(current_weapon_image, weapon_image_rect) if p.room_key is not None: main_hud_image.blit(p.room_key.image, key_image_rect) # mana hud mana_surf = font.render("mana : %s" % p.mana_amount, False, (255, 255, 255)) mana_rect = mana_surf.get_rect() mana_rect.x = hud_item_rect2.right + 6 mana_rect.centery = hud_item_rect2.centery main_hud_image.blit(mana_surf, mana_rect) # current floor hud floor_surf = font.render("floor : %s" % state.room_number, False, (255, 25, 25)) floor_rect = floor_surf.get_rect() floor_rect.x = mana_rect.right + 6 floor_rect.centery = hud_item_rect2.centery main_hud_image.blit(floor_surf, floor_rect) health_bar.draw(main_hud_image) dash_bar.draw(main_hud_image) screen.blit(main_hud_image, main_hud_rect) #scale2x_surf = pygame.transform.scale(res_surf,(640,480)) #screen.blit(scale2x_surf,(0,0)) #print state.best_score pygame.display.flip() timepassed = clock.tick(60) #fps = clock.get_fps() else: #print (state.room_number,state.best_score) # hold a temp old score to test with showDeadScreen #state.old_bestscore = state.best_score if state.exit_status == 1: #print "writing game state" state.player_health = (p.health + 7) state.player_mana = p.mana_amount state.weapon_id = p.weapon_id elif state.exit_status == 0: # Theres a save bug here thats bugging me ,I'll check it out later showDeadScreen(state.room_number) if state.room_number > state.best_score: state.saveScore(state.room_number) #print "is saving" #print state.best_score elif state.exit_status == -1: # quit to main menu pass