⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmgame.frm

📁 Vsiual Basic编写的人工智能坦克游戏
💻 FRM
📖 第 1 页 / 共 4 页
字号:
 endgame.Enabled = True
 start.Enabled = False
 cmdload.Enabled = False
 
 gamestarted = True
 update.Enabled = True
 
 Text1.SetFocus
 
 For i = 1 To enemyind
  If enemytank(i) > 0 Then
   timEnemy(enemyindexes(i)).Enabled = True
  End If
 Next
 
End Sub

Private Sub tankdown_Click()
Text1.SetFocus
End Sub

Private Sub tankleft_Click()
Text1.SetFocus
End Sub

Private Sub tankright_Click()
Text1.SetFocus
End Sub

Private Sub tankup_Click()
Text1.SetFocus
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Errorhandler
'MsgBox KeyCode
If gamestarted = False Then Exit Sub
If gameover = True Then Exit Sub

'PlaySound App.Path + "\sounds\tank.wav", 0, SND_ASYNC

If KeyCode = vbKeyUp Then
 
 'check borders
 If tankup.top - 50 < map.top Then Exit Sub
 
 'check for other conflicting objects (i.e. tanks or walls)
 getgrid tankup.left - map.left, tankup.top - map.top - 50
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub
 getgrid tankup.Width + tankup.left - map.left, tankup.top - map.top - 50
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub
 
 'move the tank
 tankup.Visible = True
 tankright.Visible = False
 tankdown.Visible = False
 tankleft.Visible = False
 tankup.top = tankup.top - 50
 tankright.top = tankup.top
 tankdown.top = tankup.top
 tankleft.top = tankup.top
 
End If

If KeyCode = vbKeyRight Then
  
 'Alows more 'freedom' for the tanks
 tankright.top = tankup.top
 tankright.left = tankup.left
 
 'check borders
 If tankright.left + tankright.Width + 50 > map.left + map.Width Then Exit Sub

 'check for other conflicting objects (i.e. tanks or walls)
 getgrid tankright.Width + tankright.left - map.left + 50, tankright.top - map.top
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub
 getgrid tankright.Width + tankright.left - map.left + 50, tankright.top - map.top + tankright.Height
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub

 tankup.Visible = False
 tankleft.Visible = False
 tankright.Visible = True
 tankdown.Visible = False
 tankright.left = tankright.left + 50
 tankup.left = tankright.left
 tankleft.left = tankright.left
 tankdown.left = tankright.left
End If

If KeyCode = vbKeyLeft Then
 
 tankleft.top = tankup.top
 tankleft.left = tankup.left
 'check borders
 If tankleft.left - 50 < map.left Then Exit Sub
 
 'check for other conflicting objects (i.e. tanks or walls)
 getgrid tankleft.left - map.left - 50, tankleft.top - map.top
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub
 getgrid tankleft.left - map.left - 50, tankleft.top - map.top + tankleft.Height
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub

 tankup.Visible = False
 tankright.Visible = False
 tankleft.Visible = True
 tankdown.Visible = False
 tankleft.left = tankleft.left - 50
 tankup.left = tankleft.left
 tankright.left = tankleft.left
 tankdown.left = tankleft.left

End If

If KeyCode = vbKeyDown Then
 
 tankdown.top = tankup.top
 tankdown.left = tankup.left
 'check borders
 If tankdown.top + tankdown.Height + 50 > map.top + map.Height Then Exit Sub

 'check for other conflicting objects (i.e. tanks or walls)
 getgrid tankdown.left - map.left, tankdown.top - map.top + 50 + tankdown.Height
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub
 getgrid tankdown.Width + tankdown.left - map.left, tankdown.top - map.top + 50 + tankdown.Height
 If (mainarr(grx + 1, gry + 1) <> 0) And (mainarr(grx + 1, gry + 1) <> 3) Then Exit Sub

 tankup.Visible = False
 tankright.Visible = False
 tankleft.Visible = False
 tankdown.Visible = True
 tankdown.top = tankdown.top + 50
 tankleft.top = tankdown.top
 tankright.top = tankdown.top
 tankup.top = tankdown.top
