|
-
Dec 31st, 2002, 11:43 AM
#1
Thread Starter
Hyperactive Member
How to make the ship move smoothly??Look in for details.
Hello Every1 and herez wishing u all a VERY HAPPY & PROSPEROUS NEW YEAR.
Well, coming straight to the point, i'm trying to make a shoot'em sort of a game like the classic Invaders. I have made a scrolling background, it works fine and i can even draw the ship on the background. So far so good. This drawing part is inside a loop which exits once the escape key is pressed. The ship moves left or right depending on the respective key pressed. So far everything is fine, the only problem is that the ship when moving jerks a little bit and doesnt signs of smooth moving. I want to make the ship move smoothly on the background. I'm ncluding the source, can anyone plz help.
N.B:::: Form.Autoredraw is set to true. The ship sprite and the mask for the ship are loaded into a pictureBox. Whereas for the background i have used LoadImage. Plz copy the code and help me. I cannot post the background image but mind u its dimension is 600x600 and its a bitmap. U guys just make any bitmap of 600x600 and load it. Plz help me. Thanq u. Below is the code
-------------------------------------------------------------------------------------------------
Private 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
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const KEY_TOGGLED As Integer = &H1
Const KEY_PRESSED As Integer = &H1000
Dim Left_EdgeTouched As Boolean, Right_EdgeTouched As Boolean
Const IMAGE_BITMAP As Long = 0
Const LR_LOADFROMFILE As Long = &H10
Const LR_CREATEDIBSECTION As Long = &H2000
'****************************************
Dim BackDC As Long
'Back ground dimensions
Const BackHeight As Long = 600
Const BackLength As Long = 600
'The width of the scrolling screen
Const ScrollHeight As Long = 600
Private Sub Form_Click()
End
End Sub
Private Sub Form_Load()
BackDC = GenerateDC(App.Path & "\space.bmp")
BitBlt Me.hdc, 20, ((Screen.Height / Screen.TwipsPerPixelY) - (picSprite.Height + 50)), picMask.ScaleWidth, picMask.ScaleHeight, picMask.hdc, 0, 0, vbSrcAnd
BitBlt Me.hdc, 20, ((Screen.Height / Screen.TwipsPerPixelY) - (picSprite.Height + 50)), picMask.ScaleWidth, picMask.ScaleHeight, picSprite.hdc, 0, 0, vbSrcPaint
Me.Refresh
GameLoop
End Sub
Private Sub GameLoop()
Static Ship_Position As Integer
Me.Show
Do
Background (Ship_Position)
If (GetKeyState(vbKeyLeft) And KEY_PRESSED) And Left_EdgeTouched = False Then
If Ship_Position <= 10 Then
Ship_Position = 10
Right_EdgeTouched = False
Left_EdgeTouched = True
Else
Ship_Position = Ship_Position - 5
Right_EdgeTouched = False
End If
End If
If (GetKeyState(vbKeyRight) And KEY_PRESSED) And Right_EdgeTouched = False Then
If Ship_Position > Me.ScaleWidth - picSprite.Width Then
Ship_Position = Me.ScaleWidth - picSprite.Width
Right_EdgeTouched = True
Left_EdgeTouched = False
Else
Ship_Position = Ship_Position + 5
Left_EdgeTouched = False
End If
End If
If (GetKeyState(vbKeyEscape) And KEY_PRESSED) Then End
DoEvents
BitBlt Me.hdc, Ship_Position, ((Screen.Height / Screen.TwipsPerPixelY) - (picSprite.Height + 50)), picMask.ScaleWidth, picMask.ScaleHeight, picMask.hdc, 0, 0, vbSrcAnd
BitBlt Me.hdc, Ship_Position, ((Screen.Height / Screen.TwipsPerPixelY) - (picSprite.Height + 50)), picMask.ScaleWidth, picMask.ScaleHeight, picSprite.hdc, 0, 0, vbSrcPaint
Me.Refresh
Loop
End Sub
Public Function GenerateDC(FileName As String) As Long
Dim DC As Long
Dim hBitmap As Long
DC = CreateCompatibleDC(0)
If DC < 1 Then
GenerateDC = 0
Exit Function
End If
hBitmap = LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
If hBitmap = 0 Then 'Failure in loading bitmap
DeleteDC DC
GenerateDC = 0
Exit Function
End If
SelectObject DC, hBitmap
GenerateDC = DC
DeleteObject hBitmap
End Function
Private Function DeleteGeneratedDC(DC As Long) As Long
If DC > 0 Then
DeleteGeneratedDC = DeleteDC(DC)
Else
DeleteGeneratedDC = 0
End If
End Function
Private Sub Background(Ship_Position As Integer)
Static Y As Long
Dim GlueHeight As Long, EndScroll As Long
If Y + ScrollHeight > BackHeight Then
GlueHeight = Y + ScrollHeight - BackHeight
EndScroll = ScrollHeight - GlueHeight
'Blit the Background
BitBlt Me.hdc, 0, 0, BackLength, EndScroll, BackDC, 0, Y, vbSrcCopy
BitBlt Me.hdc, 0, EndScroll, BackLength, GlueHeight, BackDC, 0, 0, vbSrcCopy
Else
BitBlt Me.hdc, 0, 0, BackLength, ScrollHeight, BackDC, 0, Y, vbSrcCopy
End If
Y = (Y Mod BackHeight) + 5
End Sub
If Not VB Then Exit
------------------------------------------------
visit me @ http://mzubair.50g.com/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|