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;
namespace snake
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Random r = new Random();
preypoint = new Point[preynum];
obspoint = new Point[obsnum];
tailpoint = new Point[tailnum];
//create obstacle
for (int i = 0; i < obsnum; i++)
{
obspoint[i] = new Point(-1, -1);
}
for (int j = 0; j < obsnum; )
{
Point newobs = new Point(50 * Convert.ToInt32(r.Next(10)), 50 * Convert.ToInt32(r.Next(10)));
bool replica = false;
for (int k = 0; k < obsnum; k++)
{
if (obspoint[k] == newobs)
{
replica = true;
break;
}
}
if (replica == false)
{
obspoint[j] = newobs;
j++;
}
}
//create prey
for (int i = 0; i < preynum; i++)
{
preypoint[i] = new Point(-1, -1);
}
for (int j = 0; j < preynum; )
{
Point newprey = new Point( 50 * Convert.ToInt32(r.Next(10)), 50 * Convert.ToInt32(r.Next(10)));
bool replica = false;
for (int i = 0; i < obsnum; i++)
{
if (obspoint[i] == newprey)
{
replica = true;
break;
}
}
if (replica == false)
{
for (int k = 0; k < preynum; k++)
{
if (preypoint[k] == newprey)
{
replica = true;
break;
}
}
}
if (replica == false)
{
preypoint[j] = newprey;
j++;
}
}
timer1.Start();
timer2.Start();
}
private Point head = new Point(25, 25);
private int rotateangle = 0, offsetx = 0, offsety=0, dir=2, predir=2;
private int preynum = 15,tailnum=0, obsnum = 10, picnum=0;
private Point[] preypoint,tailpoint,obspoint;
private void Form1_Paint(object sender, PaintEventArgs e)
{
//obstacle
Pen redpen = new Pen(Color.Red, 3);
for (int i = 0; i < obsnum; i++)
{
Point obs1 = new Point(obspoint[i].X + 50, obspoint[i].Y + 50);
Point obs2 = new Point(obspoint[i].X + 50, obspoint[i].Y);
Point obs3 = new Point(obspoint[i].X, obspoint[i].Y + 50);
e.Graphics.DrawLine(redpen, obspoint[i], obs1);
e.Graphics.DrawLine(redpen, obs2, obs3);
}
//prey
SolidBrush greenBrush = new SolidBrush(Color.Green);
Size rectsize = new Size(50,50);
for (int i = 0; i < preynum; i++)
{
Rectangle preyrect = new Rectangle(preypoint[i], rectsize);
e.Graphics.FillRectangle(greenBrush, preyrect);
}
//snake tail
SolidBrush blackBrush = new SolidBrush(Color.Black);
for (int i = 0; i < tailnum; i++)
{
Rectangle tailrect = new Rectangle(tailpoint[i], rectsize);
e.Graphics.FillRectangle(blackBrush, tailrect);
}
//snake head
Point point1 = new Point(0, 0);
Point point2 = new Point(50 , 0 );
Point point3 = new Point(25 , 25 );
Point point4 = new Point(50 , 50 );
Point point5 = new Point(0 , 50);
Point[] curvePoints = { point1, point2, point3, point4, point5, };
SolidBrush blueBrush = new SolidBrush(Color.Blue);
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(curvePoints);
//snake crawl
Matrix rmatrix = new Matrix(1, 0, 0, 1, 0, 0);
rmatrix.RotateAt(rotateangle,head);
gp.Transform(rmatrix);
Matrix omatrix = new Matrix(1, 0, 0, 1, 0, 0);
omatrix.Translate(offsetx, offsety);
gp.Transform(omatrix);
e.Graphics.FillPath(blueBrush, gp);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string key = e.KeyData.ToString();
switch (key)
{
case "Left":
dir = 1;
break;
case "Right":
dir = 2;
break;
case "Up":
dir = 3;
break;
case "Down":
dir = 4;
break;
default:
break;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
//cannot make 180 turn
if (predir == 1 && dir == 2) { dir = 1; }
if (predir == 2 && dir == 1) { dir = 2; }
if (predir == 3 && dir == 4) { dir = 3; }
if (predir == 4 && dir == 3) { dir = 4; }
bool eat = false;
int eatindex = 0, oldoffsetx = offsetx, oldoffsety = offsety;
//move head, if hit wall, game over
switch (dir)
{
case 1:
rotateangle = 180;
if (offsetx > 0) { offsetx -= 50; }
else { timer1.Stop(); MessageBox.Show("game over"); }
break;
case 2:
rotateangle = 0;
if (offsetx < 450) { offsetx += 50; }
else { timer1.Stop(); MessageBox.Show("game over"); }
break;
case 3:
rotateangle = -90;
if (offsety > 0) { offsety -= 50; }
else { timer1.Stop(); MessageBox.Show("game over"); }
break;
case 4:
rotateangle = 90;
if (offsety < 450) { offsety += 50; }
else { timer1.Stop(); MessageBox.Show("game over"); }
break;
default:
break;
}
predir = dir;
//if head eats tail, game over
for (int i = 0; i < tailnum; i++)
{
if (tailpoint[i].X == offsetx && tailpoint[i].Y == offsety)
{
timer1.Stop(); MessageBox.Show("game over");
}
}
//if hit obstacle, game over
for (int i = 0; i < obsnum; i++)
{
if (obspoint[i].X == offsetx && obspoint[i].Y == offsety)
{
timer1.Stop(); MessageBox.Show("game over");
}
}
//check if catch prey
for (int i = 0; i < preynum; i++)
{
if (preypoint[i].X == offsetx && preypoint[i].Y == offsety)
{
eat = true;
eatindex = i;
break;
}
}
//if catch prey, eat prey, grow tail
if (eat == true)
{
Point[] oldprey = new Point[preynum];
oldprey = preypoint;
Point[] oldtail = new Point[tailnum];
oldtail = tailpoint;
preynum--; tailnum++;
Array.Resize(ref preypoint, preynum);
Array.Resize(ref tailpoint, tailnum);
for (int j = 0; j <= preynum; j++)
{
if (j < eatindex)
{
preypoint[j] = oldprey[j];
}
else if (j > eatindex)
{
preypoint[j - 1] = oldprey[j];
}
}
for (int k = 0; k < tailnum - 1; k++)
{
tailpoint[k] = oldtail[k];
}
tailpoint[tailnum - 1].X = oldoffsetx;
tailpoint[tailnum - 1].Y = oldoffsety;
}
//no prey, continue prowl
else if (tailnum > 0)
{
for (int i = 0; i < tailnum - 1; i++)
{
tailpoint[i] = tailpoint[i + 1];
}
tailpoint[tailnum - 1].X = oldoffsetx;
tailpoint[tailnum - 1].Y = oldoffsety;
}
//win game
if (preynum == 0)
{
timer1.Stop();
MessageBox.Show("you win");
}
this.Refresh();
}
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++;
}
}
}
No comments:
Post a Comment