连连看
连连看。
很好玩。
Button startBut = new Button();
ProgressBar progressBar = new ProgressBar();
Grid MainGrid = new Grid();
double progressVal = 0;
string str_Start = "开始";
string str_ReStart = "重来";
const string MusicPath = @"/music/";
const string MusicFileType = ".mp3";
int currentFightIndex = 0;
const double ItemUnitLength = 32;
const double ItemMargin = 2;
const int MaxRowNum = 10;
const int MaxColNum = 12;
int RowNum = 5;
int ColNum = 6;
const int imageNum = 20;
int currentScore = 0;
const int MaxMusicNum = 2;
Brush gridBackground = new SolidColorBrush(Colors.LightGray);
TextBlock scoreLable = new TextBlock();
StackPanel headerPanel = new StackPanel();
Canvas MainCanvas = new Canvas();
Path collectPath = new Path();
IconItem[] itemArray = null;
GameStatus currentStatus = GameStatus.NotStarted;
List<IconItem> visibleItems = new List<IconItem>();
internal Position prevPosition = null;
internal Position collectPoint0;
internal Position collectPoint1;
internal Position currentPosition = null;
double gridLeft = 0;
double gridTop = 0;
internal IconItem prevItem;
internal IconItem currentItem;
LineSegment seg0 = new LineSegment();
LineSegment seg1 = new LineSegment();
LineSegment seg2 = new LineSegment();
PathFigure collectFigure = new PathFigure();
Storyboard hidePathStory = new Storyboard();
DispatcherTimer progressTimer = new DispatcherTimer();
const double TotalAllowTime = 0.3* 60 * 1000;
double elapsedTime = 0;
const double unitTimeSpan = 30;
MediaElement backMusic = new MediaElement();
public CollectBoard()
{
Init();
}
private void AddBackgroundMusicElement()
{
string url = MusicPath + currentFightIndex + MusicFileType;
this.backMusic.Source = new Uri(url, UriKind.RelativeOrAbsolute);
this.backMusic.AutoPlay = false;
this.Children.Add(this.backMusic);
}
private void Init()
{
this.InitElements();
this.GenerateItems();
this.AddBackgroundMusicElement();
this.RegisterEvents();
}
private void PlayMusicByFightTurn(int fightIndex)
{
this.backMusic.Play();
}
internal bool CanCollect()
{
if (this.prevItem == null ||
this.currentItem==null ||
this.prevItem==this.currentItem
)
{
return false;
}
if (!this.prevItem.AreImageEqual(this.currentItem))
{
return false;
}
return this.CanCollect(this.prevItem.ItemPosition, this.currentItem.ItemPosition);
}
private void RegisterEvents()
{
this.Loaded += new RoutedEventHandler(CollectBoard_Loaded);
}
void CollectBoard_Loaded(object sender, RoutedEventArgs e)
{
gridLeft = (this.Width - MainGrid.Width) / 2;
gridTop = (this.Height * 3 / 4 - this.MainGrid.Height) / 2;
Canvas.SetLeft(MainGrid, gridLeft);
Canvas.SetTop(MainGrid, gridTop);
}
internal void CheckGameStatus()
{
if (this.currentStatus == GameStatus.NotStarted)
{
this.BeginPlay();
}
else if (this.currentStatus == GameStatus.GameOver)
{
this.GameOver();
}
else if (this.currentStatus == GameStatus.Paused)
{
this.Resume();
}
else if (this.currentStatus == GameStatus.Processing)
{
bool win = IsCurrentFightWin();
if (win)
{
this.WinAFight();
}
else
{
bool hasCollectableItem = IsExistsCollectableItems();
while (!hasCollectableItem)
{
ReRandomItems();
hasCollectableItem = IsExistsCollectableItems();
}
}
}
}
private void BeginPlay()
{
this.progressTimer.Start();
this.PlayMusicByFightTurn(this.currentFightIndex+1);
this.currentStatus = GameStatus.Processing;
this.startBut.Content = str_ReStart;
}
void WinAFight()
{
this.currentFightIndex++;
this.currentScore += 100;
this.UpdateScore();
this.progressBar.Value = 0;
this.elapsedTime = 0;
this.progressTimer.Stop();
this.backMusic.Stop();
int musicIndex = Math.Min(this.currentFightIndex, MaxMusicNum);
string url= MusicPath+musicIndex+MusicFileType;
this.backMusic.Source = new Uri(url , UriKind.RelativeOrAbsolute);
RowNum =Math.Min( RowNum+ 1,MaxRowNum);
ColNum = Math.Min(ColNum + 1, MaxColNum);
this.GenerateGrids();
this.GenerateItems();
//this.progressTimer.Start();
//this.PlayMusicByFightTurn(this.currentFightIndex);
this.currentStatus = GameStatus.Paused;
}
void Resume()
{
this.progressTimer.Start();
this.PlayMusicByFightTurn(this.currentFightIndex);
this.currentStatus = GameStatus.Processing;
}
void RestartAFight()
{
this.startBut.Content = str_ReStart;
this.currentStatus = GameStatus.Processing;
this.currentFightIndex = 0;
this.currentScore = 0;
this.UpdateScore();
this.progressBar.Value = 0;
RowNum = 5;
ColNum = 6;
this.ClearItems();
this.GenerateGrids();
this.GenerateItems();
this.progressTimer.Start();
int musicIndex = Math.Min(this.currentFightIndex, MaxMusicNum);
string url = MusicPath + musicIndex + MusicFileType;
this.backMusic.Source = new Uri(url, UriKind.RelativeOrAbsolute);
}
void ClearItems()
{
this.MainGrid.Children.Clear();
this.MainGrid.RowDefinitions.Clear();
this.MainGrid.ColumnDefinitions.Clear();
this.itemArray = null;
this.visibleItems.Clear();
this.elapsedTime = 0;
this.progressTimer.Stop();
this.collectPath.Visibility = Visibility.Collapsed;
}
void GameOver()
{
this.progressBar.Value = 0;
this.ClearItems();
this.backMusic.Stop();
this.currentScore = 0;
this.UpdateScore();
}
private void UpdateScore()
{
this.scoreLable.Text = this.currentScore.ToString()+" 分";
}
void GenerateNextFight()
{
}
void ReRandomItems()
{
for (int i = 0; i < this.visibleItems.Count; i++)
{
int index1 = Helper.GetRandInt(this.visibleItems.Count);
int index2 = Helper.GetRandInt(this.visibleItems.Count);
if (index1 != index2)
{
IconItem item1 = this.visibleItems[index1];
IconItem item2 = this.visibleItems[index2];
int tempRow = item1.ItemPosition.Row;
int tempCol = item1.ItemPosition.Col;
item1.ItemPosition = item2.ItemPosition;
item2.ItemPosition = new Position(tempRow, tempCol);
item1.SetValue(Grid.RowProperty, item1.ItemPosition.Row);
item1.SetValue(Grid.ColumnProperty, item1.ItemPosition.Col);
item2.SetValue(Grid.RowProperty, item2.ItemPosition.Row);
item2.SetValue(Grid.ColumnProperty, item2.ItemPosition.Col);
}
}
}
bool IsExistsCollectableItems()
{
for (int i = 0; i < this.visibleItems.Count; i++)
{
for (int j = 0; j < this.visibleItems.Count; j++)
{
IconItem item1 = this.visibleItems[i];
IconItem item2 = this.visibleItems[j];
if (item1 != item2 && item1.ImageIndex==item2.ImageIndex)
{
bool canCollect = CheckCanCollect(item1.ItemPosition, item2.ItemPosition);
if (canCollect)
{
return true;
}
}
}
}
return false;
}
bool IsCurrentFightWin()
{
if (this.visibleItems.Count == 0)
{
return true;
}
return false;
}
private bool CanCollect(Position oldPos, Position currentPos)
{
if (oldPos.Row == currentPos.Row &&
oldPos.Col == currentPos.Col
)
{
return true;
}
if ((Math.Abs(oldPos.Row - currentPos.Row) == 1) && oldPos.Col == currentPos.Col)
{
this.SetPathPoints(oldPos, oldPos, currentPos, currentPos);
return true;
}
if ((Math.Abs(oldPos.Col - currentPos.Col) == 1) && oldPos.Row == currentPos.Row)
{
this.SetPathPoints(oldPos, oldPos, currentPos, currentPos);
return true;
}
List<Position> array0=FindXYPosArray(oldPos);
List<Position> array1=FindXYPosArray(currentPos);
for (int i = 0; i < array0.Count; i++)
{
for (int j = 0; j < array1.Count; j++)
{
if (array0[i].Row == array1[j].Row ||
array0[i].Col == array1[j].Col
)
{
//bool canLine0 = CanLine(this.prevItem.ItemPosition, array0[i]);
bool canLine0 = CanLine(oldPos, array0[i]);
bool canLine1 = CanLine(array0[i], array1[j]);
//bool canLine2 = CanLine(array1[j], this.currentItem.ItemPosition);
bool canLine2 = CanLine(array1[j], currentPos);
if (canLine0 &&
canLine1 &&
canLine2)
{
//this.SetPathPoints(this.prevItem.ItemPosition, array0[i],
//array1[j], this.currentItem.ItemPosition);
this.SetPathPoints(oldPos, array0[i],
array1[j], currentPos);
return true;
}
}
}
}
return false;
}
private bool CheckCanCollect(Position oldPos, Position currentPos)
{
if (oldPos.Row == currentPos.Row &&
oldPos.Col == currentPos.Col
)
{
return true;
}
if ((Math.Abs(oldPos.Row - currentPos.Row) == 1) && oldPos.Col == currentPos.Col)
{
return true;
}
if ((Math.Abs(oldPos.Col - currentPos.Col) == 1) && oldPos.Row == currentPos.Row)
{
return true;
}
List<Position> array0 = FindXYPosArray(oldPos);
List<Position> array1 = FindXYPosArray(currentPos);
for (int i = 0; i < array0.Count; i++)
{
for (int j = 0; j < array1.Count; j++)
{
if (array0[i].Row == array1[j].Row ||
array0[i].Col == array1[j].Col
)
{
bool canLine0 = CanLine(oldPos, array0[i]);
bool canLine1 = CanLine(array0[i], array1[j]);
bool canLine2 = CanLine(array1[j], currentPos);
if (canLine0 &&
canLine1 &&
canLine2)
{
return true;
}
}
}
}
return false;
}
void SetPathPoints(Position pos0,Position pos1,Position pos2,Position pos3)
{
double UnitLength=ItemUnitLength+ItemMargin;
this.collectFigure.StartPoint = new Point((pos0.Col + 0.5) * UnitLength +gridLeft, (pos0.Row + 0.5) * UnitLength+gridTop);
this.seg0.Point = new Point((pos1.Col + 0.5) * UnitLength +gridLeft, (pos1.Row + 0.5) * UnitLength +gridTop);
this.seg1.Point = new Point((pos2.Col + 0.5) * UnitLength +gridLeft, (pos2.Row + 0.5) * UnitLength +gridTop);
this.seg2.Point = new Point((pos3.Col + 0.5) * UnitLength +gridLeft, (pos3.Row + 0.5) * UnitLength +gridTop);
}
List<Position> FindXYPosArray( Position pos)
{
List<Position> result = new List<Position>();
for (int i = 0; i < itemArray.Length; i++)
{
IconItem item = itemArray[i];
if (item.ItemPosition.Row == pos.Row
)
{
if (item.ItemPosition != pos && item.Visibility==Visibility.Collapsed)
{
result.Add(item.ItemPosition);
}
}
else if(item.ItemPosition.Col==pos.Col)
{
if (item.ItemPosition != pos && item.Visibility == Visibility.Collapsed)
{
result.Add(item.ItemPosition);
}
}
}
if (CanShoot(new Position(pos.Row, -1), pos))
{
result.Add(new Position(pos.Row, -1));//
}
if (CanShoot(new Position(pos.Row, ColNum), pos))
{
result.Add(new Position(pos.Row, ColNum));
}
if (CanShoot(new Position(-1, pos.Col), pos))
{
result.Add(new Position(-1, pos.Col));//
}
if (CanShoot(new Position(RowNum, pos.Col), pos))
{
result.Add(new Position(RowNum, pos.Col));
}
return result;
}
浙公网安备 33010602011771号