用OO方式写键盘字母小游戏
<html>
<head>
<title>0.0</title>
<script>
window.onload=function Test(){
alert("游戏提示,本游戏没有关卡限制,每次得分超过整百时,进入下一关,点击确定开始游戏吧!");
var game=new Game();
game.start();
}
var times;
var words;
var down=2;
var fen=100;
var createCharDiv=new Array();
function Game(){
//alert("coming game()");
words=new Array("A","B","C","D","E","F","G","H","I","J","K","L","N","M","O","P","Q","R","S","T","U","V","W","Z","X","Y");
var createBackground=new CreateBackground();
var createScore=new CreateScore();
//var createCharDiv=new CreateCharDiv();
this.start=function(){
for(var i=0;i<5;i++){
createCharDiv[i]=new CreateCharDiv();
}
times=setInterval(function(){
for(var i=0;i<5;i++){
createCharDiv[i].move();
}
},50);
document.onkeydown=keydown;
function keydown(e){
var real=String.fromCharCode(e.which);
//alert(real);
for(var i=0;i<5;i++){
if(createCharDiv[i].divHTML()==real){
//alert("zhong");
createScore.addScore();
createCharDiv[i].againTop();
}
}
}
}
}
function CreateBackground(){
//alert("coming CreateBackground()");
var newBackground;
function initBackground(){
//alert("coming initBackground()");
newBackground=document.createElement("div");
//alert(newBackground);
newBackground.style.width="500px";
newBackground.style.height="600px";
newBackground.style.border="3px solid black";
newBackground.style.position="absolute";
//newBackground.style.background="pink";
document.body.appendChild(newBackground);
}
initBackground();
}
function CreateScore(){
//alert("1");
var scoreboard;
var score=0;
function initScore(){
//alert("2");
scoreboard=document.createElement("div");
scoreboard.style.width="100px";
scoreboard.style.height="20";
scoreboard.style.top="10px";
scoreboard.style.left="405px";
scoreboard.style.position="absolute";
scoreboard.style.border="3px solid black";
scoreboard.style.background="green";
scoreboard.innerHTML="得分:"+score;
document.body.appendChild(scoreboard);
//alert(scoreboard);
}
initScore();
this.addScore=function(){
score+=10;
scoreboard.innerHTML="";
scoreboard.innerHTML="得分:"+score;
if(score==fen){
alert("亲,即将进入下一关,请做好准备!");
down++;
fen+=100;
//alert("fen"+fen);
for(var i=0;i<5;i++){
createCharDiv[i].againTop();
}
}
}
}
function CreateCharDiv(){
//alert("CreateCharDiv()");
var newDiv;
var numbs=0;
function initCharDiv(){
//alert("initCharDiv()");
num=parseInt(Math.random()*words.length);
//alert(words.length);
newDiv=document.createElement("div");
newDiv.style.width="30px";
newDiv.style.height="30px";
newDiv.style.border="1px solid black";
newDiv.style.position="absolute";
newDiv.style.top="20px";
//规定范围内取随机数 *(上限-下限+1)+下限
//newDiv.style.left=parseInt(Math.random()*480)+"px";
newDiv.style.left=parseInt(Math.random()*(470-20+1)+20)+"px";
newDiv.style.background="red";
newDiv.innerHTML=words[num];
document.body.appendChild(newDiv);
}
initCharDiv();
this.move=function(){
//alert("move()");
newDiv.style.top=parseInt(newDiv.style.top)+down;
if(parseInt(newDiv.style.top)>580){
clearInterval(times);
alert("oh oh oh , game over !");
}
}
this.divHTML=function(){
return newDiv.innerHTML;
}
this.againTop=function(){
newDiv.style.display="none";
initCharDiv();
}
}
</script>
</head>
<body>
</body>
</html>
用OO方式写键盘字母小游戏的更多相关文章
- C语言写的2048小游戏
基于"基于C_语言的2048算法设计_颜冠鹏.pdf" 这一篇文献提供的思路 在中国知网上能找到 就不贴具体内容了 [摘 要] 针对2048的游戏规则,分析了该游戏的算法特点,对其 ...
- 用面向对象的编程方式实现飞机大战小游戏,java版
概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...
- 几款用jQuery写的h5小游戏
人人都说前端用来做游戏是一件很困难的事情,遇到这些js的逻辑性问题,是不是有点懵?其实,做完一款游戏之后就会发现,没啥难的地方,差不多都是换汤不换药,作为爱玩游戏的我,也总结收集了几款比较流行的小软件 ...
- C/C++编程笔记:C语言写推箱子小游戏,大一学习C语言练手项目
C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...
- 用python+pygame写贪吃蛇小游戏
因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...
- 无聊的周末用Java写个扫雷小游戏
周末无聊,用Java写了一个扫雷程序,说起来,这个应该是在学校的时候,写会比较好玩,毕竟自己实现一个小游戏,还是比较好玩的.说实话,扫雷程序里面核心的东西,只有点击的时候,去触发更新数据这一步. Sw ...
- 原生js写的flybird小游戏
游戏地址:http://zangzhihong.jusukeji.com/flybird/index.html html部分 <!DOCTYPE html> <!-- This ...
- 利用while循环写的简单小游戏猜数字
猜数字的大小游戏 C:\Users\Administrator>python Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:1 ...
- HttpWeb服务器之--用OO方式写
虽然写的不是很好,但 最终解释权以及版权归13东倍所有! package com.web; import java.io.IOException; public class Test { public ...
随机推荐
- Fedora 开启 ssh
Fedora 17 已经安装好openssh server了 不用再装 不过默认无开启 首先su root1.开启ssh服务# systemctl start sshd.service 2.随系统一起 ...
- C#.NET Split 的几种使用方法
第一种方法: string s = "abcdeabcdeabcde"; string[] sArray = s.Split('c'); foreach (string i in ...
- canvas总结:线段宽度与像素边界
在canvas中,我们经常需要绘制线段,主要使用moveTo和lineTo两个方法,moveTo移动至线段起始点,lineTo将线段绘制至终点.同时,绘制线段时可以指定线段的宽度,使用lineWidt ...
- js数组的操作<转>
转自 http://blog.csdn.net/xcxinghai/article/details/13502583 PS(个人理解): 1) 数组项的数据类型可以是混合多样的,同时可以含string ...
- cURL模拟POST方式提交数据
curl_post.php文件: 1 $url = 'http://localhost/test/curl_post_deal.php'; 2 3 $post_data = array( 4 'use ...
- php实现多表(四表)连接
<?php include_once "DBHelper.php"; define('HOST', '127.0.0.1'); define('USER', 'root'); ...
- Topo软件
http://jung.sourceforge.net/#! http://www.netdisco.org/ http://sourceforge.net/projects/toponet/ htt ...
- Java String.replace()方法
Java String.replace()方法用法实例教程, 返回一个新的字符串,用newChar替换此字符串中出现的所有oldChar 声明 以下是java.lang.String.replace( ...
- Eclipse:使用findBugs预先检测错误
FindBugs是用于Java的另一种静态分析工具,它在某些方面与Checkstyle和PMD类似,但是侧重点不同.FindBugs不关心格式或编码标准,对最佳实践也不太感兴趣:事实上,它专注于检查潜 ...
- hdu4740【杭州网赛、模拟、有点搜索?】
当时看了这题就感觉so easy... 本来不想写的,后来感觉是不是可以练一下搜索水平.. 比赛时有人过了就没写. 比赛完了写一下. 实现还不是那么顺利, 囧 本来自己以为这题能练下搜 ...