iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >非html5实现js版弹球游戏示例代码
  • 749
分享到

非html5实现js版弹球游戏示例代码

html5js弹球游戏 2022-11-15 23:11:18 749人浏览 八月长安
摘要

开始前的html页面  开始后的html游戏界面  html页面布局,即index.html文件源码如下: <!DOCTYPE HTML PUBLIC "

开始前的html页面
 
开始后的html游戏界面
 
html页面布局,即index.html文件源码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "Http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>弹球游戏</title>
<link rel="stylesheet" type="text/CSS" href="css/index.css"/>

</head>

<body>
<center>
<div id="gamePanel" tabindex="0">
<div class="score">分数:
<span id="score">0</span>
</div>
<div id="startBtn" onclick="Start()"></div>
</div>
</center>
<script type="text/javascript" src="js/magic.js"></script>
<script type="text/javascript" src="js/brick.js"></script>
<script type="text/javascript" src="js/ball.js"></script>
<script type="text/javascript" src="js/stick.js"></script>
<script type="text/javascript" src="js/game.js"></script>
</body>
</html>

index.css文件源码如下:

#gamePanel{

width:504px;
height:504px;
background:Black;
position:relative;

}

#gamePanel .score{

font-size:20px;
color:White;
position:absolute;
left:0;
top:0;
z-index:9999;

}

#gamePanel .bullet{

width:5px;
height:15px;
position:absolute;
background:url(../img/bullet.png);
overflow:hidden;

}

#gamePanel .stick{

width:80px;
height:18px;
position:absolute;
background:blue;

}

#gamePanel .ball{

width:15px;
height:15px;
position:absolute;
background:url(../img/ball.gif);

}

#gamePanel .brick {

width : 28px;
height : 28px;
position : relative;
background : url(../img/brick.gif);
float : left;

}

#gamePanel .hideBrick {

width : 28px;
height : 28px;
position : relative;
background : black;
float : left;

}

#gamePanel .magic {

width : 27px;
height : 11px;
position : absolute;
background : green;

}

#gamePanel .shortMagic {

width : 28px;
height : 12px;
position : absolute;
background : yellow;

}

#gamePanel .binGo{

width:18px;
height:18px;
position:absolute;
background:url(../img/bingo2.png);

}

#startBtn{

border-width:20px;
border-style:solid;
border-color:Black Black Black Green;
position:absolute;
left:240px;
top:240px;
cursor:pointer;
width:0px;
height:0px;
overflow:hidden;

}

JavaScript部分分为5个源文件,即ball.js(球类)、brick.js(砖类)、game.js(游戏类)、magic.js(魔法棒类)、stick.js(挡板类)

球类代码实现如下:

// 球类
var Ball = function() {

// 弹球dom元素
this.dom = null;

// 是否激活
this.isFirst = true;

// 弹球移动方向
this.direction = null;

this.init();

}

Ball.prototype = {

// 弹球横向移动速度
movepx : 3,

// 弹球纵向移动速度
movepy : 2,

// 弹球移动频率
movesp : 20,

// 弹球移动频率映射
movespMap : {

1 : 75,
2 : 65,
3 : 50,
4 : 40

},

// 初始化
init : function() {

this.dom = document.createElement("div");
this.dom.className = "ball";

},

// 设置弹球的初始化位置,x与y坐标
setPosition : function(x, y) {

this.dom.style.left = x + "px";
this.dom.style.top = y + "px";

},

// 弹球动画,就是移动,传入参数为游戏背景的宽与高
animation : function(gameWidth, gameHeight, stick) {

var _this = this;

// 实际的横向移动速度,左或者右
var _movepx = this.dom.offsetLeft > gameWidth/2 ? -1*this.movepx : this.movepx;
var _movepy = this.dom.offsetTop > gameHeight/2 ? this.movepy : -1*this.movepy;

// 处理移动函数
var process = function() {

// 弹球的x,y坐标
var left = _this.dom.offsetLeft;
var top = _this.dom.offsetTop;

// 是否要调转方向
if (left <= 0 || left >= gameWidth - _this.dom.clientWidth) {

_movepx *= -1;

}

var isCrashStick = _this.OnCheckCrashStick();
var isCrashBall = _this.OnCheckCrashBrick();

// 判断是否想上调转方向
if (top < 0 || isCrashStick || isCrashBall) {

_movepy *= -1;

}

// 向下移动
top = top + _movepy;
left = left + _movepx;

// 设置弹球位置
_this.dom.style.top = top + "px";
_this.dom.style.left = left + "px";

if(top > gameHeight) {

_this.onend();
alert("You Lose");

} else {

setTimeout(process, _this.movesp);

}

// 判断弹球移动方向
if (_movepx > 0 && _movepy < 0) {

_this.direction = "RightUp";

return;

}

if (_movepx > 0 && _movepy > 0) {

_this.direction = "RightDown";

return;

}

if (_movepx < 0 && _movepy < 0) {

_this.direction = "LeftUp";

return;

}

if (_movepx < 0 && _movepy > 0) {

_this.direction = "LeftDown";

return;

}

};

// 开始移动
process();

},

// 外部接口,检测是否撞到魔法棒
OnCheckCrashStick : function() {},

// 外部接口,检测是否撞到砖块
OnCheckCrashBrick : function() {},

// 弹球结束事件
onend : function() {},

// 游戏结束
gameover : function() {}

}

