标题:[转载]用C#编的贪吃蛇游戏
只看楼主
htzz
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-3-10
 问题点数:0 回复次数:6 
[转载]用C#编的贪吃蛇游戏
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <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 = 300;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(202, 196);
this.Cursor = System.Windows.Forms.Cursors.Cross;
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(210, 230);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(210, 230);
this.Name = "Form1";
this.Text = "-逍遥- 贪吃蛇";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.Click += new System.EventHandler(this.Form1_Deactivate);
this.Load += new System.EventHandler(this.Form1_Load_1);
this.Activated += new System.EventHandler(this.Form1_Activated);
this.Deactivate += new System.EventHandler(this.Form1_Deactivate);

}
#endregion


/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

Game game = new Game();
private void timer1_Tick(object sender, System.EventArgs e)
{
game.Move();
game.Refresh();
if (game.Game_Over==true)
{
timer1.Stop();
MessageBox.Show("GAME OVER");
MessageBox.Show("The length of your snake is " + game.Snake_Length.ToString());
}
}
private void Form1_Load_1(object sender, System.EventArgs e)
{
game.Spc = this.CreateGraphics();
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode==Keys.Up)
{
if (game.Course!="down")
{
game.Course="up";
}
}
else if (e.KeyCode==Keys.Down)
{
if (game.Course!="up")
{
game.Course="down";
}
}
else if (e.KeyCode==Keys.Left)
{
if (game.Course!="right")
{
game.Course = "left";
}
}
else if (e.KeyCode==Keys.Right)
{
if (game.Course!="left")
{
game.Course = "right";
}
}
//让游戏暂停的热键--P
else if (e.KeyCode==Keys.P)
{
timer1.Stop();
}
//让游戏继续的热键--C
else if (e.KeyCode==Keys.C)
{
timer1.Start();
}
//让游戏重新开始的热键--R
else if (e.KeyCode==Keys.R)
{
game = new Game();
game.Spc = this.CreateGraphics();
}
//显示帮助
else if (e.KeyCode==Keys.H)
{
timer1.Stop();
MessageBox.Show("按山下左右光标键控制方向\n按P暂停\n按C继续\n按R重新开始\n按H显示帮助\n\n\nedit by xiaoyao");
}
}
private void Form1_Activated(object sender, System.EventArgs e)
{
game.Refresh();
}
private void Form1_Deactivate(object sender, System.EventArgs e)
{
timer1.Stop();
}
}
class Game
{
//常量
const int Nothing=0;
const int SnakeBody=1;
const int Food=2;
const int Wall=3;
//画布&它的属性
private Graphics spc;
public Graphics Spc
{
set
{
spc = value;
}
}
//场地
int[,] space = new int[20,20];
//储存蛇的方向的变量,默认为right
private string course = "down";
public string Course
{
get
{
return course;
}
set
{
course = value;
}
}
//建个链表来储存蛇~~~
Link Snake = new Link();
//储存食物的位置~
Place Plc_Food = new Place(0,0);
//储存蛇的长度~
public int Snake_Length
{
get
{
return Snake.Length;
}
}
public Game()
{
//给场地初始值0
for (int i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
space[i,j]=Nothing;
}
}
//画一条4长的"蛇"
for (int i=1;i<=4;i++)
{
Place pla = new Place(i,2);
Snake.CreateHead(pla);
space[i,2]=SnakeBody;
}
//画食物,并将食物的位置储存在Plc_Food里
Setfood();
}
public void Refresh()
{
SolidBrush myBrush = new SolidBrush(Color.Yellow);
//清空(除了食物~)
//很臭的代码~~~~画个大矩形就行了~~~~~~~
//懒得改了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (int i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
if (space[i,j]!=Food)
{
if ((i==0||i==19)||(j==0||j==19))
{
myBrush.Color=Color.Beige;
spc.FillRectangle(myBrush,i*10,j*10,10,10);
space[i,j]=Wall;
}
else
{
myBrush.Color=Color.Green;
spc.FillRectangle(myBrush,i*10,j*10,10,10);
}
}
}
}
//确定蛇
Snake.point = Snake.head.next;
do
{
space[Snake.point.pla.x,Snake.point.pla.y]=SnakeBody;
Snake.point = Snake.point.next;
}while(Snake.point!=Snake.end);

//把该画的画出来~
for (int i=1;i<=18;i++)
{
for (int j=1;j<=18;j++)
{
switch (space[i,j])
{
case Nothing:
{
myBrush.Color=Color.Yellow;
spc.FillRectangle(myBrush,i*10,j*10,10,10);
break;
}
case SnakeBody:
{
myBrush.Color=Color.YellowGreen;
spc.DrawRectangle(new Pen(Color.Black,2),i*10,j*10,10,10);
spc.FillRectangle(myBrush,i*10,j*10,10,10);
break;
}
case Food:
{
myBrush.Color = Color.WhiteSmoke;
spc.FillRectangle(myBrush,i*10,j*10,10,10);
break;
}
}
}
}
}
private void Setfood()
{
//随机地画食物,并将食物的位置储存在Plc_Food里
Random rnd = new Random();
Place Plc_Food = new Place(rnd.Next(18),rnd.Next(18));

//确保将食物在不是蛇身和墙壁的地方放下~
while (space[Plc_Food.x,Plc_Food.y]==SnakeBody || Plc_Food.x==0||Plc_Food.y==0||Plc_Food.x==19||Plc_Food.y==19)
{
Plc_Food.x = rnd.Next(18);
Plc_Food.y = rnd.Next(18);
}
space[Plc_Food.x,Plc_Food.y]=Food;
}


