__author__ = 'marble_xu'
import os
import json
import pygame as pg
from .. import setup, tools
from .. import constants as c
from ..components import info, stuff, player, brick, box, enemy, powerup, coin
class Level(tools.State):
def __init__(self):
tools.State.__init__(self)
self.player = None
def startup(self, current_time, persist):
self.game_info = persist
self.persist = self.game_info
self.game_info[c.CURRENT_TIME] = current_time
self.death_timer = 0
self.castle_timer = 0
self.moving_score_list = []
self.overhead_info = info.Info(self.game_info, c.LEVEL)
self.load_map()
self.setup_background()
self.setup_maps()
self.ground_group = self.setup_collide(c.MAP_GROUND)
self.step_group = self.setup_collide(c.MAP_STEP)
self.setup_pipe()
self.setup_slider()
self.setup_static_coin()
self.setup_brick_and_box()
self.setup_player()
self.setup_enemies()
self.setup_checkpoints()
self.setup_flagpole()
self.setup_sprite_groups()
def load_map(self):
map_file = 'level_' + str(self.game_info[c.LEVEL_NUM]) + '.json'
file_path = os.path.join('source', 'data', 'maps', map_file)
f = open(file_path)
self.map_data = json.load(f)
f.close()
def setup_background(self):
img_name = self.map_data[c.MAP_IMAGE]
self.background = setup.GFX[img_name]
self.bg_rect = self.background.get_rect()
self.background = pg.transform.scale(self.background,
(int(self.bg_rect.width*c.BACKGROUND_MULTIPLER),
int(self.bg_rect.height*c.BACKGROUND_MULTIPLER)))
self.bg_rect = self.background.get_rect()
self.level = pg.Surface((self.bg_rect.w, self.bg_rect.h)).convert()
self.viewport = setup.SCREEN.get_rect(bottom=self.bg_rect.bottom)
def setup_maps(self):
self.map_list = []
if c.MAP_MAPS in self.map_data:
for data in self.map_data[c.MAP_MAPS]:
self.map_list.append((data['start_x'], data['end_x'], data['player_x'], data['player_y']))
self.start_x, self.end_x, self.player_x, self.player_y = self.map_list[0]
else:
self.start_x = 0
self.end_x = self.bg_rect.w
self.player_x = 110
self.player_y = c.GROUND_HEIGHT
def change_map(self, index, type):
self.start_x, self.end_x, self.player_x, self.player_y = self.map_list[index]
self.viewport.x = self.start_x
if type == c.CHECKPOINT_TYPE_MAP:
self.player.rect.x = self.viewport.x + self.player_x
self.player.rect.bottom = self.player_y
self.player.state = c.STAND
elif type == c.CHECKPOINT_TYPE_PIPE_UP:
self.player.rect.x = self.viewport.x + self.player_x
self.player.rect.bottom = c.GROUND_HEIGHT
self.player.state = c.UP_OUT_PIPE
self.player.up_pipe_y = self.player_y
def setup_collide(self, name):
group = pg.sprite.Group()
if name in self.map_data:
for data in self.map_data[name]:
group.add(stuff.Collider(data['x'], data['y'],
data['width'], data['height'], name))
return group
def setup_pipe(self):
self.pipe_group = pg.sprite.Group()
if c.MAP_PIPE in self.map_data:
for data in self.map_data[c.MAP_PIPE]:
self.pipe_group.add(stuff.Pipe(data['x'], data['y'],
data['width'], data['height'], data['type']))
def setup_slider(self):
self.slider_group = pg.sprite.Group()
if c.MAP_SLIDER in self.map_data:
for data in self.map_data[c.MAP_SLIDER]:
if c.VELOCITY in data:
vel = data[c.VELOCITY]
else:
vel = 1
self.slider_group.add(stuff.Slider(data['x'], data['y'], data['num'],
data['direction'], data['range_start'], data['range_end'], vel))
def setup_static_coin(self):
self.static_coin_group = pg.sprite.Group()
if c.MAP_COIN in self.map_data:
for data in self.map_data[c.MAP_COIN]:
self.static_coin_group.add(coin.StaticCoin(data['x'], data['y']))
def setup_brick_and_box(self):
self.coin_group = pg.sprite.Group()
self.powerup_group = pg.sprite.Group()
self.brick_group = pg.sprite.Group()
self.brickpiece_group = pg.sprite.Group()
if c.MAP_BRICK in self.map_data:
for data in self.map_data[c.MAP_BRICK]:
brick.create_brick(self.brick_group, data, self)
self.box_group = pg.sprite.Group()
if c.MAP_BOX in self.map_data:
for data in self.map_data[c.MAP_BOX]:
if data['type'] == c.TYPE_COIN:
self.box_group.add(box.Box(data['x'], data['y'], data['type'], self.coin_group))
else:
self.box_group.add(box.Box(data['x'], data['y'], data['type'], self.powerup_group))
def setup_player(self):
if self.player is None:
self.player = player.Player(self.game_info[c.PLAYER_NAME])
else:
self.player.restart()
self.player.rect.x = self.viewport.x + self.player_x
self.player.rect.bottom = self.player_y
if c.DEBUG:
self.player.rect.x = self.viewport.x + c.DEBUG_START_X
self.player.rect.bottom = c.DEBUG_START_y
self.viewport.x = self.player.rect.x - 110
def setup_enemies(self):
self.enemy_group_list = []
index = 0
for data in self.map_data[c.MAP_ENEMY]:
group = pg.sprite.Group()
for item in data[str(index)]:
group.add(enemy.create_enemy(item, self))
self.enemy_group_list.append(group)
index += 1
def setup_checkpoints(self):
self.checkpoint_group = pg.sprite.Group()
for data in self.map_data[c.MAP_CHECKPOINT]:
if c.ENEMY_GROUPID in data:
enemy_groupid = data[c.ENEMY_GROUPID]
else:
enemy_groupid = 0
if c.MAP_INDEX in data:
map_index = data[c.MAP_INDEX]
else:
map_index = 0
self.checkpoint_group.add(stuff.Checkpoint(data['x'], data['y'], data['width'],
data['height'], data['type'], enemy_groupid, map_index))
def setup_flagpole(self):
self.flagpole_group = pg.sprite.Group()
if c.MAP_FLAGPOLE in self.map_data:
for data in self.map_data[c.MAP_FLAGPOLE]:
if data['type'] == c.FLAGPOLE_TYPE_FLAG:
sprite = stuff.Flag(data['x'], data['y'])
self.flag = sprite
elif data['type'] == c.FLAGPOLE_TYPE_POLE:
sprite = stuff.Pole(data['x'], data['y'])
else:
sprite = stuff.PoleTop(data['x'], data['y'])
self.flagpole_group.add(sprite)
def setup_sprite_groups(self):
self.dying_group = pg.sprite.Group()
self.enemy_group = pg.sprite.Group()
self.shell_group = pg.sprite.Group()
self.ground_step_pipe_group = pg.sprite.Group(self.ground_group,
self.pipe_group, self.step_group, self.slider_group)
self.player_group = pg.sprite.Group(self.player)
def update(self, surface, keys, current_time):
self.game_info[c.CURRENT_TIME] = self.current_time = current_time
self.handle_states(keys)
self.draw(surface)
def handle_states(self, keys):
self.update_all_sprites(keys)
def update_all_sprites(self