砖类代码如下brick.js源文件:

// 砖类
var Brick = function(gamePanel) {

// 砖的dom元素
this.dom = null;

// 砖块所在的画布
this.gamePanel = gamePanel;

// 是否激活
this.isLive = true;

// 是否带有魔法棒
this.magic = null;

this.width = 28;
this.height = 28;

this.left = 0;
this.top = 0;

this.init();

}

Brick.prototype = {

// 初始化
init : function() {

this.dom = document.createElement("div");
this.dom.className = "brick";

},

// 为position: relative的Brick初始化位置
setPosition : function(x, y) {

this.left = x;
this.top = y;

},

// 为positon : relative的Brick初始化尺寸
setSize : function(width, height) {

this.width = width;
this.height = height;

},

// 初始化生成魔法棒
initMagic : function() {

var _this = this;
// 随机数
var random = parseInt(Math.random()*1000 + 1, 10);

var type = random % 5 == 0 ? "good" : random % 4 == 0 ? "bad" : "none";

// 新建一个魔法棒对象
var magic = new Magic(type);

this.magic = magic;

magic.initPosition(this);

// 将魔法棒添加进砖块中
this.gamePanel.appendChild(magic.dom);

magic.onEnd = function() {

_this.gamePanel.removeChild(magic.dom);

};

magic.animation(this.gamePanel.clientHeight);

},

// 击中后的动作
onEnd : function() {

this.isLive = false;
this.dom.className = "hideBrick";
this.initMagic();

}

}

魔法棒类代码即magic.js源文件实现如下:

// 魔法棒类
var Magic = function(type) {

// Magic的dom元素
this.dom = null;

// Magic的dom信息
this.left = 0;
this.top = 0;
this.width = 0;
this.height = 0;

this.type = type;

this.init();

}

Magic.prototype = {

// 魔法棒类型
magicType : {

"good" : "magic",
"bad" : "shortMagic",
"none" : ""

},

// 每次移动位移
movepy : 3,

// 移动速度
movespeed : 20,

// 初始化魔法棒
init : function() {

this.dom = document.createElement("div");

this.dom.className = this.magicType[this.type];
//this.dom.style.display = "none";

this.width = parseInt(this.dom.style.width, 10);
this.height = parseInt(this.dom.style.height, 10);

},

// 魔法棒初始化位置
initPosition : function(brick) {

this.left = brick.left;
this.top = brick.top;

this.dom.style.left = this.left + "px";
this.dom.style.top = this.top + "px";

},

// 更新位置
update : function() {

this.dom.style.left = this.left + "px";
this.dom.style.top = this.top + "px";

},

// 魔法棒动画,height为游戏背景高度
animation : function(height) {

if (this.type == "none") {

return;

}

var _this = this;

// 向下移动函数
var downMove = function() {

_this.top = _this.top + _this.movepy;
_this.update();

// 判断魔法棒下移是否越界,是否击中stick
if (_this.top < height && !_this.isBeatStick()) {

setTimeout(downMove, _this.movespeed);

} else {

// 动画结束触发事件
_this.onEnd();

}

};

downMove();

},

// 动画结束触发事件,外部覆盖
onEnd : function() {},

// 魔法棒是否击中挡板以及击中后处理事件,外部覆盖
isBeatStick : function() {}

}

