Saturday 17 May 2014

c# knight






video: http://youtu.be/m0dodv1QCq0

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

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

        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private int square = 100, kx = -500, ky = -500, pnum=0, enemybreach =0,kill=0,
            lightening=0, picnum=0;
        private PictureBox pic1 = new PictureBox();
        private PictureBox[] pornpic = new PictureBox[1];
        private bool mf = false, lightf,turn= false;
        private Point[] porn = new Point[0], move = new Point[8],
            plight= {new Point(-2,-2),new Point(-2,-2),new Point(-2,-2),new Point(-2,-2)};

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //board line
            /*for (int i = 0; i * square < this.Size.Width; i++)
             {
                 Pen bluepen = new Pen(Color.DarkBlue, 2);
                 Point a = new Point( square * i, 0);
                 Point b = new Point(  square * i, this.Size.Height);
                 e.Graphics.DrawLine(bluepen, a, b);
             }

             for (int i = 0; i * square < this.Size.Height; i++)
             {
                 Pen bluepen = new Pen(Color.DarkBlue, 2);
                 Point a = new Point(0,  square * i);
                 Point b = new Point(this.Size.Height, square * i);
                 e.Graphics.DrawLine(bluepen, a, b);
             }*/

            /*move[0] = new Point(kx - 2 * square, ky - square);
            move[1] = new Point(kx - square, ky - 2 * square);
            move[2] = new Point(kx + 2 * square, ky - square);
            move[3] = new Point(kx + square, ky - 2 * square);
            move[4] = new Point(kx - 2 * square, ky + square);
            move[5] = new Point(kx - square, ky + 2 * square);
            move[6] = new Point(kx + 2 * square, ky + square);
            move[7] = new Point(kx + square, ky + 2 * square);

            SolidBrush greenBrush = new SolidBrush(Color.MediumSeaGreen);

            for (int i = 0; i < 8; i++)
            {
                Rectangle rec = new Rectangle(move[i], new Size(square, square));
                e.Graphics.FillRectangle(greenBrush, rec);
            }*/

            //lightening ability
            Random r = new Random();

            if (kill - lightening == 2)
            {
                lightening = kill;

                Point pl = new Point();

                for (bool done = false; done == false; )
                {
                    done = true;
                    pl = new Point(r.Next(8), r.Next(8));

                    for (int i = 0; i < 4; i++)
                    {
                        if (pl == plight[i])
                        {
                            done = false;
                        }
                    }
                }

                for (int i = 0; i < 3; i++)
                {
                    plight[i] = plight[i + 1];
                }

                plight[3] = pl;
            }

            for (int j = 0; j < 4; j++)
            {
                SolidBrush blueBrush = new SolidBrush(Color.LightSalmon);

                Point point1 = new Point(plight[j].X * square + 50, plight[j].Y * square + 0);
                Point point2 = new Point(plight[j].X * square + 40, plight[j].Y * square + 40);
                Point point3 = new Point(plight[j].X * square + 100, plight[j].Y * square + 40);
                Point point4 = new Point(plight[j].X * square + 50, plight[j].Y * square + 100);
                Point point5 = new Point(plight[j].X * square + 60, plight[j].Y * square + 60);
                Point point6 = new Point(plight[j].X * square + 0, plight[j].Y * square + 60);

                PointF[] curvePoints = { point1, point2, point3, point4, point5, point6 };

                FillMode newFillMode = FillMode.Winding;

                e.Graphics.FillPolygon(blueBrush, curvePoints, newFillMode);
            }

            if (lightf == true)
            {
                lightf = false;

                SolidBrush lightBrush = new SolidBrush(Color.LightSalmon);
                Rectangle rec1 = new Rectangle(new Point(kx/square*square-70,0), new Size(40, square*8));
                e.Graphics.FillRectangle(lightBrush, rec1);
                Rectangle rec2 = new Rectangle(new Point(kx / square * square +30, 0), new Size(40, square * 8));
                e.Graphics.FillRectangle(lightBrush, rec2);
                Rectangle rec3 = new Rectangle(new Point(kx / square * square + 130, 0), new Size(40, square * 8));
                e.Graphics.FillRectangle(lightBrush, rec3);
            }

        }

        private void drawknight()
        {
            this.Controls.Remove(pic1);
            Image img1 = Image.FromFile("white knight.png");
               
            pic1.Location = new System.Drawing.Point(kx, ky);
            pic1.ClientSize = new Size(square, square);
            pic1.Image = img1;
            this.Controls.Add(pic1);
            pic1.BringToFront();

            move[0] = new Point(kx - 2 * square, ky - square);
            move[1] = new Point(kx - square, ky - 2 * square);
            move[2] = new Point(kx + 2 * square, ky - square);
            move[3] = new Point(kx + square, ky - 2 * square);
            move[4] = new Point(kx - 2 * square, ky + square);
            move[5] = new Point(kx - square, ky + 2 * square);
            move[6] = new Point(kx + 2 * square, ky + square);
            move[7] = new Point(kx + square, ky + 2 * square);
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

            int kxo = kx, kyo=ky;
            kx = (Cursor.Position.X - this.Location.X - 10) / square * square;
            ky = (Cursor.Position.Y - this.Location.Y - 40) / square * square;

            if (mf == true)
            {
                for (int i = 0; i < 8; i++)
                {
                    if (kx == move[i].X && ky == move[i].Y&&kx<750)
                    {
                        drawknight();

                        //step on enemy porn, kill it
                        for (int k = 0; k < pnum; k++)
                        {
                            if (new Point(kx / square, ky / square) == porn[k])
                            {
                                Point[] pornnew = deletearray(porn, k);
                                Array.Resize(ref porn, pnum - 1);
                                porn = pornnew;
                                pnum--;

                                textBox1.Text = Convert.ToString(pnum);
                                kill++;
                                textBox3.Text = Convert.ToString(kill);
                                drawporn();
                            }
                        }

                        //here comes the lightening
                        for(int k=0;k<4;k++)
                        {
                            if(new Point(kx / square, ky / square) == plight[k])
                            {
                                lightf = true;
                                plight[k] = new Point(-2, -2);

                                for (bool done = false; done == false; )
                                {
                                    done = true;

                                    for (int j = 0; j < pnum; j++)
                                    {
                                        if (kx / square == porn[j].X || kx / square - 1 == porn[j].X || kx / square + 1 == porn[j].X)
                                        {
                                            Point[] pornnew = deletearray(porn, j);
                                            Array.Resize(ref porn, pnum - 1);
                                            porn = pornnew;
                                            pnum--;                                        

                                            textBox1.Text = Convert.ToString(pnum);
                                            kill++;
                                            lightening = kill;
                                            textBox3.Text = Convert.ToString(kill);

                                            done = false;
                                            break;
                                        }
                                    }
                                }

                                drawporn();
                            }
                        }
                     
                        this.Refresh();                      
                        return;
                    }
                }

                kx = kxo; ky = kyo;
            }
            else
            {
                mf = true;
                drawknight();
                timer1.Enabled = true;
            }        
        }

        public static Bitmap ChangeOpacity(Image img, float opacityvalue)
        {
            Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image
            Graphics graphics = Graphics.FromImage(bmp);
            ColorMatrix colormatrix = new ColorMatrix();
            colormatrix.Matrix33 = opacityvalue;
            ImageAttributes imgAttribute = new ImageAttributes();
            imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
            graphics.Dispose();   // Releasing all resource used by graphics
            return bmp;
        }

        //black porn attack
        private void timer1_Tick(object sender, EventArgs e)
        {
            //sporn new enemy porn
            Random r = new Random();
            Point sporn1 = new Point(r.Next(8), 0);
            Point sporn2 = new Point();
            for (bool done = false; done == false; )
            {
                int temp = r.Next(8);
                if (temp != sporn1.X)
                {
                    sporn2.X = temp;
                    sporn2.Y = 0;
                    done = true;
                }
            }

            //old enemy porn move forward        
            for (int j = 0; j < pnum; j++)
            {
                porn[j].Y += 1;
            }

            Array.Resize(ref porn, pnum + 1);
            addarray(porn, pnum + 1, sporn1.X, sporn1.Y);
            Array.Resize(ref porn, pnum + 2);
            addarray(porn, pnum + 2, sporn2.X, sporn2.Y);

            pnum += 2;

            //blocked by knight
            bool fblock = false; int iblock = 0;

            for (int j = 0; j < pnum; j++)
            {
                if (porn[j].X == kx / square && porn[j].Y == ky / square)
                {
                    fblock = true; iblock = j;
                    break;
                }
            }

            while (fblock == true)
            {
                fblock = false;
                porn[iblock].Y--;
                for (int k = iblock + 1; k < pnum; k++)
                {
                    if (porn[k] == porn[iblock])
                    {
                        fblock = true;
                        iblock = k;
                        break;
                    }
                }
            }

            //sporn place blocked
            if (porn[pnum - 1].Y < 0)
            {
                for (bool done = false; done == false; )
                {
                    int temp = r.Next(8);
                    if (temp != sporn1.X && temp != porn[pnum - 1].X)
                    {
                        porn[pnum - 1].X = temp;
                        porn[pnum - 1].Y = 0;
                        done = true;
                    }
                }
            }

            if (porn[pnum - 2].Y < 0)
            {
                for (bool done = false; done == false; )
                {
                    int temp = r.Next(8);
                    if (temp != sporn1.X && temp != porn[pnum - 2].X)
                    {
                        porn[pnum - 2].X = temp;
                        porn[pnum - 2].Y = 0;
                        done = true;
                    }
                }
            }

            //enemy porn reach our front line
            for (bool done = false; done == false; )
            {
                done = true;

                for (int j = 0; j < pnum; j++)
                {
                    if (porn[j].Y == 8)
                    {
                        enemybreach++;
                        Point[] pornnew = deletearray(porn, j);
                        Array.Resize(ref porn, pnum - 1);
                        porn = pornnew;
                        pnum--;

                        done = false;
                        break;
                    }
                }

            }


            textBox2.Text = Convert.ToString(Convert.ToInt32(textBox2.Text) + enemybreach);

            enemybreach = 0;

            textBox1.Text = Convert.ToString(pnum);

            drawporn();

            //lighten disappear        
            for (int i = 0; i < 4; i++)
            {
                if (plight[i] != new Point(-2, -2))
                {
                    if (turn == true)
                    {
                        turn = false;

                        plight[i] = new Point(-2, -2);
                    }
                    else
                    {
                        turn = true;
                    }
                    break;
                }
            }


            lightening = kill;

            this.Refresh();
        }

        private void drawporn()
        {
            foreach (PictureBox x in pornpic)
            {
                this.Controls.Remove(x);
            }

            pornpic = new PictureBox[pnum];

            for (int i = 0; i < pnum; i++)
            {
                pornpic[i] = new PictureBox();
            }

            for (int i = 0; i < pnum; i++)
            {
                pornpic[i].Location = new System.Drawing.Point(porn[i].X * square, porn[i].Y * square);
                pornpic[i].BackColor = Color.Transparent;
                pornpic[i].ClientSize = new Size(square, square);
                Image pornimg = Image.FromFile("black porn.png");
                Image pornimgtrans = ChangeOpacity(pornimg, 0.5f);
                pornpic[i].Image = pornimgtrans;
                this.Controls.Add(pornpic[i]);
                pornpic[i].SendToBack();

                //x.MouseClick += new MouseEventHandler(Form1_MouseClick);
                pornpic[i].MouseClick += Form1_MouseClick;
            }
        }

        private Point[] addarray(Point[] array1, int num, int px, int py)
        {
            Point[] oldarray = array1;
            for (int i = 0; i < num - 1; i++)
            {
                array1[i] = oldarray[i];
            }
            array1[num - 1].X = px;
            array1[num - 1].Y = py;

            return array1;
        }

        private Point[] deletearray(Point[] array1, int index)
        {
            Point[] newarray = new Point[pnum - 1];
            for (int i = 0; i < index; i++)
            {
                newarray[i] = array1[i];
            }

            for (int i = index; i < pnum - 1; i++)
            {
                newarray[i] = array1[i + 1];
            }

            return newarray;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Rectangle bounds = this.Bounds;
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
            Graphics g = Graphics.FromImage(bitmap);

            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
            bitmap.Save("test" + picnum + ".jpg");
            picnum++;
        }
    }
}


reference:
picturebox opacity:

No comments:

Post a Comment