c#写的贪食蛇游戏(源码)

 这是一个用c#写的贪食蛇游戏源码,分享一下:

下载地址(含源码)

using  System;
using
 System.Collections.Generic;
using
 System.Text;
using
 System.Drawing;
using
 System.Windows;
using
 System.Windows.Forms;
namespace
 Snake

{
    
//面板初时数据

    public class Map
    
{
        
public  static int NodeWidth = 20
;
        
public static  int MapWidth = 300
;
        
public static int MapHeight = 300
;
        
public static int NodeRowCount = MapWidth /
 NodeWidth;
        
public static int NodeColumCount = MapWidth /
 NodeWidth;
    }

    
//用来绘制snake节点
   public  class DrawNode
    
{
       
public
  Bitmap LocalMap;
       
public static
 Graphics g;
       
public
 DrawNode(Color color)
       
{
           
int
 x, y;
           LocalMap 
= new
 Bitmap(Map.NodeWidth, Map.NodeWidth);
           
for (x = 0; x < LocalMap.Width; x++
)
           
{
               
for (y = 0; y < LocalMap.Height; y++
)
               
{
                   Color pixelColor 
=
 LocalMap.GetPixel(x, y);
                   Color newColor 
=
 color;
                   LocalMap.SetPixel(x, y, newColor);
               }

           }
          
       }
       
       
//在指定的行列处画蛇节点

       public void DrawIn(int n, int m)
       
{
           g.DrawImage(LocalMap, m 
* Map.NodeWidth, n *
 Map.NodeWidth);
       }

       
//按蛇的节点位置画蛇节点
       public void DrawInNode(Node node)
       
{
           DrawIn(node.Row, node.Column); 
       }

        
    }
   
    
//node节点当前方向

    public  enum Direction
    
{
        Up,
        Down,
        Left,
        Right
    }

    
//sanke 节点
    public class Node
    
{
        
public int Row;//行数

        public int Column;//列数
        public  Direction NodeDirection;//方向
        public Node(int Row, int Column, Direction NodeDirection)
        
{
            
this.Row =
 Row;
            
this.Column =
 Column;
            
this.NodeDirection =
 NodeDirection;
        }

        
public bool IsEqual(Node node) //判断2节点位置是否相等
        {
            
if (this.Column == node.Column && this.Row ==
 node.Row)
            
{
                
return true
;

            }

            
else
            
{
                
return false
;
            }


        }

    }

    
//snake
    public class Snake
    
{
        
public bool Over = false;//游戏是否结束

        Random R = new Random();//随机数
        public Node NextNode;//下一个食物(节点)
        public Node TempleNode;//临时保存刚刚删掉的尾节点
        public int Score = 0;//得分
        public List<Node> NodeList = new List<Node>();//蛇身节点列表
        public Node HeadNode;//头节点
        public Node TailNode;//尾节点
        public static DrawNode DrawNew;//用来画新节点
        public static DrawNode DrawOld;//用来清除旧节点
        
//随即产生下一个食物

        public void CreateNextNode()
        
{
            List
<int> A=new List<int>
();
            
int i,j=0
;             
            
for(i=0;i<Map.NodeRowCount*Map.NodeColumCount;i++
)
            
{                 
                
for (j = 0; j < NodeList.Count;j++
 )
                
{
                    
if (NodeList[j].Row * Map.NodeRowCount + NodeList[j].Column ==
 i)
                    
{
                        
break
;
                    }
                     
                }

                
if (j >= NodeList.Count)
                
{
                    A.Add(i);
                }

            }

            j 
= R.Next(A.Count);
            NextNode 
= new Node(j / Map.NodeRowCount, j %
 Map.NodeColumCount, Direction.Left);
            DrawNew.DrawInNode(NextNode);
        }

        
//是否结束
        public bool IsOver()
        
{
            
bool re = false
;
            
int headrow =
 HeadNode.Row;
            
int headColumn =
 HeadNode.Column;
            
//碰壁

            if (headrow >=Map.NodeRowCount || headrow < 0 || headColumn >= Map.NodeColumCount || headColumn < 0)
            
{
                re 
= true
;
            }

            
//碰到自身
            foreach (Node node in NodeList)
            
{
                
if (node ==
 HeadNode)
                
{
                    
continue
;
                }

                
if (node.IsEqual(HeadNode))
                
{
                    re 
= true
;
                    
break
;
                }

            }

            
//得分
            if (NextNode.IsEqual(HeadNode))
            
{
                Score 
+= 10
;
                AddNode();
                CreateNextNode();
            }

            
if (re)
            
{
                Over 
= true
;
            }

            
return re;
        }

        
//向上
        public void Up()
        
{             
            
//将头接点的前方增加一节点,然后将尾节点删掉,然后更新头尾节点指针

            int headrow = HeadNode.Row;
            headrow
--
;
            Node node 
= new
 Node(headrow, HeadNode.Column, Direction.Up);
            NodeList.Add(node);
            HeadNode 
=
 node;
            DrawNew.DrawInNode(node);
            DrawOld.DrawInNode(TailNode);
            TempleNode 
= NodeList[0
];
            NodeList.RemoveAt(
0
);
            TailNode 
= NodeList[0
];
            
if
 (IsOver())
            
{
                MessageBox.Show(
"OVER"
);
            }

        }

        
//向下
        public void Down()
        
{
            
int headrow =
 HeadNode.Row;
            headrow
++
;
            Node node 
= new
 Node(headrow, HeadNode.Column, Direction.Down);
            NodeList.Add(node);
            HeadNode 
=
 node;
            DrawNew.DrawInNode(node);
            DrawOld.DrawInNode(TailNode);
            TempleNode 
= NodeList[0
];
            NodeList.RemoveAt(
0
);
            TailNode 
= NodeList[0
];
            
if
 (IsOver())
            
{
                MessageBox.Show(
"OVER"
);
            }

        }

        
//向左
        public void Left()
        
{
            
int headcolumn =
 HeadNode.Column;
            headcolumn
--
;
            Node node 
= new
 Node(HeadNode.Row, headcolumn, Direction.Left);
            NodeList.Add(node);
            HeadNode 
=
 node;
            DrawNew.DrawInNode(node);
            DrawOld.DrawInNode(TailNode);
            TempleNode 
= NodeList[0
];
            NodeList.RemoveAt(
0
);
            TailNode 
= NodeList[0
];
            
if
 (IsOver())
            
{
                MessageBox.Show(
"OVER"
);
            }

        }

        
//向右
        public void Right()
        
{
            
int headcolumn =
 HeadNode.Column;
            headcolumn
++
;
            Node node 
= new
 Node(HeadNode.Row, headcolumn, Direction.Right);
            NodeList.Add(node);
            HeadNode 
=
 node;
            DrawNew.DrawInNode(node);
            DrawOld.DrawInNode(TailNode);
            TempleNode 
= NodeList[0
];
            NodeList.RemoveAt(
0
);
            TailNode 
= NodeList[0
];
            
if
 (IsOver())
            
{
                MessageBox.Show(
"OVER"
);
            }

        }

        
//吃到食物时,蛇身增长
        public void AddNode()
        
{
            
//吃到食物时,蛇身增长,只需将刚刚删除的尾节点加在后面即可

            NodeList.Insert(0, TempleNode);
            TailNode 
=
 TempleNode;             
        }


    }

}

