[Phaser] 小游戏——消消乐

本文介绍如何使用Phaser框架开发一款消消乐小游戏。主要内容包括设置游戏容器、放置图标、实现拖动功能以及消除逻辑。游戏允许用户拖动一行或一列,当3个或更多相同图案相邻时会自动消除。文章提供了完整的代码示例,欢迎大家关注和交流。
摘要由CSDN通过智能技术生成

体验链接

当前版本一次只能消除一处!

若有不足,欢迎指正!

let’s go!

需求:

  1. 图标成矩形随机排列
  2. 可以拖动一行或一列
  3. 3个或以上个相同图案连在一起时会被消除

0. 先把 Phaser 的主要代码搭起来

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Drag and Match</title>
    <style>
        * {
    
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <div id="game"></div>
    <script src="/public/js/phaser.min.js"></script>
    <script>
        let game
        let gameOptions = {
    
            iconW: 100, // 图标宽度
            iconH: 100, // 图标高度
            rows: 10, // 行数
            columns: 7, // 列数
            iconNum: 6, // 图标数
            gameArea: {
     // 游戏区域
                x: 25,
                y: 300
            },
            movingX: 'x', 
            movingY: 'y', 
        }

        let gameAreaW = gameOptions.iconW * gameOptions.columns
        let gameAreaH = gameOptions.iconH * gameOptions.rows
        
        class PlayGame extends Phaser.Scene {
    
            constructor() {
    
                super('PlayGame')
            }
            preload() {
    
                this.load.spritesheet('sprite', 'sprite.png', {
    
                    frameWidth: gameOptions.iconW,
                    frameHeight: gameOptions.iconH
                })
            }
            create() {
    
                
            }
        }

        let gameConfig = {
    
            type: Phaser.AUTO,
            scale: {
    
                mode: Phaser.Scale.WIDTH_CONTROLS_HEIGHT,
                autoCenter: Phaser.Scale.CENTER_BOTH,
                parent: 'game',
                width: 750,
                height: 1464
            },
            scene: PlayGame
        }

        game = new Phaser.Game(gameConfig)
    </script>
</body>

</html>

1. 设置游戏区域容器

class PlayGame extends Phaser.Scene {
   
  create() {
   
    this.itemContainer = this.add.container(gameOptions.gameArea.x, gameOptions.gameArea.y)

    let mask = this.make.graphics()
    mask.fillStyle(0x00ffff, 1);
    mask.fillRect(gameOptions.gameArea.x, gameOptions.gameArea.y, gameOptions.iconW * gameOptions.columns, gameOptions.iconH * gameOptions.rows)
    this.itemContainer.setMask(new Phaser.Display.Masks.GeometryMask(this, mask))

    this.itemArr = [] // 用来存放所有的小球
  }
}

2. 放置小球

class PlayGame extends Phaser.Scene {
   
  create() {
   
    this.putSpirte()
  }
  putSpirte() {
   
    for (let i = 0; i < gameOptions.columns; i++) {
   
      this.itemArr[i] = []
      for (let j = 0; j < gameOptions.rows; j++) {
   
        let iconX = gameOptions.iconW * i
        let iconY = gameOptions.iconH * j
        let type = Phaser.Math.Between(0, gameOptions.iconNum - 1)
        let icon = this.add.sprite(iconX, iconY, 'sprite', type)
        icon.setOrigin(0, 0)
        icon.rows = j
        icon.columns = i
        icon.type = type

        this.itemArr[i][j] = icon
        this.itemContainer.add(icon)
      }
    }
  }
}

3. 拖动小球

3.1 检测触碰区域获取触碰位置

class PlayGame extends Phaser.Scene {
   
  checkArea(pointer) {
   
    let gameArea = new Phaser.Geom.Rectangle(gameOptions.gameArea.x, gameOptions.gameArea.y, gameOptions.columns * gameOptions.iconW, gameOptions.rows * gameOptions.iconH)
    return Phaser.Geom.Rectangle.Contains(gameArea, pointer.x, pointer.y)
  }
  getPos(pointer) {
   
    let col = (pointer.downX - gameOptions.gameArea.x) / gameOptions.iconW
    let row = (pointer.downY - gameOptions.gameArea.y) / gameOptions.iconH
    this.curRow = Math.floor(row)
    this.curCol = Math.floor(col)
  }
}

3.2 监听拖动

class PlayGame extends Phaser.Scene {
   
  create() {
   
    this.input.on('pointerdown', this.startHandler, this)
    this.input.on('pointermove', this.moveHandler, this)
    this.input.on('pointerup'<
"Phaser Canvas消消乐"是一款基于Phaser框架开发的休闲益智类游戏。玩家需要在规定的时间内消除相同的方块,以获得分数和奖励。 游戏界面使用Canvas技术绘制,使得游戏画面更加流畅和精美。玩家可以通过鼠标或触摸屏幕进行游戏操作,点击相邻的两个或多个相同颜色的方块即可消除它们,如果方块消除后会导致其他方块掉落并形成新的消除组合,会获得额外的分数。 游戏设定了时间限制,玩家需要在规定的时间内尽可能地消除更多的方块,挑战自己的极限。同时,游戏还设置了难度逐渐增加的关卡,每个关卡的方块布局都不同,给玩家带来新的挑战。 游戏中还加入了道具系统,玩家可以使用道具来帮助自己更好地完成关卡。道具包括炸弹,可以一次性消除一定范围内的方块;魔法棒,可以消除指定颜色的方块等等。玩家可以通过收集游戏内的金币或者通过充值来获得道具,增加游戏的趣味性。 此外,游戏还提供了多种游戏模式供玩家选择,包括经典模式、限时模式和挑战模式等,每种模式都有不同的玩法和任务。玩家可以根据自己的喜好和时间安排来选择合适的模式进行游戏。 总而言之,"Phaser Canvas消消乐"是一款具有丰富游戏玩法,美观画面和多样化挑战的休闲益智游戏。无论是休闲时间娱乐还是挑战自我,这款游戏都能带给玩家无穷的乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值