贪吃蛇代码
小东西帮我们步入c#编程的殿堂
Formmain.cs
using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace snake
{
public partial class Formmain : Form // partial代
局部类型,也指是自动生成的代码
{
private Palette p;
public Formmain()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)//更??新?游??戏??画-面?
{
if (p != null)
{
p.PaintPalette(e.Graphics);
}
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)//相??应?|用??户?ì按ã?ä键??
{
if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up) && p.Direction != Direction.Down)
{
p.Direction = Direction.Up;
return;
}
if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) && p.Direction != Direction.Up)
{
p.Direction = Direction.Down;
return;
}
if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p.Direction != Direction.Left)
{
p.Direction = Direction.Right;
return;
}
if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p.Direction != Direction.Right)
{
p.Direction = Direction.Left;
return;
}
}
private void FormMain_Load(object sender, EventArgs e)//做Á?一??些?初?始º?化??工?è作Á?Â
{
int width, height, size;//定??义??画-布?长?è宽?ª高?,ê?及?ã每?个?蛇??块??的Ì?大ä??小?
width = height = 20;
size = 15;
//设???定??游??戏??窗ä?ã口??大ä??小?
this.pictureBox1.Width = width * size;
this.pictureBox1.Height = height * size;
this.Width = pictureBox1.Width + 30;
this.Height = pictureBox1.Height + 60;
//定??义??一??个?新?的Ì?画-布?(ê??宽?ª度??、?é高?度??、?é单Ì??位?大ä??小?、?é背À3景?ã色??、?é绘?图ª?句?柄À??,ê?游??戏??等Ì??级?)ê?
p = new Palette(width, height, size, this.pictureBox1.BackColor, Graphics.FromHwnd(this.pictureBox1.Handle), 5);
//游??戏??开a始º?
p.start();
}
}
}
Block.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace snake
{
class Block//block类用于描述蛇快的颜色、大小、坐标等的信息
{
private Color _color;//记??录??蛇????的Ì?Ì???颜??色????
private Point _point;//记??录??蛇????的Ì?Ì???坐Á?Á??é?标À?ºÀ??è??ìº
private int _size;//记??录??蛇????的Ì?Ì???单Ì??Ì????º???位??大ä??ä??ì?小??
public Block(Color color, int size, Point p)
{
this._color = color;
this._point = p;
this._size = size;
}
public Point Point
{
get { return this._point; }
}
//绘??制??自Á?Á??é?身??ª???ìª到Ì?Ì???画--布??
public virtual void Paint(Graphics g)
{
SolidBrush sb = new SolidBrush(_color);
lock (g)
{
try
{
g.FillRectangle(sb, this._point.X * this._size, this._point.Y * this._size,
this._size - 1, this._size - 1);
}
catch
{ }
}
}
}
}
Palette.cs
using System;
using System.Drawing;
using System.Collections; using System.Timers;
namespace snake
{
class Palette
{
private int _width = 20;//高??度????ì??ì
private int _height = 20;//宽?ª??ìª度????ì??ì
private Color _bgcolor;//背À3À??è3景?ã?ã颜??色????
private Graphics _bgpalette;//画--布??
private ArrayList _blocks;//蛇????块????ì?列?D???D表À?ªÀ??è??ìª
private Graphics _gpPalatte;//画-布?
private Direction _direction;//方??向??
private Timer timerblock;//更??新?器?Â
private Block _food;//当Ì?À前?ã食º3物?
private int _size = 20;//定??义??单Ì??位?大ä??小?
private int _level = 1;//等Ì??级?
private bool _isgameover = false;
private int[] _speed = new int[] { 500, 450,400,350,300,250,200,150,100,50 };//游??戏??速??度??
//构1造??函??数ºy,ê?传ä?参?,ê?初?始º?化??。?ê
public Palette(int width, int height, int size, Color bgcolor, Graphics g, int lvl)
{
this._width = width;
this._bgcolor = bgcolor;
this._size = size;
this._height = height;
this._bgpalette = g;
this._level = lvl;
this._blocks = new ArrayList();
//画-布?正y中D有?D一??节??蛇??块??的Ì?贪??ã吃?蛇??
this._blocks.Insert(0, (new Block(Color.Red, this._size, new Point(width / 2, height / 2))));
this._direction = Direction.Right;
}
public Direction Direction
{
{return this._direction;} get
set { this._direction = value; }
}
public void start()//开a始º?游??戏??
{
this._food = GetFood();//生???成??一??个?食º3物?
//初?始º?化??计?时º?À器?Â
timerblock = new System.Timers.Timer(_speed[this._level]);
timerblock.Elapsed +=new System.Timers.ElapsedEventHandler(onblocktimereven);
timerblock.AutoReset = true;
timerblock.Start();
}
private bool CheckDead()//检??查??游??戏??是º?否??结??束º?
{
Block head = (Block)(this._blocks[0]);//取??出?蛇??块??的Ì?第Ì??一??个?,ê?即?ä蛇??头ª??
//检??查??是º?否??超?出?画-布?范??围?ì
if (head.Point.X < 0 || head.Point.Y < 0 || head.Point.X >= this._width || head.Point.Y >=
this._height)
return true;
for (int i = 1; i < this._blocks.Count; i++) //检??查??是º?否??撞Á2上??自Á?己o
{
Block b = (Block)this._blocks[i];
if (head.Point.X == b.Point.X && head.Point.Y == b.Point.Y)
{
this._isgameover = true;
return true;
}
}
this._isgameover = false;
return false;
}
private void onblocktimereven(object source, ElapsedEventArgs e)//定??时º?À更??新?
{
this.move();//前?ã进?一??个?单Ì??位?
if (this.CheckDead())//检??测a是º?否??结??束º?
{
this.timerblock.Stop();
this.timerblock.Dispose();
System.Windows.Forms.MessageBox.Show("Score:" + this._blocks.Count, "GAME
OVER");
}
}
private void move()//前?ã进?一??截?
{
Point p;//下?一??个?坐Á?标À?º位?置?
Block head = (Block)(this._blocks[0]);
if (this._direction == Direction.Up)
p = new Point(head.Point.X, head.Point.Y - 1);
else if (this._direction == Direction.Down)
p = new Point(head.Point.X, head.Point.Y + 1);
else if (this._direction == Direction.Left)
p = new Point(head.Point.X - 1, head.Point.Y);
else
p = new Point(head.Point.X+1, head.Point.Y);
//生???成??新?坐Á?标À?º,ê?将?来??ä生???成??蛇??头ª??
Block b = new Block(Color.Red, this._size, p);
//如??果?下?一??坐Á?标À?º不?是º?食º3物?坐Á?标À?º,ê?那?么?ä删??除y蛇??身??ª最Á?后??一??个?蛇??块??
if (this._food.Point != p)
this._blocks.RemoveAt(this._blocks.Count - 1);
//如??果?下?一??个?坐Á?标À?º和?ª食º3物?重?合?,ê?那?么?ä就?ª生???成??一??个?新?的Ì?食º3物?
else
this._food = this.GetFood();
//把ã?下?一??个?下?一??个?坐Á?标À?º插?入??到Ì?蛇??块??信?息?é列?D表À?ª的Ì?第Ì??一??个?,ê?使º1其?成??为a蛇??头ª??
this._blocks.Insert(0, b);
this.PaintPalette(this._bgpalette);//更??新?画-板ã?
}
public void PaintPalette(Graphics gp)//更??新?画-面?函??数ºy
{
gp.Clear(this._bgcolor);
this._food.Paint(gp);
foreach (Block b in this._blocks)
b.Paint(gp);
}
private Block GetFood()//生???成??下?一??个?食º3物?
{
Block food = null;
Random r=new Random();
bool redo = false;
while (true)
{
redo = false;
int x = r.Next(this._width);
int y = r.Next(this._height);
for (int i = 0; i < this._blocks.Count; i++)//检??查??
{
Block b = (Block)(this._blocks[i]);
if(b.Point.X==x && b.Point.Y==y)//有?D冲?突ª?时º?À再??随?机??分??配?一??个?坐Á?标À?º
{
redo= true;
}
}
if (redo == false)
{
food = new Block(Color.Black, this._size, new Point(x, y));
break;
}
}
return food;
}