1 Attachment(s)
[RESOLVED] Harpoon Based Game Help Needed
Hey guys, I'm currently working on a project which involves a harpoon being shot, to retrieve objects, pull them back up, and repeat.
An example of the game I'm making is Gold Miner found on many game websites.
I've gotten my swinging motion down pretty well I believe, and had some good progress going, but have found my shooting to be quite faulty. I need the harpoon to shoot directly from whatever angle it is at when shot and travel in a straight path until it makes contact with an object or the bottom.
I simplified my game down to the bare minimum code, and would greatly appreciate input to correct my shooting actions.
Thanks :)
Re: Harpoon Based Game Help Needed
Moved From The Vb6 Codebank (which is for sharing code rather than asking questions :) )
Re: [RESOLVED] Harpoon Based Game Help Needed
Well one problem is you are using too many timers. Even one is bad enough. Multiple timers are bad cause one timer has to complete before the next one can fire. For games, even the professional ones, everyone uses whats called a managed game loop:
vb Code:
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Private Ticks_Per_Second As Currency
Private Start_Time As Currency
Private Milliseconds As Long
Private Last_Time As Long
Private Get_Frames_Per_Second As Long
Private Frame_Count As Long
Private Running As Boolean
Public Function Hi_Res_Timer_Initialize() As Boolean
If QueryPerformanceFrequency(Ticks_Per_Second) = 0 Then
Hi_Res_Timer_Initialize = False
Else
QueryPerformanceCounter Start_Time
Hi_Res_Timer_Initialize = True
End If
End Function
Public Function Get_Elapsed_Time() As Single
Dim Last_Time As Currency
Dim Current_Time As Currency
QueryPerformanceCounter Current_Time
Get_Elapsed_Time = (Current_Time - Last_Time) / Ticks_Per_Second
QueryPerformanceCounter Last_Time
End Function
Private Sub Lock_Framerate(Target_FPS As Long)
Static Last_Time As Currency
Dim Current_Time As Currency
Dim FPS As Single
Do
QueryPerformanceCounter Current_Time
FPS = Ticks_Per_Second / (Current_Time - Last_Time)
Loop While (FPS > Target_FPS)
QueryPerformanceCounter Last_Time
End Sub
Private Function Get_FPS() As String
Frame_Count = Frame_Count + 1
If Get_Elapsed_Time - Milliseconds >= 1 Then
Get_Frames_Per_Second = Frame_Count
Frame_Count = 0
Milliseconds = Get_Elapsed_Time
End If
Get_FPS = "FPS: " & Get_Frames_Per_Second
End Function
Private Sub Shutdown()
Running = False
Unload Me
End Sub
Private Sub Main()
With Me
.Show
.ScaleMode = 3
.AutoRedraw = True
.BackColor = RGB(0, 0, 0)
End With
Running = True
Hi_Res_Timer_Initialize
Milliseconds = Get_Elapsed_Time
Game_Loop
End Sub
Private Sub Game_Loop()
Do While Running = True
'//Timer Code Goes Here
Me.Caption = Get_FPS
Lock_Framerate 60
DoEvents
Loop
End Sub
Private Sub Form_Load()
Main
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
Shutdown
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shutdown
End Sub
All of your timer based code should be located within your Game_Loop:
Code:
Private Sub Game_Loop()
Do While Running = True
'//Timer Code Goes Here
Me.Caption = Get_FPS
Lock_Framerate 60
DoEvents
Loop
End Sub
Once thats done, you need to make some boolean flags. Like for your harpoon you can make a flag called Fire. Dim Fire As Boolean up in your General Declarations. In your game loop:
Code:
If Fire = True Then
//firing code here
End if
Once the harpoon is done retrieving, have Fire = False so you can fire again.