仲間を探せ2
【開発環境】
os:Windows 10
IDE(統合開発環境):VisualStudio 2017
言語;VB
【参考書】
株式会社秀和システム「作って覚えるVisualBasic2017」
著者:萩原 博之/宮崎 昭世
【仲間を探せ2】
前回の仲間を探せゲームを応用したもので、グラフィックを使って仲間を探すように改良したもの!
■仕様
1)いきなり開始。ランダムにボールが飛び交う
2)壁にボールが当たったら、跳ね返る
3)発見するまでタイマーは動いている
4)正解の文字位置はランダムに表示される
5)正解の文字を見つけたら、画面上部に表示された同じ色のボールをクリックする
6)間違った同じ色をクリックすると、ボールの移動速度が遅くなると、同時に10秒加算される
■デザインイメージ
仕上がりのイメージ
【画面作成】
①フォームコントロールにSplitContainerコントロールを貼って画面を二分割にする。
・フォームコントロールのプロパティ設定
(Name)= FormBallGame
FormBallGame.Size = 1050,800
FormBallGame.Text = 間違いボール探し
②SplitContainerコントロールのプロパティ設定
(Name)= SplitContainer1
SplitContainer1.SplitterDistance = 70
③タイマーコントロールを貼る
・タイマーコントロールのプロパティ設定
Timer.interval = 20←好み
[画面上部]
左から順に各コントロールを貼る
①TextBoxコントロールのプロパティ設定
(Name) = textHunt
textHunt.Font = メリオス,20pt
textHunt.Size = 45,47
②Labelコントロールのプロパティ設定
Label9.Text = を探せ
③Labelコントロールのプロパティ設定
Label1.Text = 下の背景に表示された漢字と同じ色の円をクリック→
④Labelコントロールのプロパティ設定
Label2.Text = ↓下のエリアをクリックするとボールの位置が変わります
⑤PicturBoxコントロールのプロパティ設定
(Name) = selectPicturBox
selectPicturBox.BackColor = white
selectPicturBox.Size = 275,50
⑥Buttonコントロールのプロパティ設定
(Name) = restartButton
restartButton.Size = 82,35
restartButton.Text = 再スタート
⑦Labelコントロールのプロパティ設定
Label8.Text = 記録:
⑧TextBoxコントロールのプロパティ設定
(Name) = textTimer
textTimer.Font = メリオス,20pt
textTimer.Size = 129,47
textTimer.Text = Rigt
⑨Labelコントロールのプロパティ設定
Label3.Text = 秒
[画面下部]
①SplitContainerコントロールの下部に、PicturBoxコントロールを貼る
(Name) = mainPictureBox
mainPictureBox.SizeMode = Zoom
mainPictureBox.Dock = Fill
mainPictureBox.BackColor = while
以上
【ためしコーデング】
Partial Class FormBallGame
'画面上部のPictureBoxに円を書くサブルーチン
Private Sub DrowCircleSelectPictureBox()
Dim 高 As Integer = SelectPictureBox.Height '高さ
Dim 幅 As Integer = SelectPictureBox.Width '幅
'図形のサイズを決める
Dim selectCanvas As Bitmap = New Bitmap(幅, 高)
'キャンパスに書くための筆を決める
Using g As Graphics = Graphics.FromImage(selectCanvas)
'塗りつぶし,幅=高さ
g.FillEllipse(Brushes.LightBlue, 0, 0, 高, 高)
'書いた内容をコントロールに割り当てる
SelectPictureBox.Image = selectCanvas
End Using
End Sub
'画面下部のPictureBoxに文字を書くサブルーチン
Private Sub DrowMainPictureBox()
'描画先とするImageオブジェクトを作成する
Dim canvas As Bitmap = New Bitmap(MainPictureBox.Height,MainPictureBox.Width )
'キャンパスに文字を描くための筆を用意
Using g As Graphics = Graphics.FromImage(canvas)
'背景に引数で指定した文字列を描画する
g.DrawString("烏", New Font("HG教科書", MainPictureBox.Height -
MainPictureBox.Height / 4),
Brushes.Orange, 0, 0, New StringFormat())
'MainPictureBoxに表示する
MainPictureBox.Image = canvas
End Using
End Sub
’イベントハンドラー
Private Sub FormBallGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DrowCircleSelectPictureBox()
DrowMainPictureBox()
End Class
■試運転
実行してみる
【コーデング】
■Form1.vb
Public Class FormBallGame
'=============================================================
'クラス共通の変数
'====================================================================
Private canvas As Bitmap '画面下の描画領域
Private
ボール As Ball 'ボールを管理
Private fontName As String = "HG教科書体" '表示する漢字のフォント名
Private correcText As String = "烏" '正解の文字:一つだけ
Private nowTime As Double = 0 '経過時間
'====================================================
'自作サブルーチン部
'===================================================
'画面上部のPictureBoxに円を書くサブルーチン
Private Sub DrowCircleSelectPictureBox()
Dim 高 As Integer = SelectPictureBox.Height '高さ
Dim 幅 As Integer = SelectPictureBox.Width '幅
'図形のサイズを決める
Dim selectCanvas As Bitmap = New Bitmap(幅, 高)
'キャンパスに書くための筆を決める
Using g As Graphics = Graphics.FromImage(selectCanvas)
'塗りつぶし,幅=高さ
g.FillEllipse(Brushes.LightBlue, 0, 0, 高, 高)
'書いた内容をコントロールに割り当てる
SelectPictureBox.Image = selectCanvas
End Using
End Sub
'画面下部のPictureBoxに文字を書くサブルーチン
Private Sub DrowMainPictureBox(color As Brush, font As String, text As String)
Dim 高 As Integer = MainPictureBox.Height '高さ
Dim 幅 As Integer = MainPictureBox.Width '幅
'描画先とするImageオブジェクトを作成する
Dim canvas As Bitmap = New Bitmap(幅, 高)
'キャンパスに文字を描くための筆を用意
Using g As Graphics = Graphics.FromImage(canvas)
'背景に引数で指定した文字列を描画する
g.DrawString("烏", New Font("HG教科書", 高 - 高 / 4),
Brushes.Orange, 0, 0, New StringFormat())
'MainPictureBoxに表示する
MainPictureBox.Image = canvas
End Using
End Sub
'=====================================================
'イベントハンドラー部
'=====================================================
'起動される時に呼ばれるハンドラー
Private Sub FormBallGame_Load(sender As Object, e As EventArgs) Handles Base.Load
'上部のPictureBoxに円を描く
DrowCircleSelectPictureBox()
'下部のPictureBoxに烏を描く
DrowMainPictureBox(Brushes.Orange, fontName, correcText)
textHunt.Text = correcText
'ボールクラスのインスタンス作成
ボール = New Ball(MainPictureBox, Brushes.LightBlue, correcText)
'位置100,100にボールを置く
ボール.PutCircle(100, 100)
'タイマーをスタートさせる
Timer1.Start()
End Sub
'再スタートボタンが押された時、呼ばれるイベントハンドラ
Private Sub RestartButton_Click(sender As Object, e As EventArgs) Handles RestartButton.Click
End Sub
'上のピクチャーボックスが押された時、呼ばれるイベントハンドラ
Private Sub SelectPictereBox_MuuseClick(sender As Object, e As MouseEventArgs) Handles SelectPictureBox.MouseClick
End Sub
'下のピクチャーボックスが押された時、呼ばれるイベントハンドラ
Private Sub MainPictureBox_MouseClick(sender As Object, e As MouseEventArgs) Handles MainPictureBox.MouseClick
End Sub
'タイマーが動いてる時、呼ばれるイベントハンドラ
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ボール.Move()
nowTime = nowTime + 0.02
TextBox2.Text = nowTime.ToString("0.00")
End Sub
End Class
■クラスの作成
Ball.vb
Imports System.Drawing
Imports System.Windows.Forms
'クラス
Public Class Ball
'------------------------------------------
'クラスのデータ部(必要な情報の定義)
'------------------------------------------
'公開データ
Public pitch As Integer '移動の割合
'===============================================
'非公開データ
'===============================================
Private pictureBox As PictureBox '描画する
Private canvas As Bitmap '描画するキャンパス
Private brushColor As Brush '塗りつぶす色
Private positionX As Integer '横位置(X座標)
Private positionY As Integer '縦位置(Y座標)
Private previousX As Integer '以前の横位置(X座標)
Private previousY As Integer '以前の縦位置(Y座標)
Private directionX As Integer '移動方向(X座標)(+1 Or -1)
Private directionY As Integer '移動方向(Y座標)(+1 Or -1)
Private radius As Integer '円の半径
Private kanji As String '表示する漢字
Private fontName As String '表示する漢字のフォント
'====================メソッド部門======================================
'---------------------------------------------
'Ballコンストラクタ
'----------------------------------------------
'4つの引数を指定しクラスの内部に保持する。4つの引数は、描画するPictureBox
'描画するキャンパス、塗りつぶす色、表示する漢字
Public Sub New(pb As PictureBox, cl As Brush, st As String)
pictureBox = pb '描画するpictureBox
canvas = pb.Image '描画するキャンパス
brushColor = cl '塗りつぶす色
kanji = st '表示する漢字
radius = 40 '円の半径の初期設定
pitch = radius / 2 '移動の割合の初期設定(半径の半分)
directionX = +1 '移動方向を+1で初期設定
directionY = +1 '移動方向を+1で初期設定
fontName = "HG教科書体" '漢字のフォント名の初期設定
End Sub
'-------------------------------------
'指定した位置にボールを描くメソッド
'-----------------------------------
Public Sub PutCircle(x As Integer, y As Integer)
'現在の位置を記憶
positionX = x
positionY = y
'------------------------------------
'usingステートメント
'-----------------------------------
Using g As Graphics = Graphics.FromImage(canvas)
'円をbrushColorで指定された色で描く
g.FillEllipse(brushColor, x, y, radius * 2, radius * 2)
'文字列を描画する
g.DrawString(kanji, New Font(fontName, radius),
Brushes.Black, x + 4, y + 12, New StringFormat())
'mainPictureBoxの表示する
pictureBox.Image = canvas
End Using
End Sub
'-----------------------------------
'指定した位置のボールを消すメソッド(白く描く)
'--------------------------------------
Public Sub DeleteCircle()
'初めて呼ばれて以前の値がない場合
If (previousX = 0) Then
previousX = positionX
End If
If (previousY = 0) Then
previousY = positionY
End If
'------------------------------------
'usingステートメント
'-----------------------------------
Using g As Graphics = Graphics.FromImage(canvas)
'円を白で描く
g.FillEllipse(Brushes.White, previousX, previousY, radius * 2, radius * 2)
'mainPictureBoxに表示する
pictureBox.Image = canvas
End Using
End Sub
'-----------------------------------
'指定した位置にボールを動かすメソッド
'--------------------------------------
Public Sub Move()
'以前の表示を削除
DeleteCircle()
'新しい移動先の計算
Dim x As Integer = positionX + pitch * directionX
Dim Y As Integer = positionY + pitch * directionY
'壁で跳ね返る補正
If (x >= pictureBox.Width - radius * 2) Then '右端に来た場合の判断
directionX = -1
End If
If (x <= 0) Then '左端に来た場合の判断
directionX = +1
End If
If (Y >= pictureBox.Height - radius * 2) Then '下端に来た場合の判断
directionY = -1
End If
If (Y <= 0) Then '上端に来た場合の判断
directionY = +1
End If
'跳ね返り補正を反映した値で新しい位置を計算
positionX = x + directionX
positionY = Y + directionY
'新しい位置に描画
PutCircle(positionX, positionY)
'新しい位置を以前の値として記憶
previousX = positionX
previousY = positionY
End Sub
End Class
■実行画面
以上で一つのボールが動くが、これでゲームは完成でない。