打豆豆游戏的C#代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第一部分是概述,第二部分为代码,第三部分为联络。

一、概述
VS打开后如下图所示,由于不能直接上传C#文件,就做成这个Word文档。

运行后效果图:
(1).得分显示区域,消除1个豆豆后可得10分,但若点击豆豆却不可消除,扣除30分。

分数图片资源文件:\ForFun\Pictures
(2).测试用。

CheckBox勾上时,会将Log输出到exe所在文件夹下。

(3).勾上时,即使豆豆上升到最大时,游戏也不会结束。

(4).勾上时,每次得分都将自动保存到本地exe所在文件夹下。

(5).勾上时,豆豆才会自动往上增长。

(6).可点击六个Button设置增长豆豆的颜色。

若不勾上则不会使用设置的颜色,而使用游戏预设的颜色。

(7).点击“开始游戏”时,游戏界面出现的豆豆行数。

(8).“自增长模式”勾上时才有效。

豆豆增长间隔时间,ms单位。

(9).开始游戏
(10).游戏界面。

鼠标点击豆豆,即打豆豆。

二、代码
1. \ForFun\Class\FBlock.cs (FBlock代表的是一个个颜色的豆豆。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ForFun.Class
{
public class FBlock
{
///<summary>
/// Block的长宽值
///</summary>
public const int BlockSize = 25;
///<summary>
///长宽值半数
///</summary>
public const int HalfSize = 12;
///<summary>
///随机数生成器
///</summary>
private static Random rand = new Random();
private Color showColor;
///<summary>
///显示的主颜色
///</summary>
public Color ShowColor
{
get { return showColor; }
set { showColor = value; }
}
///<summary>
///初始化FBlock
///</summary>
///<param name="colors"></param>
public FBlock(Color[] colors)
{
int index = rand.Next(0, colors.Length);
this.showColor = colors[index];
}
///<summary>
///初始化FBlock
///</summary>
///<param name="color"></param>
public FBlock(Color color)
{
this.showColor = color;
}
///<summary>
///填充颜色
///</summary>
///<param name="graphics"></param>
///<param name="pointMatrix"></param>
public void Draw(Graphics graphics, Point pointMatrix)
{
// 渐变色Brush生成
LinearGradientBrush brush = CreateTheBrush(pointMatrix);
// 绘制
DrawTheBlock(graphics, brush, pointMatrix);
}
///<summary>
///填充Block颜色
///</summary>
///<param name="graphics"></param>
///<param name="brush"></param>
///<param name="pointMatrix"></param>
private void DrawTheBlock(Graphics graphics, Brush brush, Point pointMatrix) {
// 获得左上坐标(PictureBox中的坐标)
Point location = FPointTranslator.GetLeftTopPoint(pointMatrix);
// 绘制区域大小
Size size = new Size(FBlock.BlockSize, FBlock.BlockSize);
Rectangle rect = new Rectangle(location, size);
graphics.FillEllipse(brush, rect);
}
///<summary>
///获得渐变色的Brush
///</summary>
///<param name="pointMatrix"></param>
///<returns></returns>
private LinearGradientBrush CreateTheBrush(Point pointMatrix)
{
// 渐变色起点:左下
Point pointLB = FPointTranslator.GetLeftButtomPoint(pointMatrix);
// 渐变色终点:右上
Point pointRT = FPointTranslator.GetRightTopPoint(pointMatrix);
return new LinearGradientBrush(pointLB, pointRT, this.ShowColor, Color.White); }
}
}
2. \ForFun\Class\FGrid.cs (FGrid代表的是整个游戏主区域,包含了m*n个Block)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace ForFun.Class
{
public class FGrid
{
///<summary>
///测试Log输出
///</summary>
public bool LogOutputFlg = false;
///<summary>
/// Block项目矩阵
///</summary>
private FBlock[,] matrix = new FBlock[14, 18];
///<summary>
///获取行数
///</summary>
public int AllRowCount
{
get { return matrix.GetLength(0); }
}
///<summary>
///获取列数
///</summary>
public int AllColCount
{
get { return matrix.GetLength(1); }
}
private Color[] colors = new Color[] { Color.Red, Color.Green, Color.Yellow, Color.Blue };
///<summary>
///可能颜色集
///</summary>
public Color[] Colors
{
get { return colors; }
set { colors = value; }
}
///<summary>
///根据行数生成Matrix的随机颜色值。

///</summary>
///<param name="rowCount"></param>
public FGrid(int rowCount)
{
// 行数限制
if (rowCount > matrix.GetLength(0))
throw new Exception("Please set the rowCount: " + rowCount);
// 随机生成指定行数、固定列数的Block
for (int row = 0; row < rowCount; row++)
{
for (int column = 0; column < matrix.GetLength(1); column++)
{
matrix[row, column] = new FBlock(this.colors);
}
}
}
///<summary>
///根据行数生成Matrix的随机颜色值。

///</summary>
///<param name="rowCount"></param>
public FGrid(int rowCount, Color[] clrs)
{
// 行数限制
if (rowCount > matrix.GetLength(0))
throw new Exception("Please set the rowCount: " + rowCount);
// 用户设置颜色集更新
this.colors = clrs;
// 随机生成指定行数、固定列数的Block
for (int row = 0; row < rowCount; row++)
{
for (int column = 0; column < matrix.GetLength(1); column++)
{
matrix[row, column] = new FBlock(this.colors);
}
}
}
///<summary>
///填充颜色
///</summary>
///<param name="graphics"></param>
///<param name="backColor"></param>
public void Draw(Graphics graphics, Color backColor)
{
// 准备重新绘制
graphics.Clear(backColor);
FBlock theBlock = null;
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int column = 0; column < matrix.GetLength(1); column++)
{
theBlock = matrix[row, column];
// 该Block不空时
if (theBlock != null)
{
// 以PictureBox为基准的、左下为(0,0)的行列坐标
Point pointMatrix = new Point(column * FBlock.BlockSize, row * FBlock.BlockSize);
// 绘制Block
theBlock.Draw(graphics, pointMatrix);
}
}
}
}
///<summary>
///增加行数
///</summary>
///<param name="count"></param>
///<param name="finishFlg">无敌模式Flg</param>
public int AddRow(int count, bool finishFlg)
{
// 非无敌模式时,检查是否已经游戏结束
if (!finishFlg)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
if (matrix[matrix.GetLength(0) - 1, col] != null)
return -1;
}
}
// 无敌模式或非无敌模式但游戏未结束时,增长一行
for (int row = matrix.GetLength(0) - 1; row >= count; row--)
{
for (int column = 0; column < matrix.GetLength(1); column++) {
matrix[row, column] = matrix[row - count, column];
}
}
// 为最底层一行重新随机生成颜色
for (int row = 0; row < count; row++)
{
for (int column = 0; column < matrix.GetLength(1); column++) {
matrix[row, column] = new FBlock(this.colors);
}
}
return 1;
}
///<summary>
///释放内存
///</summary>
public void Dispose()
{
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int column = 0; column < matrix.GetLength(1); column++)
{
if (matrix[row, column] != null)
{
matrix[row, column] = null;
}
}
}
}
///<summary>
///与被Click的Block颜色相同的Block坐标集合
///</summary>
private List<Point> lstPtsForFindNeighbors;
///<summary>
/// Block被点击
///</summary>
///<param name="ptMatrix"></param>
///<returns></returns>
public int Click(Point ptMatrix)
{
int ret = 0;
// 点击的Block不为空时
if (matrix[ptMatrix.X, ptMatrix.Y] != null)
{
// 同色标记
bool[,] neighborsFlg = new bool[AllRowCount, AllColCount];
// 根据颜色生成同色标记
ret = FindNeighbors(ptMatrix, ref neighborsFlg);
if (ret > 1)
{
// 如果返回的值大于1,则被点击的Block周围存在同色Block,则重铸Grid CollapseBlock(neighborsFlg);
}
}
return ret == 1 ? -3 : ret;
}
///<summary>
///根据基准点,查找其同色邻居
///</summary>
///<param name="ptMatrix"></param>
///<param name="neighborsFlg"></param>
///<returns></returns>
private int FindNeighbors(Point ptMatrix, ref bool[,] neighborsFlg)
{
int ret = 0;
// 初始化同色标记矩阵
for (int row = 0; row < neighborsFlg.GetLength(0); row++)
{
for (int column = 0; column < neighborsFlg.GetLength(1); column++) {
neighborsFlg[row, column] = false;
}
}
// 基准点赋值
neighborsFlg[ptMatrix.X, ptMatrix.Y] = true;
if (lstPtsForFindNeighbors == null)
lstPtsForFindNeighbors = new List<Point>();
// 基准点加入同色集合
lstPtsForFindNeighbors.Add(ptMatrix);
// 同色集合非空时
while (lstPtsForFindNeighbors.Count != 0)
{
// 获取同色集合最末一个坐标,再消除
Point ptBase = st();
ret++;
lstPtsForFindNeighbors.RemoveAt(lstPtsForFindNeighbors.Count - 1);
// 获取该坐标的同色邻居
DoFind(ptBase, ref neighborsFlg);
}
if (this.LogOutputFlg)
{
// Log需输出时,则输出
OutputLogs(neighborsFlg);
}
return ret; ;
}
///<summary>
///根据基准点,查找其同色邻居(详细方法)
///</summary>
///<param name="ptBase"></param>
///<param name="neighborsFlg"></param>
private void DoFind(Point ptBase, ref bool[,] neighborsFlg)
{
if (matrix[ptBase.X, ptBase.Y] == null)
return;
// 基准颜色
Color colorBase = matrix[ptBase.X, ptBase.Y].ShowColor;
// 查找基准点上方
if (ptBase.Y < AllColCount - 1)
{
Point pt = new Point(ptBase.X, ptBase.Y + 1);
DoJudge(colorBase, pt, ref neighborsFlg);
}
// 查找基准点右侧
if (ptBase.X < AllRowCount - 1)
{
Point pt = new Point(ptBase.X + 1, ptBase.Y);
DoJudge(colorBase, pt, ref neighborsFlg);
}
// 查找基准点下方
if (ptBase.Y > 0)
{
Point pt = new Point(ptBase.X, ptBase.Y - 1);
DoJudge(colorBase, pt, ref neighborsFlg);
}
// 查找基准点左侧
if (ptBase.X > 0)
{
Point pt = new Point(ptBase.X - 1, ptBase.Y);
DoJudge(colorBase, pt, ref neighborsFlg);
}
}
///<summary>
///判断是否为同色
///</summary>
///<param name="colorBase"></param>
///<param name="ptJudge"></param>
///<param name="neighborsFlg"></param>
private void DoJudge(Color colorBase, Point ptJudge, ref bool[,] neighborsFlg)
{
if(!neighborsFlg[ptJudge.X, ptJudge.Y] // 已经被查找过为同色的不需再次处理
&& matrix[ptJudge.X, ptJudge.Y] != null// 该Block存在颜色
&& matrix[ptJudge.X, ptJudge.Y].ShowColor.Equals(colorBase)) // 同色比较 {
// 被判定为同色,同色标记
neighborsFlg[ptJudge.X, ptJudge.Y] = true;
lstPtsForFindNeighbors.Add(ptJudge);
}
}
///<summary>
///重铸Grid
///</summary>
///<param name="neighborsFlg"></param>
private void CollapseBlock(bool[,] neighborsFlg)
{
for (int row = neighborsFlg.GetLength(0) - 1; row >= 0; row--)
{
for (int column = neighborsFlg.GetLength(1) - 1; column >= 0; column--)
{
if (neighborsFlg[row,column])
{
// 判定为同色的Block,该列需要重铸
DoCollapseRowCell(new Point(row, column));
}
}
}
for (int column = neighborsFlg.GetLength(1) - 2; column >= 0; column--)
{
// 判断,某列是否已经不存在有效Block,如是则后列整体前移
DoCollapseColumnFull(column);
}
}
///<summary>
///从某列开始,后面的列整体前移
///</summary>
///<param name="column"></param>
private void DoCollapseColumnFull(int column)
{
bool doFlg = true;
// 判断整列是否都无效
for (int row = 0; row < matrix.GetLength(0); row++)
{
if (matrix[row, column] != null)
doFlg = false;
break;
}
}
// 整列无效时
if (doFlg)
{
// 后面列前移
for (int col = column; col < matrix.GetLength(1) - 1; col++) {
for (int row = 0; row < matrix.GetLength(0); row++)
{
matrix[row, col] = matrix[row, col + 1];
}
}
// 最后列清空
for (int row = 0; row < matrix.GetLength(0); row++)
{
matrix[row, matrix.GetLength(1) - 1] = null;
}
}
}
///<summary>
///某列从某行开始,整体下移
///</summary>
///<param name="point"></param>
private void DoCollapseRowCell(Point point)
{
for (int row = point.X; row < matrix.GetLength(0) - 1; row++)
{
matrix[row, point.Y] = matrix[row + 1, point.Y];
}
matrix[matrix.GetLength(0) - 1, point.Y] = null;
}
///<summary>
///输出测试Log
///</summary>
///<param name="neighborsFlg"></param>
private void OutputLogs(bool[,] neighborsFlg)
{
{
string fileName = System.Environment.CurrentDirectory + "\\" + DateTime.Today.ToShortDateString() + "Log.txt";
if (!File.Exists(fileName))
{
File.Create(fileName);
}
StreamWriter sw = new StreamWriter(fileName, true, Encoding.Default);
sw.WriteLine(DateTime.Now);
for (int row = neighborsFlg.GetLength(0) - 1; row >= 0; row--)
{
for (int column = 0; column < neighborsFlg.GetLength(1); column++) {
sw.Write((neighborsFlg[row, column] ? "1" : "0") + " ");
}
sw.WriteLine();
}
sw.Flush();
sw.Close();
}
catch
{
}
}
}
}
3. \ForFun\Class\FPointTranslator.cs (FPointTranslator是为了坐标转换)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace ForFun.Class
{
public class FPointTranslator
{
private static int height;
///<summary>
/// PictureBox的高度,用于坐标转换
///</summary>
public static int Height
{
get { return height; }
set { height = value; }
}
///<summary>
///获得右上坐标
///</summary>
///<param name="pointMatrix"></param>
///<returns></returns>
public static Point GetRightTopPoint(Point pointMatrix)
{
Point point = new Point(pointMatrix.X + FBlock.BlockSize, Height - pointMatrix.Y - FBlock.BlockSize);
return point;
}
///<summary>
///获得右下坐标
///</summary>
///<param name="pointMatrix"></param>
///<returns></returns>
public static Point GetRightBottomPoint(Point pointMatrix)
{
Point point = new Point(pointMatrix.X + FBlock.BlockSize, Height -
FBlock.BlockSize);
return point;
}
///<summary>
///获得左下坐标
///</summary>
///<param name="pointMatrix"></param>
///<returns></returns>
public static Point GetLeftButtomPoint(Point pointMatrix)
{
Point point = new Point(pointMatrix.X, Height - pointMatrix.Y);
return point;
}
///<summary>
///获得左上坐标
///</summary>
///<param name="pointMatrix"></param>
///<returns></returns>
public static Point GetLeftTopPoint(Point pointMatrix)
{
Point point = new Point(pointMatrix.X, Height - pointMatrix.Y - FBlock.BlockSize);
return point;
}
}
}
4. \ForFun \FForm1.cs (FForm1游戏主界面)
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForFun.Class;
using System.IO;
namespace ForFun
{
public partial class FForm1 : Form
{
///<summary>
///得分图片序号
///</summary>
public List<char> EnabledChars = new List<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
///<summary>
///最大行数
///</summary>
public const int MaxRowCount = 14;
///<summary>
///坐标定数
///</summary>
public const int Coordinate = 6;
///<summary>
///得分
///</summary>
private int score = 0;
///<summary>
///得分图片集
///</summary>
Image[] numbers;
///<summary>
///数据管理
///</summary>
private FGrid grid;
public FForm1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FPointTranslator.Height = this.pictureBox1.Height; SetImages();
}
///<summary>
///加载图片资源
///</summary>
private void SetImages()
{
numbers = new Image[10];
numbers[0] = Properties.Resources._0;
numbers[1] = Properties.Resources._1;
numbers[2] = Properties.Resources._2;
numbers[3] = Properties.Resources._3;
numbers[4] = Properties.Resources._4;
numbers[5] = Properties.Resources._5;
numbers[6] = Properties.Resources._6;
numbers[7] = Properties.Resources._7;
numbers[8] = Properties.Resources._8;
numbers[9] = Properties.Resources._9;
}
///<summary>
///绘制坐标
///</summary>
///<param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
Pen penX = new Pen(Color.Red, 1);
Pen penY = new Pen(Color.Blue, 1);
Font font = new Font("Simsun", 10.0f);
SolidBrush brush = new SolidBrush(Color.Red);
Point ptBase = new Point(this.pictureBox1.Left - FBlock.HalfSize,
this.pictureBox1.Bottom + FBlock.HalfSize);
Point ptLT = new Point(this.pictureBox1.Left - FBlock.HalfSize,
this.pictureBox1.Top - FBlock.HalfSize);
Point ptRB = new Point(this.pictureBox1.Right + FBlock.HalfSize,
this.pictureBox1.Bottom + FBlock.HalfSize);
e.Graphics.DrawLine(penY, ptBase, ptLT);
e.Graphics.DrawLine(penX, ptBase, ptRB);
for (int i = 0; i <= (ptRB.X - ptBase.X) / FBlock.BlockSize; i++)
{
int coX = i * FBlock.BlockSize + ptBase.X;
int tempY = ptBase.Y - Coordinate;
e.Graphics.DrawLine(penX, new Point(coX, ptBase.Y), new Point(coX, tempY));
e.Graphics.DrawString(i.ToString(), font, brush, new PointF(coX - Coordinate, ptBase.Y + Coordinate / 2));
}
for (int j = 0; j <= (ptBase.Y - ptLT.Y) / FBlock.BlockSize; j++)
{
int coY = ptBase.Y - j * FBlock.BlockSize;
int tempX = ptBase.X + Coordinate;
e.Graphics.DrawLine(penY, new Point(ptBase.X, coY), new Point(tempX, coY));
Rectangle rect = new Rectangle(new Point(ptBase.X - 3 * Coordinate, coY - Coordinate), new Size(20, 14));
TextRenderer.DrawText(e.Graphics, j.ToString(), font, rect, Color.Blue, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
penX.Dispose();
penY.Dispose();
font.Dispose();
brush.Dispose();
}
///<summary>
///输入控制
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !EnabledChars.Contains(e.KeyChar))
e.Handled = true;
}
///<summary>
///开始游戏
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
#region Check
// 开始行数不能为空
if (string.IsNullOrEmpty(textBox1.Text))
return;
int count = int.Parse(textBox1.Text);
// 开始行数范围限制
if (count <= 0 || count > MaxRowCount)
{
MessageBox.Show("Please enter a number between 1 and 14");
return;
}
#endregion
#region初始化
// 定时器清空
timer1.Enabled = false;
this.timer1.Tick -= timer1_Tick;
// 点击事件清空
this.pictureBox1.MouseDown -= Block_Click;
// 数据管理清空
if (grid != null)
{
grid.Dispose();
}
#endregion
// 使用自定义颜色开始游戏
if (this.ckbSelfDefine.Checked)
{
// 获得自定义颜色
Color[] colors = SetGridColors();
// 初始化数据管理
grid = new FGrid(count, colors);
}
// 使用系统默认颜色开始游戏
else
{
// 初始化数据管理
grid = new FGrid(count);
}
// 是否输出Log,默认不输出
this.grid.LogOutputFlg = this.ckbLog.Checked;
// 绘制游戏主界面
grid.Draw(this.pictureBox1.CreateGraphics(), this.pictureBox1.BackColor);
// 给游戏添加Click事件
this.pictureBox1.MouseDown += new MouseEventHandler(Block_Click);
// 设置计时器间隔
this.timer1.Interval = string.IsNullOrEmpty(cmbTickTime.Text) ? 4500 : int.Parse(cmbTickTime.Text);
// 开始计时
this.timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
}
///<summary>
///获取用户自定义颜色集
///</summary>
///<returns></returns>
private Color[] SetGridColors()
{
List<Color> lstColors = new List<Color>();
if (!lstColors.Contains(this.btnColor1.BackColor))
lstColors.Add(this.btnColor1.BackColor);
if (!lstColors.Contains(this.btnColor2.BackColor))
lstColors.Add(this.btnColor2.BackColor);
if (!lstColors.Contains(this.btnColor3.BackColor))
lstColors.Add(this.btnColor3.BackColor);
if (!lstColors.Contains(this.btnColor4.BackColor))
lstColors.Add(this.btnColor4.BackColor);
if (!lstColors.Contains(this.btnColor5.BackColor))
lstColors.Add(this.btnColor5.BackColor);
if (!lstColors.Contains(this.btnColor6.BackColor))
lstColors.Add(this.btnColor6.BackColor);
return lstColors.ToArray();
}
///<summary>
///点击游戏区域,Block消除,计分
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
void Block_Click(object sender, MouseEventArgs e)
{
// 点击坐标(Matrix中坐标,非画面坐标)
Point ptMatrix = new Point(grid.AllRowCount - e.Y / FBlock.BlockSize - 1, e.X / FBlock.BlockSize);
// 数据管理进行处理,返回消除的Block数目,当没有被消除时,返回-3,即扣除30分
int count = grid.Click(ptMatrix);
// 分数计算
score += 10 * count;
if (score < 0)
score = 0;
// 重新绘制游戏图
grid.Draw(this.pictureBox1.CreateGraphics(), this.pictureBox1.BackColor);
// 显示游戏计分
int tenThousands = (score / 10000) % 10;
int thousands = (score / 1000) % 10;
int handreds = (score / 100) % 10;
int tens = (score / 10) % 10;
int ones = (score % 10);
this.pbTenThousands.Image = numbers[tenThousands];
this.pbThousands.Image = numbers[thousands];
this.pbHandreds.Image = numbers[handreds];
this.pbTens.Image = numbers[tens];
this.pbOnes.Image = numbers[ones];
}
///<summary>
///计时器
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
// 游戏已经开始且选择了自增长模式
if (grid != null && this.ckbSelfAdd.Checked)
{
// 增长一行
int ret = grid.AddRow(1, this.ckbNeverFinish.Checked);
// 非无敌模式下,返回-1,代表游戏结束
if (ret == -1)
{
// 计时器清空
timer1.Enabled = false;
this.pictureBox1.MouseDown -= Block_Click;
this.timer1.Tick -= timer1_Tick;
MessageBox.Show("您的最终得分为:" + score);
// 需要保存结果时,保存游戏结果
if (this.ckbScoreSave.Checked)
{
SaveScore();
}
}
else
grid.Draw(this.pictureBox1.CreateGraphics(),
this.pictureBox1.BackColor);
}
}
///<summary>
///游戏得分结果保存
///</summary>
private void SaveScore()
{
try
{
string fileName = System.Environment.CurrentDirectory + "\\" + DateTime.Today.ToShortDateString() + "Score.txt";
if (!File.Exists(fileName))
{
File.Create(fileName);
}
StreamWriter sw = new StreamWriter(fileName, true, Encoding.Default);
sw.WriteLine(DateTime.Now);
sw.WriteLine("\t您的最终得分为:" + score + "\r\n");
sw.Flush();
sw.Close();
}
catch
{
}
}
///<summary>
///共用方法,用户设置颜色
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void btnColor_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (this.colorSelect.ShowDialog() == DialogResult.OK)
btn.BackColor = this.colorSelect.Color;
}
}
}
5. \ForFun \FForm1.Designer.cs (FForm1游戏主界面)
namespace ForFun
{
partial class FForm1
{
///<summary>
///必要なデザイナ変数です。

///</summary>
private ponentModel.IContainer components = null;
///<summary>
///使用中のリソースをすべてクリーンアップします。

///</summary>
///<param name="disposing">マネージリソースが破棄される場合 true、破棄されない場合は false です。

</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォームデザイナで生成されたコード
///<summary>
///デザイナサポートに必要なメソッドです。

このメソッドの内容を
///コードエディタで変更しないでください。

///</summary>
private void InitializeComponent()
{
ponents = new ponentModel.Container();
ponentResourceManager resources = new
ponentResourceManager(typeof(FForm1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(ponents);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
this.pbOnes = new System.Windows.Forms.PictureBox();
this.pbTens = new System.Windows.Forms.PictureBox();
this.pbHandreds = new System.Windows.Forms.PictureBox();
this.pbThousands = new System.Windows.Forms.PictureBox();
this.pbTenThousands = new System.Windows.Forms.PictureBox();
this.ckbLog = new System.Windows.Forms.CheckBox();
this.cmbTickTime = new boBox();
this.ckbNeverFinish = new System.Windows.Forms.CheckBox();
this.ckbSelfAdd = new System.Windows.Forms.CheckBox();
this.ckbScoreSave = new System.Windows.Forms.CheckBox();
this.colorSelect = new System.Windows.Forms.ColorDialog();
this.btnColor1 = new System.Windows.Forms.Button();
this.btnColor2 = new System.Windows.Forms.Button();
this.btnColor3 = new System.Windows.Forms.Button();
this.btnColor4 = new System.Windows.Forms.Button();
this.btnColor5 = new System.Windows.Forms.Button();
this.btnColor6 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.ckbSelfDefine = new System.Windows.Forms.CheckBox();
bel1 = new bel();
bel2 = new bel();
((ponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
((ponentModel.ISupportInitialize)(this.pbOnes)).BeginInit();
((ponentModel.ISupportInitialize)(this.pbTens)).BeginInit();
((ponentModel.ISupportInitialize)(this.pbHandreds)).BeginInit();
((ponentModel.ISupportInitialize)(this.pbThousands)).BeginInit();
((ponentModel.ISupportInitialize)(this.pbTenThousands)).BeginInit();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(108, 441);
= "textBox1";
this.textBox1.Size = new System.Drawing.Size(121, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "6";
this.textBox1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// button1
//
this.button1.Location = new System.Drawing.Point(275, 464);
= "button1";
this.button1.Size = new System.Drawing.Size(85, 23);
this.button1.TabIndex = 2;
this.button1.Text = "开始游戏(&S)";
eVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// timer1
//
this.timer1.Interval = 4500;
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Black;
this.pictureBox1.Location = new System.Drawing.Point(68, 32);
= "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(450, 350);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.pbOnes);
this.panel1.Controls.Add(this.pbTens);
this.panel1.Controls.Add(this.pbHandreds);
this.panel1.Controls.Add(this.pbThousands);
this.panel1.Controls.Add(this.pbTenThousands);
this.panel1.Location = new System.Drawing.Point(572, 32);
= "panel1";
this.panel1.Size = new System.Drawing.Size(153, 96);
this.panel1.TabIndex = 3;
//
// pbOnes
//
this.pbOnes.Location = new System.Drawing.Point(112, 16);
= "pbOnes";
this.pbOnes.Size = new System.Drawing.Size(26, 67);
this.pbOnes.TabIndex = 4;
this.pbOnes.TabStop = false;
//
// pbTens
//
this.pbTens.Location = new System.Drawing.Point(88, 16);
= "pbTens";
this.pbTens.Size = new System.Drawing.Size(26, 67);
this.pbTens.TabIndex = 3;
this.pbTens.TabStop = false;
//
// pbHandreds
//
this.pbHandreds.Location = new System.Drawing.Point(64, 16); = "pbHandreds";
this.pbHandreds.Size = new System.Drawing.Size(26, 67);
this.pbHandreds.TabIndex = 2;
this.pbHandreds.TabStop = false;
//
// pbThousands
//
this.pbThousands.Location = new System.Drawing.Point(40, 16); = "pbThousands";
this.pbThousands.Size = new System.Drawing.Size(26, 67);
this.pbThousands.TabIndex = 1;
this.pbThousands.TabStop = false;
//
// pbTenThousands
//
this.pbTenThousands.Location = new System.Drawing.Point(16, 16); = "pbTenThousands";
this.pbTenThousands.Size = new System.Drawing.Size(26, 67); this.pbTenThousands.TabIndex = 0;
this.pbTenThousands.TabStop = false;
//
// ckbLog
//
this.ckbLog.AutoSize = true;
this.ckbLog.Location = new System.Drawing.Point(16, 26);。

相关文档
最新文档