def __init__(self, engine): self.engine = engine self.render_text_block = engine.render_text_block self.width, self.height = engine.get_screensize() self.game_objects = [] self.player = Player(self) self.npc = Npc(self) self.map = MapHandler(self, map_data.map_data) self.map.load_map("big_world")
class Game: def __init__(self, engine): self.engine = engine self.render_text_block = engine.render_text_block self.width, self.height = engine.get_screensize() self.game_objects = [] self.player = Player(self) self.npc = Npc(self) self.map = MapHandler(self, map_data.map_data) self.map.load_map("big_world") #self.d = DialogBox(self, "This is just a test of the dialog box system!\nThis would be some cool stuff here if there was something cool to say\nHere's another line that should be pretty cool later on") def sort_gameobjects(self): self.game_objects = sorted(self.game_objects, key=lambda game_object: game_object.z, reverse=True) def add_game_object(self, obj): """ Adds a game object to the list of objects in the game These game objects need to have update and draw methods After inserting the object it sorts the list by z for drawing """ self.game_objects.append(obj) self.sort_gameobjects() def update(self, dt): for game_object in self.game_objects: game_object.update(dt) def draw(self, canvas): for game_object in self.game_objects: game_object.draw(canvas) #self.d.draw(canvas) def handle_event(self, event): key = event.key #if the key is a key that the player requests, send key to player if key in self.player.get_keys(): self.player.handle_key(key, self.check_player_position) #else if the key is the letter m, open the menu elif key == pygame.K_m: self.engine.change_state("Menu") def is_valid_position(self, x, y, from_obj): if from_obj == "npc": map_test = self.map.is_position_ok(x, y) return map_test def c2_player_position(self, new_rect): #check if # -- player is within the screen # -- player is in scroll area # -- -- don't let player move, move camera coords instead # -- temp, if hitting npc # -- check map can_go = new_rect if not self.check_if_onscreen(new_rect): can_go = False elif new_rect.colliderect(self.npc.get_rect()): can_go = False elif not self.map.is_position_ok(new_rect.x, new_rect.y): can_go = False if can_go: self.player.x = can_go.x self.player.y = can_go.y def check_player_position(self, new_rect): can_go = new_rect if not self.check_if_onscreen(new_rect): can_go = False elif new_rect.colliderect(self.npc.get_rect()): can_go = False elif not self.map.is_position_ok(new_rect.x, new_rect.y): can_go = False if can_go: on_pad = self.check_if_on_screenpad(can_go) if on_pad: self.map.move_camera(on_pad) self.player.x = can_go.x self.player.y = can_go.y def check_if_on_screenpad(self, rect): screenpad = self.map.get_screenpad() if rect.x < screenpad: return "left" elif rect.y < screenpad: return "up" elif rect.right > self.width - screenpad: return "right" elif rect.bottom > self.height - screenpad: return "down" return False def check_if_onscreen(self, rect): """ Checks if the rect is on the screen or not """ if rect.x < 0 or rect.y < 0: return False elif rect.right > self.width or rect.bottom > self.height: return False return True def get_player_info(self): info = self.player.attributes info["name"] = self.player.name return info def get_screensize(self): return self.engine.get_screensize()