HTML5贪吃蛇游戏实现思路及源代码

阅读: 评论:0

2024年2月8日发(作者:)

HTML5贪吃蛇游戏实现思路及源代码

this.w = sw;

this.h = sh;

ion = direction;

= status;

= speed;

= image;

= flag;

}

function transfer(keyCode)

{

switch (keyCode)

{

case 37:

return 1;

case 38:

return 3;

case 39:

return 2;

case 40:

return 0;

}

}

function addFood()

{

//food_x=box_x+(()*(box_width-unit));

//food_y=box_y+(()*(box_height-unit));

food_x = unit * (() * x_cnt);

food_y = unit * (() * y_cnt);

food = new Role(food_x, food_y, unit, unit, 0, 0, 0, image_sprite, true);

}

function play(event)

{

var keyCode;

if (event == null)

{

keyCode = e;

tDefault();

}

else

{

keyCode = e;

tDefault();

}

var cur_direction = transfer(keyCode);

snake[0].direction = cur_direction;

}

function update()

{

//add a new part to the snake before move the snake

if (snake[0].x == food.x && snake[0].y == food.y)

{

var length = ;

var tail_x = snake[length - 1].x;

var tail_y = snake[length - 1].y;

var tail = new Role(tail_x, tail_y, unit, unit, snake[length - 1].direction, 0, 0, image_sprite, true);

(tail);

addFood();

}

//modify attributes

//move the head

switch (snake[0].direction)

{

case 0: //down

snake[0].y += unit;

if (snake[0].y > bound_down - unit)

snake[0].y = bound_down - unit;

break;

case 1: //left

snake[0].x -= unit;

if (snake[0].x < bound_left)

snake[0].x = bound_left;

break;

case 2: //right

snake[0].x += unit;

if (snake[0].x > bound_right - unit)

snake[0].x = bound_right - unit;

break;

case 3: //up

snake[0].y -= unit;

if (snake[0].y < bound_up)

snake[0].y = bound_up;

break;

}

//move other part of the snake

for (var i = - 1; i >= 0; i--)

{

if (i > 0)

//snake[i].direction=snake[i-1].direction;

{

snake[i].x = snake[i - 1].x;

snake[i].y = snake[i - 1].y;

}

}

}

function drawScene()

{

ect(box_x, box_y, box_width, box_height);

Style = "rgb(0,0,0";

Rect(box_x, box_y, box_width, box_height);

//detection collisions

//draw images

for (var i = 0; i < ; i++)

{

age(image_sprite, snake[i].x, snake[i].y);

}

age(image_sprite, food.x, food.y);

}

function init()

{

canvas = mentById("scene");

ctx = text('2d');

//images

image_sprite = new Image();

image_ = "images/";

image_ = function ()

{}

//ojects

snake = new Array();

var head = new Role(0 * unit, 0 * unit, unit, unit, 5, 0, 1, image_sprite, true);

(head);

ntListener('keydown', play, false);

addFood();

setInterval(update, 300); //note

setInterval(drawScene, 30);

HTML5贪吃蛇游戏实现思路及源代码

本文发布于:2024-02-08 11:26:46,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170736280667374.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:官网一览
标签:源代码   游戏   思路   实现
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23