def __init__(self, pro, gravity): dim = v.Vec2(pro["width"] * 2, pro["height"] * 2) self.velocity = pro["velocity"] self.controls = pro["controls"] self.rect = pg.Rect(-dim.x / 2, -dim.y / 2, dim.x, dim.y) self.last_pos = v.Vec2(0, 0) self.or_image = pg.image.load(os.path.join(pro["skin"])).convert() self.jump = Jump(pro["jump_force"]) self.jump.gravity = gravity
def get_pos(self): pos = v.Vec2(0, 0) pos.x = int(self.ini_pos.x / self.ref - self.offset.x) pos.y = int(self.ini_pos.y - self.offset.y) if self.entity != None: pos.x -= self.entity.rect.x + int(self.entity.rect.width / 2) pos.y -= self.entity.rect.y + int(self.entity.rect.height / 2) return pos
def game_init(): global player player_rect = rect.init_rect(150, 150, 15, 15) player = char.init_character(player_rect, player_rect, anim.Animations(), 20, 20, 20, 20, 20) global movement_info movement_info = mi.Movement_Info() global desired_movement desired_movement = vec2.Vec2() global camera camera = vec2.Vec2() global arial_40 arial_40 = pygame.font.SysFont("Arial", 40, bold=True, italic=False) global arial_20 arial_20 = pygame.font.SysFont("Arial", 20, bold=True, italic=False) # Loads all levels into array lv.load_levels()
def update(self, time): key = pg.key.get_pressed() direction = v.Vec2(0, 1) #gravity on 'y'= 1 // 'y' = 0 if key[ord(self.controls["left"])]: direction.x = -1 if key[ord(self.controls["right"])]: direction.x = 1 if key[ord(self.controls["jump"])]: self.jump.jump() #gravity on # if key[pg.K_w]: direction.y = -1 #gravity off # if key[pg.K_s]: direction.y = 1 #gravity off direction.x *= int(self.velocity * time) # direction.y *= int(self.velocity*time) #gravity off direction.y *= self.jump.get_movement(time) #gravity on self.move(direction)
def colision_entities(entity1, entity2): if in_colision(entity1.rect, entity2.rect): print("colison!!!") if entity1.rect.x == entity1.last_pos.x and entity1.rect.y == entity1.last_pos.y: pr = get_regression(entity2.rect, entity2.last_pos, entity1.rect) entity2.move(pr, False) entity2.colision(get_side(pr)) entity1.colision(invert(get_side(pr))) return True else: pr = get_regression(entity1.rect, entity1.last_pos, entity2.rect) if entity2.rect.x == entity2.last_pos.x and entity2.rect.y == entity2.last_pos.y: entity1.move(pr, False) else: x = int(pr.x/2) y = int(pr.y/2) entity1.move(v.Vec2(x,y), False) entity2.move(v.Vec2(-pr.x + x,-pr.y + y), False) entity1.colision(get_side(pr)) entity2.colision(invert(get_side(pr))) return True return False
def colision_map(entity,map): offset = v.Vec2() offset.x = entity.rect.x - map.shift.x offset.y = entity.rect.y - map.shift.y x = int(offset.x/map.tile_dim) y = int(offset.y/map.tile_dim) length = v.Vec2() length.y = int((offset.y + entity.rect.height - 1)/map.tile_dim)+1 length.x = int((offset.x + entity.rect.width - 1)/map.tile_dim)+1 for j in range(y,length.y): for i in range(x,length.x): aux = v.Vec2(i,j) if(entity.get_offset().x < 0): aux.x = length.x + x - i - 1 if(entity.get_offset().y < 0): aux.y = length.y + y - j - 1 if map.buffer_map[aux.y][aux.x] != "0": r_blk = pg.Rect(0,0, map.tile_dim, map.tile_dim) r_blk.x = aux.x*map.tile_dim + map.shift.x r_blk.y = aux.y*map.tile_dim + map.shift.y pr = get_regression(entity.rect, entity.last_pos, r_blk) entity.move(pr, False) entity.colision(get_side(pr)) return True return False
def get_regression(rect1, last_pos1, rect2, ref = None): x = rect1.x - last_pos1.x y = rect1.y - last_pos1.y v1 = v.Vec2(rect1.x, rect1.y) v2 = v.Vec2(rect2.x, rect2.y) if x > 0: v1.x += rect1.width else: v2.x += rect2.width if y > 0: v1.y += rect1.height else: v2.y += rect2.height #busca de posicio relativa i calcul del desplaçament #que cal aplicar if x==0 or (x > 0 and last_pos1.x > v2.x) or (x < 0 and last_pos1.x + rect1.width < v2.x): return v.Vec2(0, v2.y - v1.y) elif y==0 or (y > 0 and last_pos1.y > v2.y) or (y < 0 and last_pos1.y + rect1.height < v2.y): return v.Vec2(v2.x - v1.x, 0) else: last = v.Vec2(last_pos1.x, last_pos1.y) if x < 0: last.x += rect1.width if y < 0: last.y += rect1.height #areglar cas de divisio per 0 aux = [] if (v1.x - last.x) == 0 or (v2.x - last.x) == 0: aux.append(round(abs((v2.x - last.x)/(v2.y - last.y)),5)) aux.append(round(abs((v1.x - last.x)/(v1.y - last.y)),5)) else: aux.append(round(abs((v1.y - last.y)/(v1.x - last.x)),5)) aux.append(round(abs((v2.y - last.y)/(v2.x - last.x)),5)) if aux[0] < aux[1]: return v.Vec2(0, v2.y - v1.y) elif aux[0] > aux[1]: return v.Vec2(v2.x - v1.x, 0) else: if ref == None: print("eyyy") return(v.Vec2(v2.x - v1.x, v2.y - v1.y))
def get_offset(self): a = v.Vec2() a.x = self.rect.x - self.last_pos.x a.y = self.rect.y - self.last_pos.y return a
def __init__(self): self.ini_pos = v.Vec2(0, 0) self.offset = v.Vec2(0, 0) self.size = v.Vec2(200, 200) self.entity = None
def __init__(self, pro): self.tile_dim = pro["tile_size"] * 2 self.open_map(pro["dir"]) self.load_tiles(pro["tiles"]) self.shift = v.Vec2(0, 0)
def tilemap_collision(movement_x, movement_y, collider, tilemap): movement_sign = vec2.Vec2() movement_sign.x = int(math.copysign(1, movement_x)) movement_sign.y = int(math.copysign(1, movement_y)) collider_grid_pos1 = vec2.Vec2() # Top left corner collider_grid_pos2 = vec2.Vec2() # Bottom right corner movement_info = mi.Movement_Info() movement_info.movement = vec2.Vec2() modified_movement = vec2.Vec2() collision_id_1 = 0 collision_id_2 = 0 # Y Collision while modified_movement.y != movement_y and movement_y != 0: modified_movement.y += movement_sign.y # Update collider grid positions collider_grid_pos1.x = int( (collider.x + modified_movement.x) / tilemap.tile_width) collider_grid_pos2.x = int( (collider.x + modified_movement.x + collider.w) / tilemap.tile_width) collider_grid_pos1.y = int( (collider.y + modified_movement.y) / tilemap.tile_height) collider_grid_pos2.y = int( (collider.y + modified_movement.y + collider.h) / tilemap.tile_height) if movement_sign.y < 0: collision_id_1 = tilemap.collision[int(collider_grid_pos1.y * tilemap.map_width + collider_grid_pos1.x)] collision_id_2 = tilemap.collision[int(collider_grid_pos1.y * tilemap.map_width + collider_grid_pos1.x)] if collision_id_1 == 2 or collision_id_2 == 2: movement_info.col_lethal = True return movement_info elif collision_id_1 == 3 or collision_id_2 == 3: movement_info.col_finish = True return movement_info elif collision_id_1 == 1 or collision_id_2 == 1: modified_movement.y -= movement_sign.y movement_info.col_up = True break elif movement_sign.y > 0: collision_id_1 = tilemap.collision[int(collider_grid_pos2.y * tilemap.map_width + collider_grid_pos1.x)] collision_id_2 = tilemap.collision[int(collider_grid_pos2.y * tilemap.map_width + collider_grid_pos2.x)] if collision_id_1 == 2 or collision_id_2 == 2: movement_info.col_lethal = True return movement_info elif collision_id_1 == 3 or collision_id_2 == 3: movement_info.col_finish = True return movement_info elif collision_id_1 == 1 or collision_id_2 == 1: modified_movement.y -= movement_sign.y movement_info.col_down = True break # X Collision while modified_movement.x != movement_x and movement_x != 0: modified_movement.x += movement_sign.x # Update collider grid positions collider_grid_pos1.x = int( (collider.x + modified_movement.x) / tilemap.tile_width) collider_grid_pos2.x = int( (collider.x + modified_movement.x + collider.w) / tilemap.tile_width) collider_grid_pos1.y = int( (collider.y + modified_movement.y) / tilemap.tile_height) collider_grid_pos2.y = int( (collider.y + modified_movement.y + collider.h) / tilemap.tile_height) if movement_sign.x < 0: collision_id_1 = tilemap.collision[int(collider_grid_pos1.y * tilemap.map_width + collider_grid_pos1.x)] collision_id_2 = tilemap.collision[int(collider_grid_pos2.y * tilemap.map_width + collider_grid_pos1.x)] if collision_id_1 == 2 or collision_id_2 == 2: movement_info.col_lethal = True return movement_info elif collision_id_1 == 3 or collision_id_2 == 3: movement_info.col_finish = True return movement_info elif collision_id_1 == 1 or collision_id_2 == 1: modified_movement.x -= movement_sign.x movement_info.col_left = True break elif movement_sign.x > 0: collision_id_1 = tilemap.collision[int(collider_grid_pos1.y * tilemap.map_width + collider_grid_pos2.x)] collision_id_2 = tilemap.collision[int(collider_grid_pos2.y * tilemap.map_width + collider_grid_pos2.x)] if collision_id_1 == 2 or collision_id_2 == 2: movement_info.col_lethal = True return movement_info elif collision_id_1 == 3 or collision_id_2 == 3: movement_info.col_finish = True return movement_info elif collision_id_1 == 1 or collision_id_2 == 1: modified_movement.x -= movement_sign.x movement_info.col_right = True break movement_info.movement = modified_movement return movement_info
def game_update(): global game_state if game_state == 1: # In Game global player global movement_info global desired_movement global number_of_deaths # Player Movement # In air if movement_info.col_down == False: # In air touching wall on left side if movement_info.col_left == True: #if left == True: desired_movement.y = gravity_on_wall #else: #desired_movement.y += gravity if jump == True: desired_movement.y = vertical_wall_jump_speed desired_movement.x = horizontal_wall_jump_speed if right == True: desired_movement.x += accel_in_air # In air touching wall on right side elif movement_info.col_right == True: #if right == True: desired_movement.y = gravity_on_wall #else: # desired_movement.y += gravity if jump == True: desired_movement.y = vertical_wall_jump_speed desired_movement.x = -horizontal_wall_jump_speed if left == True: desired_movement.x += -accel_in_air # In air not touching wall else: # Acceleration desired_movement.y += gravity if left == True: desired_movement.x += -accel_in_air elif right == True: desired_movement.x += accel_in_air # Decceleration elif desired_movement.x < 0: if desired_movement.x + deccel_in_air >= 0: desired_movement.x = 0 else: desired_movement.x += deccel_in_air elif desired_movement.x > 0: if desired_movement.x - deccel_in_air <= 0: desired_movement.x = 0 else: desired_movement.x -= deccel_in_air if movement_info.col_up == True: desired_movement.y = 0 # On Ground else: # Acceleration desired_movement.y = gravity if jump == True: desired_movement.y = jump_speed elif left == True: desired_movement.x += -accel_on_ground elif right == True: desired_movement.x += accel_on_ground # Decceleration # Switch to ground accel/deccel elif desired_movement.x < 0: if desired_movement.x + deccel_in_air >= 0: desired_movement.x = 0 else: desired_movement.x += deccel_in_air elif desired_movement.x > 0: if desired_movement.x - deccel_in_air <= 0: desired_movement.x = 0 else: desired_movement.x -= deccel_in_air desired_movement.x = max(-max_move_speed, min(desired_movement.x, max_move_speed)) movement_info = tm.tilemap_collision(desired_movement.x, desired_movement.y, player.box_collider, lv.levels[lv.current_level_id].tilemap) # Finished level if movement_info.col_finish == True: desired_movement = vec2.Vec2() if lv.current_level_id + 1 < len(lv.levels): lv.current_level_id += 1 player.box_collider.x = lv.levels[lv.current_level_id].start_x player.box_collider.y = lv.levels[lv.current_level_id].start_y else: game_state = 2 # Game Complete # PLayer collided with lethal object and was killed elif movement_info.col_lethal == True: number_of_deaths += 1 desired_movement = vec2.Vec2() player.box_collider.x = lv.levels[lv.current_level_id].start_x player.box_collider.y = lv.levels[lv.current_level_id].start_y # Move player player.box_collider.x += movement_info.movement.x player.box_collider.y += movement_info.movement.y # Center camera on player camera.x = -player.box_collider.x + (eng.screen_width/2) camera.y = -player.box_collider.y + (eng.screen_height/2) if restart == True: number_of_deaths = 0 game_state = 0 elif game_state == 0: # Main Menu if start == True: game_state = 1 lv.current_level_id = 0 player.box_collider.x = lv.levels[lv.current_level_id].start_x player.box_collider.y = lv.levels[lv.current_level_id].start_y elif game_state == 2: # Game Complete if restart == True: number_of_deaths = 0 game_state = 0