JavaScript 实例 可以看看

[JavaScript实例解析]js计算器 本实例来自菜鸟教程 实例代码 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"> &lt 阅读详情

<head>
<meta content-type="text/html;charset=utf-8"/>
<style>
#table {
 position:absolute;
 left:0;
 top:0;
 z-index:6;
 width :100%;
 height:100%;
 border:"1px solid black";
}
#table td {
 width:18;
 height:18;
 background-color:yellow;
 padding:1;
}
#info {
 position:relative;
 top:-18;
 left:591;
 width :100;
 height:150;
 background-color:blue;
}
</style>
<script >
Game = {
 BlockWidth : 20,
 GameState :"pause",
 Level : 1,
 Scoure : 0,
 Option :{
  init:function(){
   this.speed = 200-(Game.Level-1)*50;
   //this.foodPositionLevel = 1;
   this.foodNumOfsucced = 10+((Game.Level-1)*5);
   this.eatDeficultLevel = 16-Game.Level;
  }
 },
 Map : {
  init : function(left,top,width,height,foodNum){
   this.food = [];
   this.foodNum = foodNum;
   this.left = left;
   this.top = top;
   this.width = width;
   this.height = height;
   this.instance = document.createElement("div");
   this.instance.setAttribute("id","map");
   this.instance.style.position = "absolute";
   this.instance.style.left = left;
   this.instance.style.top = top;
   //this.instance.style.offsetWidth = width;
   //this.instance.style.offsetHeight = height;
   this.instance.style.width = width;
   this.instance.style.height = height;
   this.instance.style.backgroundColor = "gray";
   document.getElementById("adiv").appendChild(this.instance);
   var table = document.createElement("table");
   table.setAttribute("id","table");
   var frag = document.createDocumentFragment();
   frag.appendChild(table);
   for(var i = 0;i<600;i+=Game.BlockWidth){
    var row = table.insertRow(-1);
    for(var j =0;j<600;j+=Game.BlockWidth){
     var cell = row.insertCell(-1);
     //cell.innerHTML = "";
    }
   }
   this.instance.appendChild(frag);
  },
  createOrRefreshFood: function(){
   var ran;
   var tempRan;
   var foodPosLeft,foodPosTop;
   for(var i=0;i<this.foodNum;i++){
    if(!this.food[i]){
     this.food[i] = Game.Food.create(Game.BlockWidth-2,Game.BlockWidth-2,"red");
     this.instance.appendChild(this.food[i]);
    }
    //写了个关数越高,食物越靠近边沿的算法
    ran = Math.random();
    if(parseInt(ran*100) % 2 == 0 ){
     tempRan = (1/(Game.Level*2))*ran;
    }else{
     tempRan = 1-(1/(Game.Level*2))*ran;
    }
    if(parseInt(ran*1000) % 3 == 0 ){
     foodPosLeft = tempRan*(this.width-20) + this.left;
     foodPosLeft = foodPosLeft - foodPosLeft%20
     foodPosTop = ran*(this.height-20) + this.top;
     foodPosTop = foodPosTop - foodPosTop%20;
    }else if(parseInt(ran*1000) % 3 == 1){
     foodPosLeft = ran*(this.width-20) + this.left;
     foodPosLeft = foodPosLeft - foodPosLeft%20;
     foodPosTop = tempRan*(this.height-20) + this.top;
     foodPosTop = foodPosTop - foodPosTop % 20;
    }else{
     foodPosLeft = tempRan*(this.width-20) + this.left;
     foodPosLeft = foodPosLeft - foodPosLeft%20;
     foodPosTop = tempRan*(this.height-20) + this.top;
     foodPosTop = foodPosTop - foodPosTop % 20;
    }
    this.food[i].style.left = foodPosLeft;
    this.food[i].style.top = foodPosTop;
   }
  }
 },
 Food : {
  create : function(width,height,color){
     this.width = width;
     this.height = height;
     this.color = color;
     this.instance = document.createElement("div");
     this.instance.style.position = "absolute";
     this.instance.style.zIndex = "9";
     this.instance.style.width = width;
     this.instance.style.height = height;
     this.instance.style.backgroundColor = color;
     return this.instance;
    }
 },
 Snack : {
  init: function(defaultFoodNum){
   this.foodNum = defaultFoodNum;
   this.foodNumOfEat = 0;
   this.instance = [];
   this.derect = -1; // -1 = left,1=right,-2=up,2=down
   this.instance[0] = document.createElement("div");
   this.instance[0].style.position = "absolute";
   this.instance[0].style.backgroundColor = "black";
   this.instance[0].style.zIndex = "9";
   this.instance[0].style.width = Game.BlockWidth;
   this.instance[0].style.height = Game.BlockWidth;
   this.instance[0].style.left = 200;
   this.instance[0].style.top = 200;
   this.tail = null;
   this.instance[0].style.border = "2px  solid   white";
   Game.Map.instance.appendChild(this.instance[0]);
   for(var i=1;i<this.foodNum;i++){
    this.instance[i] = this.instance[0].cloneNode(true);
    this.instance[i].style.width = Game.BlockWidth;
    this.instance[i].style.height = Game.BlockWidth;
    this.instance[i].style.left = (parseInt(this.instance[i-1].style.left)+ Game.BlockWidth);
    this.instance[i].style.top = 200;
    this.instance[i].style.backgroundColor = "black";
    this.instance[i].style.border = "2px  solid   white";
    //this.instance[i].style.borderColor = "red";
    Game.Map.instance.appendChild(this.instance[i]);
   }
  },
  move:function(derect){
   if(derect + this.derect!=0){
    this.derect = derect;
   }
   var head = {};
   head.left = parseInt(this.instance[0].style.left);
   head.top = parseInt(this.instance[0].style.top);
   switch (this.derect){
     case -1:{
      this.instance[0].style.left = parseInt(this.instance[0].style.left)-Game.BlockWidth;
      break;
      }
     case 1:{
      this.instance[0].style.left = parseInt(this.instance[0].style.left)+Game.BlockWidth;
      break;
     }
     case -2:{
      this.instance[0].style.top = parseInt(this.instance[0].style.top)-Game.BlockWidth;
      break;
     }
     case 2:{
      this.instance[0].style.top = parseInt(this.instance[0].style.top)+Game.BlockWidth;
      break;
     }
    }
   if(this.tail!=null){
     this.foodNum++;
   }
   if(this.foodNum>1){
    for(var i=this.foodNum-1;i>1;i--){
     this.instance[i].style.left = this.instance[i-1].style.left;
     this.instance[i].style.top = this.instance[i-1].style.top;
    }
    if(this.tail!=null){
     Game.Map.instance.appendChild(this.tail);
     this.tail = null;
    }
    this.instance[1].style.left = head.left;
    this.instance[1].style.top = head.top;
   }
   if(this.isFull()){
    alert("恭喜您,顺利闯过第"+Game.Level+"关");
    clearInterval(Game.timing);
    if(Game.Level>5){
     alert("you win!");
    }else{
     Game.Level++;
     $("act").innerHTML = Game.Level;
     Game.Option.init();
    }
   }else if(this.isDead()){
    clearInterval(Game.timing);
    window.onkeydown = null;
    alert("you fail");
   }else{
    this.eat();
   }
  },
  eat : function(){
   var map = Game.Map;
   var head = this.instance[0];
   for(var i=0;i<map.food.length;i++){
    if(Math.abs(parseInt(this.instance[0].style.left)-parseInt(map.food[i].style.left))<=Game.Option.eatDeficultLevel
     && Math.abs(parseInt(this.instance[0].style.top)-parseInt(map.food[i].style.top))<=Game.Option.eatDeficultLevel) {
     //alert("eat!");
     this.tail = this.instance[this.instance.length] = this.instance[0].cloneNode(true); 
     var foodEated = map.food[i];
     for(var j=i+1;j<map.food.length;j++){
      map.food[j-1] = map.food[j];
     }
     map.food.length--;
     foodEated.parentNode.removeChild(foodEated);
     this.foodNumOfEat++;
     //alert(Game.Scoure);
     Game.Scoure = Game.Scoure + (100*Game.Level);
     //alert($("scoure"));
     $("scoure").innerHTML = ""+Game.Scoure;
     break;
    }
   }
   if(map.food.length<=0){
    map.createOrRefreshFood(map.foodNum);
   }
  },
  isFull : function(){
   //alert(this.foodNumOfEat);
   //alert(Game.foodNumOfsucced);
   return this.foodNumOfEat >= Game.Option.foodNumOfsucced;
  },
  isDead : function(){
   var map = Game.Map;
   if(
    parseInt(this.instance[0].style.left) < map.left
    || parseInt(this.instance[0].style.left) >= (map.left+map.width)
    || parseInt(this.instance[0].style.top) < map.top
    || parseInt(this.instance[0].style.top) >= (map.top + map.height) ) {
    return true;
    }
   for(var i=1;i<this.foodNum;i++){
    if(this.instance[0].style.left == this.instance[i].style.left
     && this.instance[0].style.top == this.instance[i].style.top){
      return true; 
     }
   }
  }
 },
 GameStart :function(level){
  this.Map.init(0,0,600,600,1);
  this.Map.createOrRefreshFood();
  this.Snack.init(6);
  this.Option.init(level);
  var gameItsSelf = this;
  //if(!window.onkeydown){
  // window.onkeydown = document.body.onkeydown;
  //}
  //alert(document.body.onkeydown);
  window.onkeydown = document.body.onkeydown = function(event){
   event = window.event||event;
   var keyCode = event.keyCode;
   //alert(keyCode);
   if(keyCode==32){
    //alert("暂停");
    if(Game.GameState=="pause"){
     Game.GameState = "alive";
     window.clearInterval(Game.timing);
     Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,Game.Snack.derect),Game.Option.speed);
    }else{
     Game.GameState = "pause";
     window.clearInterval(Game.timing);
    }
   }else if(Game.GameState =="alive"){
    var tempDirect;
    switch (keyCode) {
     case 37:{
      //$("debug").innerHTML +="X";
      tempDirect = -1;
      if(tempDirect!=Game.Snack.derect){
       window.clearInterval(Game.timing);
       Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),Game.Option.speed);
      }
      break;
      }
     case 38:{
      //$("debug").innerHTML +="y";
      tempDirect = -2;
      if(tempDirect!=Game.Snack.derect){
       window.clearInterval(Game.timing);
       Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),Game.Option.speed);
      }
      break;
      }
     case 39:{
      tempDirect = 1;
      if(tempDirect!=Game.Snack.derect){
       window.clearInterval(Game.timing); 
       Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),Game.Option.speed);
      }
      break;
      }
     case 40: {
      tempDirect = 2;
      if(tempDirect!=Game.Snack.derect){
       window.clearInterval(Game.timing);
       Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),Game.Option.speed);
      }
      break;
      }
     default :
      break;
    }
   }
  }
 }
}
function bindParamsToFun(obj,fun){
 var args = Array.prototype.slice.call(arguments).slice(2);
 return function(){
 fun.apply(obj,args);
 };
}
function $(id){
 return document.getElementById(id);
}
window.onload = function(){
 Game.GameStart();
 }
