最近抽时间完成了老师要求制作的一个贪吃蛇的小游戏,使用c#完成的。
基本上的功能实现了蛇吃豆子增长身体,选择游戏速度,根据速度计分,游戏结束条件为蛇碰到自身。
图形是用c#中的Graphics绘制的图形,这和我之前写的俄罗斯方块的实现方法略有不同,但是从用户体验来说感觉是一样的。
其他的不说了,自己发个 留个纪念。以后也许还会多做一些功能。
主要代码如下:
private List<point> snake;
private System.Timers.Timer mytimer;
private enum Direction
{
UP,
RIGHT,
DOWN,
LEFT,
};
private string dirNow;
private int speed;
private bean beanfactroy;
private point bean;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;
private System.Windows.Forms.Panel gamePanel;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem3;
private System.Windows.Forms.ToolStripMenuItem 减小难度ToolStripMenuItem;
private System.Windows.Forms.Label scores;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ToolStripMenuItem 关于ToolStripMenuItem;
form:
public Form1()
{
speed = 100;
InitializeComponent();
dirNow = Direction.DOWN.ToString();
snake = new List<point>(6);
beanfactroy = new bean(snake, 400, 400);
bean = beanfactroy.creatBean();
mytimer = new System.Timers.Timer(speed);
mytimer.Elapsed += new System.Timers.ElapsedEventHandler(this.drew);
mytimer.AutoReset = true;
mytimer.Enabled = true;
}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
initGame();
if (!mytimer.Enabled)
mytimer.Start();
}
private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (mytimer.Enabled)
mytimer.Stop();
else
mytimer.Start();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void initGame()
{
speed = 100;
scores.Text = "0";
snake = new List<point>(6);
dirNow = Direction.DOWN.ToString();
for (int i = 0; i < 6; i++)
{
point p = new point(i * 10, 30,dirNow.ToString());
snake.Add(p);
}
}
public void drew(object sender, System.Timers.ElapsedEventArgs e)
{
Graphics g = gamePanel.CreateGraphics();
g.Clear(Color.Black);
int count = snake.Count-1;
for (int i = count; i > 0; i--)
{
snake[i].setX(snake[i - 1].getX());
snake[i].setY(snake[i - 1].getY());
g.DrawRectangle(new Pen(Color.Red), new Rectangle(snake[i].getX(), snake[i].getY(), 10, 10));
}
Nodeturn();
g.DrawImage(takeNode(), snake[0].getX(), snake[0].getY());
if (snake[0].getX() == bean.getX() && snake[0].getY() == bean.getY())
eatBean();
g.DrawPie(new Pen(Color.Yellow),new Rectangle( bean.getX(), bean.getY(),10,10),0.0F, 360F);
fail();
}
public void fail()
{
for (int i = 1; i < snake.Count; i++)
{
if (snake[0].getX() == snake[i].getX() && snake[0].getY() == snake[i].getY())
{
mytimer.Stop();
MessageBox.Show("亲,你咬到自己了……");
break;
}
}
}
public void eatBean()
{
bean = beanfactroy.creatBean();
snake.Add(new point(snake[snake.Count-1].getX(),snake[snake.Count-1].getY(),snake[snake.Count-1].getdir()));
scores.Text = ""+ (int.Parse(scores.Text) + 2000 / speed);
}
public Image takeNode()
{
Image img=global::snake.Properties.Resources.node;
Bitmap bitmap = new Bitmap(img);
if (dirNow.Equals(Direction.DOWN.ToString()))
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
if (dirNow.Equals(Direction.LEFT.ToString()))
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
if (dirNow.Equals(Direction.RIGHT.ToString()))
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
img = bitmap;
return img;
}
public void Nodeturn()
{
if (dirNow.Equals(Direction.UP.ToString()))
{
if (snake[0].getY() == 0)
snake[0].setY(390);
else
snake[0].setY(snake[0].getY() - 10);
}
if (dirNow.Equals(Direction.DOWN.ToString()))
{
if (snake[0].getY() == 390)
snake[0].setY(0);
else
snake[0].setY(snake[0].getY() + 10);
}
if (dirNow.Equals(Direction.LEFT.ToString()))
{
if (snake[0].getX() == 0)
snake[0].setX(390);
else
snake[0].setX(snake[0].getX() - 10);
}
if (dirNow.Equals(Direction.RIGHT.ToString()))
{
if (snake[0].getX() == 390)
snake[0].setX(0);
else
snake[0].setX(snake[0].getX() + 10);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && dirNow != Direction.DOWN.ToString())
dirNow = Direction.UP.ToString();
if (e.KeyCode == Keys.Down&&dirNow != Direction.UP.ToString())
dirNow = Direction.DOWN.ToString();
if (e.KeyCode == Keys.Right && dirNow != Direction.LEFT.ToString())
dirNow = Direction.RIGHT.ToString();
if (e.KeyCode == Keys.Left&&dirNow != Direction.RIGHT.ToString())
dirNow = Direction.LEFT.ToString();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (speed > 50)
{
speed -= 50;
mytimer.Interval = speed;
}
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
if (speed < 2000)
{
speed += 100;
mytimer.Interval = speed;
}
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Hourglass's Game. v1.0 .","CopyRight");
}
}
bean :
public bean(List<point> snake,int mapX,int mapY)
{
this.snake=snake;
this.mapX = mapX;
this.mapY = mapY;
}
public point creatBean()
{
System.Random random = new Random();
int x = random.Next(mapX/10)*10;
int y = random.Next(mapY/10)*10;
for (int i = 0; i < snake.Count(); i++)
{
point p = snake[i];
if (x == p.getX() && y == p.getY())
{
x = random.Next(mapX / 10) * 10;
y = random.Next(mapY / 10) * 10;
i=0;
}
}
return new point(x, y, null);
}
class point
{
private int x;
private int y;
private string dir;
public point(int x, int y,string dir)
{
this.x = x;
this.y = y;
this.dir = dir;
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setdir(string dir)
{
this.dir = dir;
}
public string getdir()
{
return dir;
}
}
运行效果图: