贪食蛇小游戏

面向对象编程

贪食蛇总共分为三个部分:

  • 食物
  • 游戏

一、食物

1、封装食物对象

  • 将食物的特征全部封装起来,食物的初始坐标,宽高以及颜色

    function Food(opt){
        //设置默认值
        opt = opt || {};
        opt.x = opt.x || 0;
        opt.y = opt.y || 0;
        opt.width = opt.width || 20;
        opt.height = opt.height || 20;
        opt.color = opt.color || "green";
        //设置属性
        this.x = opt.x;
        this.y = opt.y;
        this.width = opt.width;
        this.height = opt.height;
        this.color = opt.color;
        this.init();
    }
       

2、添加食物初始化方法

  • 将食物生成到页面上
        
    Food.prototype.init = function(){
        var element = document.createElement("div");
        this.element = element;
        //设置元素
        element.style.width = this.width + "px";
        element.style.height = this.height + "px";
        element.style.backgroundColor = this.color;
        element.style.position = "absolute";
    };
  

3、添加食物随机位置的方法

  • 旧的食物被吃掉的同时又会重新生成新的食物
 	
    //设置随机数
    function random(n,m){
      return Math.floor(Math.random()*(m-n+1)+n);
    }
    Food.prototype.render = function(map){
        this.x = random(0,map.clientWidth/this.width-1)*this.width;
        this.y = random(0,map.clientHeight/this.height-1)*this.height;
        this.element.style.left = this.x + "px";
        this.element.style.top = this.y + "px";
        map.appendChild(this.element);
    };
 

4、设置食物消失的方法

  • 蛇头吃到食物,食物就会消失

    Food.prototype.remove = function(){
        var father = this.element.parentNode;
        father.removeChild(this.element);
    };

二、蛇

1、创建蛇对象

  • 将蛇头和蛇身的特征全部封装起来,蛇头的宽高、初始方向颜色,蛇身的宽高颜色
    
    function Snake(opt){
        //设置默认值
        opt = opt || {};
        opt.width = opt.width || 20;
        opt.height = opt.height || 20;
        opt.direction = opt.direction || Direction.RIGHT;
        //设置属性
        this.width = opt.width;
        this.height = opt.height;
        this.direction = opt.direction;
        this.body = [
            {x:60,y:20,color:"red"},
            {x:40,y:20,color:"green"},
            {x:20,y:20,color:"green"}
        ];
    }
    

2、添加蛇初始化的方法


    Snake.prototype.init = function(map){
        //创建元素
        this.elements = [];
        for(var i=0;i< this.body.length;i++){
            var element = document.createElement("div");
            map.appendChild(element);
            this.elements.push(element);
            //设置元素
            element.style.width = this.width + "px";
            element.style.height = this.height + "px";
            element.style.backgroundColor = this.body[i].color;
            element.style.position = "absolute";
            element.style.left = this.body[i].x + "px";
            element.style.top = this.body[i].y + "px";
        }
    }
    

3、添加一个蛇移动的方法


    var Direction = Object.create({},{
        "TOP": {
            value: 0,
            writable: false,
            enumerable: false,
            configurable: false
        },
        "RIGHT": {
            value: 1,
            writable: false,
            enumerable: false,
            configurable: false
        },
        "BOTTOM": {
            value: 2,
            writable: false,
            enumerable: false,
            configurable: false
        }, "LEFT": {
            value: 3,
            writable: false,
            enumerable: false,
            configurable: false
        }
    });
    

	Snake.prototype.move = function(){
	    //蛇身体移动位置
	    for(var i=this.body.length-1;i>0;i--){
	        this.body[i].x = this.body[i-1].x ;
	        this.body[i].y = this.body[i-1].y ;
	        //设置回元素上
	        this.elements[i].style.left = this.body[i].x + "px";
	        this.elements[i].style.top = this.body[i].y + "px";
	    }
	    //蛇头移动位置
	    switch (this.direction){
	        case Direction.TOP:
	            this.body[0].y -= this.height;
	            break;
	        case Direction.RIGHT:
	            this.body[0].x += this.width;
	            break;
	        case Direction.BOTTOM:
	            this.body[0].y += this.height;
	            break;
	        case Direction.LEFT:
	            this.body[0].x -= this.width;
	            break;
	    }
	    //设置蛇头对应的元素
	    this.elements[0].style.left = this.body[0].x + "px";
	    this.elements[0].style.top = this.body[0].y + "px";
	};