public void Move()
{
//course是储存方向的变量~
switch (course)
{
case ("right"):
{
//判断是否该退出游戏
if (Snake.SNAKE_HEAD.x+1>=19||space[Snake.SNAKE_HEAD.x+1,Snake.SNAKE_HEAD.y]==SnakeBody)
{
GameOver();
}
else
{
Place newPlace = new Place(Snake.SNAKE_HEAD.x+1, Snake.SNAKE_HEAD.y);
Snake.CreateHead(newPlace);

if (space[Snake.SNAKE_HEAD.x,Snake.SNAKE_HEAD.y]!=Food)
{
space[Snake.SANKE_TAIL.x,Snake.SANKE_TAIL.y] = Nothing;
Snake.DelTheTail();
}
else
{
Setfood();
}
}
break;
}
case ("left"):
{
//判断是否该退出游戏
if (Snake.SNAKE_HEAD.x-1<=0||space[Snake.SNAKE_HEAD.x-1,Snake.SNAKE_HEAD.y]==SnakeBody)
{
GameOver();
}
else
{
Place newPlace = new Place(Snake.SNAKE_HEAD.x-1, Snake.SNAKE_HEAD.y);
Snake.CreateHead(newPlace);

if (space[Snake.SNAKE_HEAD.x,Snake.SNAKE_HEAD.y]!=Food)
{
space[Snake.SANKE_TAIL.x,Snake.SANKE_TAIL.y] = Nothing;
Snake.DelTheTail();
}
else
{
Setfood();
}
}
break;
}
case ("up"):
{
//判断是否该退出游戏
if (Snake.SNAKE_HEAD.y-1<=0||space[Snake.SNAKE_HEAD.x,Snake.SNAKE_HEAD.y-1]==SnakeBody)
{
GameOver();
}
else
{
Place newPlace = new Place(Snake.SNAKE_HEAD.x, Snake.SNAKE_HEAD.y-1);
Snake.CreateHead(newPlace);

if (space[Snake.SNAKE_HEAD.x,Snake.SNAKE_HEAD.y]!=Food)
{
space[Snake.SANKE_TAIL.x,Snake.SANKE_TAIL.y] = Nothing;
Snake.DelTheTail();
}
else
{
Setfood();
}
}
break;
}
case ("down"):
{
//判断是否该退出游戏
if (Snake.SNAKE_HEAD.y+1>=19||space[Snake.SNAKE_HEAD.x,Snake.SNAKE_HEAD.y+1]==SnakeBody)
{
GameOver();
}
else
{
Place newPlace = new Place(Snake.SNAKE_HEAD.x, Snake.SNAKE_HEAD.y+1);
Snake.CreateHead(newPlace);

if (space[Snake.SNAKE_HEAD.x,Snake.SNAKE_HEAD.y]!=Food)
{
space[Snake.SANKE_TAIL.x,Snake.SANKE_TAIL.y] = Nothing;
Snake.DelTheTail();
}
else
{
Setfood();
}
}
break;
}
}

}

public bool Game_Over=false;
private void GameOver()
{
Game_Over=true;
}
}
class Place
{
public int x=0;
public int y=0;
public Place(int x,int y)
{
this.x=x;
this.y=y;
}
}
//还得建个链表!~~汗~~~~~~~~~~
//以下是建链表的两个类~~~~~~

class Node
{
public Node next=null;
public Node rear=null;
public Place pla=null;
}

class Link
{

public Node head=new Node(); //表头
public Node end = new Node(); //表尾
public Node point=null; //指针

public Link() //构造函数
{
head.rear = null;
end.next = null;
head.next = end;
end.rear = head;
point = head;
head.pla = null;
end.pla = null;
}


private void CreateNode(Place place)
{
Node newnode = new Node(); //产生新节点

newnode.rear = point; //将新节点的上一节指向指针所指向的节点
newnode.next=point.next; //将新节点的下一结指向指针的下一个节点


point.next.rear=newnode;
point.next=newnode;

point = newnode; //将指针指向新节点

newnode.pla = place; //将数据导入

}

public void DelTheTail()
{
end.rear = end.rear.rear;
end.rear.rear.next = end;
}


public void CreateHead(Place place)
{
Node temp = point;
point = head;
CreateNode(place);
point = temp;
}

public Place SNAKE_HEAD
{
get
{
return head.next.pla;
}
set
{
head.next.pla = value;
}
}
public Place SANKE_TAIL
{
get
{
return end.rear.pla;
}
set
{
end.rear.pla = value;
}
}


public int Length
{
get
{
int len=0;//储存链表长度的变量
Node xx = point; //临时变量记录指针原地址
point = head.next;//将指针移回表头

while(point!=end)
{
len++;
point=point.next; //将指针下移

}
point=xx; //将指针归位
return len+1;
}
}

}
}

fEyXek4O.rar (6.07 KB) [转载]用C#编的贪吃蛇游戏


搜索更多相关主题的帖子: 贪吃 游戏 
2006-03-10 20:39
htzz
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-3-10
得分:0 
代码有很多看不懂
请高手指点指点

2006-03-10 20:40
ayiueo
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-3-22
得分:0 
应用程序正常初始化(0xc0000135)失败.

2006-03-23 13:04
零点冻结
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2005-12-2
得分:0 
不错,看看
多谢了

2006-03-23 15:24
零点冻结
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2005-12-2
得分:0 
你这个怎么无法改变的窗体的大小呀?

2006-03-23 15:52
htzz
Rank: 1
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-3-10
得分:0 
这是限定窗体大小的代码
this.MinimumSize = new System.Drawing.Size(210, 230);
this.MaximumSize = new System.Drawing.Size(210, 230);

2006-03-23 17:39
AlexXu
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2012-9-27
得分:0 
谢谢拉,看看
2012-10-23 23:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-49645-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025981 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved