|
-
Dec 4th, 2001, 07:35 PM
#1
Thread Starter
New Member
Help me!!!!
I have to make a clone of galaxian for a computer project and I'm having some trouble. I cannot get the ship to shoot. I've looked at some code form other programs, but I can't understand it very well. Can anyone help me? I want the program to show a moving object or line when the user hits the space bar, and when it hits the enemy, I want the computer to record it. Can anyone help me with the code?
If you want more info, just ask. Thanks
-
Dec 5th, 2001, 12:57 AM
#2
I wouldn't mind helping, but what do you have so far? Unless you just want me to make one.. you need to tell the forum what you got so far.
You using DirectX? API (Bitblt)? or just the form and simple objects?
If you are using the latter then you may have your code in the wrong objects keypress sub routine. Such that if another object has the focus when you press the space bar it runs that other objects keypress. I would suggest using bitblt, keeps the form as one of the only objects so your keypresses goto the form. Or use GetASyncKeyState
VB Code:
Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _
(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
Declare Function GetAsyncKeyState Lib "user32" _
Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
Another idea is to make a function that handles the shooting and then make every objects keypressed sub call that function, so you can't miss it.
For all you hard core coders out there I know this may not be the best, actually, I know it isn't the best. DX is the best and the stagities aren't that hot either. But, they do work and a game as simple as Galaxian can still work for a school project using just form objects! I've done it before.
NOMAD
-
Dec 5th, 2001, 07:36 AM
#3
Use BitBlt, or DirectX or PaintPicture as much as you want, but a game using VB objects is walking on a crutch. If the user decides to click on some object, your form loses focus, and the game stops. You can get around this, of course, but there is still the nasty flickering that comes with some objects. If you decide to go this route, be aware of the alternatives.
Z.
-
Dec 5th, 2001, 03:01 PM
#4
Thread Starter
New Member
Can anyone help me with this
I'm using a normal form, not DX. Right now I can only get the ship to moveHere's my code so far (It's very small):
Option Explicit
Dim points As Double
Private Sub Cmdnew_Click()
Cmdnew.Visible = False
lblpoints = 0
lbllives.Caption = 2
lblcallsign.Visible = True
lblpointlabel.Visible = True
lblpoints.Visible = True
lblliveslabel.Visible = True
lbllives.Visible = True
imgship.Visible = True
End Sub
Private Sub Form_Load()
Dim name As String
name = InputBox("What is your callsign?")
lblcallsign.Caption = "Callsign: " + name
lblcallsign.Visible = False
lblpointlabel.Visible = False
lblpoints.Visible = False
lblliveslabel.Visible = False
lbllives.Visible = False
imgship.Visible = False
End Sub
Private Sub imgship_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then
If imgship.Top > 5000 Then
imgship.Move imgship.Left, imgship.Top + 0
Else
imgship.Move imgship.Left, imgship.Top + 60
End If
End If
If KeyCode = vbKeyUp Then
If imgship.Top < 4000 Then
imgship.Move imgship.Left, imgship.Top - 0
Else
imgship.Move imgship.Left, imgship.Top - 60
End If
End If
If KeyCode = vbKeyLeft Then
If imgship.Left < 0 Then
imgship.Move imgship.Left - 0
Else
imgship.Move imgship.Left - 60
End If
End If
If KeyCode = vbKeyRight Then
If imgship.Left > 6500 Then
imgship.Move imgship.Left + 0
Else
imgship.Move imgship.Left + 60
End If
End If
End Sub
-
Dec 5th, 2001, 07:48 PM
#5
Here is the new code for shooting (and a minor fix to the moving code):
VB Code:
Option Explicit
Dim BulletON(4) As Boolean
Dim points As Double
Dim i As Integer
Private Sub Cmdnew_Click()
Cmdnew.Visible = False
lblpoints = 0
lbllives.Caption = 2
lblcallsign.Visible = True
lblpointlabel.Visible = True
lblpoints.Visible = True
lblliveslabel.Visible = True
lbllives.Visible = True
imgShip.Visible = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call GotKey(KeyCode)
End Sub
Private Sub Form_Load()
Dim name As String
name = InputBox("What is your callsign?")
lblcallsign.Caption = "Callsign: " + name
lblcallsign.Visible = False
lblpointlabel.Visible = False
lblpoints.Visible = False
lblliveslabel.Visible = False
lbllives.Visible = False
imgship.Visible = False
For i = 0 To 4
linBullet(i).Visible = False
Next i
End Sub
Public Function GotKey(KeyCode As Integer)
If KeyCode = vbKeyDown Then
If imgShip.Top > 5000 Then
imgShip.Move imgShip.Left, imgShip.Top + 0
Else
imgShip.Move imgShip.Left, imgShip.Top + 60
End If
End If
If KeyCode = vbKeyUp Then
If imgShip.Top < 4000 Then
imgShip.Move imgShip.Left, imgShip.Top - 0
Else
imgShip.Move imgShip.Left, imgShip.Top - 60
End If
End If
If KeyCode = vbKeyLeft Then
If imgShip.Left < 0 Then
imgShip.Move imgShip.Left - 0
Else
imgShip.Move imgShip.Left - 60
End If
End If
If KeyCode = vbKeyRight Then
If imgShip.Left > 6500 Then
imgShip.Move imgShip.Left + 0
Else
imgShip.Move imgShip.Left + 60
End If
End If
'=======================
'===I ADDED THIS CODE===
'=======================
Dim NextBullet As Integer
NextBullet = -1
For i = 0 To 4 'This sees which bullet is next ready
If BulletON(i) = False Then NextBullet = i
Next i
If KeyCode = vbKeySpace And NextBullet <> -1 Then
BulletON(NextBullet) = True
linBullet(NextBullet).Visible = True
linBullet(NextBullet).X1 = imgShip.Left + (imgShip.Width / 2)
linBullet(NextBullet).X2 = imgShip.Left + (imgShip.Width / 2)
linBullet(NextBullet).Y1 = imgShip.Top + 250
linBullet(NextBullet).Y2 = linBullet(NextBullet).Y1 + 240
End If
End Function
Public Function MoveBullet(bullet As Integer)
For i = 0 To 4 '(4 is just the number of bullets
'I chose you may even want a varible)
If BulletON(i) = True Then
linBullet(i).Y1 = linBullet(i).Y1 - 100 'again 100 is arbitrary
linBullet(i).Y2 = linBullet(i).Y1 + 240 ' 240 is just how long the line is
If linBullet(i).Y2 <= 0 Then BulletON(i) = False
End If
Next i
End Function
Private Sub imgship_KeyDown(KeyCode As Integer, Shift As Integer)
Call GotKey(KeyCode)
End Sub
Private Sub Timer1_Timer()
For i = 0 To 4
If BulletON(i) = True Then Call MoveBullet(i)
Next i
End Sub
I hope this all makes scense. I'll try and explain though.
I added a GotKey function this can be used by any object on the form. I then added a call to each or the objects KeyDown sub routines. This is what I was talking about in my first post, about objects taking the focus, this fixes that. Secondly, I added the ability to shoot. You get 5 shots and the shots recycle after they get turned "off". Notice the new array at the top of the form? This array tells if the bullet is going or not (on or off). You can add more bullets by adding more Line objects in the linBullet control array and changing the numbers... you know what a control array is? Its like an array for objects (very handy ) There is a movebullet function that will move a specific bullet one click forward (up) when you send the control array index number. I also added a timer (Timer1) this is set on 1 milli and calls the bulletmove function if bulleton(i) = true, so if the bullet is "on" then it gets moved. This routine will also turn the bullet "off" (false) when it goes off the top. You'd need to add code in a Enemy sub routine to make the bullet turn off when it hits an enemy, or else it will go right through them and keep on truckin' all the way to the top of the screen.
I suggest getting real close to making functions and using timers. Functions simplify and timers regulate. A handy combo. Its also easier to plan and and correct you code like this.
Need explaination on anything I'll be watching this thread, just ask!
NOMAD
-
Dec 6th, 2001, 08:47 PM
#6
Addicted Member
-
Dec 6th, 2001, 09:39 PM
#7
Thread Starter
New Member
Thank a lot everyone, your advice is really helping me.
-
Dec 9th, 2001, 10:58 AM
#8
Thread Starter
New Member
Can anyone tell me what BitBlt is?
-
Dec 9th, 2001, 12:21 PM
#9
Good Ol' Platypus
Bitblt is a graphical API call that uses device contexts (DC, hDC properties). Read about it at my tutorial: http://vbden.tripod.com/articles/invmask.htm
If you need any help, just reply.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
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
|