4、设置一个蛇尾加长的方法

  • 蛇每次吃到食物蛇尾就会增加一节

    Snake.prototype.growth = function(){
        //蛇尾加一节
        var last = this.body[this.body.length-1];
        var newLast = {
            x:last.x,
            y:last.y,
            color:last.color
        };
        this.body.push(newLast);
        //设置最后一节对应的元素
        var element = document.createElement("div");
        map.appendChild(element);
        this.elements.push(element);
        //设置元素
        element.style.width = this.width + "px";
        element.style.height = this.height + "px";
        element.style.backgroundColor = newLast.color;
        element.style.position = "absolute";
        element.style.left = newLast.x + "px";
        element.style.top = newLast.y + "px";
    };

三、创建游戏

1、添加一个开始游戏的方法

	
	Game.prototype.start = function(){
	    //设置一个定时器
	    this.timer = setInterval(function(){
	        this.snake.move();
	        //判断蛇的移动范围
	        var head = this.snake.body[0];
	        var maxX = this.map.clientWidth - this.food.width;
	        var maxY = this.map.clientHeight - this.food.height;
	        if(head.x<0 || head.x> maxX || head.y<0 || head.y>maxY){
	            alert("Game Over!");
	            clearInterval(this.timer);
	        }
	        //处理蛇吃食物的情况,蛇头的坐标与食物坐标一致
	        if(head.x == this.food.x && head.y == this.food.y){
	            //1.食物消失
	            this.food.remove();
	            //2.生成新的食物
	            this.food = new Food();
	            this.food.render(this.map);
	            //3.蛇尾加长
	            this.snake.growth(this.map);
	        }
	    }.bind(this),100)
	};
	

2、创建键盘操控游戏事件

  • 利用上下左右键来操控蛇的方向

	Game.prototype.keyCode = function(){
	    //注册键盘按下事件
	    document.addEventListener("keydown",function(e){
	        switch (e.keyCode){
	            case 37:
	                this.snake.direction = window.Direction.LEFT;
	                break;
	            case 38:
	                this.snake.direction = window.Direction.TOP;
	                break;
	            case 39:
	                this.snake.direction = window.Direction.RIGHT;
	                break;
	            case 40:
	                this.snake.direction = window.Direction.BOTTOM;
	                break;
	        }
	    }.bind(this),false)
	};

四、完整代码

1、HTML代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .map {
            width: 800px;
            height: 600px;
            background-color: #000;
            margin: 0 auto;
            position: relative;
        }
        #begin{
            position: fixed;
            top: 100px;
            left: 100px;
        }
    </style>
</head>
<body>
<div class="map"></div>
<input type="button" value="开始游戏" id="begin"/>
  <--引入封装好的js代码-->
<script src="Game.js"></script>

<script>
    var map = document.querySelector(".map");
    var btn = document.querySelector("#begin");
    var game = new Game(map);
    btn.onclick = function(){
        game.start();
    }
</script>
</body>
</html>

2、封装好的js代码

  • 利用沙箱模式,保护代码不被污染

