2

我的网络编程课程中有一个小项目,我应该用 javascript 制作一个小游戏。这就是游戏:两门大炮固定在游戏空间的两端。大炮轮流互相射击,玩家将大炮倾斜到一定程度并选择一个力量来射击炮弹。如果有时间,我正在考虑将添加许多其他内容。

我找到了一个不错的网站http://www.rodedev.com/tutorials/gamephysics/来帮助我学习 2d 物理,并且我已经进行了一些测试,请参见下面的代码。然而,我确实有两个大问题:

  1. 非平坦的地板/地面。
  2. 回转!

现在脚本只检查 floorDiv 的 style.top 值,因此框将始终“降落”在这一行像素上。如果我想拥有可变高度的地形,这还不够。你们对如何实现这一点有任何想法吗?无论如何要检查特定像素的颜色吗?如果是这样,我可以检查非天空或地面彩色像素并将其作为着陆标准。

我需要旋转大炮,以便玩家可以调整他们的目标!我想我会制作两个不同的大炮对象,大炮“基地”和大炮枪管。然后我可以在玩家按键时旋转炮筒。不幸的是,轮换在网络编程中似乎很难。我在http://code.google.com/p/jquery-rotate/找到了这个 jquery 插件,但我没有让它像我想要的那样工作。尽管我已经确保炮弹在图片中居中,但也有一些平移,而不仅仅是旋转。我想这可能是因为图片没有围绕它的中心旋转?请参阅下面的轮换测试代码(如果您想尝试该代码,则必须下载轮换插件)。我想我可以为每个角度拍一张照片,但这似乎有点浪费,因为否则大炮没有实际的视觉变化。

另外,如果我想同时为多个对象设置动画,你认为我应该只更改同一个 gameloop 函数中的所有对象吗?对此有什么想法吗?

以下是物理测试的代码

CSS 文件

#moveDiv {
    position:absolute;
    width:100px;
    height:100px;
    background-color:#000;
}

#floorDiv {
    position:absolute;
    width:800px;
    height:1px;
    background-color:#2E2;
}

table {
text-align:right;

}
table input {
    width:35px;
}

HTML 文件

<script type="text/javascript">
//global vars
var gravity = 0.5;
var friction = 0.5;
var moving = false;
var moveDiv;
var floorDiv;
var yPos;
var xPos;
var floorPos;
var velocity_y = 0;
var velocity_x = 0;
</script>
</head>

<body onload="initialize();">
<input type="button" value="fall" onclick="fall();"></button>
<input type="button" value="Push" onclick="push();"></button>
<input type="button" value="reset" onclick="reset();"></button>

<table>
<tr>
<td>Angle: <input type="text" value="45" id="angle" /></td>
<td>Speed: <input type="text" value="10" id="speed"/></td>
</tr>
<tr>
<td>Gravity: <input type="text" value="0.5" id="gravity" /></td>
<td>Friction: <input type="text" value="0.5" id="friction" /></td>
</table>

<div id="moveDiv"></div>
<div id="floorDiv"></div>
</body>
</html>

js文件

function initialize() {
    moveDiv = document.getElementById('moveDiv');
    floorDiv = document.getElementById('floorDiv'); 

    moveDiv.style.left=400;
    moveDiv.style.top=50;

    floorDiv.style.left=200;
    floorDiv.style.top=400;
}//end initialize

function fall() 
{
    moving=true;
    var angle = 275;
    var rad = angle / (Math.pi * 2);
    var scale_x = Math.cos(rad);
    var scale_y = Math.sin(rad);

    var moveDivSpeed = 0;

    gameLoop();

}//end fall

function push() 
{
    moving=true;
    var angle = document.getElementById('angle').value;
    var rad = angle / (Math.PI * 2);
    var scale_x = Math.cos(rad);
    var scale_y = Math.sin(rad);
    var moveDivSpeed = document.getElementById('speed').value;
    friction = document.getElementById('friction').value;
    gravity = document.getElementById('gravity').value;
    velocity_x = moveDivSpeed*scale_x;
    console.log("original velocity_x is " + velocity_x);
    velocity_y = moveDivSpeed*scale_y;
    gameLoop();
}

