console
var canvas = document.getElementById("canvas")
var context = canvas.getContext("2d")
var userScore = 0
var systemScore = 0
var gameIsOver = false
let radius = 5
var ballPos = {x:canvas.width/2, y:canvas.height/2}
var speedX = 2
var speedY = 1
var panelYOffset = 20
var panelYSpeed = -1
var panelHeight = 50
var leftPanelY = (canvas.height-panelHeight)/2
var direction = "no-repeat"
var img=document.getElementById("mood")
var pat=context.createPattern(img,direction)
var musicButtonStateIsOn = true
function render(){
context.clearRect(0,0,canvas.width,canvas.height)
context.fillStyle="black";
context.fillRect(0,0,canvas.width,canvas.height)
context.shadowBlur=1
context.shadowOffsetY=2
context.shadowOffsetX=-2
context.shadowColor="gray"
drawPanel(canvas.width-10,(canvas.height-panelHeight)/2-panelYOffset)
drawPanel(0,leftPanelY)
context.fillStyle="white"
context.beginPath()
context.arc(ballPos.x, ballPos.y, radius, 0, 2*Math.PI)
context.fill()
drawText(20,30, "用户 "+userScore)
var txt = "系统 " + systemScore
drawText(canvas.width-20-context.measureText(txt).width,30, txt)
drawMusicButton()
}
function drawPanel(x, y){
context.fillStyle=pat
context.fillRect(x,y,10,panelHeight)
}
canvas.addEventListener("mousemove", function (e) {
var y = e.clientY -canvas.getBoundingClientRect().top-panelHeight/2
if (y > 0 && y < (canvas.height-panelHeight)) {
leftPanelY = y
}
})
function hitTest(){
if (ballPos.x > canvas.width-radius){
speedX = -speedX
}else if (ballPos.x < radius){
speedX = -speedX
}
if (ballPos.y > canvas.height-radius){
speedY = -speedY
}else if (ballPos.y < radius){
speedY = -speedY
}
}
function hitPanelTest(){
if (ballPos.x > canvas.width-radius-10){
var rightPanelY = (canvas.height-panelHeight)/2-panelYOffset
if (ballPos.y > rightPanelY && ballPos.y < (rightPanelY+panelHeight)){
speedX = -speedX
systemScore++
playHitAudio()
checkScore()
}
}else if (ballPos.x < radius+10){
if (ballPos.y > leftPanelY && ballPos.y < (leftPanelY+panelHeight)){
speedX = -speedX
userScore++
playHitAudio()
checkScore()
}
}
}
function checkScore(){
if (systemScore >=3 || userScore >= 3){
gameIsOver = true
}
}
function drawText(x,y,text){
context.fillText(text,x,y)
}
function playHitAudio(){
var audio = document.getElementById("hit-sound")
audio.play()
}
function playBackgroundSound(){
var audio = document.getElementById("bg-sound")
audio.play()
musicButtonStateIsOn = true
}
function stopBackgroundSound(){
var audio = document.getElementById("bg-sound")
audio.pause()
musicButtonStateIsOn = false
}
function drawMusicButton(){
var img= new Image()
if (musicButtonStateIsOn){
img.src = "http://xiuxing-1252822131.cossh.myqcloud.com/book/sound-on.jpg"
}else{
img.src = "http://xiuxing-1252822131.cossh.myqcloud.com/book/sound-off.jpg"
}
context.drawImage(img,canvas.width-20,0);
}
canvas.addEventListener("click", function (e) {
var pos = {x:e.offsetX, y:e.offsetY}
if (pos.x > (canvas.width-20) && pos.y < 25) {
if (musicButtonStateIsOn){
stopBackgroundSound()
}else{
playBackgroundSound()
}
return
}
if (gameIsOver){
playHitAudio()
userScore = 0
systemScore = 0
gameIsOver = false
run()
playBackgroundSound()
}
})
function run(){
panelYOffset += panelYSpeed
if (panelYOffset < -20 || panelYOffset > 20){
panelYSpeed = -panelYSpeed
}
ballPos.x += speedX
ballPos.y += speedY
hitPanelTest()
hitTest()
render()
if (!gameIsOver){
requestAnimationFrame(run)
}else{
var txt = "游戏结束"
drawText(canvas.width/2-context.measureText(txt).width/2,canvas.height/2, txt)
txt = "单击屏幕重新开始"
drawText(canvas.width/2-context.measureText(txt).width/2,canvas.height/2+20, txt)
stopBackgroundSound()
}
}
run()
playBackgroundSound()
<br/><br/>
<canvas id="canvas" style="width:100%">
您的浏览器不支持Html5 Canvas标签。
</canvas>
<img id="mood" style="width:100px;" src="http://pic35.photophoto.cn/20150525/0046043399250753_b.jpg"/>
<audio id="hit-sound">
<source src='https://opengameart.org/sites/default/files/audio_preview/click.wav.ogg' tpe='audio/ogg'>
<source src='https://opengameart.org/sites/default/files/audio_preview/click.wav.mp3' tpe='audio/mp3'>
</audio>
<audio id="bg-sound">
<source src='https://opengameart.org/sites/default/files/01%20track%201.ogg' tpe='audio/ogg'>
<source src='https://opengameart.org/sites/default/files/audio_preview/01%20track%201.ogg.mp3' tpe='audio/mp3'>
</audio>
<img src="http://xiuxing-1252822131.cossh.myqcloud.com/book/sound-on.jpg"/>
<img src="http://xiuxing-1252822131.cossh.myqcloud.com/book/sound-off.jpg"/>