Author |
Message |
Darkmantis
|
Posted: Mon May 15, 2006 2:46 pm Post subject: Im made a program but some is not right but it should be |
|
|
I have this game Im working on where u have 2 tanks and you shoot each other ive only got the movement and shooting code made. I have the movement for both tanks made and it should work but the 1st tank only works for some reason and I cant figure it out.
ill also attach the file too.
here is my code:
code: |
Dim fire As Boolean
Dim fire2 As Boolean
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 97 Then
If tankright1.Visible = True Then
tankleft1.Left = tankright1.Left
tankleft1.Top = tankright1.Top
tankright1.Visible = False
tankleft1.Visible = True
tankleft1.Left = tankleft1.Left - 100
ElseIf tankup1.Visible = True Then
tankleft1.Left = tankup1.Left
tankleft1.Top = tankup1.Top
tankup1.Visible = False
tankleft1.Visible = True
tankleft1.Left = tankleft1.Left - 100
ElseIf tankdown1.Visible = True Then
tankleft1.Left = tankdown1.Left
tankleft1.Top = tankdown1.Top
tankdown1.Visible = False
tankleft1.Visible = True
tankleft1.Left = tankleft1.Left - 100
ElseIf tankleft1.Visible = True Then
tankleft1.Left = tankleft1.Left
tankleft1.Top = tankleft1.Top
tankleft1.Visible = False
tankleft1.Visible = True
tankleft1.Left = tankleft1.Left - 100
End If
End If
If KeyAscii = 100 Then
If tankright1.Visible = True Then
tankright1.Left = tankright1.Left
tankright1.Top = tankright1.Top
tankright1.Left = tankright1.Left + 100
ElseIf tankup1.Visible = True Then
tankright1.Left = tankup1.Left
tankright1.Top = tankup1.Top
tankup1.Visible = False
tankright1.Visible = True
tankright1.Left = tankright1.Left + 100
ElseIf tankdown1.Visible = True Then
tankright1.Left = tankdown1.Left
tankright1.Top = tankdown1.Top
tankdown1.Visible = False
tankright1.Visible = True
tankright1.Left = tankright1.Left + 100
ElseIf tankleft1.Visible = True Then
tankright1.Left = tankleft1.Left
tankright1.Top = tankleft1.Top
tankleft1.Visible = False
tankright1.Visible = True
tankright1.Left = tankright1.Left + 100
End If
End If
If KeyAscii = 119 Then
If tankright1.Visible = True Then
tankup1.Left = tankright1.Left
tankup1.Top = tankright1.Top
tankright1.Visible = False
tankup1.Visible = True
tankup1.Top = tankup1.Top - 100
ElseIf tankup1.Visible = True Then
tankup1.Left = tankup1.Left
tankup1.Top = tankup1.Top
tankup1.Top = tankup1.Top - 100
ElseIf tankdown1.Visible = True Then
tankup1.Left = tankdown1.Left
tankup1.Top = tankdown1.Top
tankdown1.Visible = False
tankup1.Visible = True
tankup1.Top = tankup1.Top - 100
ElseIf tankleft1.Visible = True Then
tankup1.Left = tankleft1.Left
tankup1.Top = tankleft1.Top
tankleft1.Visible = False
tankup1.Visible = True
tankup1.Top = tankup1.Top - 100
End If
End If
If KeyAscii = 115 Then
If tankright1.Visible = True Then
tankdown1.Left = tankright1.Left
tankdown1.Top = tankright1.Top
tankright1.Visible = False
tankdown1.Visible = True
tankdown1.Top = tankdown1.Top + 100
ElseIf tankup1.Visible = True Then
tankdown1.Left = tankup1.Left
tankdown1.Top = tankup1.Top
tankup1.Visible = False
tankdown1.Visible = True
tankdown1.Top = tankdown1.Top + 100
ElseIf tankdown1.Visible = True Then
tankdown1.Left = tankdown1.Left
tankdown1.Top = tankdown1.Top
tankdown1.Top = tankdown1.Top + 100
ElseIf tankleft1.Visible = True Then
tankdown1.Left = tankleft1.Left
tankdown1.Top = tankleft1.Top
tankleft1.Visible = False
tankdown1.Visible = True
tankdown1.Top = tankdown1.Top + 100
End If
End If
If KeyAscii = 32 And fire = False Then
If tankright1.Visible = True Then
bullet.Top = tankright1.Top + (tankright1.Height / 2)
bullet.Left = tankright1.Left + (tankright1.Width / 2)
bullet.Visible = True
Timer1.Enabled = True
fire = True
ElseIf tankleft1.Visible = True Then
bullet.Top = tankleft1.Top + (tankleft1.Height / 2)
bullet.Left = tankleft1.Left + (tankleft1.Width / 2)
bullet.Visible = True
Timer1.Enabled = True
fire = True
ElseIf tankup1.Visible = True Then
bullet.Top = tankup1.Top + (tankup1.Height / 2)
bullet.Left = tankup1.Left + (tankup1.Width / 2)
bullet.Visible = True
Timer1.Enabled = True
fire = True
ElseIf tankdown1.Visible = True Then
bullet.Top = tankdown1.Top + (tankdown1.Height / 2)
bullet.Left = tankdown1.Left + (tankdown1.Width / 2)
bullet.Visible = True
Timer1.Enabled = True
fire = True
End If
If KeyAscii = 52 Then
If tankright2.Visible = True Then
tankleft2.Left = tankright2.Left
tankleft2.Top = tankright2.Top
tankright2.Visible = False
tankleft2.Visible = True
tankleft2.Left = tankleft2.Left - 100
ElseIf tankup2.Visible = True Then
tankleft2.Left = tankup2.Left
tankleft2.Top = tankup2.Top
tankup2.Visible = False
tankleft2.Visible = True
tankleft2.Left = tankleft2.Left - 100
ElseIf tankdown2.Visible = True Then
tankleft2.Left = tankdown2.Left
tankleft2.Top = tankdown2.Top
tankdown2.Visible = False
tankleft2.Visible = True
tankleft2.Left = tankleft2.Left - 100
ElseIf tankleft2.Visible = True Then
tankleft2.Left = tankleft2.Left
tankleft2.Top = tankleft2.Top
tankleft2.Left = tankleft2.Left - 100
End If
End If
If KeyAscii = 53 Then
If tankright2.Visible = True Then
tankdown2.Left = tankright2.Left
tankdown2.Top = tankright2.Top
tankright2.Visible = False
tankdown2.Visible = True
tankdown2.Top = tankdown2.Top + 100
ElseIf tankup2.Visible = True Then
tankdown2.Left = tankup2.Left
tankdown2.Top = tankup2.Top
tankup2.Visible = False
tankdown2.Visible = True
tankdown2.Top = tankdown2.Top + 100
ElseIf tankdown2.Visible = True Then
tankdown2.Left = tankdown2.Left
tankdown2.Top = tankdown2.Top
tankdown2.Top = tankdown2.Top + 100
ElseIf tankleft2.Visible = True Then
tankdown2.Left = tankleft2.Left
tankdown2.Top = tankleft2.Top
tankleft2.Visible = False
tankdown2.Visible = True
tankdown2.Top = tankdown2.Top + 100
End If
End If
If KeyAscii = 54 Then
If tankright2.Visible = True Then
tankright2.Left = tankright2.Left
tankright2.Top = tankright2.Top
tankright2.Left = tankright2.Left + 100
ElseIf tankup2.Visible = True Then
tankright2.Left = tankup2.Left
tankright2.Top = tankup2.Top
tankup2.Visible = False
tankright2.Visible = True
tankright2.Left = tankright2.Left + 100
ElseIf tankdown2.Visible = True Then
tankright2.Left = tankdown2.Left
tankright2.Top = tankdown2.Top
tankdown2.Visible = False
tankright2.Visible = True
tankright2.Left = tankright2.Left + 100
ElseIf tankleft2.Visible = True Then
tankright2.Left = tankleft2.Left
tankright2.Top = tankleft2.Top
tankleft2.Visible = False
tankright2.Visible = True
tankright2.Left = tankright2.Left + 100
End If
End If
If KeyAscii = 56 Then
If tankright2.Visible = True Then
tankup2.Left = tankright2.Left
tankup2.Top = tankright2.Top
tankright2.Visible = False
tankup2.Visible = True
tankup2.Top = tankup2.Top - 100
ElseIf tankup2.Visible = True Then
tankup2.Left = tankup2.Left
tankup2.Top = tankup2.Top
tankup2.Top = tankup2.Top - 100
ElseIf tankdown1.Visible = True Then
tankup2.Left = tankdown2.Left
tankup2.Top = tankdown2.Top
tankdown2.Visible = False
tankup2.Visible = True
tankup2.Top = tankup2.Top - 100
ElseIf tankleft2.Visible = True Then
tankup2.Left = tankleft2.Left
tankup2.Top = tankleft2.Top
tankleft2.Visible = False
tankup2.Visible = True
tankup2.Top = tankup2.Top - 100
End If
End If
End If
If KeyAscii = 48 And fire2 = False Then
bullet2.Top = tankleft2.Top + (tankleft2.Height / 2)
bullet2.Left = tankleft2.Left + (tankleft2.Width / 2)
bullet2.Visible = True
Timer2.Enabled = True
fire2 = True
End If
End Sub
Private Sub tankboundary_Timer()
If tankleft1.Left < arena.Left Then
tankleft1.Left = arena.Left
ElseIf tankright1.Left > arena.Left + (arena.Width - tankright1.Width) Then
tankright1.Left = arena.Left + (arena.Width - tankright1.Width)
ElseIf tankup1.Top < arena.Top Then
tankup1.Top = arena.Top
ElseIf tankdown1.Top > arena.Top + (arena.Height - tankdown1.Height) Then
tankdown1.Top = arena.Top + (arena.Height - tankdown1.Height)
End If
End Sub
Private Sub Timer1_Timer()
If tankright1.Visible = True Then
bullet.Left = bullet.Left + 50
ElseIf tankleft1.Visible = True Then
bullet.Left = bullet.Left - 50
ElseIf tankdown1.Visible = True Then
bullet.Top = bullet.Top + 50
ElseIf tankup1.Visible = True Then
bullet.Top = bullet.Top - 50
End If
If bullet.Left > arena.Left + arena.Width Then
bullet.Visible = False
bullet.Left = tankright1.Left
fire = False
Timer1.Enabled = False
ElseIf bullet.Left < arena.Left Then
bullet.Visible = False
bullet.Left = tankleft1.Left
fire = False
Timer1.Enabled = False
ElseIf bullet.Top < arena.Top Then
bullet.Visible = False
bullet.Left = tankup1.Left
fire = False
Timer1.Enabled = False
ElseIf bullet.Top > arena.Top + arena.Height Then
bullet.Visible = False
bullet.Left = tankdown1.Left
fire = False
Timer1.Enabled = False
End If
End Sub
Private Sub Timer2_Timer()
If tankright2.Visible = True Then
bullet2.Left = bullet2.Left + 50
ElseIf tankleft2.Visible = True Then
bullet2.Left = bullet2.Left - 50
ElseIf tankdown2.Visible = True Then
bullet2.Top = bullet2.Top + 50
ElseIf tankup2.Visible = True Then
bullet2.Top = bullet2.Top - 50
End If
If bullet2.Left > arena.Left + arena.Width Then
bullet2.Visible = False
bullet2.Left = tankright2.Left
fire2 = False
Timer2.Enabled = False
ElseIf bullet2.Left < arena.Left Then
bullet2.Visible = False
bullet2.Left = tankleft2.Left
fire2 = False
Timer2.Enabled = False
ElseIf bullet2.Top < arena.Top Then
bullet2.Visible = False
bullet2.Left = tankup2.Left
fire2 = False
Timer2.Enabled = False
ElseIf bullet2.Top > arena.Top + arena.Height Then
bullet2.Visible = False
bullet2.Left = tankdown2.Left
fire2 = False
Timer2.Enabled = False
End If
End Sub
|
Description: |
|
Download |
Filename: |
Newgame.zip |
Filesize: |
7.11 KB |
Downloaded: |
82 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
NikG
|
Posted: Mon May 15, 2006 3:32 pm Post subject: (No subject) |
|
|
For some reason, Form_KeyPress doesn't seem to recognize input from the arrow keys. Use Form_KeyUp instead.
Also, your code is, for lack of a better word, a mess! Here's some suggestions to clean it up:
- Try to use the built in constants for the keyboard input, and use a case statement. i.e: code: | Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyA
' move player 1 left
Case vbKeyLeft
' move player 2 left
'...
End Select
End Sub |
- Keep a separate variable for the direction of the shot. Right now, the shot will change direction if you change the direction of the tank
- Create a image control array for each player. Store the 4 different pics in that control array (so that you'll end up with Tank1 (0)/(1)/(2)/(3). Have a variable to keep track (numerically) of what direction the tank is facing (i.e. Tank1Dir). Then you will be able to use simple code like this: code: | Case vbKeyA
Tank1(1).Left = Tank1(Tank1Dir).Left 'assuming Tank1(1) is the left image
Tank1(1).Top = Tank1(Tank1Dir).Top
Tank1(Tank1Dir).Visible = False
Tank1Dir = 1
Tank1(Tank1Dir).Visible = True |
|
|
|
|
|
|
Darkmantis
|
Posted: Mon May 15, 2006 4:40 pm Post subject: (No subject) |
|
|
lol I have an end if in the wrong place so now it works, but your help will still benefit me thx Ill start cleaning up my code as soon as possable
and thx for the help
|
|
|
|
|
|
Darkmantis
|
Posted: Mon May 15, 2006 4:41 pm Post subject: (No subject) |
|
|
oh and 2nd I have no clue how to use arrays Ive never learned them in class, maybe I will next year
|
|
|
|
|
|
Darkmantis
|
Posted: Mon May 15, 2006 4:53 pm Post subject: (No subject) |
|
|
dang, I just noticed another thing thats wrong with my program and its that I cant have both tanks moving at the same time how do I fix that?
|
|
|
|
|
|
cool dude
|
Posted: Mon May 15, 2006 10:15 pm Post subject: (No subject) |
|
|
Darkmantis wrote: oh and 2nd I have no clue how to use arrays Ive never learned them in class, maybe I will next year
if u want to learn about arrays which i strongly suggest u do!!!(trust me on that) u can check out this tutorial http://www.compsci.ca/v2/viewtopic.php?t=71
as for your problem with accepting 2 keys at once i'm not sure u can. for example can u press and hold 2 keys at the same time in ms word? no. i might be mistaken but i don't think its possible
|
|
|
|
|
|
wtd
|
Posted: Mon May 15, 2006 11:00 pm Post subject: (No subject) |
|
|
Holy copy and paste, batman!
code: | tankright1.Left = tankright1.Left
tankright1.Top = tankright1.Top |
|
|
|
|
|
|
Darkmantis
|
Posted: Tue May 16, 2006 5:37 am Post subject: (No subject) |
|
|
wtd wrote: Holy copy and paste, batman!
code: | tankright1.Left = tankright1.Left
tankright1.Top = tankright1.Top |
ya i only get to use the computer for an hour per day so I prefer to take the fastest route possible.
To cooldude: when I make my pong I was able to have both paddles move at the same time so it is possable Im just mixed up and confused
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Tue May 16, 2006 10:20 am Post subject: (No subject) |
|
|
Darkmantis wrote: wtd wrote: Holy copy and paste, batman!
code: | tankright1.Left = tankright1.Left
tankright1.Top = tankright1.Top |
ya i only get to use the computer for an hour per day so I prefer to take the fastest route possible.
Laziness is a virtue for a programmer. However, this is not an example of true, enlightened laziness, since it creates more work later on. Breaking your code down into a set of well-constructed functions would be much lazier.
|
|
|
|
|
|
Darkmantis
|
Posted: Tue May 16, 2006 10:45 am Post subject: (No subject) |
|
|
wow thats very true.
|
|
|
|
|
|
cool dude
|
Posted: Tue May 16, 2006 7:00 pm Post subject: (No subject) |
|
|
Darkmantis wrote:
To cooldude: when I make my pong I was able to have both paddles move at the same time so it is possable Im just mixed up and confused
so how did u do it? if u could do that then it should be no problem figuring out how to do it now
|
|
|
|
|
|
Darkmantis
|
Posted: Wed May 17, 2006 6:40 am Post subject: (No subject) |
|
|
the problem is I have no clue how I got it to work before, it just works.
|
|
|
|
|
|
cool dude
|
Posted: Wed May 17, 2006 4:20 pm Post subject: (No subject) |
|
|
Darkmantis wrote: the problem is I have no clue how I got it to work before, it just works.
post the code!
|
|
|
|
|
|
Darkmantis
|
Posted: Thu May 18, 2006 5:32 pm Post subject: (No subject) |
|
|
I dont have it anymore, my computer crashed 2 months ago and I have to get it repaired and everything got deleted off
|
|
|
|
|
|
|