Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    15
  • comments
    150
  • views
    3350

bug invaders

Sign in to follow this  
phil67rpg

618 views

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

 

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!