キーボードの複数のキーを取得

<戻る

ここに載せてあるソースコードは、参考のために載せてあります

サンプルコードは、一番下にLZHとしてあります

このソースは、何ヶ月か前に作りました。(作りかけ And コメントなし)




Option Explicit

Declare Function GetTickCount Lib "kernel32" () As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Type Shot_Type
    x As Single
    y As Single
    xPlas As Single
    yPlas As Single
    flg As Boolean
End Type

Type Char_Type
    x As Integer
    y As Integer
    Shot(10) As Shot_Type
    MaxShot As Integer
End Type

Type Enemy_Type
    Individual(30) As Char_Type
    
End Type

Public Char As Char_Type, Enemy As Enemy_Type

Sub Main()
    Randomize Time
    Dim j1, j2, j
    
    Char.x = 110
    Char.y = 270
    Char.MaxShot = 6
    With Enemy
        For j = 0 To 5
            .Individual(j).x = Int(Rnd * Form1.Picture1.ScaleWidth - 32)
            .Individual(j).y = Int(Rnd * Form1.Picture1.ScaleHeight - 32)
            .Individual(j).MaxShot = 1
        Next
    End With
    
    Draw
    Form1.Show
    
    With Form1
        Do
            
            DoEvents
            j1 = GetTickCount() ¥ 30
            If j1 <> j2 Then
                j2 = j1
                
                CharMove
                EnemyMove
                .Picture1.Cls
                Draw
                .Picture1.Refresh
                
            End If
            
        Loop
    End With
End Sub

Sub Draw()
    Dim j, i, jm, px, py, picw, pich
    
    picw = Form1.Picture1.ScaleWidth
    pich = Form1.Picture1.ScaleHeight
    For j = 0 To 10
        With Enemy.Individual(j)
            jm = .MaxShot - 1
            BitBlt Form1.Picture1.hDC, .x, Enemy.Individual(j).y, 32, 32, Form1.Picture2.hDC, 32, 32, vbSrcAnd
            BitBlt Form1.Picture1.hDC, .x, Enemy.Individual(j).y, 32, 32, Form1.Picture2.hDC, 32, 0, vbSrcInvert
            For i = 0 To jm
                If .Shot(i).flg Then
                    px = .Shot(i).x
                    py = .Shot(i).y
                    If px > 0 And px < picw And py > 0 And py < pich Then
                        .Shot(i).x = .Shot(i).x + .Shot(i).xPlas
                        .Shot(i).y = .Shot(i).y + .Shot(i).yPlas
                        Form1.Picture1.PSet (.Shot(i).x, .Shot(i).y), RGB(255, 0, 0)
                    Else
                        .Shot(i).flg = False
                    End If
                End If
            Next
        End With
    Next
    
    With Char
        jm = .MaxShot - 1
        BitBlt Form1.Picture1.hDC, .x, .y, 32, 32, Form1.Picture2.hDC, 0, 32, vbSrcAnd
        BitBlt Form1.Picture1.hDC, .x, .y, 32, 32, Form1.Picture2.hDC, 0, 0, vbSrcInvert
        For i = 0 To jm
            If .Shot(i).flg Then
                px = .Shot(i).x
                py = .Shot(i).y
                If px > 0 And px < picw And py > 0 And py < pich Then
                    .Shot(i).x = .Shot(i).x + .Shot(i).xPlas
                    .Shot(i).y = .Shot(i).y + .Shot(i).yPlas
                    Form1.Picture1.PSet (.Shot(i).x, .Shot(i).y), RGB(0, 255, 255)
                Else
                    .Shot(i).flg = False
                End If
            End If
        Next
    End With
End Sub

Sub CharMove()

    With Char
        If Sgn(GetKeyState(vbKeyRight)) = -1 Then
            .x = .x + 3
        End If
        If Sgn(GetKeyState(vbKeyLeft)) = -1 Then
            .x = .x - 3
        End If
        If Sgn(GetKeyState(vbKeyDown)) = -1 Then
            .y = .y + 3
        End If
        If Sgn(GetKeyState(vbKeyUp)) = -1 Then
            .y = .y - 3
        End If
        If Sgn(GetKeyState(vbKeyC)) = -1 Then
            CharShot 1
        End If
        If Sgn(GetKeyState(vbKeyZ)) = -1 Then
            CharShot 2
        End If
        
    End With
End Sub

Sub EnemyMove()
    Dim j
    
    With Enemy
        For j = 0 To 10
            .Individual(j).x = .Individual(j).x + Int(Rnd * 3) - 1
            .Individual(j).y = .Individual(j).y + Int(Rnd * 3) - 1
        Next
    End With
End Sub

Sub CharShot(flg As Integer)
    Dim j, jm, i, jb As Boolean
    
    If flg = 1 Then
        With Char
            jm = .MaxShot - 1
            For j = 0 To jm
                If .Shot(j).flg = False And jb = False Then
                    .Shot(j).flg = True
                    .Shot(j).x = .x + 15
                    .Shot(j).y = .y
                    .Shot(j).xPlas = 0
                    .Shot(j).yPlas = -4
                    jb = True
                    i = i + 1
                End If
            Next
        End With
    End If
    Debug.Print "Shoot! " & i
End Sub



<戻る

Sample23.lzh


http://www.vector.co.jp/authors/VA015521/