//window.onload = function(){
// alert(document.getElementById("theBody"));
//}
</script>
</head>
<body id="theBody">
 <div id="adiv" width="1000" height="1000">
  <div id="info">
   <table>
    <tr>
     <td>关数 :</td>
     <td><span id="act">0</span></td>
    </tr>
    <tr>
     <td>分数 :</td>
     <td><span id="scoure">0</span></td>
    </tr>
   </table>
  </div>
 </div>
</body>

Vue.js 高频原理解析与 JavaScript 实例 本文详细解答了Vue.js的高频原理,包括响应式数据绑定、虚拟DOM和组件化开发。通过JavaScript代码示例,我们演示了如何在Vue.js中应用这些原理,实现了响应式数据绑定、虚拟DOM更新和组件化开发。当数据变化时,Vue.js首先创建一个新的虚拟DOM树,然后比较新旧虚拟DOM树的差异,并只更新需要更新的部分。Vue.js支持组件化的开发模式,将应用程序拆分为独立的组件,每个组件都有自己的状态和视图。在上面的示例中,我们使用了Vue.js的模板语法来定义一个包含数据绑定和事件绑定的视图。 阅读详情

相关推荐

从格雷码到多频外差:结构光相位展开实战指南(附Python仿真代码)

本文深入探讨了结构光三维测量中的相位展开技术,重点对比了格雷码和多频外差法在工业检测、逆向工程等领域的应用。通过详细的原理图解和Python仿真代码,展示了两种方法在精度、抗噪性和适用场景上的差异,为实际工程中的技术选型提供实用指南。

