【Python游戏】Python实现一个推箱子小游戏 | 附带源码

相关文件

想学Python的小伙伴可以关注小编的公众号【Python日志】
有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!!
需要源码的小伙伴可以在公众号回复推箱子小游戏
Python源码、问题解答学习交流群:773162165

开发环境

Python版本:3.7.8
相关模块:
requests模块;
tqdm模块;
pyfreeproxy模块;
pyecharts模块;
以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

效果展示

【Python游戏】Python实现一个推箱子小游戏 | 附带源码_第1张图片
【Python游戏】Python实现一个推箱子小游戏 | 附带源码_第2张图片

代码实现

模块导入

import pygame
import sys
from os import path
from map import list
from pygame import mixer
import math

关卡

class GameApp:
  level = 0 # 第一关
  map = None
  background = None
  wall = None
  ball = None
  box = None
  down_people = None
  left_people = None
  right_people = None
  up_people = None
  direction = 'down'
  levelFont = None
  ballNum = 0
  def __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 + '/' + filename
  
  def 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'] = x
      peopleDir['y'] = y
      return True
    if preItem == 3: # 推箱子走路
      if nextItem == 0 or nextItem == 2:
        boxList[preIndex] = 0
        boxList[nextIndex] = 3
        peopleDir['x'] = x
        peopleDir['y'] = y
        self.checkGameover(nextIndex)
        self.checkWin()
        return True
    return False
  def checkGameover(self, nextIndex):
    y = math.floor(nextIndex/16)
    x = nextIndex%16
    preItem = 0
    if 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 = 0
        elif item == 1 and preItem == 0:
          preItem = 1
        elif item == 1 and preItem == 1: # 如果相邻是两面墙及失败了
          self.level = 0
          initData(self.level)
          break

代码可能有点长,就不全部展示出来啦

公众号:Python日志
需要源码的小伙伴可以在公众号回复推箱子小游戏

你可能感兴趣的:(Python游戏,python,游戏,pygame)