Reputation: 11
I am making a game for a piece of coursework and I have came across a problem. It is a very simplistic game. An enemy will spawn (Picture Box) and you will shoot it (Left Click) to make it die (Disappear). I want the user to lose 5 health for every 3 seconds the enemy is alive. The only way I could think of doing this is by using a timer and a text box. The timer is disabled when the game begins. When the enemy spawns the timer becomes enabled and the text box begins to increment by one every second. When the user kills the enemy the timer becomes disabled again and the text box is reset to 0. Now all I need to do is for the user to lose health every 3 seconds the enemy is alive. The following code is the code I currently have:
Private Sub timerenabled()
If PicBoxEnemy.Visible = True Then
Timer2.Enabled = True
Else
Timer2.Enabled = False
TxtBox.Text = 0
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
TxtBox.Text = TxtBox.Text + 1
End Sub
Private Sub checkvalue()
Dim x As Integer
x = TxtBox.Text / 3
If CInt(x) Then
Do
health = health - 5
Loop Until health = 0
End If
End Sub
Any other more efficient ways to do this would be appreciated. I hope you understand what I am trying to do.
Upvotes: 1
Views: 321
Reputation: 4439
First and foremost, Stack Overflow isn't really a tutorial site, but I can't resist answering you.
OK there are to be honest several issues with your code. But first, to your question. Instead of using a TextBox, Use a Label. The textbox could be modified by the user. This brings me to one of the issues.
First, It's really bad practice to use controls as the repository for data. You have the right idea with the variable health
.
Second. Turn on Option Strict in Visual studio's settings. While you are there, make sure that Explicit is on, Compare is Binary, and Infer is Off.
Have a look at this Stack Overflow answer
Changing these options will mean that you will write less buggy code , but on the downside, you will need to write a bit more.
Finally take a little time to choose meaningful names for your variables and objects, it will make it a lot easier to remember what they're for. For example call Timer2
something like TmrGameRunning
- Not something like TmrGR
in six months time you probably wont remember what a name like that means. :-)
You'll need to create a label called LblHealth
. I'm assuming that the TxtBox
control can be discarded as it is merely there to count timer ticks. You don't need it. Also assuming that you added the timer as a Timer control, in the timer's properties, just set the interval to 3000 which is the number of milliseconds between ticks = 3 seconds
Have a look at the modified code and the comments for explanations
Public Class Form1
Dim health As Integer
' This will be the variable that note if your player is alive or dead .. True if alive, False if dead
Dim PlayerAlive As Boolean = True
'This is slightly different to your code. In VB, there is an event that will fire when the
'visibility of a textbox changes. The following method will execute when this happens. Just like code
'that you would write when you're handling a button.click event
Private Sub PicBoxEnemy_VisibleChanged(sender As Object, e As EventArgs) Handles PicBoxEnemy.VisibleChanged
If PicBoxEnemy.Visible = True Then
Timer2.Enabled = True
Else
Timer2.Enabled = False
End If
End Sub
'This is a modified version of your timer tick - Don't forget to change the timer .Interval property
'to 3000
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
health = health - 5
'This will change the text of the label to whatever your player's health is and the line below
'will force the label to update
LblHealth.Text = health.ToString
LblHealth.Update()
'Also while the timer is ticking the method below will check the health of your player and decide if
'they are dead or not. If they are, the timer is disabled and the PlayerDead method is called.
AliveOrDead()
End Sub
Private Sub AliveOrDead()
If health <= 0 Then
Timer2.Enabled = False
PlayerDead()
End If
End Sub
'This will be the method that executes when the player is dead. You'll need to add your own code
'for this of course, depending on what you want to do.
Private Sub PlayerDead()
'code here for what happens at the end of the game
End Sub
End Class
Hint. You'll probably need a button control and a Button.Click event handler method to start the game, a way of making the PictureBox visible (possibly at random intervals) while the game is running,(dont forget to stop this timer when the PictureBox is made visible), and finally an event handler that is called when you click on the picture to make it invisible(and stop the timer that reduces health)
Upvotes: 2