这是一个用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
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