Introduction:

When I was a child to dominate all flip phone games, in addition to snake, that is pushing boxes.

The simple operation of controlling the little man to put all boxes in the designated position accompanied me through countless times without cartoons.

Does this look familiar?

Xiaobian is also from playing “pushing boxes” that era. At that time, I took a learning machine, under the eyes of the teacher, through another pass. Now think of, still feel very happy.

I haven’t updated the game for you all day, so it’s time to start preparing.

So today for everyone to prepare the classic childhood game – push boxes, have a fancy to get on the car soon ~

The body of the

Rules of the game:

Chest pushing game is a highly playable strategy puzzle solving mobile game, in the game players will play a cute Q cute role,

You can easily win the game by pushing boxes in the scene and placing them in the right place.

The whole process is extremely simple, but requires the player to think, use the space effectively, and push the box to the right place to win the game.

Not only that, but the overall style of the game is very simple and refreshing, using a simple and stylized graphic design, giving players an unprecedented sense of experience.

First of all,

Player, box, background and other picture materials:

​​

Environment installation.

Python3.6, PyCharm, and PyGame game modules are not limited.

pip install pygame
Copy the code

** Import game material, ** add game elements.

 def addElement(self, elem_type, col, row):
        if elem_type == 'wall':
            self.walls.append(elementSprite('wall.png', col, row, cfg))
        elif elem_type == 'box':
            self.boxes.append(elementSprite('box.png', col, row, cfg))
        elif elem_type == 'target':
            self.targets.append(elementSprite('target.png', col, row, cfg))
Copy the code

Game start and end interface Settings.

