游戏来源于 Mdn学习网站

该例子用于对象的理解非常有效(建议看完上面网站的内容在开始练习)



弹球

body {

        margin: 0;

        overflow: hidden;

        font-family: "PingFangSC-Regular", "微软雅黑", sans-serif;

        height: 100%;

}

h1 {

        font-size: 2rem;

        letter-spacing: \-1px;

        position: absolute;

        margin: 0;

        top: \-4px;

        right: 5px;

        color: transparent;

        text-shadow: 0  0  4px  white;

}

p {

        position: absolute;

        margin: 0;

        top: 35px;

        right: 5px;

        color: #aaa;

}

弹球

//弹球的个数

const  BALLS\_COUNT  \=  25;

//弹球的半径范围

const  BALL\_SIZE\_MIN  \=  10;

const  BALL\_SIZE\_MAX  \=  20;

// 弹球的速度

const  BALL\_SPEED\_MAX  \=  7;

// 设定画布

var  canvas  \=  document.querySelector("canvas");

var  pcount  \=  document.querySelector("p");

var  ctx  \=  canvas.getContext("2d");

//获取屏幕的宽高 并设置为画布的宽高

var  width  \=  (canvas.width  \=  window.innerWidth);

var  height  \=  (canvas.height  \=  window.innerHeight);

// 生成随机数的函数

function  random(min,  max)  {

        return  Math.floor(Math.random()  \*  (max  \-  min))  +  min;

}

// 生成随机颜色的函数

function  randomColor()  {

    return  (
"rgb(" + random(0, 255) + ", " + random(0, 255) + ", " +random(0, 255) + ")");
} //定义形状 function Shape(x, y, velX, velY, exists) { this.x \= x; this.y \= y; this.velX \= velX; this.velY \= velY; this.exists \= exists; } // 创建弹球对象 function Ball(x, y, velX, velY, color, size, exists) { Shape.call(this, x, y, velX, velY, exists); this.color \= color; this.size \= size; } Ball.prototype \= Object.create(Shape.prototype); Ball.prototype.constructor \= Ball; // 定义绘制球的函数 Ball.prototype.draw \= function() { ctx.beginPath(); ctx.fillStyle \= this.color; ctx.arc(this.x, this.y, this.size, 0, 2 \* Math.PI); ctx.fill(); }; //更新跳球的位置 Ball.prototype.update \= function() { //到达右边,反弹 if (this.x + this.size \>= width) { this.velX \= \-this.velX; } //到达左边,反弹 if (this.x \- this.size <= 0) { this.velX \= \-this.velX; } //到达底部,反弹 if (this.y + this.size \>= height) { this.velY \= \-this.velY; } //到达顶部 反弹 if (this.y \- this.size <= 0) { this.velY \= \-this.velY; } this.x += this.velX; this.y += this.velY; }; //弹球的碰撞处理 Ball.prototype.collisionDetect \= function() { for (var j \= 0; j < balls.length; j++) { //是不是弹球本身 if (balls\[j\] \=== this) continue; var dx \= this.x \- balls\[j\].x; var dy \= this.y \- balls\[j\].y; var distance \= Math.sqrt(dx \* dx + dy \* dy); //碰撞改变两个球的颜色 if (distance < this.size + balls\[j\].size) { this.color \= balls\[j\].color \= randomColor(); } } }; //定义一个小吃货 function EvilCircle(x, y, exists) { Shape.call(this, x, y, 20, 20, exists); this.color \= "white"; this.size \= 10; } EvilCircle.prototype \= Object.create(Shape.prototype); EvilCircle.prototype.constructor \= EvilCircle; // 定义绘制小吃货的函数 EvilCircle.prototype.draw \= function() { ctx.beginPath(); ctx.strokeStyle \= this.color; ctx.arc(this.x, this.y, this.size, 0, 2 \* Math.PI); ctx.stroke(); }; // 检查小吃货是否碰壁 EvilCircle.prototype.checkBounds \= function() { if (this.x + this.size \>= width) { this.velX \= \-this.velX; } if (this.x \- this.size <= 0) { this.x \= this.size; } if (this.y + this.size \>= height) { this.y \= height \- this.size; } if (this.y \- this.size <= 0) { this.y \= this.size; } }; // 定义小吃货移动函数 EvilCircle.prototype.setControls \= function() { window.onkeydown \= e \=> { if (e.key \=== "a") { this.x \-= this.velX; } else if (e.key \=== "d") { this.x += this.velX; } else if (e.key \=== "w") { this.y \-= this.velY; } else if (e.key \=== "s") { this.y += this.velY; } //上下左右移动 if (e.key \=== "ArrowLeft") { this.x \-= this.velX; } else if (e.key \=== "ArrowRight") { this.x += this.velX; } else if (e.key \=== "ArrowUp") { this.y \-= this.velY; } else if (e.key \=== "ArrowDown") { this.y += this.velY; } }; }; //小吃货碰撞到了弹球 EvilCircle.prototype.collisionDetect \= function() { for (var j \= 0; j < balls.length; j++) { //弹球没被吃 if (balls\[j\].exists) { var dx \= this.x \- balls\[j\].x; var dy \= this.y \- balls\[j\].y; var distance \= Math.sqrt(dx \* dx + dy \* dy); if (distance < this.size + balls\[j\].size) { //标记弹球被吃 balls\[j\].exists \= false; } } } }; //激活小吃货 EvilCircle.prototype.run \= function() { this.draw(); this.checkBounds(); this.collisionDetect(); }; //创建吃货 var evilCircle \= new EvilCircle(20, 20, true); evilCircle.setControls(); // 定义一个数组来保存所有的球 var balls \= \[\]; pcount.textContent \= "还剩" + BALLS\_COUNT + "个球"; for (let i \= 0; i < BALLS\_COUNT; i++) { var size \= random(BALL\_SIZE\_MIN, BALL\_SIZE\_MAX); var ball \= new Ball( // 为避免绘制错误,球至少离画布边缘球本身一倍宽度的距离 random(0 + size, width \- size), random(0 + size, height \- size), random(\-BALL\_SPEED\_MAX, BALL\_SPEED\_MAX), random(\-BALL\_SPEED\_MAX, BALL\_SPEED\_MAX), randomColor(), size, true ); balls.push(ball); } // 定义一个循环来不停地播放 function loop() { background("rgb(0, 0, 0)"); for (var i \= 0; i < balls.length; i++) { if (!balls\[i\].exists) { continue; } balls\[i\].draw(); balls\[i\].update(); balls\[i\].collisionDetect(); } evilCircle.run(); var newballs \= balls.filter(balls \=> balls.exists); pcount.textContent \= "还剩" + newballs.length + "个球"; requestAnimationFrame(loop); } function background(color) { ctx.fillStyle \= color; ctx.fillRect(0, 0, width, height); ctx.beginPath(); } loop();

