bug invaders

posted in phil67rpg's Blog
Published June 05, 2019
Advertisement

I am  working on a space invaders clone called bug invaders. here is my code. let me know what you think. let me know what to do next.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Imaging;

    
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 20;
            timer1.Start();
            timer2.Interval = 100;
            timer2.Start();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }
        int x = 375, y = 520;
        Bitmap bug_one = new Bitmap("bug_one.bmp", true);
        Bitmap ship = new Bitmap("ship.bmp", true);
        Bitmap bullet = new Bitmap("bullet.bmp", true);
        Bitmap coll = new Bitmap("coll.bmp", true);
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                x-=5;
                if(x<=0)
                {
                    x = 0;
                }
            }
            if(e.KeyCode == Keys.Right)
            {
                x+=5;
                if(x>=725)
                {
                    x = 725;
                }
            }
            if (e.KeyCode == Keys.Space)
            {
                if (y <= 0)
                {
                    y = 520;
                }
            }
        }
        public class Bug
        {
            public int px;
            public int py;
            public int width = 30;
            public int height = 30;
            public bool drawflag = true;
            public bool collision = false;
            public int timer = 0;
        };

        IList<Bug> bugs = new List<Bug>() {
            new Bug() { px=120, py=0 },
            new Bug() { px=180, py=0 },
            new Bug() { px=240, py=0 },
            new Bug() { px=300, py=0 },
            new Bug() { px=360, py=0 },
            new Bug() { px=420, py=0 },
            new Bug() { px=480, py=0 },
            new Bug() { px=540, py=0 },
            new Bug() { px=600, py=0 },};

        IList<Bug> bugs_one = new List<Bug>() {
            new Bug() { px=120, py=40 },
            new Bug() { px=180, py=40 },
            new Bug() { px=240, py=40},
            new Bug() { px=300, py=40 },
            new Bug() { px=360, py=40 },
            new Bug() { px=420, py=40 },
            new Bug() { px=480, py=40 },
            new Bug() { px=540, py=40 },
            new Bug() { px=600, py=40 },};

        IList<Bug> bugs_two = new List<Bug>() {
            new Bug() { px=120, py=80 },
            new Bug() { px=180, py=80 },
            new Bug() { px=240, py=80},
            new Bug() { px=300, py=80 },
            new Bug() { px=360, py=80 },
            new Bug() { px=420, py=80 },
            new Bug() { px=480, py=80 },
            new Bug() { px=540, py=80 },
            new Bug() { px=600, py=80 },};

        IList<Bug> bugs_three = new List<Bug>() {
            new Bug() { px=120, py=120 },
            new Bug() { px=180, py=120 },
            new Bug() { px=240, py=120},
            new Bug() { px=300, py=120 },
            new Bug() { px=360, py=120 },
            new Bug() { px=420, py=120 },
            new Bug() { px=480, py=120 },
            new Bug() { px=540, py=120 },
            new Bug() { px=600, py=120 },};

        IList<Bug> bugs_four = new List<Bug>() {
            new Bug() { px=120, py=160 },
            new Bug() { px=180, py=160 },
            new Bug() { px=240, py=160},
            new Bug() { px=300, py=160 },
            new Bug() { px=360, py=160 },
            new Bug() { px=420, py=160 },
            new Bug() { px=480, py=160 },
            new Bug() { px=540, py=160 },
            new Bug() { px=600, py=160 },};

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(ship, x-25, 530);
            e.Graphics.DrawImage(bullet, x, y);
            foreach (Bug bug in bugs)
            {
                if (bug.collision==false)
                {
                    e.Graphics.DrawImage(bug_one, bug.px, bug.py);
                }
            }
                foreach (Bug bug in bugs)
                {
                    if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height)
                    {
                        e.Graphics.DrawImage(coll, bug.px, bug.py);
                        bug.collision = true;
                    }
                }
            foreach (Bug bug in bugs_one)
            {
                if (bug.collision == false)
                {
                    e.Graphics.DrawImage(bug_one, bug.px, bug.py);
                }
            }
            foreach (Bug bug in bugs_one)
            {
                if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height)
                {
                    e.Graphics.DrawImage(coll, bug.px, bug.py);
                    bug.collision = true;
                }
            }
            foreach (Bug bug in bugs_two)
            {
                if (bug.collision == false)
                {
                    e.Graphics.DrawImage(bug_one, bug.px, bug.py);
                }
            }
            foreach (Bug bug in bugs_two)
            {
                if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height)
                {
                    e.Graphics.DrawImage(coll, bug.px, bug.py);
                    bug.collision = true;
                }
            }
            foreach (Bug bug in bugs_three)
            {
                if (bug.collision == false)
                {
                    e.Graphics.DrawImage(bug_one, bug.px, bug.py);
                }
            }
            foreach (Bug bug in bugs_three)
            {
                if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height)
                {
                    e.Graphics.DrawImage(coll, bug.px, bug.py);
                    bug.collision = true;
                }
            }
            foreach (Bug bug in bugs_four)
            {
                if (bug.collision == false)
                {
                    e.Graphics.DrawImage(bug_one, bug.px, bug.py);
                }
            }
            foreach (Bug bug in bugs_four)
            {
                if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height)
                {
                    e.Graphics.DrawImage(coll, bug.px, bug.py);
                    bug.collision = true;
                }
            }
        }
        public void timer1_Tick(object sender, EventArgs e)
        {
            y -= 5;
            Invalidate();
        }
        public void timer2_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}

 

Previous Entry c# book
Next Entry win 32 pong
-1 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2661 views

plane game

3410 views

rgg platformer

2042 views

win 32 pong

2523 views

bug invaders

2190 views

c# book

2473 views

c# bug invaders

2131 views

c# console snake game

23812 views
Advertisement