def startInterface(screen, cfg): screen.fill(cfg.BACKGROUNDCOLOR) clock = pygame.time.Clock() while True: Button_1 = Button(screen, (95, 150), 'start game ', CFG) button_2 = Button(screen, (95, 305),' exit game ', cfg) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if button_1.collidepoint(pygame.mouse.get_pos()): return elif button_2.collidepoint(pygame.mouse.get_pos()): pygame.quit() sys.exit(0) clock.tick(60) pygame.display.update() def endInterface(screen, cfg): screen.fill(cfg.BACKGROUNDCOLOR) clock = pygame.time.Clock() font_path = os.path.join(cfg.FONTDIR, 'simkai. TTF ') text = 'Congratulations! ' font = pygame.font.Font(font_path, 30) text_render = font.render(text, 1, (255, 255, 255)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.blit(text_render, (120, 200)) clock.tick(60) pygame.display.update()Copy the code

As follows:

​​

****** set up the interface of the game, ** import the level map.

class gameInterface(): def __init__(self, screen): self.screen = screen self.levels_path = cfg.LEVELDIR self.initGame() def loadLevel(self, game_level): with open(os.path.join(self.levels_path, game_level), 'r') as f: Self.game_map = gameMap(Max ([len(line) for line in lines]) -1, Len (lines) # game surface height = cfg.blocksize * self.game_map.num_rows width = cfg.blocksize * self.game_map.num_cols self.game_surface = pygame.Surface((width, height)) self.game_surface.fill(cfg.BACKGROUNDCOLOR) self.game_surface_blank = self.game_surface.copy() for row, elems in enumerate(lines): for col, elem in enumerate(elems): if elem == 'p': self.player = pusherSprite(col, row, cfg) elif elem == '*': self.game_map.addElement('wall', col, row) elif elem == '#': self.game_map.addElement('box', col, row) elif elem == 'o': self.game_map.addElement('target', col, row)Copy the code

Because the game interface area > the game window interface, so need to scroll according to the character position.

 def scroll(self):
        x, y = self.player.rect.center
        width = self.game_surface.get_rect().w
        height = self.game_surface.get_rect().h
        if (x + cfg.SCREENSIZE[0] // 2) > cfg.SCREENSIZE[0]:
            if -1 * self.scroll_x + cfg.SCREENSIZE[0] < width:
                self.scroll_x -= 2
        elif (x + cfg.SCREENSIZE[0] // 2) > 0:
            if self.scroll_x < 0:
                self.scroll_x += 2
        if (y + cfg.SCREENSIZE[1] // 2) > cfg.SCREENSIZE[1]:
            if -1 * self.scroll_y + cfg.SCREENSIZE[1] < height:
                self.scroll_y -= 2
        elif (y + 250) > 0:
            if self.scroll_y < 0:
                self.scroll_y += 2
Copy the code

Set up the player’s Sprite class, can move up and down around and so on.

class pusherSprite(pygame.sprite.Sprite): def __init__(self, col, row, cfg): pygame.sprite.Sprite.__init__(self) self.image_path = os.path.join(cfg.IMAGESDIR, 'player.png') self.image = pygame.image.load(self.image_path).convert() color = self.image.get_at((0, 0)) self.image.set_colorkey(color, Self.rleaccel) self.rect = self.image.get_rect() self.col = col self.row = row "" def move(self, direction) Is_test: if is_test: if direction == 'up': return self.col, self.row -1 elif direction == 'down': return self.col, self.row + 1 elif direction == 'left': return self.col - 1, self.row elif direction == 'right': return self.col + 1, self.row else: if direction == 'up': self.row -= 1 elif direction == 'down': self.row += 1 elif direction == 'left': self.col -= 1 elif direction == 'right': Def draw(self, screen): self.col += 1 "" def draw(self, screen): self.rect.x = self.rect.width * self.col self.rect.y = self.rect.height * self.row screen.blit(self.image, Self. Rect: def __init__(self, sprite_name, col, row, CFG): Pygame. Sprite. Sprite. __init__ (self) # import box. PNG/target. PNG/wall • PNG self. Image_path = OS. Path. Join (CFG) IMAGESDIR, sprite_name) self.image = pygame.image.load(self.image_path).convert() color = self.image.get_at((0, 0)) self.image.set_colorkey(color, RLEACCEL) self.rect = self.image.get_rect() # sprite_type = sprite_name.split('.')[0] # sprite_name Def draw(self, screen): self.col = col self.row = row "def draw(self, screen): self.rect.x = self.rect.width * self.col self.rect.y = self.rect.height * self.row screen.blit(self.image, Def move(self, direction, is_test=False): if self.sprite_type == 'box': If is_test: if direction == 'up': return self.col, self.row -1 elif direction == 'down': return self.col, self.row + 1 elif direction == 'left': return self.col - 1, self.row elif direction == 'right': return self.col + 1, self.row else: if direction == 'up': self.row -= 1 elif direction == 'down': self.row += 1 elif direction == 'left': self.col -= 1 elif direction == 'right': self.col += 1Copy the code

Game level cycle, when a level can not pass, want to come back to press the R key can return to the level.

def runGame(screen, game_level): clock = pygame.time.Clock() game_interface = gameInterface(screen) game_interface.loadLevel(game_level) font_path = Os.path. join(cfg.FONTDIR, 'simkai. TTF ') text = 'R' font = pygame.font. 15) text_render = font.render(text, 1, (255, 255, 255)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: next_pos = game_interface.player.move('left', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('left') else: box = game_interface.game_map.getBox(*next_pos) if box: next_pos = box.move('left', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('left') box.move('left') break if event.key == pygame.K_RIGHT: next_pos = game_interface.player.move('right', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('right') else: box = game_interface.game_map.getBox(*next_pos) if box: next_pos = box.move('right', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('right') box.move('right') break if event.key == pygame.K_DOWN: next_pos = game_interface.player.move('down', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('down') else: box = game_interface.game_map.getBox(*next_pos) if box: next_pos = box.move('down', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('down') box.move('down') break if event.key == pygame.K_UP: next_pos = game_interface.player.move('up', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('up') else: box = game_interface.game_map.getBox(*next_pos) if box: next_pos = box.move('up', is_test=True) if game_interface.game_map.isValidPos(*next_pos): game_interface.player.move('up') box.move('up') break if event.key == pygame.K_r: game_interface.initGame() game_interface.loadLevel(game_level) game_interface.draw(game_interface.player, game_interface.game_map) if game_interface.game_map.levelCompleted(): return screen.blit(text_render, (5, 5)) pygame.display.flip() clock.tick(100)Copy the code

As follows:

​​

Check **** if all boxes in the level are in the specified location, if so, the level is completed.

 def levelCompleted(self):
        for box in self.boxes:
            is_match = False
            for target in self.targets:
                if box.col == target.col and box.row == target.row:
                    is_match = True
                    break
            if not is_match:
                return False
        return True
Copy the code

The second level is as follows:

​​

conclusion

The Jungle Book

** Difficulty number of stars: **5 stars

Xiaobian is now stumped in this 2 close! Do you remember how to play it? How many levels can you pass? Emmm,

Very brain-burning, interested partners can try, in short, this game difficult ~

Production is not easy, remember one key three even oh!!

If you need this article’s complete code + images, Python novice installation package, free activation code, and more Python materials

Source base [private letter xiaobian 06] can be free oh!!