2009年8月2日日曜日


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class mainForm : Form
{
//ゲームレベルの最高値を表す定数
private const int MAX_GAME_LEVEL = 15;
//動物の種類の数を表す定数
private const int ANIMAL_COUNT = 6; private int gamelevel = 1; //ゲームレベル
private int score = 0; //スコア private int clickcount = 0; //クリック回数
private bool isAnswering = false; //解答中を示すフラグ //規定の背景画像
private Image defaultbackgroundImage;
//○と×の画像
private Image correctImage;
private Image uncorrectImage;
//ポイント(点数)として表示する画像からなる配列
private Image[] pointimageArray = new Image[10];
//解答歴に表示する画像からなる配列
private Image[] answerimageArray = new Image[ANIMAL_COUNT];
//動物の画像を保持しているピクチャボックスからなる配列
private PictureBox[] showuppictureArray = new PictureBox[ANIMAL_COUNT];
//正解を示す数値からなる配列
private int[] answerArray = new int[MAX_GAME_LEVEL]; private playerInformation playingplayerInformation = null;
private playerInformation[] playerinformationArray;
private int maxIndex = -1;
private string logfilePath = Application.StartupPath + "\\play.log";
private bool WriteNewData(int score, bool isCleared)
{
bool isNewrecord = playingplayerInformation.SetNewData(score, isCleared);
string outputbuffer = playingplayerInformation.Name +
"\t" + score.ToString() + "\t" + isCleared.ToString();
System.IO.StreamWriter writer =
new System.IO.StreamWriter(logfilePath, true);
writer.WriteLine(outputbuffer);
writer.Close();
return isNewrecord;
} public mainForm()
{
InitializeComponent();
} private void ResetBackGround()
{
this.backgroundPicture.Image = defaultbackgroundImage;
}
private void HideAnswerPictures()
{
foreach (PictureBox answerbox in this.answerpicturesGroup.Controls)
{
answerbox.Visible = false;
}
}
private void DrawImageOnBackGround(Image drawimage, int x, int y)
{
Bitmap canvasbitmap = new Bitmap(this.backgroundPicture.Image);
Graphics.FromImage(canvasbitmap).DrawImage(drawimage, x - this.backgroundPicture.Left, y - this.backgroundPicture.Top);
this.backgroundPicture.Image = canvasbitmap;
}
private void DrawImageOnBackGround(PictureBox drawpicture)
{
DrawImageOnBackGround(drawpicture.Image, drawpicture.Left, drawpicture.Top);
}
private void DrawImageOnBackGround(Image drawimage, PictureBox locationpicture)
{
int x = locationpicture.Left;
int y = locationpicture.Top;
DrawImageOnBackGround(drawimage, x, y); } private void DrawClearBonus(int clearbonus)
{
int temp = clearbonus / 10;
if (temp / 10 > 0)
{
DrawImageOnBackGround(pointimageArray[temp / 10], this.clearbonuspoint1Picture);
}
if (temp % 10 > 0 temp / 10 > 0)
{
DrawImageOnBackGround(pointimageArray[temp % 10], this.clearbonuspoint2Picture);
}
DrawImageOnBackGround(pointimageArray[clearbonus % 10],
this.clearbonuspoint3Picture);
}
private void DrawTimeBonus(int timebonus)
{
int temp = timebonus / 10;
if (temp / 10 > 0)
{
DrawImageOnBackGround(pointimageArray[temp / 10], this.timebonuspoint1Picture);
}
if ( temp % 10 > 0 temp / 10 > 0)
{
DrawImageOnBackGround(pointimageArray[temp % 10], this.timebonuspoint2Picture);
}
DrawImageOnBackGround(pointimageArray[timebonus % 10],this.timebonuspoint3Picture);
}
private void QuestionStart()
{
this.levelLabel.Text = gamelevel.ToString();
ResetBackGround();
this.Refresh();
System.Threading.Thread.Sleep(500);
for (int i = 0; i < gamelevel; i++)
{
DrawImageOnBackGround(showuppictureArray[answerArray[i]]);
this.Refresh();
System.Threading.Thread.Sleep(500);
ResetBackGround();
this.Refresh();
System.Threading.Thread.Sleep(500);
}
this.timerProgress.Value = gamelevel * 2000;
foreach (PictureBox temppicture in showuppictureArray)
{
DrawImageOnBackGround(temppicture);
}
clickcount = 0;
isAnswering = true;
this.limittimeTimer.Start();
}
private void GameStart()
{
this.gamestartMenu.Enabled = false;
this.playerMenu.Enabled = false;
Random r = new Random();
for (int i = 0; i < MAX_GAME_LEVEL; i++)
{
answerArray[i] = r.Next(0, ANIMAL_COUNT);
((PictureBox)this.answerpicturesGroup.Controls[i]).Image =
answerimageArray[answerArray[i]];
}
HideAnswerPictures();
score = 0;
this.scoreLabel.Text = score.ToString();
gamelevel = 1;
QuestionStart();
}
private void GameClear()
{
bool isNewrecord = WriteNewData(score, true);
ResetBackGround();
HideAnswerPictures();
DrawImageOnBackGround(this.gameclearPicture);
if (isNewrecord)
{
this.Refresh();
System.Threading.Thread.Sleep(1000);
ResetBackGround();
DrawImageOnBackGround(this.newrecordPicture);
}
this.standbyTimer.Start();
}
private void GameOver()
{
WriteNewData(score, false);
ResetBackGround();
HideAnswerPictures();
DrawImageOnBackGround(this.gameoverPicture);
this.standbyTimer.Start();
}
private void QuestionClear()
{
isAnswering = false;
this.limittimeTimer.Stop();
int clearbonus = gamelevel * 30;
int timebonus = this.timerProgress.Value / 100;
DrawImageOnBackGround(this.clearPicture);
DrawImageOnBackGround(this.clearbonusPicture);
DrawImageOnBackGround(this.timebonusPictrue);
DrawClearBonus(clearbonus);
DrawTimeBonus(timebonus);
this.Refresh();
System.Threading.Thread.Sleep(1000);
score += clearbonus + timebonus;
this.scoreLabel.Text = score.ToString();
this.timerProgress.Value = 0;
HideAnswerPictures();
gamelevel++;
if (gamelevel > MAX_GAME_LEVEL)
{
GameClear();
}
else
{
QuestionStart();
}
}
private void pictureBox9_Click(object sender, EventArgs e)
{ } private void label3_Click(object sender, EventArgs e)
{ } private void label4_Click(object sender, EventArgs e)
{ } private void プレToolStripMenuItem_Click(object sender, EventArgs e)
{ } private void toolStripSeparator1_Click(object sender, EventArgs e)
{ } private void mainForm_Load(object sender, EventArgs e)
{
const int DOG_INDEX = 0;
const int SQUIRREL_INDEX = 1;
const int BIRD_INDEX = 2;
const int SNAKE_INDEX = 3;
const int FROG_INDEX = 4;
const int FISH_INDEX = 5;
defaultbackgroundImage = this.backgroundPicture.Image;
showuppictureArray[DOG_INDEX] = this.showupdogPicture;
showuppictureArray[SQUIRREL_INDEX] = this.showupsquirrelPicture;
showuppictureArray[BIRD_INDEX] = this.showupbirdPicture;
showuppictureArray[SNAKE_INDEX] = this.showupsnakePicture;
showuppictureArray[FROG_INDEX] = this.showupdogPicture;
showuppictureArray[FISH_INDEX] = this.showupfishPicture;
correctImage = Properties.Resources.Correct;
uncorrectImage = Properties.Resources.Uncorrect;
pointimageArray[0] = Properties.Resources.Number0;
pointimageArray[1] = Properties.Resources.Number1;
pointimageArray[2] = Properties.Resources.Number2;
pointimageArray[3] = Properties.Resources.Number3;
pointimageArray[4] = Properties.Resources.Number5;
pointimageArray[5] = Properties.Resources.Number5;
pointimageArray[6] = Properties.Resources.Number6;
pointimageArray[7] = Properties.Resources.Number7;
pointimageArray[8] = Properties.Resources.Number8;
pointimageArray[9] = Properties.Resources.Number9;
answerimageArray[DOG_INDEX] = Properties.Resources.AnswerDog;
answerimageArray[SQUIRREL_INDEX] = Properties.Resources.AnswerSquirrel;
answerimageArray[BIRD_INDEX] = Properties.Resources.AnswerBird;
answerimageArray[SNAKE_INDEX] = Properties.Resources.AnswerSnake;
answerimageArray[FROG_INDEX] = Properties.Resources.AnswerFrog;
answerimageArray[FISH_INDEX] = Properties.Resources.AnswerFish;
} private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("ゲームを終了しますか?", "終了", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.No)
{
e.Cancel = true;
}
} private void backgroundPicture_MouseDown(object sender, MouseEventArgs e)
{
if (isAnswering == false) return;
int clickedX = e.X + this.backgroundPicture.Left;
int clickedY = e.Y + this.backgroundPicture.Top;
PictureBox correctshowupPicture = showuppictureArray[answerArray[clickcount]];
double r = 32.0;
foreach (PictureBox tempshowupPicture in showuppictureArray)
{
double centerX = tempshowupPicture.Left + r;
double centerY = tempshowupPicture.Top + r;
double d = Math.Sqrt(Math.Pow(clickedX - centerX, 2) + Math.Pow(clickedY - centerY, 2));
if(d < r)
{
Image tempImage = this.backgroundPicture.Image;
bool isCorrect = tempshowupPicture.Equals(correctshowupPicture);
if (isCorrect)
{
DrawImageOnBackGround(correctImage, tempshowupPicture);
this.answerpicturesGroup.Controls[clickcount].Visible=true;
}
else
{
DrawImageOnBackGround(uncorrectImage, tempshowupPicture);
}
this.Refresh();
System.Threading.Thread.Sleep(500);
this.backgroundPicture.Image = tempImage;
if (isCorrect)
{
clickcount++;
if ( clickcount == gamelevel) QuestionClear();
}
break;
}
}
} private void limittimeTimer_Tick(object sender, EventArgs e)
{
this.timerProgress.Value -= 100;
if (this.timerProgress.Value == 0)
{
this.limittimeTimer.Stop();
isAnswering = false;
GameOver();
}
} private void standbyTimer_Tick(object sender, EventArgs e)
{
this.standbyTimer.Stop();
ResetBackGround();
this.gamestartMenu.Enabled = true;
this.playerMenu.Enabled = true;
} private void gamestartMenu_Click(object sender, EventArgs e)
{
GameStart();
} private void gamequitMenu_Click(object sender, EventArgs e)
{
this.Close();
}
}
public class playerInformation
{
private string nameDATA = null;
private int playtimesData = 0;
private int cleartimesData = 0;
private int totalscoreData = 0;
private int highscoreData = 0;
public playerInformation(string playerName)
{
nameDATA = playerName;
}
public bool SetNewData(int score, bool isCleared)
{
playtimesData++;
totalscoreData += score;
if (isCleared)
{
cleartimesData++;
if (highscoreData < score)
{
highscoreData = score;
return true;
}
}
return false;
}
public string Name
{
get
{
return nameDATA;
} public int PlayTimes
{
get
{
return playtimesData;
}
} public int ClearTimes
{
get{
return cleartimesData;
}
} pulic int HighScore
{
get
{
return highscoreData;
}
} pulic double AverageScore
{
get
{
if (playtimesData > 0)
{
return (double)totalscoreData / playtimesDataData;
}
else
{
return (double)0;
}
}
}
}
}

p.150まで終了。

0 コメント:

コメントを投稿