Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);


            if (mIsFired)
                mIsFired = false;


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;

                    mGraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 1);

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

                    mGraphicsDevice.ReferenceStencil = 1;
share|improve this question
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);

    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()
   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

Your Answer


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.