赞
踩
在正式开始敲代码之前,开发者们需要先下载安装 Huawei DevEco Studio,如果对于这个流程不甚熟悉,可以参照官网的教程操作。
- HuaweiDevEco Studio 安装指南:Huawei DevEco Studio
- <div class="container" onswipe="touchMove">
- <text class="title">Snake Game</text>
- <canvas ref="canvasref" style="width: 600px; height: 600px;
- background-color:#000000" >
- </canvas>
- </div>
- .container {
- flex-direction: column;
- justify-content: center;
- align-items: center;
- background-color: #d6d8d8;
- }
- const canvas = this.$refs.canvasref;
- this.ctx = canvas.getContext("2d")
- <image src='/common/up.png' class="backBtnup"
- onclick="onStartGame(1)"></image>
- <div class="directsecond">
- <image src='/common/left.png' class="backBtnleft"
- onclick="onStartGame(2)"></image>.
- <image src='/common/down.png' class="backBtncenter"
- onclick="onStartGame(3)"></image>
- <image src='/common/right.png' class="backBtnright"
- onclick="onStartGame(4)"></image>
- </div>
- .backBtnup, .backBtncenter, .backBtnleft, .backBtnright {
- width: 100px;
- height: 100px;
- margin-bottom: 20px;
- margin-top: 20px;
- border-radius: 10px;
- background-color: #000000;
- }
- .directsecond {
- flex-direction: row;
- justify-content: center;
- align-items: center;
- }
- .backBtnup {
- margin-top: 80px;
- }
- .backBtncenter {
- margin-left: 40px;
- margin-right: 40px;
- }
- onStartGame(direct){
- if (direct == 1) {
- this.direction = 'up'
- } else if (direct == 2) {
- this.direction = 'left'
- } else if (direct == 3) {
- this.direction = 'down'
- } else if (direct == 4) {
- this.direction = 'right'
- }
- },
<text if="{{!gameOver}}"class="scoretitle">Score: {{score}}</text>
- .scoretitle {
- font-size: 50px;
- margin-top: 30px;
- }
- //index.js
- data: {
- title: "",
- snakeSize: 10,
- w: 600,
- h: 600,
- score: 0,
- snake : [],
- ctx: null,
- food: null,
- direction: '',
- gameOver: false,
- tail: {
- x: 0,
- y: 0
- },
- drawSnake() {
- var len = 7;
- var snake = [];
- for (var i = len - 1; i >= 0; i--) {
- snake.push({
- x: 0,
- y: i
- });
- }
- this.snake = snake;
- },
- cookie(x, y) {
- var ctx= this.ctx;
- ctx.fillStyle = '#e28743';
- ctx.fillRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize);
- //border color of the cookie
- ctx.fillStyle = '#e28743';
- ctx.fillRect(x * this.snakeSize + 1, y * this.snakeSize + 1, this.snakeSize- 2,
- this.snakeSize - 2);
- this.ctx = ctx;
- }
- createFood() {
- this.food = {
- x: Math.floor((Math.random() * 30) + 1),
- y: Math.floor((Math.random() * 30) + 1)
- }
- for (var i = 0; i > this.snake.length; i++) {
- var snakeX = this.snake[i].x;
- var snakeY = this.snake[i].y;
- if (this.food.x === snakeX && this.food.y === snakeY || this.food.y ===
- snakeY && this.food.x === snakeX) {
- this.food.x = Math.floor((Math.random() * 30) + 1);
- this.food.y = Math.floor((Math.random() * 30) + 1);
- }
- }
- }
- var ctx = this.ctx;
- ctx.fillStyle = '#e28743';
- ctx.fillRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize);
- //border color of snake
- ctx.strokeStyle = '#063970';
- ctx.strokeRect(x * this.snakeSize, y * this.snakeSize, this.snakeSize, this.snakeSize);
- this.ctx = ctx;
在 index.js 文件中,判断按键导航的方向,并沿此方向增加蛇头,减少蛇尾。
- if (this.direction == 'right') {
- snakeX++;
- }
- else if (this.direction == 'left') {
- snakeX--;
- }
- else if (this.direction == 'up') {
- snakeY--;
- } else if (this.direction == 'down') {
- snakeY++;
- }
- if(snakeX == this.food.x && snakeY== this.food.y) {
- this.tail = {x: snakeX, y: snakeY}; //Create a new head instead of moving thetail
- this.score = this.score+5;
- this.createFood(); //Create new food
- } else {
- this.tail = this.snake.pop(); //pops out the last cell
- this.tail.x = snakeX;
- this.tail.y = snakeY;
- }
与此同时,增加蛇的长度,并绘制新的食物。
- //index.js
- this.snake.unshift(this.tail); //putsback the tail as the first cell
- for(var i = 0; i < this.snake.length; i++) {
- this.bodySnake(this.snake[i].x, this.snake[i].y);
- }
- this.cookie(this.food.x, this.food.y);
- checkCollision(x, y, array) {
- for(var i = 0; i < array.length; i++) {
- if(array[i].x === x && array[i].y === y)
- return true;
- }
- return false;
- }
- if (snakeX == -1 || snakeX == this.w / this.snakeSize ||snakeY == -1 || snakeY
- ==this.h / this.snakeSize ||this.checkCollision(snakeX, snakeY, this.snake)) {
- ctx.clearRect(0,0,this.w,this.h); //cleanup the canvas
- this.restart()
- return;
- }
<text if="{{gameOver}}" class="scoretitle">Game Over!!</text>
- //index.js
- restart() {
- this.drawArea()
- this.drawSnake()
- this.createFood()
- this.gameOver = true
- this.score = 0
- },
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。