您的位置:首页 > 其它

wince 贪吃蛇

2016-06-24 09:18 309 查看
snake.cs 文件

enum Direction //定义蛇的移动方向的枚举类型

{

UP, DOWN, LEFT, RIGHT

}

class Snake

{

public const int side = 10; //蛇身每个节点的长度

Point[] body = new Point[100]; //蛇最大长度

Direction direction; //蛇的初始移动方向

Boolean live; //蛇的存活状态

int number; //蛇身节点个数

int speed = 200; //蛇的移动速度

public int Speed

{

get

{

return speed;

}

set

{

speed = value;

}

}

public int Score

{

get

{

return (number - 2) * 10;

}

}

public Direction Direction

{

get

{

return direction;

}

set

{

direction = value;

}

}

public Boolean Live

{

get

{

return live;

}

}

public int Number

{

get

{

return number;

}

}

public Point[] Body

{

get

{

return body;

}

}

public Snake() //蛇的初始化,长度为2,方向为向右,存活状态为活

{

Point node1 = new Point(100, 40);

Point node2 = new Point(90, 40);

body[0] = node1;

body[1] = node2;

number = 2;

direction = Direction.RIGHT;

live = true;

}

public void Move(Size range) //蛇的移动方法,参数为蛇的移动范围

{

for (int i = this.number - 1; i > 0; i--)

{

body[i] = body[i - 1]; //从蛇尾开始将蛇身的每前一个节点坐标值赋给后一个坐标点

}

switch (this.direction) //根据蛇的移动方向来设定蛇头的移动方向

{

case Direction.UP:

body[0].Y -= side; break;

case Direction.DOWN:

body[0].Y += side; break;

case Direction.LEFT:

body[0].X -= side; break;

case Direction.RIGHT:

body[0].X += side; break;

}

//如果蛇头的位置超过了参数的范围,则将蛇的存活状态设为false,即蛇死亡

if ((body[0].X < 0) || (body[0].X > range.Width - side) || (body[0].Y < 0) || (body[0].Y > range.Height - side))

{

this.live = false;

}

for (int i = 1; i <= number - 1; i++) //蛇如果撞到自身,蛇也死亡

{

if (body[0].X == body[i].X && body[0].Y == body[i].Y)

{

this.live = false;

}

}

}

//蛇吃食物的方法,参数传递的是蛇将要吃的食物

public void Eat(Food food)

{

body[number] = food.Position; //通过将要吃的食物的坐标点值赋给蛇的最后一位,以实现蛇吃食物

this.number++; //蛇身的长度加

}

public void Draw(Graphics g) //画蛇的方法,传递的参数为画蛇所用的面板

{

Pen pens = new Pen(Color.DarkRed, 1);

SolidBrush brush = new SolidBrush(Color.DarkRed);

g.DrawRectangle(pens, body[0].X, body[0].Y, side, side);

g.FillRectangle(brush, body[0].X, body[0].Y, side, side);

for (int i = 1; i <= number - 1; i++)

{

Pen pen = new Pen(Color.Red);

g.DrawRectangle(pen, body[i].X, body[i].Y, side, side);

}

if (this.live == false)

{

g.DrawString("GAME OVER!", new Font("楷体_GB2312", 36, FontStyle.Bold), brush, 120, 200);

}

}

public void resetSnake() //重新设置蛇的状态

{

Point node1 = new Point(100, 40);

Point node2 = new Point(90, 40);

body[0] = node1;

body[1] = node2;

number = 2;

direction = Direction.RIGHT;

live = true;

}

}

food.cs 文件

class Food

{

Point position; //食物的出现位置

Boolean exist; //食物的存在状态

public Food()

{

position = this.creatFood();

exist = true;

}

public Point Position

{

get

{

return position;

}

set

{

position = value;

}

}

public Boolean Exist

{

get

{

return exist;

}

set

{

exist = value;

}

}

public Point creatFood() //随机创建一个食物返回的值为食物的坐标点

{

Point position = new Point();

Random ran = new Random();

position.X = ran.Next(1, 23) * 10;

position.Y = ran.Next(1, 19) * 10;

return position;

}

public void Draw(Graphics g) //在传递过来的绘图面板中绘制一个食物

{

Pen pen1 = new Pen(Color.Black, 1);

SolidBrush brush1 = new SolidBrush(Color.Black);

//g.FillRectangle(Brush brush, position.X, position.Y, 10, 10);

g.DrawRectangle(pen1, position.X, position.Y, 10, 10);

g.FillRectangle(brush1, position.X, position.Y, 10, 10);

}

}

