VBAでカードゲームを作る
神経衰弱ゲーム
ユーザーフォームデザイン
使用したコントロール コマンドボタン テキストボックス イメージ
コマンドボタン 1のプロ゜ティでcaptionをCommandButton1を消して「覚えてね」
またはCommandButton1をゆっくりクリックして名称を変更「覚えてね」
download マクロを有効にして読み込む
練習1 Image1_Click()の理解
Image1をダブルクリックして 赤字を入力Private Sub Image1_Click()
Image1.Visible = False
Image1.Picture = Image15.Picture
Image1.Visible = True
End Sub
実行
注意 Image1.Visible = Falseを設定しないと画像が変わらない
練習2 CommandButton1_Click()の理解
CommandButton1をダブルクリックして
赤字を入力 ここでは「覚えてね」をクリック
先頭に今回使用する変数を宣言する
Dim x, z, a(14), w, kotae, flag, y, miss, iq, ok, no
Private Sub CommandButton1_Click()
x = Int(Rnd() * 13 + 1)
disp2
Image1.Picture = Image29.Picture
End Sub
Sub disp2()
If x = 1 Then Image29.Picture = Image15.Picture
If x = 2 Then Image29.Picture = Image16.Picture
If x = 3 Then Image29.Picture = Image17.Picture
If x = 4 Then Image29.Picture = Image18.Picture
If x = 5 Then Image29.Picture = Image19.Picture
If x = 6 Then Image29.Picture = Image20.Picture
If x = 7 Then Image29.Picture = Image21.Picture
If x = 8 Then Image29.Picture = Image22.Picture
If x = 9 Then Image29.Picture = Image23.Picture
If x = 10 Then Image29.Picture = Image24.Picture
If x = 11 Then Image29.Picture = Image25.Picture
If x = 12 Then Image29.Picture = Image26.Picture
If x = 13 Then Image29.Picture = Image27.Picture
End Sub
実行 imag1 imag29の2カ所にコマンドボタン1を押すたびにランダムにトランプが表示される
練習3 乱数を利用してトランプを並べる
青字を残して跡を消し赤字を入れる 同じようなステートメントが続くのでコピーをして数値だけを変更する
Private Sub CommandButton1_Click()
w = 1
Do While w <= 13
x = Int(Rnd() * 13 + 1)
a(w) = x
w = w + 1
Loop
disp
End Sub
Sub disp()
x = a(1)
disp2
Image1.Picture = Image29.Picture
x = a(2)
disp2
Image2.Picture = Image29.Picture
x = a(3)
disp2
Image3.Picture = Image29.Picture
x = a(4)
disp2
Image4.Picture = Image29.Picture
x = a(5)
disp2
Image5.Picture = Image29.Picture
x = a(6)
disp2
Image6.Picture = Image29.Picture
x = a(7)
disp2
Image7.Picture = Image29.Picture
x = a(8)
disp2
Image8.Picture = Image29.Picture
x = a(9)
disp2
Image9.Picture = Image29.Picture
x = a(10)
disp2
Image10.Picture = Image29.Picture
x = a(11)
disp2
Image11.Picture = Image29.Picture
x = a(12)
disp2
Image12.Picture = Image29.Picture
x = a(13)
disp2
Image13.Picture = Image29.Picture
End Sub
Sub disp2()
If x = 1 Then Image29.Picture = Image15.Picture
If x = 2 Then Image29.Picture = Image16.Picture
If x = 3 Then Image29.Picture = Image17.Picture
If x = 4 Then Image29.Picture = Image18.Picture
If x = 5 Then Image29.Picture = Image19.Picture
If x = 6 Then Image29.Picture = Image20.Picture
If x = 7 Then Image29.Picture = Image21.Picture
If x = 8 Then Image29.Picture = Image22.Picture
If x = 9 Then Image29.Picture = Image23.Picture
If x = 10 Then Image29.Picture = Image24.Picture
If x = 11 Then Image29.Picture = Image25.Picture
If x = 12 Then Image29.Picture = Image26.Picture
If x = 13 Then Image29.Picture = Image27.Picture
End Sub
実行 同じカードが出てくる
練習4 乱数を利用してトランプを並べる同じカードが出ないようにする
青字を残して跡を消し赤字を入れる 同じようなステートメントが続くのでコピーをして数値だけを変更する
Private Sub CommandButton1_Click()
For w = 1 To 13
a(w) = 0
Next w
w = 1
Do While w <= 13
x = Int(Rnd() * 13 + 1)
flag = 0
For y = 1 To 13
If a(y) = x Then flag = 1
Next y
If flag = 0 Then
a(w) = x
w = w + 1
End If
Loop
disp
End Sub
実行 同じカードが出てこない
これの説明 各項目をセルに代入してみましょう
次の赤字を追加して実行 セルの結果を見る
Dim a(14), w, i, x
Private Sub CommandButton3_Click()
For w = 1 To 13
a(w) = 0
Next w
w = 1
Do While w <= 13
i = i + 1
x = Int(Rnd() * 13 + 1)
Cells(i, w) = x
flag = 0
For y = 1 To 13
If a(y) = x Then flag = 1
Next y
If flag = 0 Then
a(w) = x
w = w + 1
i = 0
End If
Loop
End Sub
緑のセルは同じ乱数が発生したため、再度乱数を発生している
For y = 1 To 13
If a(y) = x Then flag = 1
Next y
同じ数値が配列に代入されていた場合flag 変数を1にして知らせる
1の時は次のステートメントが実行されず乱数数値は代入されない
If flag = 0 Then
a(w) = x
w = w + 1