End If

If KeyCode = 17 And timplayer.Enabled = False Then
 If tankup.Visible = True Then
  dirplayer = 1
  missileplayer.Picture = missup.Picture
 End If
 If tankright.Visible = True Then
  dirplayer = 2
  missileplayer.Picture = missright.Picture
 End If
 If tankdown.Visible = True Then
  dirplayer = 3
  missileplayer.Picture = missdown.Picture
 End If
 If tankleft.Visible = True Then
  dirplayer = 4
  missileplayer.Picture = missleft.Picture
 End If
 
 'places initial missile position
 If dirplayer = 1 Then missileplayer.Move _
 tankup.left + (tankup.Width / 2) - (missileplayer.Width / 2), tankup.top - _
 missileplayer.Height - 30, missileplayer.Width, _
 missileplayer.Height
 If dirplayer = 2 Then missileplayer.Move _
 tankright.left + tankright.Width + 30, tankright.top + _
 (tankright.Height / 2) - (missileplayer.Height / 2), missileplayer.Width, _
 missileplayer.Height
 If dirplayer = 3 Then missileplayer.Move _
 tankdown.left + (tankdown.Width / 2) - (missileplayer.Width / 2), tankdown.top + _
 tankdown.Height + 30, missileplayer.Width, _
 missileplayer.Height
 If dirplayer = 4 Then missileplayer.Move _
 tankup.left - missileplayer.Width - 30, tankleft.top + _
 (tankleft.Height / 2) - (missileplayer.Height / 2), missileplayer.Width, _
 missileplayer.Height
 missileplayer.Visible = True
 PlaySound App.Path + "\sounds\shoot.wav", 0, SND_ASYNC
 timplayer.Enabled = True
End If


Errorhandler:
Exit Sub

End Sub

Private Sub timBomb_Timer(Index As Integer)
 
 bombarray(Index) = bombarray(Index) + 1
 
 If bombarray(Index) = 1 Then block1(Index).Picture = LoadPicture(App.Path + "\pictures\tankexplode.bmp")
 
 If bombarray(Index) = 10 Then
  bombarray(Index) = 0
  block1(Index).Picture = emptyblock.Picture
  block1(Index).Visible = False
  Unload timBomb(Index)
 End If
 
End Sub

Private Sub timEnemy_Timer(Index As Integer)
Dim r As Integer
Dim prevleft, prevtop As Integer
Dim distancex, distancey As Integer
Dim done As Boolean

