Click to See Complete Forum and Search --> : Need help with my game-programming
Ole Vig
Jan 9th, 2001, 03:05 AM
Hi, I'm making a game called Alien Invasion, and the main happening in this game is that an ufo appears on the screen and u try to shoot it before it dissapears.
I've written the code to make the ufo appear in random places, but i can't make the code to understand that i'm clicking on the ufo and to register hits/misses and also make the ufo dissapear if i use to much time...
this is the code i got so far:
Private Sub Form_Load()
Hits = 0
Life = 3
frmMainpage.Top = 0
frmMainpage.Left = 0
frmMainpage.Height = 567 * 15.15
frmMainpage.Width = 567 * 21.25
Run
End Sub
Private Sub imgAlien_Click()
Time = 0
Hits = Hits + 1
lblHit = Hit
Run
End Sub
Private Sub Run()
Dim Top As Single
Dim Left As Single
Dim b As Long
b = 0
Do While b <= 4
Randomize
Topp = Rnd(1) * (567 * 15)
Venstre = Rnd(1) * (567 * 21)
If Topp > 567 * 12 Then
Topp = 567 * 10
ElseIf Venstre > 567 * 17 Then
Venstre = 567 * 16
End If
With imgAlien
.Visible = True
.Top = Top
.Left = Left
End With
For Tid = 0 To 900000
Next Tid
b = b + 1
Loop
End Sub
This make the ufo appear on random places, but i need to make the program stop, register hits and then run again...
How do i do this?
Balder
Jan 11th, 2001, 01:14 PM
Hi norwegian neighbour!
For Time=0 To 90000
Next
(Check out "vb code" on top of the page next time you write a post with code in it, makes is easier to read for others ;))
This code doesnīt allow anything else to happen until time has run out, if you click the mouse nothing will happen.
Try read about the function "Timer", this one makes the time run constant. The loop in the code above will take longer on slower machines and shorter on faster ones.
Most people do say that the API call "GetTickCount" works better, but donīt bother right now if you havenīt tried APIs before.
Note: Donīt use "Time" as above either, there is already a function called Time in vb which returns the current time. (I think..)
Also read about the function "DoEvents", which will allow other events to occur while your loop is running.
Itīs very simple though, just place it inside your loop somewhere.
Using your "imgAlien.click" looks like a good idea.
I hope I have given you a few hints, I really think the best thing for you is to just try-change-learn over and over...
Do it my way: buy two bottles of Coke, turn your mobile telephone OFF, tell your friends you are out of town for the weekend and make sure you have gotten enough sleep the night before...then you are ready to make some code! :cool:
Ole Vig
Jan 12th, 2001, 06:33 AM
I've tried to use that timer function, but i've got no idea how to use it...
If someone could show me cinda how to write a timer fuction or just a simple way to use it i would become a very happy man...
Please explain in a simple way how to use the timer function, please. :-)
Alan777
Jan 12th, 2001, 07:15 AM
It sounds like you might want to get a book or 2 on VB before you go any further ;)
Put a timer on your form. Double-click on its icon (on the form).
That will take you to the code window and the method for the Timer's interval event. Now look at the properties window of the timer and change the Interval to the interval that you require (in miliseconds ie. 1000 = 1 second). You can now write the code that you want to be executed on the interval.
Ole Vig
Jan 12th, 2001, 08:21 AM
Thanks, but i got to loan a more advanced bock than the one i got, so i found it out meself...
The problems i am encountering now is how to make the program find out what screen resolution the screen has and setting the resolution to a fixed one, like 800:600 or sommin...
And also i can't change the interval in the timer function, i used the frase: tmrSynlig.interval=1000, when the original interval was to be 2000, but it didn't work, i'm sure i'll find it out soon enough ;-)
Balder
Jan 13th, 2001, 01:51 AM
Ok, there is the component Timer and there is the function Timer.
The component works as Alan777 described.
The function Timer, which you might find useful, returns the number of milliseconds elapsed since midnight.
I havenīt used it for a while, but something like this.
Dim Delay as Long
Delay = Timer
Do
DoEvents 'so other events can occur, like ClickUfo.click
Loop Until Timer = (Delay+1000) 'one second delay time
If you make "Delay" public, you can change it anywhere in your form code. So if you click the ufo you can set it to the Timer value again. Delay=Timer, and your loop will begin another one-second-interval again. Your ClickUfo.click might also contain code to delete the hitted ufo and then to create a new one.
Urgh...think itīs hard to think out the best way even for smaller problems. I always need to test my way to the most "optimal" solution.
Iīm sure there are better and worse ways to do it, but itīs an idea.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.