def gen_walls(): r"""生成墙 :return: 墙数组 """ wall_upper = [Wall(x, 0) for x in range(0, WIDTH, PIXEL_SIZE)] wall_down = [Wall(x, HEIGHT - PIXEL_SIZE) for x in range(0, WIDTH, PIXEL_SIZE)] wall_left = [Wall(0, y) for y in range(PIXEL_SIZE, HEIGHT - PIXEL_SIZE, PIXEL_SIZE)] wall_right = [Wall(WIDTH - PIXEL_SIZE, y) for y in range(PIXEL_SIZE, HEIGHT - PIXEL_SIZE, PIXEL_SIZE)] return wall_upper + wall_down + wall_left + wall_right
def load_grid_from_file(self, filename): self.current_level = filename level = open("levels/" + filename) self.size = int(level.readline().strip()) self.set_empty_grid() for entry in level: tokens = entry.strip().split(" ") if tokens[0] == '': continue x = int(tokens[0]) y = int(tokens[1]) holder: Holder = self.get_holder(x, y) entity = None # <x> <y> arrow <up|down|left|right> <index> if tokens[2] == "arrow": entity = Arrow(Compass.from_str(tokens[3]), int(tokens[4])) self.actors.append(entity) if tokens[2] == "wall": entity = Wall() if tokens[2] == "rock": entity = Rock() if tokens[2] == "core": entity = Core() if entity is not None: entity.put_into(holder)
def level_tree(block_list, all_sprite_list, walls_list, width): for i in range(2): a = 0 for j in range(6): if j == 3: a += 100 block = Blocks(115 + width / 8 * j + a, 30 + 70 * i, 1) all_sprite_list.add(block) block_list.add(block) for i in range(2, 4): a = 0 for j in range(6): if j == 3: a += 100 block = Blocks(115 + width / 8 * j + a, 30 + 70 * i, 2) all_sprite_list.add(block) block_list.add(block) for i in range(4, 6): a = 0 for j in range(6): if j == 3: a += 100 block = Blocks(115 + width / 8 * j + a, 30 + 70 * i, 3) all_sprite_list.add(block) block_list.add(block) wall = Wall(625, 0, 1) all_sprite_list.add(wall) walls_list.add(wall)
def level_five(block_list, all_sprite_list, walls_list): while len(walls_list) > 0: walls_list.pop() for i in 300, 477, 654, 831: for j in 100, 170: block = Blocks(i, j, 1) all_sprite_list.add(block) block_list.add(block) for i in 50, 1080: for j in 100, 170, 240, 310, 380: block = Blocks(i, j, 2) all_sprite_list.add(block) block_list.add(block) for i in 300, 477, 654, 831: for j in 240, 310, 380: block = Blocks(i, j, 3) all_sprite_list.add(block) block_list.add(block) h_wall2 = HorWall(300, 450, 2) all_sprite_list.add(h_wall2) walls_list.add(h_wall2) for i in 250, 1000: wall2 = Wall(i, 240, 2) all_sprite_list.add(wall2) walls_list.add(wall2)
def init_board(self, reset=False): '''# initialize and setup the frame of the board''' if reset: self.frame_counter = 0 # scaling the wall piece w = Wall(self.width / 20, self.height / 20) w_height, w_width = w.get_size() # creating the rows full_row, full_row[:, :] = \ np.chararray((w_height, self.width)), config._wall emp_row, emp_row[:, :] = \ np.chararray((w_height, self.width)), config._empty emp_row[:, :w_width] = emp_row[:, -w_width:] = w.structure alt_row, alt_row[:, :] = \ np.chararray((w_height, self.width)), config._empty for c in range(1, int(self.width / w_width) + 1): if c % 2: alt_row[:, (c - 1) * w_width:(c * w_width)] = w.structure # assigning top and bottom self._b[:w_height, :] = self._b[-w_height:, :] = full_row # assigning other rows for r in range(2, int(self.height / w_height)): # alt row if r % 2: cur_row = alt_row else: cur_row = emp_row self._b[(r - 1) * w_height:(r * w_height), :] = cur_row # create the inital points for spawning objects # subtracting two edge blocks for each top bottom # and dividing by two for range of motion fp = (5, 3) # each object is 4 px wide total_block_x = int((self.width / config.x_fac - 2) / 2 + 1) # each object is 2px tall total_block_y = int((self.height / config.y_fac - 2) / 2 + 1) for r in range(total_block_x): for c in range(total_block_y): self.init_points.append((fp[0] + r * (2 * config.x_fac), fp[-1] + c * (2 * config.y_fac))) self.init_points = list(set(self.init_points))
def obj_maker(self): """Makes all the Objects in the Game""" for i in self.coords: if i[2] == "brick": self.walls.append(Wall(i[0], i[1])) elif i[2] == "unknown": self.unknowns.append(Unknown(i[0], i[1])) for i in self.pipe_coords: self.pipes.append(Pipe(i[0], i[1])) for i in self.enemy_coords: self.enemies.append(Enemy(i[0], i[1], i[2])) self.flag.append(Flag(config.FLAG_X, config.FLAG_Y))
def generateMap(self): image = self.loadImage("sprites.png") i = 0 j = 0 for line in self.map: for w in line: if (w == 'W'): wall = Wall(image, j, i) self.allsprites.add(wall) self.walls.append(wall) if (w == 'E'): self.end = End(image, j, i) self.allsprites.add(self.end) self.walls.append(self.end) j += 32 i += 32 j = 0
def __init__(self, width: int, height: int, gameobjects: dict): """ Create the board """ self.gameobjects = gameobjects self.MAX = 15 # Cutoff for when we want to stop dividing sections # make the range + 1 so we can avoid out of range problems self.width = width + 1 self.height = height + 1 self.leaves = [] self.dungeon = [] self.rooms = [] for h in range(self.height): row = [] for w in range(self.width): row.append(Wall(self.width, self.height, w, h)) self.dungeon.append(row)
def update_frame(self): '''# main function to check updates at every frame''' # clean up debris of the previous bombs for bomb in self._storage[config.types[config._bomb]]: if not bomb.active: bomb.structure[:, :] = config._empty x, y = bomb.get_coords() height, width = bomb.get_size() # removing the bomb as well bomb.blast_radius.insert(0, bomb.get_coords()) for x_i, y_i in bomb.blast_radius: self._b[y_i - 1:y_i - 1 + height, x_i - 1:x_i - 1 + width] = config._empty self._storage[config.types[config._bomb]].remove(bomb) # check if any bomb needs to detonate for bomb in self._storage[config.types[config._bomb]]: if (not bomb.timer) and bomb.active: bomb.structure[:, :] = config._expl x, y = bomb.get_coords() height, width = bomb.get_size() for x_i, y_i in bomb.blast_radius: # if a wall blocks the way remove the blast coords try: if np.any(self._b[y_i - 1:y_i - 1 + height, x_i - 1:x_i - 1 + width] == Wall(0, 0).structure): raise IndexError except BaseException: if x - x_i == 1 * x_fac: bomb.blast_radius.remove((x + 2 * x_fac, y)) elif x - x_i == -1 * x_fac: bomb.blast_radius.remove((x - 2 * x_fac, y)) elif y - y_i == 1 * y_fac: bomb.blast_radius.remove((x, y - 2 * y_fac)) elif y - y_i == -1 * y_fac: bomb.blast_radius.remove((x, y + 2 * y_fac)) bomb.blast_radius.remove((x_i, y_i)) continue # kill all enemies in trajectory for en in self._storage[config.types[config._enemy]]: if en.get_coords() == (x_i, y_i) and en.is_killable: self.clear_storage(en) bomb.owner.score += config.scores[config._enemy] # kill all players in trajectory for pl in self.players: if pl.get_coords() == (x_i, y_i) and pl.is_killable: pl.lives = 0 # destory all bricks in trajectory for brick in self._storage[config.types[config._bricks]]: if brick.get_coords() == (x_i, y_i): self.clear_storage(brick) bomb.owner.score += config.scores[config._bricks] # detonate other bombs by chain for bmb in self._storage[config.types[config._bomb]]: if bmb.active and bmb != bomb and bmb.get_coords( ) in bomb.blast_radius: bmb.timer = 0 # rendering the "explosion" for x_i, y_i in bomb.blast_radius: self._b[y_i - 1:y_i - 1 + height, x_i - 1:x_i - 1 + width] = config._expl bomb.active = False # countdown everybomb for bomb in self._storage[config.types[config._bomb]]: bomb.countdown() self.refresh_obj(bomb) # move the enemies randomly for _ in self._storage[config.types[config._enemy]]: _dir = random.choice(config.DIR) self.process_input(_, _dir) self.frame_counter += 1
def __init__(self, window_class, window, fpsclock): self.window_class = window_class self.window = window self.clock = fpsclock self.groups = Groups() self.fps = False self.fpsmeter = None self.player = Player(window_class, (450, 450), 10, 10, (0, 0), self.groups) self.deathscreen_ticker = 255 self.groups.addtogroup(self.player, self.groups.sprites) with open('levels/lvl_1.json', 'r') as data_file: data = json.loads(data_file.read(), parse_float=decimal.Decimal) for object in data["scene"]["objects"]: if data["scene"]["objects"][object]["type"] == 'wall': x = int(data["scene"]["objects"][object]["x"]) y = int(data["scene"]["objects"][object]["y"]) width = int(data["scene"]["objects"][object]["width"]) height = int(data["scene"]["objects"][object]["height"]) color = (int(data["scene"]["objects"][object]["color"][0]), int(data["scene"]["objects"][object]["color"][1]), int(data["scene"]["objects"][object]["color"][2])) damp = float(data["scene"]["objects"][object]["dampening"]) fric = float(data["scene"]["objects"][object]["friction"]) soft = bool(data["scene"]["objects"][object]["soft"]) emitter = bool(data["scene"]["objects"][object]["emitter"]) wall = Wall(x, y, width, height, color, damp, fric, soft) self.groups.addtogroup(wall, self.groups.walls) if emitter: self.groups.addtogroup(wall, self.groups.emitters) elif data["scene"]["objects"][object]["type"] == "text": text = data["scene"]["objects"][object]["text"] x = int(data["scene"]["objects"][object]["x"]) y = int(data["scene"]["objects"][object]["y"]) font = data["scene"]["objects"][object]["font"] size = int(data["scene"]["objects"][object]["size"]) antialias = bool(data["scene"]["objects"][object]["antialias"]) color = (int(data["scene"]["objects"][object]["color"][0]), int(data["scene"]["objects"][object]["color"][1]), int(data["scene"]["objects"][object]["color"][2])) textobj = Text(self.window, x, y, size, font, text, antialias, color) self.groups.addtogroup(textobj, self.groups.text) for property in data["scene"]["properties"]: if property == "music": music = pygame.mixer.Sound( data["scene"]["properties"]["music"]) music.play(-1, fade_ms=1000) elif property == "fps" and bool( data["scene"]["properties"]["fps"]): self.fps = True pos = (int(data["scene"]["properties"]["fps_pos"][0]), int(data["scene"]["properties"]["fps_pos"][1])) fpstext = str(self.clock.get_fps()) + " FPS" self.fpsmeter = Text(self.window, pos[0], pos[1], 20, None, fpstext, True, (255, 255, 255)) self.groups.addtogroup(self.fpsmeter, self.groups.text)
pygame.init() # Create an 800x600 sized screen screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) # Set the title of the window pygame.display.set_caption('Test') # List to hold all the sprites all_sprite_list = pygame.sprite.Group() # Make the walls. (x_pos, y_pos, width, height) wall_list = pygame.sprite.Group() # Left wall = Wall(0, 0, 10, 600) wall_list.add(wall) all_sprite_list.add(wall) # Right wall = Wall(790, 0, 10, 600) wall_list.add(wall) all_sprite_list.add(wall) # Top wall = Wall(10, 0, 790, 10) wall_list.add(wall) all_sprite_list.add(wall) # Botton wall = Wall(10, 590, 790, 10)