游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3607|回复: 3

[转载][用C#编的贪吃蛇游戏]

[复制链接]

2

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2006-3-8 19:28:00 | 显示全部楼层 |阅读模式

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;
                        }
                }

        }
}

sf_200638192744.exe

28 KB, 下载次数:

2

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
 楼主| 发表于 2006-3-8 19:30:00 | 显示全部楼层

Re:[转载][用C#编的贪吃蛇游戏]

代码有很多看不懂
请高手指点指点

11

主题

24

帖子

24

积分

注册会员

Rank: 2

积分
24
发表于 2006-3-9 11:36:00 | 显示全部楼层

Re:[转载][用C#编的贪吃蛇游戏]

NO,代码作者功底很好,但是对.NET不是很熟悉,作了很多“重新发明车轮”的工作。

1

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2006-4-6 11:56:00 | 显示全部楼层

Re:[转载][用C#编的贪吃蛇游戏]

程序非常不错,楼上的能不能把程序改进呀,初学C#,谢谢啦!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 02:44

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表