操作界面

public partial class TCS : Form

{

Snake snake;

Food food;

public TCS()

{

InitializeComponent();

snake = new Snake();

food = new Food();

movetimer.Enabled = false;

movetimer.Interval = snake.Speed;

cmb_level.Text = "一般级";

}

private void movetimer_Tick(object sender, EventArgs e)

{

snake.Move(panel1.Size);

panel1.Invalidate();

panel1.Update();

}

private void panel1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

movetimer.Interval = snake.Speed;

snake.Draw(g);

if (snake.Live == false)

{

movetimer.Enabled = false;

}

if (snake.Body[0].X == food.Position.X && snake.Body[0].Y == food.Position.Y)

{

snake.Eat(food);

food.Exist = false;

this.txt_score.Text = ((snake.Number - 2) * 100).ToString();

}

if (!food.Exist)

{

food.Position = food.creatFood();

food.Exist = true;

food.Draw(g);

}

else

{

food.Draw(g);

}

g.Dispose();

}

private void btn_up_Click(object sender, EventArgs e)

{

if (snake.Direction != Direction.DOWN)

{

snake.Direction = Direction.UP;

}

}

private void btn_down_Click(object sender, EventArgs e)

{

if (snake.Direction != Direction.UP)

{

snake.Direction = Direction.DOWN;

}

}

private void btn_left_Click(object sender, EventArgs e)

{

if (snake.Direction != Direction.RIGHT)

{

snake.Direction = Direction.LEFT;

}

}

private void btn_right_Click(object sender, EventArgs e)

{

if (snake.Direction != Direction.LEFT)

{

snake.Direction = Direction.RIGHT;

}

}

private void btn_start_Click(object sender, EventArgs e)

{

movetimer.Enabled = true;

}

private void btn_restart_Click(object sender, EventArgs e)

{

panel1.Invalidate();

snake.resetSnake();

}

private void btn_pause_Click(object sender, EventArgs e)

{

movetimer.Enabled = false;

}

private void btn_exit_Click(object sender, EventArgs e)

{

this.Close();

}

private void cmb_level_SelectedIndexChanged(object sender, EventArgs e)

{

if (cmb_level.SelectedItem.ToString() == "脑残级")

{

snake.Speed = 200;

}

if (cmb_level.SelectedItem.ToString() == "一般级")

{

snake.Speed = 130;

}

if (cmb_level.SelectedItem.ToString() == "牛x级")

{

snake.Speed = 100;

}

if (cmb_level.SelectedItem.ToString() == "大神级")

{

snake.Speed = 50;

}

if (cmb_level.SelectedItem.ToString() == "坑爹级")

{

snake.Speed = 10;

}

}

private void TCS_KeyPress(object sender, KeyPressEventArgs e)

{

//if (e.KeyChar == '')

//{

// btn_pause_Click(this.btn_pause, e);

//}

//else

if (e.KeyChar == '2')

{

btn_up_Click(this.btn_up, e);

}

else if (e.KeyChar == '8')

{

btn_down_Click(this.btn_down, e);

}

else if (e.KeyChar == '4')

{

btn_left_Click(this.btn_left, e);

}

else if (e.KeyChar == '6')

{

btn_right_Click(this.btn_right, e);

}

else

{

// MessageBox.Show("未识别按键: " + e.KeyChar.ToString());

}

}

//protected override bool ProcessDialogKey(Keys keyData)

//{

// switch (keyData)

// {

// case Keys.Up: //TODO:

// if (snake.Direction != Direction.DOWN)

// {

// snake.Direction = Direction.UP;

// }

// break;

// case Keys.Down: //TODO:

// if (snake.Direction != Direction.UP)

// {

// snake.Direction = Direction.DOWN;

// }

// break;

// case Keys.Left: //TODO:

// if (snake.Direction != Direction.RIGHT)

// {

// snake.Direction = Direction.LEFT;

// }

// break;

// case Keys.Right: //TODO:

// if (snake.Direction != Direction.LEFT)

// {

// snake.Direction = Direction.RIGHT;

// }

// break;

// }

// return base.ProcessDialogKey(keyData);

//}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wince