相关文件
想学Python的小伙伴可以关注小编的公众号【Python日志】
有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!!
需要源码的小伙伴可以在公众号回复推箱子小游戏
Python源码、问题解答学习交流群:773162165
开发环境
Python版本:3.7.8
相关模块:
requests模块;
tqdm模块;
pyfreeproxy模块;
pyecharts模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
代码实现
模块导入
import pygame
import sys
from os import path
from map import list
from pygame import mixer
import math
关卡
class GameApp:level = 0 # 第一关map = Nonebackground = Nonewall = Noneball = Nonebox = Nonedown_people = Noneleft_people = Noneright_people = Noneup_people = Nonedirection = 'down'levelFont = NoneballNum = 0def __init__(self):self.loadFile()icon = pygame.image.load(self.resolve('img/down.png'))pygame.display.set_icon(icon)mixer.music.load(self.resolve('img/background.wav'))self.levelFont = pygame.font.Font(self.resolve('img/msyh.ttc'), 20)mixer.music.play(-1)self.runGame()def loadFile(self):self.background = pygame.image.load(self.resolve('img/bg.jpg'))self.wall = pygame.image.load(self.resolve('img/wall.png'))self.ball = pygame.image.load(self.resolve('img/ball.png'))self.box = pygame.image.load(self.resolve('img/box.png'))self.down_people = pygame.image.load(self.resolve('img/down.png'))self.left_people = pygame.image.load(self.resolve('img/left.png'))self.right_people = pygame.image.load(self.resolve('img/right.png'))self.up_people = pygame.image.load(self.resolve('img/up.png'))def resolve(self, filename):dirName = path.dirname(__file__)return dirName + '/' + filenamedef renderLevel(self):levelText = self.levelFont.render('第'+str(self.level+1)+'关', True, (0, 0, 0))screen.blit(levelText, (490, 5))def renderPeople(self, i, j):if self.direction == 'down':screen.blit(self.down_people, (j*35-7, i*35-27))if self.direction == 'left':screen.blit(self.left_people, (j*35-7, i*35-27))if self.direction == 'right':screen.blit(self.right_people, (j*35-7, i*35-27))if self.direction == 'up':screen.blit(self.up_people, (j*35-7, i*35-27))
人物移动
def hasGo(self, preItem, nextItem, preIndex, nextIndex, x, y):if preItem == 0 or preItem == 2:peopleDir['x'] = xpeopleDir['y'] = yreturn Trueif preItem == 3: # 推箱子走路if nextItem == 0 or nextItem == 2:boxList[preIndex] = 0boxList[nextIndex] = 3peopleDir['x'] = xpeopleDir['y'] = yself.checkGameover(nextIndex)self.checkWin()return Truereturn Falsedef checkGameover(self, nextIndex):y = math.floor(nextIndex/16)x = nextIndex%16preItem = 0if ballList[nextIndex] != 2:checkList = [wallList[(y-1)*16 + x],wallList[y*16 + x-1],wallList[(y+1)*16 + x],wallList[y*16 + x+1],wallList[(y-1)*16 + x]]for item in checkList:if item == 0:preItem = 0elif item == 1 and preItem == 0:preItem = 1elif item == 1 and preItem == 1: # 如果相邻是两面墙及失败了self.level = 0initData(self.level)break
代码可能有点长,就不全部展示出来啦
公众号:Python日志
需要源码的小伙伴可以在公众号回复推箱子小游戏