<html>
<head>
<title>Sean Coyne</title>
<link rel="stylesheet" type="text/css" href="home.css">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<section>
<article>
<div id="logo"><img src="LogoComic.png" id="Logo"></div><br></br>
<div id="canvas">
<canvas id="c" style="border:5px solid orange" height="500" width="500"></canvas>
<p id="p1"></p>
<script>
var basket_x=100;
var basket_y=100;
var ball_x=100;
var ball_y=100;
var points=0;
//画布的背景颜色
var c = document.getElementById("c");
var ctx = c.getContext("2d");
ctx.fillStyle = "#0000";
ctx.fillRect(0,0,500,500);
//这是事件监听器
mycanv.addEventListener("mousemove",seenmotion,false);
function seenmotion(e) {
//这是鼠标的代码 //在画布上移动。
var bounding_box=mycanv.getBoundingClientRect();
basket_x=(e.clientX-bounding_box.left) *
(mycanv.width/bounding_box.width);
basket_y=(e.clientY-bounding_box.top) *
(mycanv.height/bounding_box.height);
}
function start_game() {
setInterval(game_loop, 50);
}
function game_loop() {
//上面的代码每50ms调用一次,是一个 // frame-redraw-game-animation循环。
mycanv.width=mycanv.width;
//下面是绘制对象的代码
draw_basket(basket_x,basket_y);
draw_ball(ball_x,ball_y);
//下面是更新球位置的代码
ball_x++;
if (ball_x>mycanv.width) {
ball_x=0;
}
//这是碰撞检测代码
if (collision(basket_x, basket_y, ball_x, ball_y)) {
points -= 1;
}
//这是点系统的代码
points+=1
//让我们把它贴在右上角。
var integerpoints=Math.floor(points); // make it into an integer
ctx.font="bold 24px sans-serif #fff";
ctx.fillText(integerpoints, mycanv.width-50, 50);
}
function collision(basket_x, basket_y, ball_x, ball_y) {
if(basket_y + 85 < ball_y) {
return false;
}
if (basket_y > ball_y + 91) {
return false;
}
if (basket_x + 80 < ball_x) {
return false;
}
if (basket_x > ball_x + 80) {
return false;
}
return true;
}
//我们完成游戏后停止游戏的代码
function stop_game() {
}
//球的代码
function draw_ball(x,y) {
var c = document.getElementById("c");
var ctx = c.getContext("2d");
ctx.fillStyle = "#fff";
ctx.fillRect(0,0,20,20);
}
//购物篮的代码
function draw_basket(x,y) {
var basket_img=new Image();
basket_img.src="basket.png";
ctx.drawImage(basket_img,x,y);
}
start_game()
</script>
</div>
</article>
</section>
答案 0 :(得分:1)
您永远不会调用start_game()
来启动程序,因此程序只是等待。相反,在<script>
的末尾添加start_game()
。
只是提示:您的行mycanv.width = mycanv.width
完全没必要,相当于说var x = 1; x = x;
。