gpt4 book ai didi

javascript - 尝试用 javascript 创建一个简单的游戏

转载 作者:行者123 更新时间:2023-12-03 02:08:15 26 4
gpt4 key购买 nike

//Minimal

function dibujarAsteroide(x, y){
var fondo = document.getElementById("fondo");
var ctx = fondo.getContext("2d");
var asteroide = document.getElementById("asteroide");
y = 0;
x = Math.floor((Math.random() * 600) + 1);
ctx.drawImage(asteroide, x, y);

}

function moverAsteroide(){
setInterval(dibujarAsteroide, 500);
var datosAsteroide = document.getElementById("asteroide");
datosAsteroide.style.top = 700 + "px";
}

//Verificable
<body>
<section>
<canvas id="fondo" width = "600" height = "600"></canvas>
<div>
<img src="img/nave.png" id="nave"/>
<img src="img/asceroide.png" id="asteroide"/>
</div>


</section>
</body>

<script>

fondo=document.getElementById("fondo");
fondo.onclick=moverNave;



function moverNave(evento)
{
var nave=document.getElementById("nave");
x=evento.clientX;
y=evento.clientY;

if (x>=700 || y>=500){
alert("fuera del espacio");
}
else
{
nave.style.left=x+"px";
nave.style.top=y+"px";
}

}


function dibujarAsteroide(x, y){
var fondo = document.getElementById("fondo");
var ctx = fondo.getContext("2d");
var asteroide = document.getElementById("asteroide");
y = 0;
x = Math.floor((Math.random() * 600) + 1);
ctx.drawImage(asteroide, x, y);

}

function moverAsteroide(){
setInterval(dibujarAsteroide, 500);
var datosAsteroide = document.getElementById("asteroide");
datosAsteroide.style.top = 700 + "px";
}


</script>

/*Complete
This is the script*/

<!DOCTYPE html>
<html lang = "es">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/estilos.css">
<title>Nave</title>
</head>

<body>

<section>
<canvas id="fondo" width = "600" height = "600"></canvas>
<div>
<img src="img/nave.png" id="nave"/>
<img src="img/asceroide.png" id="asteroide"/>
</div>


</section>
</body>

<script>

fondo=document.getElementById("fondo");
fondo.onclick=moverNave;



function moverNave(evento)
{
var nave=document.getElementById("nave");
x=evento.clientX;
y=evento.clientY;

if (x>=700 || y>=500){
alert("fuera del espacio");
}
else
{
nave.style.left=x+"px";
nave.style.top=y+"px";
}

}


function dibujarAsteroide(x, y){
var fondo = document.getElementById("fondo");
var ctx = fondo.getContext("2d");
var asteroide = document.getElementById("asteroide");
y = 0;
x = Math.floor((Math.random() * 600) + 1);
ctx.drawImage(asteroide, x, y);

}

function moverAsteroide(){
setInterval(dibujarAsteroide, 500);
var datosAsteroide = document.getElementById("asteroide");
datosAsteroide.style.top = 700 + "px";
}


</script>


</html>

//This the css code
#fondo
{
width: 600px;
height: 600px;
background-image: url("../img/space.jpg");
background-repeat: no-repeat;

}
#nave{
position: absolute;
left:375px;
top: 300px;
transition-duration: 1s;
}

#asteroide{
width:20px;
height: 20px;
transition-duration: 5s;


}

第一个函数应该在 Canvas 顶部的随机位置绘制小行星。第二个必须移动它来模拟跌倒,我在 css 中使用过渡属性来使跌倒的持续时间为 3 秒。

问题

小行星并不是随机出现的,它只是一颗,而且远离 Canvas 。绘图功能有问题吗?

最佳答案

您有一个拼写错误:setInterval('drawAsteroid', 500); 应该是 setInterval(drawAsteroid, 500);setInterval('drawAsteroid() ', 500);

关于javascript - 尝试用 javascript 创建一个简单的游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49699081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com