weixin_42535703的博客 433

js html示例,javascript 什么是实例?

实例,就是一个类的真实对象。比如我们都是“人”,你和我都是“人”类的实例了。而实例化就是创建对象的过程,用New关键字创建。什么是实例?书面语中,实例是实际的例子。在计算机语言中,“类”在实例化之后叫做一个“实例”。 “类”是静态的,不占进程内存,而“实例”拥有动态内存。在数据库中,代表一些程序的集合。如Oracle中,实例就是一些能支撑数据库运行的数据库程序。例子:function start(...

weixin_39556853的博客 334

C++游戏开发入门:从环境配置到第一个完整项目

C++ 作为游戏开发领域的关键语言,其学习路径常因语法枯燥而令初学者却步。以项目驱动的方式学编程,能帮助学习者将变量、循环、函数、指针等基础概念融入实际游戏逻辑中,获得即时反馈与完整项目经验。掌握合适的开发环境是高效实践的前提,例如在 Windows 下使用 VS Code 搭配 MinGW-w64 编译器,配合调试器与构建脚本,即可搭建轻量可扩展的 C++ 游戏开发工作流。此类课程通常面向无基础人群,从控制台小游戏逐步过渡到带游戏循环的完整作品,过程中还需关注性能观察、素材授权与版本管理。无论你是刚接触编

weixin_30621959的博客 540

js中的实例是什么,javascript小例子

在这种情况下,浏览器将访问 id=”demo” 的 HTML 元素,并把它的内容(innerHTML)替换为 “My First JavaScript”。JavaScript (JS)是属于网络的脚本语言,目前被数百万计的网页用来改进设计、验证表单、检测浏览器、创建cookies,以及更多的应用。与代数一样,JavaScript 变量可用于存放值(比如 x=2)和表达式(比如 z=x+y)。当向变量赋的值是数值时,不要使用引号。数组下标是基于零的,所以第一个项目是 [0],第二个是 [1],以此类推。

2301_79533350的博客 283

别再乱调栅极电阻了!手把手教你用示波器调试FOC驱动MOS管,避开EMC和震荡坑

本文详细介绍了FOC驱动中MOS管栅极参数的精准调试方法,涵盖示波器配置、波形特征分析及栅极电阻选型策略。通过实战案例和工程经验,帮助工程师有效解决EMC和震荡问题,提升驱动电路稳定性和效率。

weixin_28728495的博客 502

深入理解DQN中的Q值:核心概念和应用

在强化学习中,Q值(或称为动作价值函数)是核心概念之一。Q值函数 ( Q(s, a) ) 表示在给定状态 ( s ) 下,采取动作 ( a ) 并遵循某策略后,智能体从当前状态到未来状态所能获得的预期总回报。简言之,Q值是评估特定状态和动作对的好坏的指标。

qlkaicx的博客 2975

js实例

js实例 js实例 js实例 js实例

652

java三角套利_三角对冲套利-EA原理全解析

它是两个直盘货币和一个交叉盘货币之间的相互对冲,交易员往往受到外界因素的影响无法严格执行造成亏损。该系统经过大量数据统计分析和严格的科学论证计算,并通过多年实盘交易验证可以达到稳定盈利。希望交易员严格按照交易规则执行。此策略利用交叉外汇对的市场价格与其合成价格的价差来实现套利,还以EUR/USD和GBP为例:1,我们关注三个外汇对:EUR/USD,GBP/USD,以及GBP/EUR2,计算GBP/...

weixin_29531897的博客 2417

JavaScript实战 JavaScript、jQuery、HTML5、Node.js实例大全pdf

链接:https://pan.baidu.com/s/11Ff7jd5rnwKoilP9gK4ffw 密码:9xws 本书为完整版,以下为内容截图:  

qq_38460377的博客 819

SolidWorks2020详细步骤安装教程

1、在安装软件前请断开网络连接,需要安装.NETFramework3.5和4.0,最后还要确保把之前安装的旧版本完全卸载,以管理员身份运行SolidWorks_Flexnet_Server\server_remove.bat,然后等到服务“SolidWorksFlexnet服务器”将被删除。12、先不要运行软件,找到_SolidSQUAD_文件夹,将文件夹“ProgramFiles\SOLIDWORKSCorp”中的所有破解文件复制替到“ProgramFiles\SOLIDWORKSCorp”目录下;

qq_53545309的博客 1万+

2026年1至9月武汉市房地产价格深度分析报告

本报告基于2026年1月至9月武汉市实际成交案例,从成交价格、区域分布、户型结构、价格走势等维度进行深度分析。数据来源包括武汉市住房保障和房屋管理局备案数据、主要中介机构成交记录及典型楼盘网签案例,覆盖全市15个行政区及主要功能板块。

王国平的博客 321

php类实例化js,JavaScript类继承及实例化的方法_javascript技巧

本文实例讲述了JavaScript类继承及实例化的方法。分享给大家供大家参考。具体如下:(function(){var Class = {//扩展类create: function(aBaseClass, aClassDefine){var $class = function(){for(var member in aClassDefine){this[member] = aClassDefine...

weixin_42515906的博客 108

RK3588 C++ MeloTTS 推理

本文介绍了基于RK3588平台和RKNN工具链实现的端侧语音合成(TTS)系统。该系统支持中英文混合输入处理,通过自动合并英文单词、转换标点符号等方式预处理文本,并将其转换为音素和声调序列。核心推理流程包含编码器、中间处理和解码器三个阶段:编码器将音素序列转为隐藏状态,中间处理调节音调和语速,解码器生成最终波形。代码示例展示了输入数据设置和模型推理过程,利用RK3588内置NPU实现高效语音合成。该系统展现了在边缘计算设备上实现复杂AI任务的能力。

J。 199

JavaScript中Require调用js的实例

在我最初开始写 JavaScript 函数时,通常是这样的:  代码如下 function fun1() {   // some code here } function fun2() {   // some other code here } ... 函数全写在全局环境中,项目很小时,通常不会有什么冲突问题。 但代码多了后,

3万+

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

js调用java_如何从JavaScript调用Java实例的方法?

小编典典当要在范围内将java方法(静态或非静态)用作全局函数时,我们使用以下逻辑:FunctionObject javascriptFunction = new FunctionObject(/* String*/ javascriptFunctionName, /* Method */ javaMethod, /*Scriptable */ parentScope);boundScope.pu...

weixin_39955423的博客 998

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[JavaScript]原型与实例

文章目录构造函数 实例 原型三者的关系注意prototype属性属性或成员的搜索原则 构造函数 实例 原型三者的关系 1.任何函数都有一个prototype属性,该属性是一个对象 function F () {} console.log(F.prototype) // => object //原型对象 F.prototype.sayHi = function () { console.log('hi!') } 2.构造函数的prototype对象默认都有一个constructor属性,指向pro

程序员云锦的博客 3634

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

php类实例化js,JavaScript如何创建对象?实例化对象的方法

本篇文章给大家带来的内容是介绍JavaScript如何创建对象,让大家了解用js创建并实例化对象的三种方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。在语法方面上,JavaScript是一种灵活的面向对象语言。在我们介绍JavaScript创建并实例化对象的不同方法之前,我们需要注意的是JavaScript是一种无类语言,函数会以某种方式被使用,以便它们模拟一个类。【相关视频...

weixin_28907839的博客 180

php类实例化js,JavaScript类的定义和实例化

类的定义严格的说,JavaScript 是基于对象的编程语言,而不是面向对象的编程语言。在面向对象的编程语言中(如Java、C++、C#、PHP等),声明一个类使用 class 关键字。例如:public class Person{}但是在JavaScript中,没有声明类的关键字,也没有办法对类的访问权限进行控制。JavaScript 使用函数来定义类。语法:function className...

weixin_33595380的博客 175

简单的javascript例子,js中的实例是什么

Deepl降重。

wenangou的博客 108

基础 JavaScript 实例

基础 JavaScript 实例

m0_72763148的博客 927

[javascript]通过js获取cookie的实例及简单分析

今天review新人写的javascript代码的时候发现了很多的问题。这里以function getCookie(name){}为例。 其中比较典型的一个问题就是如何通过javascript获取cookie里面的一个值。 那么我们先来看看cookie到底长什么样子呢? 直接在浏览器地址栏输入: javascript:alert(document.cookie); 回车。(这行代码的意思是,让

null 1223

JavaScript简单实例

JavaScript (JS)是属于网络的脚本语言,目前被数百万计的网页用来改进设计、验证表单、检测浏览器、创建cookies,以及更多的应用。 初学JS,将自己学习过程中的简单实例代码放在这里,由于以前写过html,所以初次接触JS并不陌生,方便以后的学习和工作,现将简单实例在此列出: 一、简单实例: 1、通过指定的id来访问HTML元素并修改其内容(按钮触发): (1)代码<!DOCTY

Linda的博客 2万+

js调用java静态方法_如何从JavaScript调用Java实例的方法?

当一个java方法(无论是静态的还是非静态的)作为范围内的全局函数可用时,我们使用以下逻辑:FunctionObject javascriptFunction = new FunctionObject(/* String*/ javascriptFunctionName, /* Method */ javaMethod, /*Scriptable */ parentScope);boundScop...

weixin_39920338的博客 1007
上一篇: Java内存分析
下一篇: java内存分析、泄露、代码怎么更健壮
J2EE_dev
博客等级 码龄18年 237粉丝 94原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值