windowsゲームプログラミング実践(もぐら叩きを作ってみよう)
今回は画像を使った。もぐら叩きゲームを作ります。
かわいい女の子の画像を使わせていただくので申し訳ないですがフリー素材なのでお借りします。
まずは前回と同じ様にwindowsフォームアプリケーションを作成しましょう。
Contents
ウインドウサイズの変更
以下の画像を参考に
- windowsフォームデザイナのフォームをクリック
- プロパティウィンドウのプロパティをクリック
- sizeを550,550に変更

PictureBox(画像)を設置
- ツールボックスからPictureBoxをドラッグ&ドロップでwindowsフォームに配置する
- pictureBox1をクリックしてプロパティウィンドウのプロパティをクリック
- imageから…をクリックして使用する画像をインポートする
- サイズを150,130にする
タイマーを設定
- ツールボックスからTimeをドラッグ&ドロップでwindowsフォームに配置する
- timer1をクリックして、プロパティウィンドウのプロパティをクリック
- EnableをTrueに変更
- Interbalを800に変更(ここの数値でタイマーの感覚が変わります)
ソースコードを書く
windowsフォームをダブルクリックすると
Form1_Loadという関数が自動生成されます。そして以下の様にプログラムを書きます。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Text = cnt.ToString();
}
private void Timer1Tick(object sender, EventArgs e)
{
int[] pic_point = new int[] { 0, 100, 200, 300 };
int len = pic_point.Length;
Random rand = new Random();
int index = rand.Next(0, len);
int x = pic_point[index];
index = rand.Next(0, len);
int y = pic_point[index];
pictureBox1.Left = x;
pictureBox1.Top = y;
}
int cnt = 10;
private void HitClickJudge(object sender, EventArgs e)
{
if(0 < cnt)
{
--cnt;
}
Text = cnt.ToString();
if (cnt == 0 && timer1.Enabled)
{
timer1.Enabled = false;
MessageBox.Show("終了");
}
}
}
}
timer1のTickにTimer1Tick関数を登録

- time1をクリック
- ⚡マークをクリック
- TickにTimer1Tick関数を登録する
pictureBox1のClickにHitClickJudge関数を登録

- pictureBox1をクリック
- ⚡マークをクリック
- ClickにHitClickJudgeを登録する
実行結果