;(function(window,document){
    function random(n,m){
        return Math.floor(Math.random()*(m-n+1)+n);
    }

    function Food(opt){
        opt = opt || {};
        opt.x = opt.x || 0;
        opt.y = opt.y || 0;
        opt.width = opt.width || 20;
        opt.height = opt.height || 20;
        opt.color = opt.color || "green";
        this.x = opt.x;
        this.y = opt.y;
        this.width = opt.width;
        this.height = opt.height;
        this.color = opt.color;
        this.init();
    }
    Food.prototype.init = function(){
        var element = document.createElement("div");
        this.element = element;
        element.style.width = this.width + "px";
        element.style.height = this.height + "px";
        element.style.backgroundColor = this.color;
        element.style.position = "absolute";
    };
    Food.prototype.render = function(map){
        this.x = random(0,map.clientWidth/this.width-1)*this.width;
        this.y = random(0,map.clientHeight/this.height-1)*this.height;
        this.element.style.left = this.x + "px";
        this.element.style.top = this.y + "px";
        map.appendChild(this.element);
    };


    window.Food = Food;
})(window,document);

(function(window,document){
    var Direction = Object.create({},{
        "TOP": {
            value: 0,
            writable: false,
            enumerable: false,
            configurable: false
        },
        "RIGHT": {
            value: 1,
            writable: false,
            enumerable: false,
            configurable: false
        },
        "BOTTOM": {
            value: 2,
            writable: false,
            enumerable: false,
            configurable: false
        }, "LEFT": {
            value: 3,
            writable: false,
            enumerable: false,
            configurable: false
        }
    });
    
    function Snake(opt){
        opt = opt || {};
        opt.width = opt.width || 20;
        opt.height = opt.height || 20;
        opt.direction = opt.direction || Direction.RIGHT;
        this.width = opt.width;
        this.height = opt.height;
        this.direction = opt.direction;
        this.body = [
            {x:60,y:20,color:"red"},
            {x:40,y:20,color:"green"},
            {x:20,y:20,color:"green"}
        ];
    }
    
    Snake.prototype.init = function(map){
        this.elements = [];
        for(var i=0;i< this.body.length;i++){
            var element = document.createElement("div");
            map.appendChild(element);
            this.elements.push(element);
            element.style.width = this.width + "px";
            element.style.height = this.height + "px";
            element.style.backgroundColor = this.body[i].color;
            element.style.position = "absolute";
            element.style.left = this.body[i].x + "px";
            element.style.top = this.body[i].y + "px";
        }
    };
    
    Snake.prototype.move = function(){
        for(var i=this.body.length-1;i>0;i--){
            this.body[i].x = this.body[i-1].x ;
            this.body[i].y = this.body[i-1].y ;
            this.elements[i].style.left = this.body[i].x + "px";
            this.elements[i].style.top = this.body[i].y + "px";
        }
        switch (this.direction){
            case Direction.TOP:
                this.body[0].y -= this.height;
                break;
            case Direction.RIGHT:
                this.body[0].x += this.width;
                break;
            case Direction.BOTTOM:
                this.body[0].y += this.height;
                break;
            case Direction.LEFT:
                this.body[0].x -= this.width;
                break;
        }
        this.elements[0].style.left = this.body[0].x + "px";
        this.elements[0].style.top = this.body[0].y + "px";
    };
    
    Snake.prototype.growth = function(){
        var last = this.body[this.body.length-1];
        var newLast = {
            x:last.x,
            y:last.y,
            color:last.color
        };
        this.body.push(newLast);
        var element = document.createElement("div");
        map.appendChild(element);
        this.elements.push(element);
        element.style.width = this.width + "px";
        element.style.height = this.height + "px";
        element.style.backgroundColor = newLast.color;
        element.style.position = "absolute";
        element.style.left = newLast.x + "px";
        element.style.top = newLast.y + "px";
    };
    window.Snake = Snake;
    window.Direction = Direction;
})(window,document);

