Question Astro Invaders (1980's arcade game)

ca2nv

New member
Joined
Sep 5, 2014
Messages
2
Programming Experience
Beginner
I'm trying to duplicate the Astro invaders (Stern) version. This a work in early progress. I'm hung up,
where I'm trying to have the first invader 'ship' appear @ the top of the screen and begin movement in a
downward direction. It sort of works (but not the way I think it should).

What I did is created one ship, located it in the starting position, and it moves down to a certain determined
level.

My problem is the control (PictureBox) appears in the correct position and moves down (Black color...) and
at the right level turns 'Yellow'. I'm trying to get to appear 'Yellow' before it starts it's downward movement.

This is my current code. Not sure how to have code show up in a certain format/view on this forum.

        public Timer ship_m = new Timer();
        public int i = 0;
        public int x = 527;
        public int y = 0;
        public int t = 0;
        public int shipDwn = 1;
        public PictureBox ship = new PictureBox();

        //  int gunX = 571; int gunY = 540;



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            for (i = 0; i < 5; i++)
            {
                drawship();


                Timer ship_m = new Timer();
                for (y = 0; y < 68; y++)
                {

                    ship_m.Enabled = true;
                    ship_m.Interval = 1;

                    ship.Location = new Point(x, y + shipDwn);

                    //                            //ship_m.Enabled = false;
                    for (t = 0; t < (5000000 * 3); t++) ;
                    //                            
                }
            }
        }

        private void drawship()
        {
            ship.Size = new Size(31, 12);
            ship.Location = new Point(x, y);
            ship.BorderStyle = BorderStyle.FixedSingle;
            ship.BackColor = Color.Yellow;
            this.Controls.Add(ship);
            ship.Show();
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,274
Location
Sydney, Australia
Programming Experience
10+
Having a `for` loop in the Click event handler of a Button is never going to work long-term so don't start out that way. Also, why are you creating a Timer at all because you're not handling its Tick event so it's useless. You've also got two Timer variables with the same name at different scopes and that can't be good. You should start out the right way and then you'll avoid having to completely rework what you're doing later on.

If you're making a very basic game then you might think about using a Timer. In that case, create one Timer and handle its Tick event. In that event handler, change whatever needs changing at that particular time. A better option would be to do it the proper way though, i.e. use a game loop. I would suggest that you do some reading on game loops first and then construct your code the right way. You may then find that some of your problems just disappear but at least you won't waste your time solving problems in a way that you can't use later when things get more complex.
 

ca2nv

New member
Joined
Sep 5, 2014
Messages
2
Programming Experience
Beginner
Thanks !

Having a `for` loop in the Click event handler of a Button is never going to work long-term so don't start out that way. Also, why are you creating a Timer at all because you're not handling its Tick event so it's useless. You've also got two Timer variables with the same name at different scopes and that can't be good. You should start out the right way and then you'll avoid having to completely rework what you're doing later on.

If you're making a very basic game then you might think about using a Timer. In that case, create one Timer and handle its Tick event. In that event handler, change whatever needs changing at that particular time. A better option would be to do it the proper way though, i.e. use a game loop. I would suggest that you do some reading on game loops first and then construct your code the right way. You may then find that some of your problems just disappear but at least you won't waste your time solving problems in a way that you can't use later when things get more complex.

I'm going to "step back" and try to figure out what you just said.
I'll do some reading, then some coding and come back later with
the results.

Once again Thank You for your response !
Cecil
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,274
Location
Sydney, Australia
Programming Experience
10+
I'm going to "step back" and try to figure out what you just said.
I'll do some reading, then some coding and come back later with
the results.

Once again Thank You for your response !
Cecil

Good plan. It will save you time and effort in the long run.
 
Top Bottom