On Error GoTo Errorhandler
 
  
 ''Generate a new direction (if necessary)
 If enemytrack(Index).distance > 0 Then
  If (enemytrack(Index).direction = 1) Or (enemytrack(Index).direction = 3) Then
   enemytrack(Index).distance = enemytrack(Index).distance - (gridheight / 18)
  End If
  If (enemytrack(Index).direction = 2) Or (enemytrack(Index).direction = 4) Then
   enemytrack(Index).distance = enemytrack(Index).distance - (gridwidth / 18)
  End If
 Else 'The tank has reached a new grid position
  enemytrack(Index).indanger = False
  getnextgrid enemytrack(Index).direction, Index
  
  ''Random method:
  If aimethod(0).Picture = check.Picture Then
   If (nextgridx > 0) And (nextgridy > 0) Then
    If mainarr(nextgridx, nextgridy) = 0 Then
    rand = enemytrack(Index).direction
     Else
     gen
    End If
   Else
    gen
   End If
  End If
      
  ''Shortest axis method
  If aimethod(1).Picture = check.Picture Then
   distancex = 0
   distancey = 0
   If tankup.left > block1(Index).left Then distancex = tankup.left - block1(Index).left
   If tankup.left < block1(Index).left Then distancex = block1(Index).left - tankup.left
   If tankup.top > block1(Index).top Then distancey = tankup.top - block1(Index).top
   If tankup.top < block1(Index).top Then distancey = block1(Index).top - tankup.top
   'Check which direction to go
   If distancex <= distancey Then
    If tankup.left > block1(Index).left Then
     rand = 2
    Else
     rand = 4
    End If
   Else
    If tankup.top > block1(Index).top Then
     rand = 3
    Else
     rand = 1
    End If
   End If
   'Check if next block is empty
   r = rand
   getnextgrid r, Index
   If (nextgridx > 0) And (nextgridy > 0) Then
    If mainarr(nextgridx, nextgridy) = 1 Then shoot r, Index
   End If
  End If
      
  If (rand = 1) Or (rand = 3) Then
   enemytrack(Index).distance = gridheight
  End If
  If (rand = 2) Or (rand = 4) Then
   enemytrack(Index).distance = gridwidth
  End If
  enemytrack(Index).direction = rand
 
 End If 'end if of next grid position
 
 If enemytrack(Index).indanger = False Then
 ''Tries to avoid missiles
  If (avoid.Value = 1) And (missileplayer.Visible = True) Then
    
   If missileplayer.Picture = missup.Picture Then
   done = False
    If (missileplayer.left > block1(Index).left) And (missileplayer.left < block1(Index).left + block1(Index).Width) Then
     If (missileplayer.top - 10000 < block1(Index).top) Then
      rand = 4
      enemytrack(Index).distance = gridwidth
      done = True
      enemytrack(Index).indanger = True
      enemytrack(Index).direction = rand
      
     End If
    End If
    If (missileplayer.left + missileplayer.Width > block1(Index).left) And (missileplayer.left + missileplayer.Width < block1(Index).left + block1(Index).Width) Then
     If (missileplayer.top - 10000 < block1(Index).top) Then
      If done = False Then
       rand = 2
       enemytrack(Index).distance = gridwidth
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      Else
       gen2
       If rand2 < 6 Then rand = 2
       If rand2 > 5 Then rand = 4
       enemytrack(Index).distance = gridwidth
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      End If
     End If
    End If
   End If
   
   If missileplayer.Picture = missright.Picture Then
   done = False
    If (missileplayer.top > block1(Index).top) And (missileplayer.top < block1(Index).top + block1(Index).Height) Then
     If (missileplayer.left + 10000 > block1(Index).left + block1(Index).Width) Then
      rand = 1
      enemytrack(Index).distance = gridheight
      done = True
      enemytrack(Index).indanger = True
      enemytrack(Index).direction = rand
      
     End If
    End If
    If (missileplayer.top + missileplayer.Height > block1(Index).top) And (missileplayer.top + missileplayer.Height < block1(Index).top + block1(Index).Height) Then
     If (missileplayer.left + 10000 > block1(Index).left + block1(Index).Width) Then
      If done = False Then
       rand = 3
       enemytrack(Index).distance = gridheight
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      Else
       gen2
       If rand2 < 6 Then rand = 3
       If rand2 > 5 Then rand = 1
       enemytrack(Index).distance = gridheight
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      End If
     End If
    End If
   End If
   
   If missileplayer.Picture = missdown.Picture Then
    done = False
    If (missileplayer.left > block1(Index).left) And (missileplayer.left < block1(Index).left + block1(Index).Width) Then
     If (missileplayer.top + 10000 > block1(Index).top + block1(Index).Height) Then
      rand = 4
      enemytrack(Index).distance = gridwidth
      done = True
      enemytrack(Index).indanger = True
      enemytrack(Index).direction = rand
      
     End If
    End If
    If (missileplayer.left + missileplayer.Width > block1(Index).left) And (missileplayer.left + missileplayer.Width < block1(Index).left + block1(Index).Width) Then
     If (missileplayer.top + 10000 > block1(Index).top + block1(Index).Height) Then
      If done = False Then
       rand = 2
       enemytrack(Index).distance = gridwidth
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      Else
       gen2
       If rand2 < 6 Then rand = 2
       If rand2 > 5 Then rand = 4
       enemytrack(Index).distance = gridwidth
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      End If
     End If
    End If
   End If
   
   If missileplayer.Picture = missleft.Picture Then
    done = False
    If (missileplayer.top > block1(Index).top) And (missileplayer.top < block1(Index).top + block1(Index).Height) Then
     If (missileplayer.left - 10000 < block1(Index).left) Then
      rand = 1
      enemytrack(Index).distance = gridheight
      done = True
      enemytrack(Index).indanger = True
      enemytrack(Index).direction = rand
      
     End If
    End If
    If (missileplayer.top + missileplayer.Height > block1(Index).top) And (missileplayer.top + missileplayer.Height < block1(Index).top + block1(Index).Height) Then
     If (missileplayer.left - 10000 < block1(Index).left) Then
      If done = False Then
       rand = 3
       enemytrack(Index).distance = gridheight
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      Else
       gen2
       If rand2 < 6 Then rand = 3
       If rand2 > 5 Then rand = 1
       enemytrack(Index).distance = gridheight
       enemytrack(Index).indanger = True
       enemytrack(Index).direction = rand
       
      End If
     End If
    End If
   End If
  End If
 End If
  
 ''Checks tanks that are frozen
 If (block1(Index).left <> enemytrack(Index).left) Or (block1(Index).top <> enemytrack(Index).top) Then
  enemytrack(Index).left = block1(Index).left
  enemytrack(Index).top = block1(Index).top
  enemytrack(Index).timeout = 0
 End If
 If (block1(Index).left = enemytrack(Index).left) And (block1(Index).top = enemytrack(Index).top) Then
  enemytrack(Index).timeout = enemytrack(Index).timeout + 1
  If enemytrack(Index).timeout = 2 Then
   gen
   enemytrack(Index).direction = rand
   If (rand = 1) Or (rand = 3) Then
    enemytrack(Index).distance = gridheight
   End If
   If (rand = 2) Or (rand = 4) Then
    enemytrack(Index).distance = gridwidth
   End If
   enemytrack(Index).timeout = 0
  End If
 End If
 
 ''Searches for the players tank
 If enemytrack(Index).indanger = False Then
  If (block1(Index).top - 49 < tankup.top) And (block1(Index).top + 49 > tankup.top) Then
   If block1(Index).left < tankup.left Then
    enemytrack(Index).direction = 2
    enemytrack(Index).distance = gridwidth
    If block1(Index).Picture <> enemyright.Picture Then
     block1(Index).left = block1(Index).left - (enemyup.Height - enemyup.Width)
    End If
    block1(Index).Picture = enemyright.Picture
    shoot 2, Index
    Exit Sub
   Else
    enemytrack(Index).direction = 4
    enemytrack(Index).distance = gridwidth
    If block1(Index).Picture <> enemyleft.Picture Then
     block1(Index).left = block1(Index).left + (enemyup.Height - enemyup.Width)
    End If
    block1(Index).Picture = enemyleft.Picture
    shoot 4, Index
    Exit Sub
   End If
  End If
  If (block1(Index).left - 49 < tankup.left) And (block1(Index).left + 49 > tankup.left) Then
   If block1(Index).top < tankup.top Then
    enemytrack(Index).direction = 3
    enemytrack(Index).distance = gridheight
    If block1(Index).Picture <> enemydown.Picture Then
     block1(Index).top = block1(Index).top - (enemyup.Height - enemyup.Width)
    End If
    block1(Index).Picture = enemydown.Picture
    shoot 3, Index
    Exit Sub
   Else
    enemytrack(Index).direction = 1
    enemytrack(Index).distance = gridheight
    If block1(Index).Picture <> enemyup.Picture Then
     block1(Index).top = block1(Index).top + (enemyup.Height - enemyup.Width)
    End If
    block1(Index).Picture = enemyup.Picture
    shoot 1, Index
    Exit Sub
   End If
  End If

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -