canvas 乒乓球
<!DOCTYPE html>
<html>
<head>
<title>Bouncing Ball With inputs</title>
<style type="text/css">
form {
width: 330px;
margin: 20px;
background-color: brown;
padding: 20px;
}
</style>
<script type="text/javascript">
var boxx = 0;
var boxy = 0;
var boxwidth = 350;
var boxheight = 250;
var ballrad = 10;
var boxboundx = boxwidth + boxx - ballrad;
var boxboundy = boxheight + boxy - ballrad;
var inboxboundx = boxx + ballrad;
var inboxboundy = boxy + ballrad;
var ballx = 50;
var bally = 60;
var ctx;
var ballvx = 4;
var ballvy = 8; var step = 10;
var isRight = false; var pai = {
width: 2,
height: 80,
position: {
x: 60,
y: 80
}
}
var inter;
function init() {
ctx = document.getElementById("canvas").getContext("2d");
document.body.onkeydown = function (e) {
var up = 38;
var down = 40; if (e.keyCode == up) {
if (pai.position.y > 0) {
pai.position.y -= step;
} } else if (e.keyCode == down) {
if (pai.position.y < boxheight - pai.height) {
pai.position.y += step;
}
} } ctx.lineWidth = ballrad;
ctx.fillStyle = "rgb(200,0,50)";
moveball(); inter = setInterval(moveball, 100);
}
function moveball() {
ctx.clearRect(boxx, boxy, boxwidth, boxheight);
moveandcheck();
ctx.beginPath();
ctx.arc(ballx, bally, ballrad, 0, Math.PI * 2, true);
if (ballx > (pai.position.x + pai.width)) {
isRight = true;
} else if (ballx < (pai.position.x - pai.width)) {
isRight = false;
}
console.log(isRight);
ctx.rect(pai.position.x, pai.position.y, pai.width, pai.height);
ctx.fill();
ctx.strokeRect(boxx, boxy, boxwidth, boxheight);
} function moveandcheck() {
var nballx = ballx + ballvx;
var nbally = bally + ballvy; if (nballx > boxboundx) {//碰到右边的墙
ballvx = -ballvx;
nballx = boxboundx;
}
if (nballx < inboxboundx) {//碰到左边的墙
nballx = inboxboundx;
ballvx = -ballvx;
}
if (nbally > boxboundy) {//碰到下面的墙
nbally = boxboundy;
ballvy = -ballvy;
} if (nbally < inboxboundy) {//碰到上面的墙
nbally = inboxboundy;
ballvy = -ballvy;
} if ((isRight && (nballx < pai.position.x + ballrad) && (nbally < pai.position.y || nbally > pai.position.y + pai.height))) {
clearInterval(inter);
alert('游戏结束l');
} //右边过来
if (isRight && (nballx < pai.position.x + ballrad) && (nbally > (pai.position.y) && nbally < pai.position.y + pai.height)) { ballvx = -ballvx; }
//左边过来
else if (!isRight && (nballx > pai.position.x - ballrad) && (nbally > pai.position.y && nbally < pai.position.y + pai.height)) { ballvx = -ballvx;
nballx = pai.position.x - ballrad; }
ballx = nballx;
bally = nbally; }
function change() {
ballvx = Number(f.hv.value);
ballvy = Number(f.vv.value);
return false;
} </script>
</head>
<body onload="init()"> <td>
<table align=center background="a.jpg">
<td> <canvas id="canvas" width="400" height="300">不支持canvas</canvas>
<br/> <form name="f" id="f" onsubmit="return change();">
Horizontal velocity <input name="hv" id="hv" value="4" type="number" min="-10" max="10"/><br/>
Vertical velocity <input name="vv" id="vv" value="8" type="number" min="-10" max="10"/>
<input type="submit" value="Change"/>
</form>
</body> </html>
canvas 乒乓球的更多相关文章
- Android使用学习之画图(Canvas,Paint)与手势感应及其应用(乒乓球小游戏)
		作为一个没有学习Android的菜鸟,近期一直在工作之外努力地学习的Android的使用. 这周看了下Android的画图.主要是Canvas,Paint等,感觉须要实践下.下午正好有空,就想整一个乒 ... 
- html5 乒乓球(碰撞检测)
		演示地址 http://koking.8u.hanmandarin.com/html5/1.html 简单介绍 小球可以在方框内部自由运动 可以通过方向键控制黑色砖块上下左右移动去与小球发生碰撞 代码 ... 
