python 推箱子小游戏-pygame

按方向键上、下、左、右控制人物移动,按小键盘0键返回上一步,通关界面点击“下一关”或按Enter键可进入下一关,关卡布置从level_file.txt文件读取,玩家可自主添加修改。

# -*- coding: utf-8 -*-
"""
Created on Mon May 31 22:51:46 2021

@author: Administrator
"""

import pygame as pg
from pygame.locals import *
import sys
import time

mmap=list()
with open("F:/其它/level_file.txt",'r') as f:
    for line in f.readlines():
        l=line.strip()
        mmap.append(l)

white=(255,255,255)
black=(0,0,0)
man=(50,250,50)
box=(200,200,0)
dest=(100,100,100)

img_man=pg.image.load('F:/images/pig.jpg')
img_box=pg.image.load('F:/images/box.jpg')
img_wall=pg.image.load('F:/images/wall.jpg')
img_dest=pg.image.load('F:/images/dest.png')

pg.init()
screen=pg.display.set_mode((200,200))
pg.display.set_caption("推箱子")

it=0
x,y=0,0
man_pos,box_pos,barrier_pos,dest_pos,space_pos=[],[],[],[],[]
def get_map(mmap,it):
    man_pos,box_pos,barrier_pos,dest_pos,space_pos=[],[],[],[],[]
    for i in range(len(mmap[it])):
        x=(i%10)*20
        y=(i//10)*20
        if mmap[it][i]=='4':
            man_pos.extend((x,y))
        elif mmap[it][i]=='2':
            box_pos.append([x,y])
        elif mmap[it][i]=='1':
            barrier_pos.append([x,y])
        elif mmap[it][i]=='3':
            dest_pos.append([x,y])
        if mmap[it][i]=='0' or mmap[it][i]=='2' or mmap[it][i]=='3' or mmap[it][i]=='4':
            space_pos.append([x,y])
    return [man_pos,box_pos,barrier_pos,dest_pos,space_pos]

last_move=['0']
def move(man_pos,box_pos,barrier_pos,space_pos,last_move):
    for event in pg.event.get():
        if event.type==QUIT:
            pg.quit()
            sys.exit()
        elif event.type==KEYDOWN:
            #人物上移
            if event.key==K_UP and [man_pos[0],man_pos[1]-20] in space_pos:
                if [man_pos[0],man_pos[1]-20] in box_pos:
                    if [man_pos[0],man_pos[1]-40] in space_pos and [man_pos[0],man_pos[1]-40] not in box_pos:
                        for i in range(len(box_pos)):
                            if box_pos[i]==[man_pos[0],man_pos[1]-20]:
                                box_pos[i]=[man_pos[0],man_pos[1]-40]
                        man_pos[1]-=20
                        last_move.append('UP')
                else:
                    man_pos[1]-=20
                    last_move.append('up')
            #人物下移
            elif event.key==K_DOWN and [man_pos[0],man_pos[1]+20] in space_pos:
                if [man_pos[0],man_pos[1]+20] in box_pos:
                    if [man_pos[0],man_pos[1]+40] in space_pos and [man_pos[0],man_pos[1]+40] not in box_pos:
                        for i in range(len(box_pos)):
                            if box_pos[i]==[man_pos[0],man_pos[1]+20]:
                                box_pos[i]=[man_pos[0],man_pos[1]+40]
                        man_pos[1]+=20
                        last_move.append('DOWN')
                else:
                    man_pos[1]+=20
                    last_move.append('down')
            #人物左移
            elif event.key==K_LEFT and [man_pos[0]-20,man_pos[1]] in space_pos:
                if [man_pos[0]-20,man_pos[1]] in box_pos:
                    if [man_pos[0]-40,man_pos[1]] in space_pos and [man_pos[0]-40,man_pos[1]] not in box_pos:
                        for i in range(len(box_pos)):
                            if box_pos[i]==[man_pos[0]-20,man_pos[1]]:
                                box_pos[i]=[man_pos[0]-40,man_pos[1]]
                        man_pos[0]-=20
                        last_move.append('LEFT')
                else:
                    man_pos[0]-=20
                    last_move.append('left')
            #人物右移
            elif event.key==K_RIGHT and [man_pos[0]+20,man_pos[1]] in space_pos:
                if [man_pos[0]+20,man_pos[1]] in box_pos:
                    if [man_pos[0]+40,man_pos[1]] in space_pos and [man_pos[0]+40,man_pos[1]] not in box_pos:
                        for i in range(len(box_pos)):
                            if box_pos[i]==[man_pos[0]+20,man_pos[1]]:
                                box_pos[i]=[man_pos[0]+40,man_pos[1]]
                        man_pos[0]+=20
                        last_move.append('RIGHT')
                else:
                    man_pos[0]+=20
                    last_move.append('right')
            elif event.key==K_KP0:
                print(last_move[-1])
                if last_move[-1]=='UP':
                    last_move.pop()
                    man_pos[1]+=20
                    for i in range(len(box_pos)):
                        if box_pos[i]==[man_pos[0],man_pos[1]-40]:
                            box_pos[i]=[man_pos[0],man_pos[1]-20]
                elif last_move[-1]=='up':
                    last_move.pop()
                    man_pos[1]+=20
                if last_move[-1]=='DOWN':
                    last_move.pop()
                    man_pos[1]-=20
                    for i in range(len(box_pos)):
                        if box_pos[i]==[man_pos[0],man_pos[1]+40]:
                            box_pos[i]=[man_pos[0],man_pos[1]+20]
                elif last_move[-1]=='down':
                    last_move.pop()
                    man_pos[1]-=20
                if last_move[-1]=='LEFT':
                    last_move.pop()
                    man_pos[0]+=20
                    for i in range(len(box_pos)):
                        if box_pos[i]==[man_pos[0]-40,man_pos[1]]:
                            box_pos[i]=[man_pos[0]-20,man_pos[1]]
                elif last_move[-1]=='left':
                    last_move.pop()
                    man_pos[0]+=20
                if last_move[-1]=='RIGHT':
                    last_move.pop()
                    man_pos[0]-=20
                    for i in range(len(box_pos)):
                        if box_pos[i]==[man_pos[0]+40,man_pos[1]]:
                            box_pos[i]=[man_pos[0]+20,man_pos[1]]
                elif last_move[-1]=='right':
                    last_move.pop()
                    man_pos[0]-=20
                

def game_pass(screen,box_pos,dest_pos):
    not_over=0
    for i in box_pos:
        if i not in dest_pos:
            not_over=1
            break
    if not_over==0:
        return 1

def next_task():
    pg.font.init()
    fontObj=pg.font.SysFont('SimHei', 15)
    textSurfaceObj=fontObj.render('下一关', True, white,black)
    textRectObj=textSurfaceObj.get_rect()
    textRectObj.center=(150,150)
    screen.blit(textSurfaceObj, textRectObj)
    pg.display.update()
    for event in pg.event.get():
        if event.type==QUIT:
            pg.quit()
            sys.exit()
        elif event.type==MOUSEBUTTONDOWN:
            pos=pg.mouse.get_pos()
            if 125<=pos[0]<=175 and 125<=pos[1]<175:
                return 1
        elif event.type==KEYDOWN:
            if event.key==K_RETURN:
                return 1
    return 0

def draw_flip(screen,man_pos,box_pos,barrier_pos,dest_pos):
    for i in dest_pos:
        screen.blit(img_dest,(i[0],i[1]))
    for i in barrier_pos:
        screen.blit(img_wall,(i[0],i[1]))
    for i in box_pos:
        screen.blit(img_box,(i[0],i[1]))
    screen.blit(img_man,(man_pos[0],man_pos[1]))

it=0
def main(screen,it,mmap,man_pos,barrier_pos,box_pos,dest_pos):
    while True:
        #fpsClock = pg.time.Clock()
        last_move=['0']
        x=get_map(mmap, it)
        man_pos,box_pos,barrier_pos,dest_pos,space_pos=x[0],x[1],x[2],x[3],x[4]
        out=0
        while True:
            screen.fill(black)
            draw_flip(screen,man_pos,box_pos,barrier_pos,dest_pos)
            move(man_pos,box_pos,barrier_pos,space_pos,last_move)
            for event in pg.event.get():
                if event.type==QUIT:
                    pg.quit()
                    sys.exit()
            if game_pass(screen,box_pos,dest_pos)==1:
                while True:
                    screen.fill(black)
                    pg.font.init()
                    fontObj=pg.font.SysFont('SimHei', 18)
                    textSurfaceObj=fontObj.render('关卡通过', True, white,black)
                    textRectObj=textSurfaceObj.get_rect()
                    textRectObj.center=(100,100)
                    screen.blit(textSurfaceObj, textRectObj)
                    if next_task()==1:
                        out=1
                        it+=1
                        break
            pg.display.update()
            #fpsClock.tick(5)
            if out==1:
                break
        if it>=len(mmap):
            screen.fill(black)
            pg.font.init()
            fontObj=pg.font.SysFont('SimHei', 18)
            textSurfaceObj=fontObj.render('恭喜全部通关!', True, white,black)
            textRectObj=textSurfaceObj.get_rect()
            textRectObj.center=(100,100)
            screen.blit(textSurfaceObj, textRectObj)
            pg.display.update()
            break
    while True:
        for event in pg.event.get():
            if event.type==QUIT:
                pg.quit()
                sys.exit()

if __name__=='__main__':
    main(screen,it,mmap,man_pos,barrier_pos,box_pos,dest_pos)

level_file.txt:
在这里插入图片描述
墙纸图片:
在这里插入图片描述箱子图片:
在这里插入图片描述人物图片:
在这里插入图片描述目的地图片:
在这里插入图片描述
游戏界面:
在这里插入图片描述

在这里插入图片描述

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python推箱子游戏的实现需要使用pygame这个库,pygame是一个Python语言的多媒体处理库,可以用来制作游戏等多媒体应用程序。在Python推箱子游戏中,我们需要绘制地图、箱子、人物等图形,同时还需要响应键盘事件,对人物进行移动,并检测是否完成目标等等。 具体实现过程可以参考以下步骤: 1. 初始化pygame库,设置窗口大小和标题等参数。 2. 加载地图、箱子、人物等图片素材。 3. 绘制地图、箱子、人物等元素。 4. 监听键盘事件,对人物进行移动,并检测是否完成目标。 5. 更新游戏界面。 下面是一个简单的Python推箱子游戏的代码示例,仅供参考: ```python import pygame # 初始化pygame pygame.init() # 设置窗口大小和标题 screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("推箱子游戏") # 加载图片素材 wall_img = pygame.image.load("wall.png") box_img = pygame.image.load("box.png") target_img = pygame.image.load("target.png") player_img = pygame.image.load("player.png") # 地图数据 map_data = [ "#####", "# #", "#o #", "# #", "#####" ] # 箱子和目标的坐标 boxes = [(2, 1)] targets = [(3, 1)] # 人物的初始坐标 player_pos = (2, 2) # 绘制地图、箱子、目标和人物 def draw_map(): for i in range(len(map_data)): for j in range(len(map_data[i])): if map_data[i][j] == "#": screen.blit(wall_img, (j * 50, i * 50)) elif (j, i) in boxes: screen.blit(box_img, (j * 50, i * 50)) elif (j, i) in targets: screen.blit(target_img, (j * 50, i * 50)) elif (j, i) == player_pos: screen.blit(player_img, (j * 50, i * 50)) # 判断是否完成目标 def check_win(): for box in boxes: if box not in targets: return False return True # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: # 处理键盘事件,对人物进行移动,并检测是否完成目标 if event.key == pygame.K_LEFT: if (player_pos - 1, player_pos) not in boxes and map_data[player_pos][player_pos - 1] != "#": player_pos = (player_pos - 1, player_pos) elif (player_pos - 1, player_pos) in boxes and (player_pos - 2, player_pos) not in boxes and map_data[player_pos][player_pos - 2] != "#": index = boxes.index((player_pos - 1, player_pos)) boxes[index] = (boxes[index] - 1, boxes[index]) player_pos = (player_pos - 1, player_pos) elif event.key == pygame.K_RIGHT: if (player_pos + 1, player_pos) not in boxes and map_data[player_pos][player_pos + 1] != "#": player_pos = (player_pos + 1, player_pos) elif (player_pos + 1, player_pos) in boxes and (player_pos + 2, player_pos) not in boxes and map_data[player_pos][player_pos + 2] != "#": index = boxes.index((player_pos + 1, player_pos)) boxes[index] = (boxes[index] + 1, boxes[index]) player_pos = (player_pos + 1, player_pos) elif event.key == pygame.K_UP: if (player_pos, player_pos - 1) not in boxes and map_data[player_pos - 1][player_pos] != "#": player_pos = (player_pos, player_pos - 1) elif (player_pos, player_pos - 1) in boxes and (player_pos[0], player_pos - 2) not in boxes and map_data[player_pos[1] - 2][player_pos] != "#": index = boxes.index((player_pos, player_pos - 1)) boxes[index] = (boxes[index], boxes[index] - 1) player_pos = (player_pos, player_pos - 1) elif event.key == pygame.K_DOWN: if (player_pos, player_pos + 1) not in boxes and map_data[player_pos + 1][player_pos] != "#": player_pos = (player_pos, player_pos + 1) elif (player_pos, player_pos + 1) in boxes and (player_pos, player_pos + 2) not in boxes and map_data[player_pos + 2][player_pos] != "#": index = boxes.index((player_pos, player_pos + 1)) boxes[index] = (boxes[index], boxes[index] + 1) player_pos = (player_pos, player_pos + 1) # 绘制游戏界面 screen.fill((255,255,255)) draw_map() pygame.display.update() # 判断是否完成目标,如果完成则弹出提示框并结束游戏 if check_win(): pygame.time.delay(500) pygame.quit() exit() ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值