挡板类代码即stick.js源文件如下:

// 新建棒类
var Stick = function() {

// 飞机对应的dom元素
this.dom = null;

// 是否移动中
this.isMove = false;

// 移动的ID
this.moveId = null;

// 是否弹球中
this.isSend = false;

// 变大标记
this.biGCount = 0;

// 变小标记
this.smallCount = 0;

// 接棒的宽度变大变小时做存储
this.width = 0;

this.init();

}

Stick.prototype = {

// 游戏背景Dom
gamePanel : null,

// 游戏背景宽度
gameWidth : 0,

// 游戏背景高度
gameHeight : 0,

// 魔法棒移动速度
movepx : 10,

// 魔法棒移动频率
movesp : 30,

// 方向键值对应
keyCodeAndDirection : {

37 : "left",
39 : "right"

},

// 初始化
init : function() {

this.dom = document.createElement("div");
this.dom.className = "stick";

},

// 设置位置
setPosition : function(gamePanel, width, height) {

// 将魔法棒添加进游戏背景中
this.gamePanel = gamePanel;
this.gamePanel.appendChild(this.dom);

// 设置飞机的初始位置
this.dom.style.left = (width - this.dom.clientWidth)/2 + "px";
this.dom.style.top = height - this.dom.clientHeight + "px";

// 获取到游戏背景的宽和高
this.gameWidth = width;
this.gameHeight = height;

},

// 键盘按下事件
keydown : function(e) {

var keyCode = e.keyCode;

if (!this.isMove) {

this.move(keyCode);

}

},

// 键盘释放事件
keyup : function(e) {

// 判断是否为键盘释放
if (this.keyCodeAndDirection[e.keyCode]) {

// 停止移动
this.stopMove();

} else if (e.keyCode == 32) {

// 设置为非发弹中
this.isSend = false;

}

},

// 移动
move : function(keyCode) {

// 设置为移动中
this.isMove = true;
var _this = this;

// 判断移动方向
switch(this.keyCodeAndDirection[keyCode]) {

case "left" : {

this.moveId = setInterval(function() {_this.moveLeft();}, _this.movesp);
break;

}

case "right" : {

this.moveId = setInterval(function() {_this.moveRight();}, _this.movesp);
break;

}

default : break;

}

},

// 向左移动
moveLeft : function() {

var left = this.dom["offsetLeft"];
left = left - this.movepx >= 0 ? left - this.movepx : 0;
this.dom.style["left"] = left + "px";

if (left == 0) {

this.stopMove();

}

},

// 向右移动
moveRight : function() {

var left = this.dom["offsetLeft"];
var maxDistance = this.gameWidth - this.dom.clientWidth;
left = left + this.movepx <= maxDistance ? left + this.movepx: maxDistance;
this.dom.style["left"] = left + "px";

if (left == maxDistance) {

this.stopMove();

}

},

// 变小
changeSmall : function() {

if (this.smallCount >= 1) {

return;

} else {

this.dom.style.width = 80 + "px";
this.smallCount ++;

this.bigCount >= 1 ? this.bigCount -- : this.bigCount + 0;

}

this.dom.style.left = parseInt(this.dom.style.left, 10) + 20 + "px";
this.dom.style.width = 40 + "px";

},

// 变大
changeBig : function() {

if (this.bigCount >= 1) {

return;

} else {

this.dom.style.width = 80 + "px";
this.bigCount ++;

this.smallCount >= 1 ? this.smallCount -- : this.smallCount + 0;

}

if (parseInt(this.dom.style.left, 10) <= 75 ) {

this.dom.style.width = parseInt(this.dom.style.width, 10) + 75 + parseInt(this.dom.style.left, 10)+ "px";
this.dom.style.left = 0 + "px";

return;

} else if (this.dom.style.width + 150 + parseInt(this.dom.style.left, 10) >= this.gamePanel.clientWidth) {

this.dom.style.left = parseInt(this.dom.style.left, 10) - 150 + "px";
this.dom.style.width = this.dom.style.width + 150 + "px";

return;

} else {

this.dom.style.left = parseInt(this.dom.style.left, 10) - 75 + "px";
this.dom.style.width = 150 + "px";

}

},

// 停止移动
stopMove : function() {

this.isMove = false;
clearInterval(this.moveId);

},

// 发射弹球,外部接口,
onSendBall : function() {},


// 改分数外部接口
onChangeScore : function() {}

}

部分难点技术实现

通过键盘左右方向键移动挡板的代码实现:

// 键盘按下事件
keydown : function(e) {

var keyCode = e.keyCode;

if (!this.isMove) {

this.move(keyCode);

}

},

// 键盘释放事件
keyup : function(e) {

// 判断是否为键盘释放
if (this.keyCodeAndDirection[e.keyCode]) {

// 停止移动
this.stopMove();

} else if (e.keyCode == 32) {

// 设置为非发弹中
this.isSend = false;

}

},

// 移动
move : function(keyCode) {

// 设置为移动中
this.isMove = true;
var _this = this;

// 判断移动方向
switch(this.keyCodeAndDirection[keyCode]) {

case "left" : {

this.moveId = setInterval(function() {_this.moveLeft();}, _this.movesp);
break;

}

case "right" : {

this.moveId = setInterval(function() {_this.moveRight();}, _this.movesp);
break;

}

default : break;

}

},

// 向左移动
moveLeft : function() {

var left = this.dom["offsetLeft"];
left = left - this.movepx >= 0 ? left - this.movepx : 0;
this.dom.style["left"] = left + "px";

if (left == 0) {

this.stopMove();

}

},

// 向右移动
moveRight : function() {

var left = this.dom["offsetLeft"];
var maxDistance = this.gameWidth - this.dom.clientWidth;
left = left + this.movepx <= maxDistance ? left + this.movepx: maxDistance;
this.dom.style["left"] = left + "px";

if (left == maxDistance) {

this.stopMove();

}

},

--结束END--

本文标题: 非html5实现js版弹球游戏示例代码

