Advertisement

bug invaders

Started by April 13, 2019 12:26 AM
59 comments, last by Tom Sloper 5 years, 10 months ago

 well I have converted pseudocode into c# but it  still does not work, I will have to tweak it a little bit. here is the converted code


        bool collision = false;
        bool drawflag = true;
        int killtimer = 10;
        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(ship, 350 + x, 530);
            e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
            e.Graphics.DrawImage(bug_one, 120, 0);
            e.Graphics.DrawImage(bug_one, 180, 0);
            e.Graphics.DrawImage(bug_one, 240, 0);
//            e.Graphics.DrawImage(bug_one, 300, 0);
//            e.Graphics.DrawImage(bug_one, 360, 0);
            e.Graphics.DrawImage(bug_one, 420, 0);
            e.Graphics.DrawImage(bug_one, 480, 0);
            e.Graphics.DrawImage(bug_one, 540, 0);
            e.Graphics.DrawImage(bug_one, 600, 0);
            if (y <= -510 && x >= -15 && x <= 15)
            {
                drawflag = false;
                collision = true;
                killtimer = 1;
            }
            if (y <= -510 && x >= -75 && x <= -45)
            {
                drawflag = false;
                collision = true;
                killtimer = 1;
            }
            if(collision==true)
            {
                e.Graphics.DrawImage(coll, 360, 0);
            }
            else if(drawflag==true)
            {
                e.Graphics.DrawImage(bug_one, 360, 0);
            }
            if (collision == true)
            {
                e.Graphics.DrawImage(coll, 300, 0);
            }
            else if (drawflag == true)
            {
                e.Graphics.DrawImage(bug_one, 300, 0);
            }
        }
        public void timer1_Tick(object sender, EventArgs e)
        {
            y -= 5;
            Invalidate();
        }
        public void timer2_Tick(object sender, EventArgs e)
        {
            if(killtimer>0)
            {
                killtimer--;
            }
            if(killtimer <=0)
            {
                collision = false;
            }
            Invalidate();
        }

 

Okay, that is progress...I don't see you trying to erase anymore. good. Looks like both your timers are ready to go. Both are frame rate bound (if that's a thing in winForms) instead of elapsed time driven, but fine. Now I see you trying to unroll a loop and hard coding your bug position values? Will these bugs not move? This is probably not what you want to do. If they are movable then we want to go back to the pseudocode and think about turning the bug into an object (struct or class) so we can work with it easier.  

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

Advertisement

I want the bugs to move at a later date. can you help with turning the bugs into a class.

yes, very good.


public class Bug
{
  public int px;                     // position horizontal
  public int py;                     // position vertical
  public bool drawFlag = true;       // active?
  public bool collision = false;     // about to deactivate?
  public int killTimer = 0;          // deferred deactivation
};

List<Bug> bugs = new List<Bug>();
bugs.Add(new Bug() { px = 120, py = 0 });
bugs.Add(new Bug() { px = 180, py = 0 });
bugs.Add(new Bug() { px = 240, py = 0 });
bugs.Add(new Bug() { px = 420, py = 0 });
bugs.Add(new Bug() { px = 480, py = 0 });


.
.
.
  
// and later when we draw
foreach( Bug bug in bugs) 
{
  if(bug.drawFlag == true)
  {
    // do collision stuff here
  }

  if(bug.collision == true)
  {
    e.Graphics.DrawImage(coll, bug.px, bug.py);
  }
  else
  {
    if(bug.drawFlag == true)
    {
      e.Graphics.DrawImage(bug_one, bug.px, bug.py);
    }
  }
}
  

I can not check against a compiler at the moment. Hope this helps.

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

On 4/13/2019 at 10:21 PM, phil67rpg said:

maybe I can post some pseudocode, I am unsure of how to post flowcharts

You can draw them in this free online editor: https://drakonhub.com/

See this short video (7 minutes) how to use the editor: How to draw a flowchart in DrakonHub

 

can I use an array instead of a list?

Advertisement

Sure you bet. I actually prefer that here because they will always be there. Even when you are not drawing them. Initialization looks only slightly different. Maybe something like this. What do you think? 


const int numBugs = 5;
int[,] positionBuffer = { { 120, 0 }, { 180, 0 }, { 240, 0 }, { 420, 0 }, { 480, 0 } };
Bug[] bugs = new Bug[numBugs];
for(int i = 0; i < numBugs; i++)
{
   bugs[i] = new Bug();
   bugs[i].px = positionBuffer[i, 0];
   bugs[i].py = positionBuffer[i, 1];
}

Again, I didn't check it. Watch out for indexing outside of your array sizes. You are probably better off with a List because of safety I think perhaps. Get yourself in less trouble maybe, but this is okay. (I think, try it)

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

I tried to use the list but I have run into some compiler errors for some  reason I cant add the bug objects.

Can someone check me, my C# is rough at best. The array example may be too cryptic. What say we?

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

let me do some research on lists, I am getting better at c#.

This topic is closed to new replies.

Advertisement