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是用的比较多也比较复杂的一个,简单的入门了一下, ...
随机推荐
- 【推理】UVa 10771 - Barbarian tribes
Barbarian tribes In a lost land two primitive tribes coexist: Gareds and Kekas. Every summer sols ...
- mongodb添加权限
1.连接mongodb数据库(如果mongo命令没有做环境变量配置,需要定位到有mongo命令的目录) root@AY140709212620347s22Z:~# mongo MongoDB shel ...
- 如何解决linux(ubuntu/CENTOS)中gedit中文乱码的问题
http://jingyan.baidu.com/article/1709ad80a443c54634c4f09c.html 同时按键盘的Alt 和 F2,就可以打开“运行程序”对话框,这个功能类似于 ...
- gluster 安装配置基本指南
基于网络上的多篇文章,做了一些调整. gluster安装 ### Installing Gluster wget -P /etc/yum.repos.d http://download.gluste ...
- (一)问候Spring4
第一节:Spring 简介 Spring 作者:Rod Johnson: 官方网站:http://spring.io/ 最新开发包及文档下载地址:http://repo.springsource.or ...
- 轮子来袭 vJine.Core 之 AppConfig<T>
1.引用vJine.Core; 2.定义配置类; using System; using System.Collections.Generic; using System.Text; using Sy ...
- 用pelican搭建完美博客
前面有文章介绍本站采用了Python编写的Pelican静态生成博客系统, 之所以没有使用当前很火的Jekyll, 是因为它是Ruby编写, 而我又对Ruby没有啥兴趣, 所以还是选择了使用了我熟悉的 ...
- QT/C++ 智能指针
什么是智能指针? 为什么用智能指针? 还有哪些关于内存管理方面的知识点,需要注意的?
- OpenJudge / Poj 1928 The Peanuts C++
链接地址:http://bailian.openjudge.cn/practice/1928 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 Mr. Robinson and h ...
- 简单模拟struts2核心控制器利用反射原理实现参数传递和物理视图跳转
在能够运用struts2框架进行简单网站开发后,对struts2的一些较原框架强大的功能希望有更深刻的理解,于是尝试从底层开始摸索,本文就在重新学习struts2后,利用简单代码对核心控制器的主要功能 ...