贪吃蛇
1.贪吃蛇图片(不浪费大家时间)
## 代码 index.html文件代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
background: -webkit-linear-gradient(rgba(0, 255, 200, 0.925), skyblue);
color: #fff;
}
table{
border-collapse: collapse;
background: transparent;
border: 1px solid #fff;
}
td{
width: 24px;
height: 24px;
color: rgb(255, 0, 64);
text-align: center;
background: transparent;
border: 1px solid #fff;
border-radius: 50%;
}
</style>
</head>
<body>
<h3 id="f">帧编号:0</h3>
<h3 id="score">分数:0</h3>
<dic id="app"></dic>
<script src="js/Game.js"></script>
<script src="js/Snake.js"></script>
<script src="js/food.js"></script>
<script>
var game = new Game();
</script>
</body>
</html>
以下是food.js,snake.js,game.js的代码 1.food.js
function Food(gameSnake){
var self = this;
do{
this.row = parseInt(Math.random() * gameSnake.row);
this.col = parseInt(Math.random() * gameSnake.col);
}while((function(){
for(var i = 0; i < gameSnake.snake.body.length; i++){
if(gameSnake.snake.body[i].row == self.row || gameSnake.snake.body[i].col == self.col){
return true;
}
}
return false;
})());
console.log(this.row, this.col);
}
Food.prototype.render = function() {
game.setHtml(this.row, this.col, "?");
}
2.snake.js
function Snake(){
this.body = [
{"row":3, "col":5},
{"row":3, "col":4},
{"row":3, "col":3},
{"row":3, "col":2}
];
this.direction = "R";
this.willDirection = "R";
}
Snake.prototype.update = function() {
this.direction = this.willDirection;
switch(this.direction){
case "R":
this.body.unshift({"row":this.body[0].row, "col":this.body[0].col + 1});
break;
case "D":
this.body.unshift({"row":this.body[0].row + 1, "col":this.body[0].col});
break;
case "L":
this.body.unshift({"row":this.body[0].row, "col":this.body[0].col - 1});
break;
case "U":
this.body.unshift({"row":this.body[0].row - 1, "col":this.body[0].col});
break;
}
if(this.body[0].col > game.col-1 || this.body[0].row > game.row - 1 || this.body[0].col < 0 || this.body[0].row < 0){
alert("游戏结束!您当前的得分为:"+game.score+"分。");
this.body.shift();
clearInterval(game.timer);
}
for(var i = 1; i <this.body.length; i++){
if (this.body[0].col == this.body[i].col && this.body[0].row == this.body[i].row){
alert("游戏结束!您当前的得分为:"+game.score+"分。");
clearInterval(game.timer);
}
}
if(this.body[0].row == game.food.row && this.body[0].col == game.food.col){
game.food = new Food(game);
game.score++;
game.f = 0;
}else{
this.body.pop();
}
};
Snake.prototype.changeDirection = function(d){
this.willDirection = d;
}
Snake.prototype.render = function(){
game.setColor(this.body[0].row, this.body[0].col, '-webkit-radial-gradient(center center , pink, red)');
for(var i = 1; i < this.body.length; i++){
game.setColor(this.body[i].row, this.body[i].col, '-webkit-radial-gradient(center center , orange, red)');
}
}
3.game.js
function Game(){
this.row = 20;
this.col = 20;
this.score = 0;
this.init();
this.snake = new Snake();
this.food = new Food(this);
this.start();
this.bindEvent();
}
Game.prototype.init = function(){
this.dom = document.createElement("table");
var tr,td;
for(var i = 0; i < this.row; i++){
tr = document.createElement("tr");
for(var j = 0; j < this.col; j++){
td = document.createElement("td");
tr.appendChild(td);
}
this.dom.appendChild(tr);
}
document.getElementById("app").appendChild(this.dom);
};
Game.prototype.clear = function() {
for(var i = 0; i < this.row; i++){
for(var j = 0; j < this.col; j++){
this.dom.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].style.background = "transparent";
this.dom.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerHTML = "";
}
}
};
Game.prototype.setColor = function(row, col, color){
this.dom.getElementsByTagName("tr")[row].getElementsByTagName("td")[col].style.background = color;
};
Game.prototype.setHtml = function(row, col, html){
this.dom.getElementsByTagName("tr")[row].getElementsByTagName("td")[col].innerHTML = html;
};
Game.prototype.bindEvent = function(){
var self = this;
document.onkeydown = function(event){
switch(event.keyCode){
case 37:
if(self.snake.direction == "R")
break;
self.snake.changeDirection("L");
break;
case 38:
if(self.snake.direction == "D")
break;
self.snake.changeDirection("U");
break;
case 39:
if(self.snake.direction == "L")
break;
self.snake.changeDirection("R");
break;
case 40:
if(self.snake.direction == "U")
break;
self.snake.changeDirection("D");
break;
}
}
};
Game.prototype.start = function(){
this.f = 0;
this.timer = setInterval(function() {
game.f++;score
document.getElementById("f").innerHTML = "帧编号:"+game.f;
document.getElementById("score").innerHTML = "分数:"+game.score;
game.clear();
var during = game.snake.body.length < 30 ? 30 - game.snake.body.length : 1;
game.f % during == 0 && game.snake.update();
game.snake.render();
game.food.render();
}, 20)
};
贪吃蛇游戏我打算进行很多创新,希望大家任意发表一些玩法方面的想法我尽力去实现,感谢各位佬们的观看。
个人简介
本人是大连理工大学软件工程专业(存活于著名的大黑山程序员养殖基地)的一名大一学生,现在在比特科技旗下学习。 本人github网址(里面有大一上我打的所有c语言代码,包含没有实现文件读取的学生管理系统,通讯录以及一些小游戏等等)
个人能力
现已完成c语言的学习(不过在这个假期仍然会发一些c语言的学习心得分享因为比特课需要先跟鹏哥学习c语言一段时间),在学校参加了两大科创社团,帮我在这半年内了解了网络安全的入门级知识(包括逆向软件的使用,抓包软件的使用,了解密码学,网络安全隐写,web渗透,pwn太难了搞不明白),了解了Linux的基本命令和简单shell的编写,在假期自学了js,html和css的基础(vue框架,node.js等还未入门),了解了部分数据结构,基本能够使用eclipse和pycharm来进行java和python的代码编写。
未来如何学习编程
会从底层出发去学习,结合实际情况去制定适当目标。
未来编程付出的时间
每周会有6天进行代码的编写
最想进入的公司
当然是Microsoft,待遇很好。
未来博客暂时面向方向
由于下学期课程的影响,我会进行c加加和数据结构的学习,还有就是下学期我选社团方向为web方向,所以我需要实现各种功能的贪吃蛇(功能会在后期逐渐完善包括吃啥色变什么色以及上一局回放等),也就意味着要进行前端的学习。
写博客目的
我这种菜鸡当然不是为了赚钱(也赚不了了),主要是为了进行一些代码的记录和分享,希望大家的监督!未来我会不断优化博客文章带来一些好的代码给大家!
|