本文链接: https://www.lsjlt.com/news/172752.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 非html5实现js版弹球游戏示例代码
    开始前的html页面  开始后的html游戏界面  html页面布局,即index.html文件源码如下: 复制代码 代码如下: <!DOCTYPE HTM...
    99+
    2022-11-15
    html5 js 弹球游戏
  • 使用非html5实现js板连连看游戏示例代码
    向大家分享一款如何实现js版连连看游戏,如下图所示: 首先看一下html的布局方式在index.html文件中: 复制代码 代码如下: <!DOCTYPE html PUBLI...
    99+
    2022-11-15
    html5 js 连连看游戏
  • JS+Canvas实现接球小游戏的示例代码
    目录写在最前git地址成果展示实现思路详细说明写在最后写在最前 看了canvas的动画系列,已经抑制不住内心的冲动想写个小游戏了,还是那个套路——多写写,你才...
    99+
    2024-04-02
  • html5实现弹跳球小游戏
    这篇文章主要介绍“html5实现弹跳球小游戏”,在日常操作中,相信很多人在html5实现弹跳球小游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”html5实现弹跳球小游戏”...
    99+
    2024-04-02
  • VUE+Canvas 实现桌面弹球消砖块小游戏的示例代码
    大家都玩过弹球消砖块游戏,左右键控制最底端的一个小木板平移,接住掉落的小球,将球弹起后消除画面上方的一堆砖块。 那么用VUE+Canvas如何来实现呢?实现思路很简单,首先来拆分一下...
    99+
    2024-04-02
  • javaScript实现网页版的弹球游戏
    利用javeScript对象以及方法实现的网页弹球游戏,供大家参考,具体内容如下 <!DOCTYPE html> <html> <head> ...
    99+
    2024-04-02
  • javaScript如何实现网页版弹球游戏
    这篇文章给大家分享的是有关javaScript如何实现网页版弹球游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下<!DOCTYPE html><html><h...
    99+
    2023-06-20
  • HTML+JS实现猜拳游戏的示例代码
    目录效果图关于JS构建过程添加事件监听器函数 gameRules()函数 whoWon()效果图 游戏可以通过这个链接进入 关于JS构建过程 首先,我创建了一个对象,其中包含每种可...
    99+
    2024-04-02
  • JS实现简单打砖块弹球小游戏
    本文实例为大家分享了JS实现打砖块弹球小游戏的具体代码,供大家参考,具体内容如下 使用原生JS写的,还有一点瑕疵。代码直接复制到html就能使用 速度随机的 因为设涉及横向和纵向速度...
    99+
    2024-04-02
  • Java实现warcraft java版游戏的示例代码
    目录前言主要需求功能截图代码实现启动入口ModelAttacker类ModelUnit类总结前言 致敬经典的warcraft,《warcraft java版》是一款即时战略题材单机游...
    99+
    2024-04-02
  • 基于JS实现Flappy Bird游戏的示例代码
    前言 Flappy Bird 是一款无尽的游戏,玩家可以控制一只鸟。玩家必须保护小鸟免于与管道等障碍物相撞。每次小鸟通过管道时,分数都会增加一。当小鸟与管道碰撞或因重力而坠落时,游戏...
    99+
    2024-04-02
  • JS实现别踩白块游戏的示例代码
    目录实现思路核心代码HTML代码CSS代码JS代码实现思路 1、offsetTop,与style.top 2、我们看到的是白块在向下运动,其实不是,政治运动的是装着白块的盒子,白块...
    99+
    2024-04-02
  • Python实现生命游戏的示例代码(tkinter版)
    目录生命游戏(Game of Life)游戏概述生存定律图形结构代码实现运行界面使用简介后续改进生命游戏(Game of Life) 由剑桥大学约翰·何顿·...
    99+
    2024-04-02
  • 原生JS实现H5转盘游戏的示例代码
    目录1.基础的页面布局(index.html)1.1html布局1.2css布局(style.css)2.工具函数(用于调整概率)3.传参及接收值配置4.dom操作方法及具体逻辑处理...
    99+
    2024-04-02
  • JS实现飞机大战小游戏的示例代码
    小编给大家分享一下JS实现飞机大战小游戏的示例代码,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!先制作好要做好的几步以及背景样式var canvas = document.getElement...
    99+
    2023-06-15
  • 基于Unity实现3D版2048游戏的示例代码
    分享三个无聊的时候用Unity写的小游戏 包含 2048 2D版本和3D版本 Voodoo的小游戏 Sticky block 开源仓库: https://gitee.com/wel...
    99+
    2023-02-02
    Unity实现2048游戏 Unity 2048游戏 Unity 2048 Unity 游戏
  • 基于JS实现接粽子小游戏的示例代码
    目录游戏设计游戏实现添加粽子元素粽子掉落难度选择开始游戏总结端午节马上就到了,听说你们公司没发粽子大礼包?没关系,这里用 JS 实现了一个简单的接粽子小游戏,能接到多少粽子,完全看你...
    99+
    2024-04-02
  • 基于JS实现飞机大战游戏的示例代码
    目录演示技术栈源码定义敌方战机定义我方战机碰撞检测演示 技术栈 今天没有什么特别要讲的,要不我们提前介绍下次要做的技术吧。你不说话就是同意了。我们开始了。 下图是正则表达式的一些总...
    99+
    2024-04-02
  • 基于JS实现的消消乐游戏的示例代码
    目录前言游戏的准备工作总结一下棋盘渲染画面动画效果genCollapse()genDownfall()genEmerge()整合效果genLoop()genSwap()前言 一直对小...
    99+
    2024-04-02
  • Pygame实现简易版趣味小游戏之反弹球
    目录导语一、准备中1)游戏规则2)素材准备3)环境安装二、敲代码1)配置文件2)设置球的反弹、移动规则3)设置球拍电脑的移动等4)设置游戏开始界面5)定义游戏结束页面6)运行游戏De...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作