c# bug invaders

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

well I am working on a space invaders like game. I am using c# and gdi+. I have managed to draw a space ship and have it shoot a bullet, I am just starting. here is my starting code. 


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.Windows.Forms;
using System.Drawing.Imaging;

    
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }
        int x = 0, y = 0;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Left)
            {
                x-=5;
                if(x<=-350)
                {
                    x = -350;
                }
            }
            if(e.KeyCode == Keys.Right)
            {
                x+=5;
                if(x>=375)
                {
                    x = 375;
                }
            }
            if (e.KeyCode == Keys.Space)
            {

                for(int i=0; i<5;i++)
                {
                    y -= 5;
                }
                if (y <= -530)
                {
                    y = 0;
                }
            }
            Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Bitmap ship = new Bitmap("ship.bmp", true);
            Bitmap bullet = new Bitmap("bullet.bmp", true);
            Graphics g1 = this.CreateGraphics();
            g1.DrawImage(ship, 350+x, 530);
            g1.DrawImage(bullet, 375, 520 + y);
            g1.Dispose();
        }
    }
}

 

Previous Entry c# blackjack graphics
Next Entry c# book
0 likes 2 comments

Comments

phil67rpg

well everything works except that the bug flickers. here is my code.


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.Windows.Forms;
using System.Drawing.Imaging;

    
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 20;
            timer1.Start();
        }
        public void Form1_Load(object sender, EventArgs e)
        {

        }
        int x = 0, y = 0;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Left)
            {
                x-=5;
                if(x<=-350)
                {
                    x = -350;
                }
            }
            if(e.KeyCode == Keys.Right)
            {
                x+=5;
                if(x>=375)
                {
                    x = 375;
                }
            }
            if (e.KeyCode == Keys.Space)
            {
                if (y <= -530)
                {
                    y = 0;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            y -= 5;
            Invalidate();
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {

        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bug_one = new Bitmap("bug_one.bmp", true);
            Bitmap ship = new Bitmap("ship.bmp", true);
            Bitmap bullet = new Bitmap("bullet.bmp", true);
            e.Graphics.Clear(this.BackColor);
            e.Graphics.DrawImage(ship, 350 + x, 530);
            e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
            e.Graphics.DrawImage(bug_one, 350, 0);
            e.Graphics.Dispose();
        }
    }
}

 

March 07, 2019 11:12 PM
phil67rpg

I have made progress. I have drawn a spaceship , a bug and shoot  a bullet and a collision bmp when the bullet hits the bug.


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.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();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }
        int x = 0, y = 0;
        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<=-350)
                {
                    x = -350;
                }
            }
            if(e.KeyCode == Keys.Right)
            {
                x+=5;
                if(x>=375)
                {
                    x = 375;
                }
            }
            if (e.KeyCode == Keys.Space)
            {
                if (y <= -530)
                {
                    y = 0;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            y -= 5;
            Invalidate();
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {

        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);
            e.Graphics.DrawImage(ship, 350 + x, 530);
            e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
            e.Graphics.DrawImage(bug_one, 350, 0);
            if(y <= -510)
            {
                e.Graphics.DrawImage(coll, 350, 0);
            }
        }
    }
}

 

March 08, 2019 11:46 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2709 views

plane game

3454 views

rgg platformer

2089 views

win 32 pong

2571 views

bug invaders

2239 views

c# book

2518 views

c# bug invaders

2176 views

c# console snake game

23971 views
Advertisement