using  System;
using
 System.Collections.Generic;
using
 System.ComponentModel;
using
 System.Data;
using
 System.Drawing;
using
 System.Text;
using
 System.Windows.Forms;

namespace
 Snake
{
    
    
public partial class
 Form1 : Form
    
{
        
        Graphics g;
        Direction CurrentDirection;
        DrawNode DrawNew;
        DrawNode DrawOld;
        Snake snake;
        
public
 Form1()
        
{            
            InitializeComponent();
        }

        
private void Form1_Load(object sender, EventArgs e)
        
{
            timer1.Enabled 
= false
;
            
this.pictureBox1.Size = new
 System.Drawing.Size(Map.MapWidth, Map.MapHeight);
        }

        
public void InitData()
        
{
            g 
=
 pictureBox1.CreateGraphics();
            g.Clear(pictureBox1.BackColor);
            DrawNode.g 
=
 g;
            snake 
= new
 Snake();
            DrawNew 
= new
 DrawNode(Color.Red);
            DrawOld 
= new
 DrawNode(pictureBox1.BackColor);
            Snake.DrawNew 
=
 DrawNew;
            Snake.DrawOld 
=
 DrawOld;
            Node node 
= new Node(44
, Direction.Right);
            DrawNew.DrawInNode(node);
            snake.NodeList.Add(node);
            snake.HeadNode 
=
 node;
            CurrentDirection 
=
 node.NodeDirection;
            snake.TailNode 
=
 node;
            snake.CreateNextNode();
            timer1.Enabled 
= true
;
        }
        
        
public void
 ReDraw()
        
{
            
if (snake == null
)
                
return
;
            g.Clear(pictureBox1.BackColor);
            
for (int i = 0; i < snake.NodeList.Count; i++
)
            
{
                DrawNew.DrawInNode(snake.NodeList[i]);
            }

            DrawNew.DrawInNode(snake.NextNode);

        }

        
private void pictureBox1_Paint(object sender, PaintEventArgs e)
        
{
            ReDraw();
        }
         
        
private void timer1_Tick(object
 sender, EventArgs e)
        
{
            label1.Text 
=
 snake.Score.ToString();
            label2.Text 
=
 snake.NodeList.Count.ToString();
            
if (snake == null || snake.NodeList.Count <= 0
)
            
{
                
return
;
            }

            
if (snake.Over)
            
{
                timer1.Enabled 
= false
;
                
return
;
            }

            
if (snake.HeadNode.NodeDirection ==Direction.Up&& CurrentDirection == Direction.Down
                
|| snake.HeadNode.NodeDirection == Direction.Down && CurrentDirection ==
 Direction.Up
                
|| snake.HeadNode.NodeDirection == Direction.Left && CurrentDirection ==
 Direction.Right
                
|| snake.HeadNode.NodeDirection == Direction.Right && CurrentDirection ==
 Direction.Left)
            
{
                CurrentDirection 
=
 snake.HeadNode.NodeDirection;                
            }

            snake.HeadNode.NodeDirection 
= CurrentDirection;
            
switch
 (snake.HeadNode.NodeDirection)
            
{
                
case
 Direction.Up:
                    
{
                        snake.Up();
                        
break
;
                    }

                
case Direction.Down:
                    
{
                        snake.Down();
                        
break
;
                    }

                
case Direction.Left:
                    
{
                        snake.Left();
                        
break
;
                    }

                
case Direction.Right:
                    
{
                        snake.Right();
                        
break
;
                    }

            }


        }

        
private void newToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            InitData();
        }
       
        
private void Form1_KeyDown(object
 sender, KeyEventArgs e)
        
{
            Keys key 
=
 e.KeyData;
            
switch
 (key)
            
{
                
case
 Keys.Up:
                    
{
                        CurrentDirection 
=
 Direction.Up;
                        
break
;
                    }

                
case Keys.Down:
                    
{
                        CurrentDirection 
=
 Direction.Down;
                        
break
;
                    }

                
case Keys.Left:
                    
{
                        CurrentDirection 
=
 Direction.Left;
                        
break
;
                    }

                
case Keys.Right:
                    
{
                        CurrentDirection 
=
 Direction.Right;
                        
break
;
                    }

            }


        }

        
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            timer1.Interval 
= 100
;
        }

        
private void professionToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            timer1.Interval 
= 50
;
        }

        
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            MessageBox.Show(
"Designed By qwking. qq:83639234"
);
        }

        
private void beginnerToolStripMenuItem1_Click(object sender, EventArgs e)
        
{
            timer1.Interval 
= 200
;
        }

        
private void 等级ToolStripMenuItem_Click(object sender, EventArgs e)
        
{

        }


        
private void panel2_Paint(object sender, PaintEventArgs e)
        
{

        }

 
    }

}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页