WEB开发网
开发学院软件开发C语言 C#实现的找茬游戏 阅读

C#实现的找茬游戏

 2009-06-02 08:30:56 来源:WEB开发网   
核心提示:今天下午写了一个找茬的小游戏,暂时还没有完整的想法,C#实现的找茬游戏,只是先实现一下,所以不是很完善,如果current的坐标在first和end的坐标范围内则返回true /// </summary> /// <param name="current">当前坐标</

今天下午写了一个找茬的小游戏,暂时还没有完整的想法,只是先实现一下,所以不是很完善,但主要功能已经具备了。其实很简单,主要流程是,首先确定两张图片中不同之处的坐标,然后将两张图片显示到窗体上,开始游戏后,鼠标点击图片时进行判断,如果当前鼠标的坐标在错误坐标范围内则在该范围处显示一个红色框,当找出所有错误后即跳转到下一关。

这里复制了所有的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace GameTemp
{
public partial class Form1 : Form
{
#region 变量定义
/// <summary>
/// 图片中错误处的坐标数组,local[图片组数][错误位置数*2]
/// </summary>
private Point[][] local;
/// <summary>
/// 找到的错误
/// </summary>
private bool[] check;
/// <summary>
/// 已经通关的关数
/// </summary>
private int index;
/// <summary>
/// 关数
/// </summary>
private const int COUNT = 2;
/// <summary>
/// 秒数
/// </summary>
private int second;
#endregion
public Form1()
{
InitializeComponent();
}
#region Form1_Load 初始化局部变量
/// <summary>
/// 初始化局部变量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
check = new bool[COUNT];
local = new Point[COUNT][];
local[0] = new Point[4];
local[1] = new Point[4];
//初始化坐标
local[0][0] = new Point(245, 50);
local[0][1] = new Point(253, 61);
local[0][2] = new Point(263, 520);
local[0][3] = new Point(287, 546);
local[1][0] = new Point(190, 237);
local[1][1] = new Point(203, 249);
local[1][2] = new Point(121, 490);
local[1][3] = new Point(137, 497);
}
#endregion
#region button1_Click 开始按钮单击事件
/// <summary>
/// 开始按钮单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
index = 0;
//初始化图片
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
loadPicture();
this.button1.Enabled = false;
this.timer1.Start();
}
#endregion
#region loadPicture 加载图片
/// <summary>
/// 加载图片
/// </summary>
private void loadPicture()
{
switch (index)
{
case 0:
{
this.pictureBox1.ImageLocation = @"boy1A.jpg";
this.pictureBox2.ImageLocation = @"boy1B.jpg"; break;
}
case 1:
{
this.pictureBox1.ImageLocation = @"boyA.jpg";
this.pictureBox2.ImageLocation = @"boyB.jpg"; break;
}
}
}
#endregion
#region pictureBox1_Click 图片框单击事件
/// <summary>
/// 图片框单击事件,如果点击的位置的坐标在错误坐标范围内,则重绘图片框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Click(object sender, EventArgs e)
{
//取得当前鼠标单击位置的坐标
Point currentLocation = ((System.Windows.Forms.MouseEventArgs)(e)).Location;
//进行坐标比较
if (locationCompare(currentLocation, local[index][0], local[index][1]))
{
check[0] = true;
this.pictureBox1.Refresh();
}
if (locationCompare(currentLocation, local[index][2], local[index][3]))
{
check[1] = true;
this.pictureBox1.Refresh();
}
//如果已经达到最大关数
if (index == COUNT)
{
this.timer1.Stop();
this.second = 0;
MessageBox.Show("恭喜你,已经通关!");
this.button1.Enabled = true;
}
}
#endregion
#region locationCompare 坐标比较
/// <summary>
/// 坐标比较,如果current的坐标在first和end的坐标范围内则返回true
/// </summary>
/// <param name="current">当前坐标</param>
/// <param name="first">左上角坐标</param>
/// <param name="end">右下脚坐标</param>
/// <returns>true:current的坐标在first和end的坐标范围内;false:current的坐标在first和end的坐标范围外</returns>
private bool locationCompare(Point current, Point first, Point end)
{
bool result = false;
//只有当前坐标在坐标范围内才有效,所以逐个比较两个point的x和y的值
if (current.X >= first.X)
{
if (current.Y >= first.Y)
{
if (current.X <= end.X)
{
if (current.Y <= end.Y)
{
result = true;
}
}
}
}
return result;
}
#endregion
#region pictureBox1_Paint 图片框重绘事件
/// <summary>
/// 图片框重绘事件,如果找到错误位置,用红色线画框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics.FromHwnd(this.pictureBox1.Handle);
Pen pen = new Pen(Color.Red, 3);
if (check[0])
{
e.Graphics.DrawRectangle(pen, local[index][0].X, local[index][0].Y, local[index][1].X - local[index][0].X, local[index][1].Y - local[index][0].Y);
}
if (check[1])
{
e.Graphics.DrawRectangle(pen, local[index][2].X, local[index][2].Y, local[index][3].X - local[index][2].X, local[index][3].Y - local[index][2].Y);
}
if (check[0] && check[1])
{
Thread.Sleep(100);
index++;
check[0] = false;
check[1] = false;
loadPicture();
}
}
#endregion
#region timer1_Tick 数秒事件
/// <summary>
/// 数秒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
this.lblTime.Text = string.Empty;
second++;
this.lblTime.Text = second.ToString() + " 秒";
}
#endregion
}
}

Tags:实现 找茬 游戏

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接