(function(window,document){
    function Game(map){
        this.map = map;
        this.food = new Food();
        this.food.render(this.map);
        this.snake = new Snake();
        this.snake.init(this.map);
        this.keyCode();
    }
  
    Game.prototype.start = function(){
        this.timer = setInterval(function(){
            this.snake.move();
            var head = this.snake.body[0];
            var maxX = this.map.clientWidth - this.food.width;
            var maxY = this.map.clientHeight - this.food.height;
            if(head.x<0 || head.x> maxX || head.y<0 || head.y>maxY){
                alert("Game Over!");
                clearInterval(this.timer);
            }
            if(head.x == this.food.x && head.y == this.food.y){
                this.food.remove();
                this.food = new Food();
                this.food.render(this.map);
                this.snake.growth(this.map);
            }
        }.bind(this),100)
    };

	Game.prototype.keyCode = function(){
        document.addEventListener("keydown",function(e){
            switch (e.keyCode){
                case 37:
                    this.snake.direction = window.Direction.LEFT;
                    break;
                case 38:
                    this.snake.direction = window.Direction.TOP;
                    break;
                case 39:
                    this.snake.direction = window.Direction.RIGHT;
                    break;
                case 40:
                    this.snake.direction = window.Direction.BOTTOM;
                    break;
            }
        }.bind(this),false)
    };
    window.Game = Game;
})(window,document);

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我来给你写一个贪食蛇小游戏: import pygame import random # 定义颜色 red = pygame.Color(255, 0, 0) green = pygame.Color(0, 255, 0) black = pygame.Color(0, 0, 0) white = pygame.Color(255, 255, 255) # 初始化pygame pygame.init() # 创建一个窗口 width = 500 height = 500 screen = pygame.display.set_mode((width, height)) # 设置游戏标题 pygame.display.set_caption("贪食蛇") # 定义一个贪食蛇类 class Snake: def __init__(self, color, position): self.color = color self.position = position self.body = [] self.direction = "RIGHT" # 绘制贪食蛇 def draw_snake(self): for x, y in self.position: pygame.draw.rect(screen, self.color, (x, y, 10, 10)) # 更新贪食蛇位置 def update_snake_position(self): head_x, head_y = self.position[0] if self.direction == "RIGHT": new_head_position = (head_x + 10, head_y) elif self.direction == "LEFT": new_head_position = (head_x - 10, head_y) elif self.direction == "UP": new_head_position = (head_x, head_y - 10) else: new_head_position = (head_x, head_y + 10) self.position.insert(0, new_head_position) self.position.pop() # 设置贪食蛇的移动方向 def set_direction(self, direction): if direction == "RIGHT" and self.direction != "LEFT": self.direction = direction elif direction == "LEFT" and self.direction != "RIGHT": self.direction = direction elif direction == "UP" and self.direction != "DOWN": self.direction = direction elif direction == "DOWN" and self.direction != "UP": self.direction = direction # 增加贪食蛇的长度 def add_length(self): tail_x, tail_y = self.position[-1] if self.direction == "RIGHT": self.position.append((tail_x - 10, tail_y)) elif self.direction == "LEFT": self.position.append((tail_x + 10, tail_y)) elif self.direction == "UP": self.position.append((tail_x, tail_y + 10)) else: self.position.append((tail_x, tail_y - 10)) # 定义一个食物类 class Food: def __init__(self): self.position = (random.randint(0, 49) * 10, random.randint(0, 49) * 10) self.is_food_eaten = False # 绘制食物 def draw_food(self): pygame.draw.rect(screen, red, (self.position[0], self.position[1], 10, 10)) # 定义游戏的主循环 def game_loop(): # 初始化贪食蛇 snake = Snake(green, [(100, 100), (90, 100), (80, 100)]) # 初始化食物 food = Food() # 游戏的主循环 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: snake.set_direction("RIGHT") elif event.key == pygame.K_LEFT: snake.set_direction("LEFT") elif event.key == pygame.K_UP: snake.set_direction("UP") elif event.key == pygame.K_DOWN: snake.set_direction("DOWN") screen.fill(white) snake.update_snake_position() # 判断贪食蛇是否食物 if snake.position[0] == food.position: food.is_food_eaten = True snake.add_length() # 如果食物掉,重新生成食物 if food.is_food_eaten: food = Food() # 绘制食物 food.draw_food() # 绘制贪食蛇 snake.draw_snake() # 更新画面 pygame.display.update() game_loop()

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值