- 【带着canvas去流浪(8)】碰撞
		目录 一. canvas的能力 二. 动画框架 三. 在canvas中模拟碰撞 3.1定义小球的属性 3.2 生成新的小球 3.3 帧动画绘制函数step 3.4 定义小球的update方法 3.5 ... 
- 『Python Kivy』官方乒乓球游戏示例解析
		本篇文章用于对Kivy框架官方所给出的一个「乒乓球」小游戏的源码进行简单地解析.我会尽可能的将方方面面的内容都说清楚.在文章的最下方为官方所给出的这个小游戏的教程以及游戏源码. 由于篇幅所限,本文只简 ... 
- 带着canvas去流浪系列之八 碰撞
		[摘要] canvas动画-碰撞仿真 示例代码托管在:http://www.github.com/dashnowords/blogs 经过前面章节相对枯燥的练习,相信你已经能够上手canvas的原生A ... 
- 带着canvas去流浪系列之八 碰撞【华为云技术分享】
		[摘要] canvas动画-碰撞仿真 示例代码托管在:http://www.github.com/dashnowords/blogs 经过前面章节相对枯燥的练习,相信你已经能够上手canvas的原生A ... 
- html5 canvas常用api总结(三)--图像变换API
		canvas的图像变换api,可以帮助我们更加方便的绘画出一些酷炫的效果,也可以用来制作动画.接下来将总结一下canvas的变换方法,文末有一个例子来更加深刻的了解和利用这几个api. 1.画布旋转a ... 
- 【探索】利用 canvas 实现数据压缩
		前言 HTTP 支持 GZip 压缩,可节省不少传输资源.但遗憾的是,只有下载才有,上传并不支持.如果上传也能压缩,那就完美了.特别适合大量文本提交的场合,比如博客园,就是很好的例子. 虽然标准不支持 ... 
- 简单入门canvas - 通过刮奖效果来学习
		一 .前言 一直在做PC端的前端开发,从互联网到行业软件.最近发现移动端已经成为前端必备技能了,真是不能停止学习.HTML5新增的一些东西,canvas是用的比较多也比较复杂的一个,简单的入门了一下, ... 
随机推荐
- Elasticsearch aggregations API
			聚合能力 Aggregation API 类似 SQL 中的 GROUP BY 语句,可以以某个字段来进行分组. Aggregation API 支持分级分组,多级的分组过程是由外到里的. Aggre ... 
- NoSQL选型
			传统“关系型数据库”在应付互联网WEB2.0应用已显示的力不从心,由其是超大规模和高并发的SNS类型的WEB2.0网站.主要需要应对以下三方面难题: 1.对数据库高并发读写的要求. 2.对数据库高可扩 ... 
- wordpress 提取头像的src
			获取用户头像,可以通过 $avatar_html = get_avatar( $email ); 获取到头像的html /** * Retrieve the avatar url for a user ... 
- js内置对象处理-打印学生成绩单
			效果图: 任务: 1.通过js的内置对象得到当前日期 var date=new Date(); var year=date.toString().slice(11,15); document.writ ... 
- dirname(__FILE__) 的使用总结
			dirname(__FILE__) php中定义了一个很有用的常数,即 __file__ 这个内定常数是当前php程序的就是完整路径(路径+文件名). 即使这个文件被其他文件引用(include或re ... 
- <你不知道的JavaScript>读书笔记
			近几天看了一本不错的 JavaScript 的书,是 Kyle Simpson 写的 <You Don't know JS>.这本书是 Kyle Simpson 在 Github 上的开源 ... 
- C#中调用存储过程
			[csharp] view plain copy print? string strsql = "Data Source=192.168.24.53;Initial Catalog=JF_C ... 
- iOS自学之NSOperation、NSOperationQueue、Background
			iOS中多线程编程主要分为NSThread.NSOperation和GCD,今天主要记录下自己在学习NSOperation中的点滴-如有不对的地方帮忙指出下,PS:人生第一次写blog,各位看官请轻虐 ... 
- 07_控制线程_join_线程插队
			[join线程简述] join()方法:Thread提供的让一个线程去等待另一个线程完成.当在某个程序执行流中(如main线程)调用其它线程(如t2线程)的join方法(t2.join()),调用线程 ... 
- bat里如何用相对路径
			在bat中直接使用绝对路径没有问题,但是文件传到其他地方时,绝对路径会发生改变,因此想通过使用相对路径来解决. 可以通过在bat获取当前bat所在的目录,然后cd 该目录来解决该问题 在bat前面增加 ... 
