海中遊泳と海底歩行

海中遊泳と海底歩行は、ダッシュとジャンプと粗同じで有る。但し、遊泳中は、ジャンプ(此処では上昇の意味)中にジャンプ(更に上昇)しても良い為、ジャンプフラグに依る制限は行う必要が無い。
猶、此処では、海底歩行も出来る様に仕て居るので、キャラクタの画像を遊泳中と歩行中で変える必要が有るので、遊泳中か歩行中を示すフラグ(下記のプログラムでは変数Ws、フラグだがキャラクタ画像の使用部分の位置を算出するのに利用するのでInteger型に仕て居る)が必要と成る。
当然の事だが、海中遊泳と海底歩行では、ダッシュとジャンプとは異なり、水の抵抗を受けるので、各数値は小さめに設定して、其れらしく観える様に調整する必要が有る。
此のプログラムでは、キャラクタを、左右の矢印キーで左右に移動させて、スペースキーで遊泳させて居る。亦、左シフトキーで移動速度を速めて居る(所謂Bダッシュ)。猶、無限ループを抜けるには、エスケープキーを押す。
キャラクタ画像を下記に示す(嘲笑にもメゲず、頑張って描きました)。

| Visual Basic 2005/2008/2010 |
Imports System.Runtime.InteropServices
Public Class swim
<DllImport("user32")> Shared Function GetAsyncKeyState( _
ByVal vKey As Keys) As Short
End Function
Private Bm, Bp As Bitmap
Private Gb, Gf As Graphics
Private Sub swim_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim P As String = Application.StartupPath
If Not P.EndsWith("\") Then P &= "\"
Bm = New Bitmap(P & "map.gif")
Bp = New Bitmap(P & "mariko.gif")
With picDisp
.BackgroundImage = New Bitmap(.Width, .Height)
.Image = New Bitmap(.Width, .Height)
Gb = Graphics.FromImage(.BackgroundImage)
Gf = Graphics.FromImage(.Image)
End With
Gb.Clear(Color.LightBlue)
Gf.Clear(Color.Transparent)
For I As Integer = 0 To 639 Step 32
Gb.DrawImage(Bm, New Rectangle(I, 336, 32, 32), New Rectangle(384, 160, 32, 32), _
System.Drawing.GraphicsUnit.Pixel)
Gb.DrawImage(Bm, New Rectangle(I, 368, 32, 32), New Rectangle(384, 160, 32, 32), _
System.Drawing.GraphicsUnit.Pixel)
Next
End Sub
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
Call Game()
btnStart.Enabled = True
End Sub
Private Sub Game()
Dim Cx As Single = 304.0F ' キャラクタX座標
Dim Cy As Single = 272.0F ' キャラクタY座標
Dim Sp As Single = 1.0F ' 左右移動速度
Dim Ac As Single = 0.0F ' 上下移動速度
Dim Dr As Integer = 0 ' 方向(0:左向き、1:右向き)
Dim St As Integer = 0 ' 表示キャラクタ(0〜3)
Dim Ws As Integer = 1 ' 行動(0:遊泳、1:歩行)
Dim Fl As Boolean = False ' 左移動中
Dim Fr As Boolean = False ' 右移動中
Dim Jp As Boolean = False ' ジャンプ(スイム)
Dim Cnt As Integer = 0 ' ループカウンタ
Dim Tm As DateTime ' タイマカウント(VSYNC相当)
Do
Tm = DateTime.Now
' Quit
If GetAsyncKeyState(Keys.Escape) < 0 Then
Exit Sub
End If
' Dash
If GetAsyncKeyState(Keys.LShiftKey) < 0 Then
Sp += 0.3F : If Sp > 5.0F Then Sp = 5.0F
Else
Sp = 1.0F
End If
' Left Move
If GetAsyncKeyState(Keys.Left) < 0 Then
Fl = True : Dr = 0 : Cx -= Sp : If Cx < 0.0F Then Cx = 0.0F
Else
Fl = False
End If
' Right Move
If GetAsyncKeyState(Keys.Right) < 0 Then
Fr = True : Dr = 1 : Cx += Sp : If Cx > 608 Then Cx = 608
Else
Fr = False
End If
' Jump
If GetAsyncKeyState(Keys.Space) < 0 Then
Jp = True : Ws = 0 : Ac = -3
End If
' While Jump
If Jp Then
Ac += 0.4
Cy += Ac
If Cy > 272.0F Then Cy = 272.0F : Jp = False : Ws = 1
End If
' Disp Caractor
Gf.Clear(Color.Transparent)
Gf.DrawImage(Bp, New Rectangle(Cx, Cy, 64, 64), New Rectangle(St * 64, (Ws * 2 + Dr) * 64, 64, 64), _
System.Drawing.GraphicsUnit.Pixel)
picDisp.Refresh()
' Loop Counter
If Fl OrElse Fr Then Cnt += (Sp + 1)
If Cnt > 10 Then St += 1 : Cnt = 0 : If St > 3 Then St = 0
' Loop Cycle Adjust
Do
Application.DoEvents()
Loop While Tm.AddMilliseconds(50) > DateTime.Now
Loop
End Sub
End Class
|