自己做的一个贪吃蛇的小游戏。
我想让这个小游戏可以根据自己的要求选择背景的格子的行数与列数,还想添加暂停按钮,随机选取背景中的几个格子,作为障碍物,用其他的颜色显示。
希望高手指点一下。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace snake
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
enum Direction {east,west,north,south};
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
Keys direction;//蛇行进的方向
Size s;
ArrayList snake;
Brush b_p;
Brush b_s;
Brush b_sh;
Brush b_b;
Point bean;//豆子
int speed;
int acc_speed
{
get
{
return speed;
}
set
{
if(value>=0&&value<9)
speed=value;
}
}
Direction dr;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
s=new Size(29,29);
Initialize_snake();
b_p=new SolidBrush(Color.Gold);
b_s=new SolidBrush(Color.BlueViolet);
b_sh=new SolidBrush(Color.Red);
b_b=new SolidBrush(Color.Silver);
direction =Keys.Right;//初始化方向为向右
bean=new Point();
Generate_bean();
acc_speed=0;
// dr=Direction.east;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void Initialize_snake()
{
snake = new ArrayList();
snake.Add(new Point(0, 0));
// snake.Add(new Point(0, 30));
// snake.Add(new Point(0, 60));
// snake.Add(new Point(0, 90));
// snake.Add(new Point(0, 120));
}
private void Generate_bean()
{
Random r=new Random();
do
{
int x=r.Next(10);//随机生成大于等于0小于10的整数
int y=r.Next(10);
bean.X=x*30;
bean.Y=y*30;
}while(point_in_snake(bean));//判断豆子在不在蛇上
}
private bool point_in_snake(Point p)//p——豆子的点坐标
{
for(int i=0;i<snake.Count;i++)
{
if(p.Equals(snake[i]))
return true;
}
return false;
}
private void draw_bean(Graphics g)
{
draw_rec(bean,g,b_b);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.WindowText;
this.ClientSize = new System.Drawing.Size(296, 300);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(304, 330);
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
draw_panel(e.Graphics);
draw_snake(e.Graphics);
draw_bean(e.Graphics);
}
private bool point_valid(Point p)
{
if(p.X<0||p.X>=300||p.Y<0||p.Y>=300||point_in_snake(p))
return false;
else
return true;
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Right||e.KeyCode==Keys.Left||
e.KeyCode==Keys.Up||e.KeyCode==Keys.Down)
{
draw(e.KeyCode);
direction=e.KeyCode;
}
}
private void draw(Keys direct)
{
Graphics g_key=this.CreateGraphics();
draw_panel(g_key);
find_snake_d(direct);
if(valid_sanke())//如果蛇的新位置合法,判断蛇是否吃到豆
{
if(point_in_snake(bean))//吃到豆子
{
snake_add();//使蛇长长
draw_snake(g_key);//画长长的新蛇
Generate_bean();//重新生成豆子
draw_bean(g_key);//画豆子
}
else//没吃到豆子
{
draw_snake(g_key);//画蛇
g_key.FillRectangle(new SolidBrush(Color.Silver),
bean.X,bean.Y,
29,29);//画豆子
}
}
else
{
timer1.Enabled=false;
MessageBox.Show("you are lost!");
Initialize_snake();
Generate_bean();
draw_snake(g_key);
draw_bean(g_key);
direction=Keys.Right;
timer1.Enabled=true;
}
g_key.Dispose();
}
private void snake_add()
{
Point p1=(Point)snake[snake.Count-1];
Point p;
if(point_valid(new Point(p1.X-30,p1.Y)))
{
p=new Point(p1.X-30,p1.Y);
snake.Add(p);
return;
}
else if(point_valid(new Point(p1.X+30,p1.Y)))
{
p=new Point(p1.X+30,p1.Y);
snake.Add(p);
return;
}
else if(point_valid(new Point(p1.X,p1.Y-30)))
{
p=new Point(p1.X,p1.Y-30);
snake.Add(p);
return;
}
else if(point_valid(new Point(p1.X,p1.Y+30)))
{
p=new Point(p1.X,p1.Y+30);
snake.Add(p);
return;
}
else
Initialize_snake();
}
private bool valid_sanke()
{
Point p=(Point)snake[0];
if((p.X<0)||(p.X>=300)||(p.Y<0)||(p.Y>=300))
return false;
if(headinsnake())
return false;
else
return true;
}
private bool headinsnake()
{
Point p=(Point)snake[0];
for(int i=1;i<snake.Count;i++)
{
Point pc=(Point)snake[i];
if(p.Equals(pc))
return true;
}
return false;
}
private void find_snake_d(Keys key)
{
int l = snake.Count;
for (int i = l - 1; i > 0; i--)
snake[i] = snake[i - 1];
Point p0 = (Point)snake[0];
switch (key)
{
case Keys.Down: p0.Y += 30; break;
case Keys.Up: p0.Y -= 30; break;
case Keys.Left: p0.X -= 30; break;
case Keys.Right: p0.X += 30; break;
default:break;
}
snake[0] = p0;
}
private void draw_panel(Graphics g)
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
Point p = new Point(i * 30, j * 30);
draw_rec(p, g, b_p);
}
}
private void draw_snake(Graphics g)
{
foreach (Point snake_i in snake)
{
draw_rec(snake_i, g, b_s);
}
Point p=(Point)snake[0];
draw_rec(p,g,b_sh);
}
private void draw_rec(Point p,Graphics g,Brush b)
{
g.FillRectangle(b,new Rectangle(p,s));
}
private void timer1_Tick(object sender, System.EventArgs e)
{
Graphics g_key=this.CreateGraphics();
draw_panel(g_key);
find_snake_d(direction);
if(valid_sanke())//如果蛇的新位置合法,判断蛇是否吃到豆
{
if(point_in_snake(bean))//吃到豆子
{
snake_add();//使蛇长长
draw_snake(g_key);//画长长的新蛇
Generate_bean();//重新生成豆子
draw_bean(g_key);//画豆子
}
else//没吃到豆子
{
draw_snake(g_key);//画蛇
g_key.FillRectangle(new SolidBrush(Color.Silver),
bean.X,bean.Y,
29,29);//画豆子
}
}
else
{
timer1.Enabled=false;
MessageBox.Show("you are lost!");
Initialize_snake();
Generate_bean();
draw_snake(g_key);
draw_bean(g_key);
timer1.Enabled=true;
}
g_key.Dispose();
}
}
}