Python怎么实现推箱子游戏
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,小编给大家分享一下Python怎么实现推箱子游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1.游戏规则推箱子游戏是一款
千家信息网最后更新 2025年02月02日Python怎么实现推箱子游戏2.材料准备
小编给大家分享一下Python怎么实现推箱子游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
1.游戏规则
推箱子游戏是一款可玩性极高的策略解谜手游,游戏中玩家将扮演一名可爱Q萌的角色,
我们需通过将场景内的箱子,推送到合适的位置上进行摆放,才可以轻松获得游戏胜利。
整个过程虽然极其简单,但极需玩家动脑思考,充分的利用有效地空间,合理得将箱子推送到指定位置,从而获得游戏胜利。
不仅如此,游戏整体画风十分简洁清爽,采用了简单和程式化的图形设计,给予了玩家前所未有的体验感哦。
2.材料准备
玩家、箱子、背景等图片素材:
3.环境安装
Python3.6、pycharm、pygame游戏模块不能少。
pip install pygame
**导入游戏的素材,**增加游戏元素
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))
4.游戏开始、结束界面设置
def startInterface(screen, cfg): screen.fill(cfg.BACKGROUNDCOLOR) clock = pygame.time.Clock() while True: button_1 = Button(screen, (95, 150), '开始游戏', cfg) button_2 = Button(screen, (95, 305), '退出游戏', 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 = '机智如你~恭喜通关!' 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()
如下:
******设置游戏的界面,**导入关卡地图。
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: lines = f.readlines() # 游戏地图 self.game_map = gameMap(max([len(line) for line in lines]) - 1, len(lines)) # 游戏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)
因为游戏界面面积>游戏窗口界面, 所以需要根据人物位置滚动。
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
设置玩家的精灵类,可上下左右移动等。
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, pygame.RLEACCEL) self.rect = self.image.get_rect() self.col = col self.row = row '''移动''' def move(self, direction, is_test=False): # 测试模式代表模拟移动 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 += 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) '''游戏元素精灵类'''class elementSprite(pygame.sprite.Sprite): def __init__(self, sprite_name, col, row, cfg): pygame.sprite.Sprite.__init__(self) # 导入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, pygame.RLEACCEL) self.rect = self.image.get_rect() # 元素精灵类型 self.sprite_type = sprite_name.split('.')[0] # 元素精灵的位置 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, self.rect) '''移动游戏元素''' 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 += 1
游戏关卡循环,当某个关卡过不去的时候,想重新来按住R键即可返回本关卡。
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.Font(font_path, 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)
如下:
判断****该关卡中所有的箱子是否都在指定位置, 在的话就是通关了。
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
效果图第二关卡如下:
以上是"Python怎么实现推箱子游戏"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
元素
界面
位置
关卡
玩家
移动
箱子
精灵
推箱子
游戏界面
篇文章
人物
代表
内容
地图
模式
素材
测试
胜利
合适
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器内存4g但是识别不到4g
四川税务网络技术体系
barra多因子数据库
代理服务器 灰色
网络安全高考专业
怎么找网站的数据库
盐城做软件开发的公司
网络安全认证证书图片
家庭用记账软件开发有哪些
山东邮件营销外贸软件开发
浅然网络技术论坛
普恒网络技术有限公司地址
什么是28软件开发
2018网络安全法律法规
xcel如何做数据库
纪录小康工程河南省数据库
党员的网络安全总结
微软把服务器放海里怎么充电
网络安全高级工程师论文
中控考勤机更改数据库
信贷系统软件开发商
网络安全宣传周活动总策划
天津超频服务器服务至上
文献数据库检索有什么途径
c c 软件开发高级工程师证书
网络安全拓展工作怎么开展
魔兽世界9.1部落服务器
新乡网络安全工程师就业前景
网络安全电子报刊作品ppt
软件开发等流程