Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I currently have an asteroid texture loaded as my "test player" for the game I'm writing. What I'm trying to figure out how to do is get a triangle to shoot from the center of the asteroid, and keep going until it hits the top of the screen. What happens in my case (as you'll see from the code I've posted), is that the triangle will show, however it will either be a long line, or it will just be a single triangle which stays in the same location as the asteroid moving around (that disappears when I stop pressing the space bar), or it simply won't appear at all. I've tried many different methods, but I could use a formula here.

All I'm trying to do is write a space invaders clone for my final in C#. I know how to code fairly well, my formulas just need work is all.

So far, this is what I have:

Main Logic Code

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1, 1);

            mAsteroid.Draw(mSpriteBatch);

            if (mIsFired)
            {
                mPositions.Add(mAsteroid.LastPosition);
                mRay.Fire(mPositions);
                mIsFired = false;
                mRay.Bullets.Clear();
                mPositions.Clear();
            }

            base.Draw(gameTime);
        }

Draw Code

            public void Draw()
            {
                VertexPositionColor[] vertices = new VertexPositionColor[3];

                int stopDrawing = mGraphicsDevice.Viewport.Width / mGraphicsDevice.Viewport.Height;

                for (int i = 0; i < mRayPos.Length(); ++i)
                {
                    vertices[0].Position = new Vector3(mRayPos.X, mRayPos.Y + 5f, 10);
                    vertices[0].Color = Color.Blue;
                    vertices[1].Position = new Vector3(mRayPos.X - 5f, mRayPos.Y - 5f, 10);
                    vertices[1].Color = Color.White;
                    vertices[2].Position = new Vector3(mRayPos.X + 5f, mRayPos.Y - 5f, 10);
                    vertices[2].Color = Color.Red;

                    mShader.CurrentTechnique.Passes[0].Apply();
                    mGraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 1);

                    mRayPos += new Vector2(0, 1f);

                    mGraphicsDevice.ReferenceStencil = 1;
                }
            }
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

So you want to shoot a triangle bullet from an asteriod player at the bottom of the screen?

Firstly, why is your logic code in the Game.Draw() function?

You should maintain a list of triangles in your game:

//WARNING: psuedo code
// In Game.cpp

List<Triangle> _mTriangles = new List<Triangles>();

public override void Update()
{
    if(Keyboard.GetState() == SPACE_DOWN)
    {
       var triangle = new Triangle(mAsteroid.pos);
       _mTriangles.add(triangle);
    }

    if(Keyboard.GetState() == LEFT_RIGHT)
        mAsteroid.pos.x += Keyboard.GetState().X_OFFSET

    foreach(var t in mTriangles) t.y -= Triangle.Speed * elapsedTime;

    // TODO: another loop to remove triangles outside of screen.
}

public override void Draw()
{
   mAsteriod.Draw();
   foreach(var t in mTriangles) t.Draw();
}
share|improve this answer
    
...and secondly? What about the question? –  Tharwen May 31 '12 at 7:19
    
Please read your question again. It's not possible to know what you are doing in your code. What is mRayPos? what is mRay? what is Bullets.Clear() doing? What is mPositions? If you can draw your asteroid, just use the exact same method to draw your triangles. I have shown in my answer how to organise the objects for Draw() to access and Update() to move. Instead of drawing trangles, draw some asteroids shooting out first. –  Jake May 31 '12 at 9:19
    
@Jake Tharwen is not the OP... –  Hackworth May 31 '12 at 9:42
    
@Tharwen Really sorry, didn't realize you are not the OP, and I cannot edit that comment now. But my concern is the same: It's not possible to know for sure which object the OP's code snippet belongs to. –  Jake Jun 1 '12 at 5:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.