Python推箱子V2.00

先拿资源:

百度网盘

链接:https://pan.baidu.com/s/1sH6PSyvtipphtL9-ibkQvg 
提取码:xki8 

再看效果:

 

最后看源码:

import pygame
import sys
import time
"""
空地:0
墙壁:1
障碍:2
目的地:3
箱子:4
玩家:5
玩家到达目的地:8
箱子到达目的地: 7
"""
"""
列表[i][j]: i-行(竖),j-列(横)
贴图[i][j]: i-x轴(横) , j-y轴(竖)
"""
# 游戏状态检测
result_flag = 0
# 定义游戏地图
init_game_list = []
# 箱子完成个数
xz = 0
# 页面检测
ym = 1  # 1.游戏页面 -1.说明页面
# 关卡
level = 0


class Game(object):
    def __init_game_map_list__(self):
        global init_game_list, result_flag
        list1 = [1, 1, 1, 1, 1, 1, 1, 1]
        list2 = [1, 0, 0, 2, 0, 0, 0, 1]
        list3 = [1, 0, 0, 2, 0, 7, 0, 1]
        list4 = [1, 0, 0, 2, 0, 3, 2, 1]
        list5 = [1, 2, 0, 4, 0, 0, 0, 1]
        list6 = [1, 0, 4, 0, 0, 3, 0, 1]
        list7 = [1, 5, 0, 2, 2, 0, 0, 1]
        list8 = [1, 1, 1, 1, 1, 1, 1, 1]
        init_game_list_level_0 = [list1, list2, list3, list4, list5, list6, list7, list8]
        list09 = [1, 1, 1, 1, 1, 1, 1, 1]
        list10 = [1, 0, 0, 0, 2, 3, 0, 1]
        list11 = [1, 3, 0, 0, 4, 0, 0, 1]
        list12 = [1, 2, 4, 2, 2, 0, 0, 1]
        list13 = [1, 0, 0, 2, 2, 4, 2, 1]
        list14 = [1, 0, 0, 5, 0, 0, 0, 1]
        list15 = [1, 0, 3, 2, 0, 0, 0, 1]
        list16 = [1, 1, 1, 1, 1, 1, 1, 1]
        init_game_list_level_1 = [list09, list10, list11, list12, list13, list14, list15, list16]
        list17 = [1, 1, 1, 1, 1, 1, 1, 1]
        list18 = [1, 0, 0, 0, 2, 0, 0, 1]
        list19 = [1, 0, 0, 3, 4, 0, 0, 1]
        list20 = [1, 2, 4, 2, 2, 0, 0, 1]
        list21 = [1, 0, 3, 2, 2, 4, 2, 1]
        list22 = [1, 0, 0, 5, 3, 0, 0, 1]
        list23 = [1, 0, 0, 2, 0, 0, 0, 1]
        list24 = [1, 1, 1, 1, 1, 1, 1, 1]
        init_game_list_level_2 = [list17, list18, list19, list20, list21, list22, list23, list24]
        list25 = [1, 1, 1, 1, 1, 1, 1, 1]
        list26 = [1, 0, 0, 5, 0, 0, 0, 1]
        list27 = [1, 0, 2, 2, 2, 3, 2, 1]
        list28 = [1, 0, 0, 0, 4, 3, 2, 1]
        list29 = [1, 2, 0, 4, 2, 3, 0, 1]
        list30 = [1, 2, 4, 0, 2, 0, 0, 1]
        list31 = [1, 0, 0, 0, 0, 0, 0, 1]
        list32 = [1, 1, 1, 1, 1, 1, 1, 1]
        init_game_list_level_3 = [list25, list26, list27, list28, list29, list30, list31, list32]
        init_game_list = [init_game_list_level_0, init_game_list_level_1, init_game_list_level_2, init_game_list_level_3]


    def __event_hander__(self):
        global init_game_list, result_flag, xz, ym, level
        # test 是妥协的产物,for(for())嵌套无法退出导致down键循环运行(理论上right键也应该有这种问题,但是没有),test阻止循环
        test = 1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # 关闭程序
                pygame.quit()
                sys.exit()
            if xz == 3:  # 判断胜利
                result_flag = 1
                print("游戏获胜")
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                if ym == -1:
                    if x > 200 and x < 300 and y >= 600 and y <= 700:
                        level = 0
                        result_flag = 0
                        ym = 1
                    elif x > 300 and x < 400 and y >= 600 and y <= 700:
                        level = 1
                        result_flag = 0
                        ym = 1
                    elif x > 400 and x < 500 and y >= 600 and y <= 700:
                        level = 2
                        result_flag = 0
                        ym = 1
                    elif x > 500 and x < 600 and y >= 600 and y <= 700:
                        level = 3
                        result_flag = 0
                        ym = 1

                if x >= 300 and x <= 500 and y >= 710 and y <= 790:# 判断重新开始
                    self.game()
                elif x >= 0 and x <= 100 and y >= 700 and y <= 800:
                    ym = -ym
                # 下两个判断是胜利后选择上一关或者下一关
                if result_flag == 1 and x >= 100 and x <= 300 and y >= 700 and y <= 800 and level > 0:
                    level -= 1
                    self.game()
                if result_flag == 1 and x >= 500 and x <= 700 and y >= 700 and y <= 800 and level < 4:
                    level += 1
                    self.game()

            if event.type == pygame.KEYDOWN and result_flag == 0 and ym == 1:  # 判断按键
                for i in range(1, 7, 1):
                    for j in range(1, 7, 1):
                        if (init_game_list[level][i][j] == 5 or init_game_list[level][i][j] == 8) and test == 1:
                            print(i, i, j, j)
                            if event.key == pygame.K_UP or event.key == pygame.K_w:
                                if init_game_list[level][i - 1][j] == 0 or init_game_list[level][i - 1][j] == 3:
                                    init_game_list[level][i - 1][j] += 5
                                    init_game_list[level][i][j] -= 5
                                elif (init_game_list[level][i - 1][j] == 4 or init_game_list[level][i - 1][j] == 7) and (
                                        init_game_list[level][i - 2][j] == 0 or init_game_list[level][i - 2][j] == 3):
                                    init_game_list[level][i - 1][j] += 1
                                    init_game_list[level][i - 2][j] += 4
                                    init_game_list[level][i][j] -= 5
                            elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                                if init_game_list[level][i + 1][j] == 0 or init_game_list[level][i + 1][j] == 3:
                                    init_game_list[level][i + 1][j] += 5
                                    init_game_list[level][i][j] -= 5
                                    test = 0  # 只有这里会出现一按就运行到底的问题
                                    break
                                elif (init_game_list[level][i + 1][j] == 4 or init_game_list[level][i + 1][j] == 7) and (
                                        init_game_list[level][i + 2][j] == 0 or init_game_list[level][i + 2][j] == 3):
                                    init_game_list[level][i + 1][j] += 1
                                    init_game_list[level][i + 2][j] += 4
                                    init_game_list[level][i][j] -= 5
                                    test = 0  # 只有这里会出现一按就运行到底的问题
                            elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
                                if init_game_list[level][i][j - 1] == 0 or init_game_list[level][i][j - 1] == 3:
                                    init_game_list[level][i][j - 1] += 5
                                    init_game_list[level][i][j] -= 5
                                elif (init_game_list[level][i][j - 1] == 4 or init_game_list[level][i][j - 1] == 7) and (
                                        init_game_list[level][i][j - 2] == 0 or init_game_list[level][i][j - 2] == 3):
                                    init_game_list[level][i][j - 1] += 1
                                    init_game_list[level][i][j - 2] += 4
                                    init_game_list[level][i][j] -= 5
                            elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                                if init_game_list[level][i][j + 1] == 0 or init_game_list[level][i][j + 1] == 3:
                                    init_game_list[level][i][j + 1] += 5
                                    init_game_list[level][i][j] -= 5
                                elif (init_game_list[level][i][j + 1] == 4 or init_game_list[level][i][j + 1] == 7) and (
                                        init_game_list[level][i][j + 2] == 0 or init_game_list[level][i][j + 2] == 3):
                                    init_game_list[level][i][j + 1] += 1
                                    init_game_list[level][i][j + 2] += 4
                                    init_game_list[level][i][j] -= 5
                            break
    def __print_map__(self):
        global init_game_list, result_flag, ym, xz
        xz = 0  # 妥协的代码
        for i in range(0, 8, 1):
            for j in range(0, 8, 1):
                if init_game_list[level][i][j] == 0:
                    self.screen.blit(self.kd, (j * 100, i * 100))
                elif init_game_list[level][i][j] == 1:
                    self.screen.blit(self.qb, (j * 100, i * 100))
                elif init_game_list[level][i][j] == 2:
                    self.screen.blit(self.za, (j * 100, i * 100))
                elif init_game_list[level][i][j] == 3:
                    self.screen.blit(self.mdd, (j * 100, i * 100))
                elif init_game_list[level][i][j] == 4:
                    self.screen.blit(self.xz1, (j * 100, i * 100))
                elif init_game_list[level][i][j] == 5:
                    self.screen.blit(self.r, (j * 100, i * 100))
                elif init_game_list[level][i][j] == 7:
                    self.screen.blit(self.xz2, (j * 100, i * 100))
                    xz += 1  # 检测有多少个箱子到达目的地
                elif init_game_list[level][i][j] == 8:
                    self.screen.blit(self.r, (j * 100, i * 100))
                else:
                    self.screen.blit(self.error, (j * 100, i * 100))
        if result_flag == 1:  # 胜利提示图片
            self.screen.blit(self.win, (200, 7))
            self.screen.blit(self.last, (100, 700))
            self.screen.blit(self.next, (500, 700))
        if ym == -1:  # 说明页面和返回游戏图片
            self.screen.blit(self.menu, (0, 0))
            self.screen.blit(self.ret, (0, 700))
        if ym == 1:  # 查看说明页面图片
            self.screen.blit(self.search, (0, 700))
            self.screen.blit(self.restart, (300, 710))  # 重新开始图片
        pygame.display.update()

    def __image__(self):
        global init_game_list, result_flag, xz, ym
        pygame.display.set_caption("Python推箱子V1.00")  # 设置窗口标题
        result_flag = 0  # 游戏状态检测
        init_game_list = []  # 定义游戏地图
        xz = 0  # 箱子完成个数
        ym = -1  # 页面检测  1.游戏页面 -1.说明页面
        pygame.init()
        self.screen = pygame.display.set_mode((800, 800), 0, 0)
        self.sms = pygame.image.load("./images_txz/sm.png")
        self.sm_plus = pygame.image.load("./images_txz/sm_plus.png")
        self.qb = pygame.image.load("./images_txz/qb.png")
        self.kd = pygame.image.load("./images_txz/kd.png")
        self.xz1 = pygame.image.load("./images_txz/xz1.png")
        self.xz2 = pygame.image.load("./images_txz/xz2.png")
        self.za = pygame.image.load("./images_txz/za.png")
        self.r = pygame.image.load("./images_txz/r.png")
        self.mdd = pygame.image.load("./images_txz/mdd.png")
        self.error = pygame.image.load("./images_txz/error.png")
        self.restart = pygame.image.load("./images_txz/restart.png")
        self.win = pygame.image.load("./images_txz/win.png")
        self.search = pygame.image.load("./images_txz/search.png")
        self.ret = pygame.image.load("./images_txz/return.png")
        self.menu = pygame.image.load("./images_txz/menu.png")
        self.last = pygame.image.load("./images_txz/last_level.png")
        self.next = pygame.image.load("./images_txz/next_level.png")

    def game(self):
        global init_game_list, result_flag
        self.__image__()  # 初始化图片及窗口资源
        self.__init_game_map_list__()  # 初始化地图列表数据
        self.__print_map__()  # 打印地图
        while True:
            self.__event_hander__()  # 检测按键,更改玩家周围九格的数值
            self.__print_map__()  # 打印地图


if __name__ == '__main__':
    song = Game()
    song.game()


这是第二版本的推箱子小程序

第一版本:https://mp.csdn.net/editor/html/114602796

请多多支持!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值