js 学习四 对象应用 吃货游戏的更多相关文章

  1. js学习日记-对象字面量

    一.对象字面量语法 var person={ name:'小王', age:18, _pri:233 } 成员名称的单引号不是必须的 最后一个成员结尾不要用逗号,不然在某些浏览器中会抛出错误 成员名相 ...

  2. JS学习四(BOM DOM)

    BOM                Screen对象 console.log(window.width);//屏幕宽度 console.log(window.height);//屏幕高度 conso ...

  3. js学习--浏览器对象计时器setInterval()与setTimeout()的使用与区别

    一.setInterval()与setTimeout()的定义: 二.setInterval()与setTimeout()的使用:    1.setInterval()与clearInterval() ...

  4. Hibernate基础学习(四)—对象-关系映射(上)

    一.映射对象标识符      Java语言按内存地址来识别或区分同一个类的不同对象,而关系数据库按主键值来识别或区分同一个表的不同记录.Hibernate使用对象标识符(OID)来建立内存中的对象和数 ...

  5. spring学习 四 对象的创建

    spring中,有三种创建对象的方式 (1)构造创建 (2)实例工厂构造 (3)静态工厂构造 一  构造器创建 在构造器创建对象时,有无参构造和有参构造 两种 (1)在spring中,默认的是无参构造 ...

  6. JS 学习(四)对象

    对象 在JS中,对象是数据(变量),拥有属性和方法. JS中所有事物都是对象:字符串.数字.数组.日期等. 对象是拥有属性和方法的特殊数据类型. 属性是与对象相关的值. 方法是能够在对象上执行的动作. ...

  7. Node.js学习笔记(四): 全局对象

    在浏览器 JavaScript 中,通常 window 是全局对象, 而 Node.js 中的全局对象是 global,所有全局变量(除了 global 本身以外)都是 global 对象的属性. 这 ...

  8. web前端学习(四)JavaScript学习笔记部分(6)-- js内置对象

    1.JS内置对象-什么是对象 1.1.什么是对象: JavaScript中的所有事物都是对象:字符串.数值.数组.函数 每个对象带有属性和方法 JavaScript允许自定义对象 1.2.自定义对象: ...

  9. JavaScript学习12 JS中定义对象的几种方式

    JavaScript学习12 JS中定义对象的几种方式 JavaScript中没有类的概念,只有对象. 在JavaScript中定义对象可以采用以下几种方式: 1.基于已有对象扩充其属性和方法 2.工 ...

随机推荐

  1. 堆里面的分区:Eden,Survivor(from+ to),老年代,各自的特点

    堆里面分为新生代和老生代(java8取消了永久代,采用了Metaspace),新生代包含Eden+Survivor区,survivor区里面分为from和to区,内存回收时,如果用的是复制算法,从fr ...

  2. LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...

  3. Uep必填字段校验

    在开发中常常有必填字段, <span style="color:Red">*</span>服务地址:</td><hy:formfield ...

  4. AI换脸必备知识:如何查看显卡型号以及显存大小!

    使用Deepfakes(AI换脸) 软件,拼的就是配置,耗的就是时间,考验的是耐心. 配置好了,时间就少了. 所以玩这种软件,硬核需求就是:配置,配置,配置.  我的电脑能跑这个软件么?也是很多新手的 ...

  5. java 深入HashTable

    在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相 ...

  6. 浏览器端-W3School-HTML:HTML DOM Select 对象

    ylbtech-浏览器端-W3School-HTML:HTML DOM Select 对象 1.返回顶部 1. HTML DOM Select 对象 Select 对象 Select 对象代表 HTM ...

  7. android studio中方法和类被调用多次,但是AS显示灰色,解决办法

    Android Studio里面的一些类及方法,明明有被其他的类或者方法调用,但是去看的时候显示灰色,鼠标放上面的时候显示:Class ‘XXX’ is never used或者Method ‘XXX ...

  8. flutter ListView列表和导航传值以及回调

    main.dart import 'package:flutter/material.dart'; void main(){ return runApp(MyApp()); } class Produ ...

  9. 清除表单input输入框内数据

    清除表单input输入框内数据 1. $(':input','#addVoucherType') //'#addVoucherType'表单id .not(':button') .val('') .r ...

  10. hash模块MD5加密

    MD5加密:获取32位加密字符串: 示例(MD5加密'123456')import hashlibhashObject=hashlib.md5(b'123456') #实例化,加密字符串不能直接加密, ...