2D游戏-导弹射击问题

发布于 2024-10-12 14:56:32 字数 2321 浏览 7 评论 0原文

我必须制造一辆静止不动但可以移动炮塔并发射导弹的坦克。

因为这是我的第一个 Android 应用程序,而且我也没有做过任何游戏开发,所以我遇到了一些问题...

现在,在阅读 LunarLander 示例的 Android 教程后,我制作了坦克和移动炮塔代码。所以这段代码是基于LunarLander代码的。

但是当我按下空格按钮时,我在发射导弹时遇到了麻烦。

private void doDraw(Canvas canvas) {

canvas.drawBitmap(backgroundImage, 0, 0, null);
// draws the tank
canvas.drawBitmap(tank, x_tank, y_tank, new Paint());
// draws and rotates the tank turret
canvas.rotate((float) mHeading, (float) x_turret + mTurretWidth, y_turret);
canvas.drawBitmap(turret, x_turret, y_turret, new Paint());

// draws the grenade that is a regular circle  from ShapeDrawable class
bullet.setBounds(x_bullet, y_bullet, x_bullet + width, y_bullet + height);
bullet.draw(canvas);

}

UPDATE GAME 方法

private void updateGame() throws InterruptedException {

long now = System.currentTimeMillis();

if (mLastTime > now) 
return;
double elapsed = (now - mLastTime) / 1000.0;
mLastTime = now;


// dUP and dDown, rotates the turret from 0 to 75 degrees.
if (dUp)
mHeading += 1 * (PHYS_SLEW_SEC * elapsed);

if (mHeading >= 75) mHeading = 75;

if (dDown)
mHeading += (-1) * (PHYS_SLEW_SEC * elapsed);
if (mHeading < 0) mHeading = 0;


if (dSpace){
//  missile Logic, a straight trajectorie for now
x_bullet -= 1;
y_bullet -= 1;

}

}

是运行游戏的方法...

public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        if (mMode == STATE_RUNNING) 
                         updateGame();
                        doDraw(c);
                    }
                } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

所以问题是,如何使子弹在从炮塔到屏幕末端的单个空格键按下时发射?

你能帮我一下吗,我似乎在这里一片黑暗……

谢谢,

Niksa

I have to make a tank that sits still but moves his turret and shoots missiles.

As this is my first Android application ever and I haven't done any game development either, I've come across a few problems...

Now, I did the tank and the moving turret once I read the Android tutorial for the LunarLander sample code. So this code is based on the LunarLander code.

But I'm having trouble doing the missile firing then SPACE button is being pressed.

private void doDraw(Canvas canvas) {

canvas.drawBitmap(backgroundImage, 0, 0, null);
// draws the tank
canvas.drawBitmap(tank, x_tank, y_tank, new Paint());
// draws and rotates the tank turret
canvas.rotate((float) mHeading, (float) x_turret + mTurretWidth, y_turret);
canvas.drawBitmap(turret, x_turret, y_turret, new Paint());

// draws the grenade that is a regular circle  from ShapeDrawable class
bullet.setBounds(x_bullet, y_bullet, x_bullet + width, y_bullet + height);
bullet.draw(canvas);

}

UPDATE GAME method

private void updateGame() throws InterruptedException {

long now = System.currentTimeMillis();

if (mLastTime > now) 
return;
double elapsed = (now - mLastTime) / 1000.0;
mLastTime = now;


// dUP and dDown, rotates the turret from 0 to 75 degrees.
if (dUp)
mHeading += 1 * (PHYS_SLEW_SEC * elapsed);

if (mHeading >= 75) mHeading = 75;

if (dDown)
mHeading += (-1) * (PHYS_SLEW_SEC * elapsed);
if (mHeading < 0) mHeading = 0;


if (dSpace){
//  missile Logic, a straight trajectorie for now
x_bullet -= 1;
y_bullet -= 1;

}

}

a method run that runs the game...

public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        if (mMode == STATE_RUNNING) 
                         updateGame();
                        doDraw(c);
                    }
                } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

So the question would be, how do I make that the bullet is fired on a single SPACE key press from the turret to the end of the screen?

Could you help me out here, I seem to be in the dark here...

Thanks,

Niksa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-10-19 14:56:32

从评论来看,您的问题似乎是您希望子弹以抛物线移动,以便看起来更真实。 距离 = 时间 * 速度 已经解决了一半的问题,但您还需要速度 = 时间 * 加速度。您希望在每次更新中都做到这一点:

x_bullet += elapsed * vx_bullet;
y_bullet += elapsed * vy_bullet;
vx_bullet += elapsed * ax_bullet;  // ax_bullet = 0 unless you want simple wind resistance
vy_bullet += elapsed * ay_bullet;  // ay_bullet = gravity

您的重力常数将取决于图形比例。现实生活中的速度为 9.8 m/s^2,但您必须根据每米的像素进行调整。速度矢量将根据坦克的枪口速度进行初始化。如果枪口速度存储在 muzzle_vel 中,并且炮塔与正 x 轴的角度为 theta,那么您将有

vx_bullet = muzzle_vel * Math.cos(theta);
vy_bullet = muzzle_vel * Math.sin(theta);

很多调整使轨迹更加真实,但这应该可以帮助您入门。

From the comments it sounds like your problem is that you want the bullet to move in a parabola to look more realistic. You have half of the solution with distance = time * velocity, but you also need velocity = time * acceleration. You want this in each update:

x_bullet += elapsed * vx_bullet;
y_bullet += elapsed * vy_bullet;
vx_bullet += elapsed * ax_bullet;  // ax_bullet = 0 unless you want simple wind resistance
vy_bullet += elapsed * ay_bullet;  // ay_bullet = gravity

Your gravity constant will depend on the graphics scale. It's 9.8 m/s^2 in real life, but you'll have to adjust that according to your pixels per meter. The velocity vector will be initialized according to the muzzle velocity of your tank. If the muzzle velocity is stored in muzzle_vel, and the angle of the turret from the positive x-axis is theta, then you'll have

vx_bullet = muzzle_vel * Math.cos(theta);
vy_bullet = muzzle_vel * Math.sin(theta);

There are a lot of tweaks to make the trajectory more realistic, but this should get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文