function gameLoop () {
    //console.log("gameLoop start");
    var len = moveDiv.style.top.length;
    var presentTop = parseInt(moveDiv.style.top.substr(0, len-2));

    var lenX = moveDiv.style.left.length;
    var presentLeft = parseInt(moveDiv.style.left.substr(0, lenX-2));

    var len2 = floorDiv.style.top.length;
    var floorTop = parseInt(floorDiv.style.top.substr(0, len2-2));

    if (moving == true) 
    {
        velocity_y -= gravity;

        if ((presentTop+100) - velocity_y < floorTop) 
        { //if moveDiv hasn't hit the floor yet...
            moveDiv.style.top = presentTop - velocity_y;
            moveDiv.style.left = presentLeft + velocity_x;
        }
        else if (presentTop + 100 == floorTop) //if moveDiv is ON the floor
        {
            velocity_x = velocity_x*(1-friction);
            moveDiv.style.left = presentLeft + velocity_x;
            console.log("on the floor");
            console.log("friction is " + friction);
            console.log(" and velocity_x is " + velocity_x);
            console.log("moveDiv.style.left is " +moveDiv.style.left);

            if (velocity_x <= 1)
            {
                console.log("stopped moving");
                moving = false;
            }

        }
        else //if moveDiv will hit the floor/go through the floor this time
        {
            var diff = floorTop - (presentTop + 100);
            moveDiv.style.top = presentTop + diff;
            moveDiv.style.left = presentLeft + velocity_x;
        }
    }
    else if (moving == false) 
    {
        clearTimeout(runAgain);
        console.log("else if moving == false");
        return false;
    }

    var runAgain = setTimeout("gameLoop()", 5);
    //console.log("end of gameLoop");
    return false;
}//end gameLoop

function reset () {
    moving = false;

    moveDiv.style.left=400;
    moveDiv.style.top=50;

    floorDiv.style.left=200;
    floorDiv.style.top=400;
}//end reset

旋转测试代码(仅 html 文件和旋转插件)

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.rotate.1-1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {

        $("#hide").click(function() {
            $("p").hide(500);
            $(".box").animate({height:300, opacity:0}, 1000);
        });

        $("#show").click(function() {
            $("p").show(500);
            $(".box").animate({height:100, opacity:100}, 1000);
        });

        $(".box").mouseenter(function() {
            $(".box").animate({height:300, opacity:0}, 500);
        });

        $(".box").mouseleave(function() {
            $(".box").animate({height:100, opacity:10}, 1000);
        });

        $("#move").click(function() {
            $("#grey").animate({left: -300, top: -100}, 1000)
            .animate({left: -600, top: 200}, 1000);
        });

        $("#cannonball").click(function() {
            $("#cannonball").animate({"left": "+=300", "top": "-=100"}, 500)
            .animate({"left": "+=300", "top": "+=100"}, 500);

        });

        $("p.fading").css("opacity","0.3");
        $("p.fading").hover(function() {
            $(this).stop().animate({opacity: 1.0}, "slow");}, 
                function () {
                $(this).stop().animate({opacity: 0.3}, "slow");
            });

        $("#rotateRight").click(function() {
            $("#cannonball").rotate(10);
        });

    }); //end jquery document.ready scripts

function initialize () {
    document.getElementById('box').style.top = 250;
}
</script>

<style type="text/css">
body {
    background-color: #ccc;
}

.box {
    background-color: #000;
    width:100px;
    height:100px;
    margin-left:300px;
}

.divMove {
    position:relative;
    top:350px;
    float:right;
    background-color: #ddd;
    width:100px;
    height:100px;
    margin-right:100px;
}   

#cannonball {
    position: relative;
}
</style>
</head>
<body onload="initialize();">
<p>Let's make this disappear and appear.</p>

<button id="hide">Hide</button>
<button id="show">Show</button>
<button id="move">Move</button>
<button id="rotateRight">Rotate+</button>

<div id="box" class="box">
</div>


<div class="divMove" id="grey">
</div>

<img src="http://dl.dropbox.com/u/18833130/Webprogrammering/test/cannonball.png" id="cannonball" border="1" />
<p class="fading">This is a paragraph that fades. Let's see how it looks.</p>
</body>
</html> 

我为长篇大论和代码量道歉,但如果你想尝试一下,你基本上可以复制粘贴,我认为这样讨论会更容易!

4

1 回答 1

0

我想我会制作两个不同的大炮对象,大炮“基地”和大炮枪管。然后我可以在玩家按键时旋转炮筒。不幸的是,轮换在网络编程中似乎很难。

最简单的(现在)是使用CSStransform属性

const angleInputElement = document.getElementById('angle');
const degrees = angleInputElement.value;
const barrelElement = document.getElementById('barrel'); // I don't see this in your sample though
barrelElement.style.transform = `rotate(${degrees}deg)`;

另外,如果我想同时为多个对象设置动画,你认为我应该只更改同一个 gameloop 函数中的所有对象吗?对此有什么想法吗?

对于这个应该可以正常工作的简单实现。

于 2020-05-15T15:35:47.013 回答