2017-11-10 9 views
1

私は、教科のためにゲームを作っていますが、問題が出てきました。それは非常に単純なゲームです。敵が生き残り(ピクチャーボックス)、それを撃って(左クリックして)死にます(消える)。私は敵が生きている3秒ごとに5人の健康を失いたい。私がこれを行うと考えることができる唯一の方法は、タイマーとテキストボックスを使用することです。ゲームが始まると、タイマーは無効になります。敵が発生すると、タイマーが有効になり、テキストボックスが毎秒1ずつ増加し始めます。ユーザーが敵を殺すと、タイマーは再び無効になり、テキストボックスは0にリセットされます。これで、ユーザーは3秒ごとに健康を失うことができます。次のコードは、私が現在持っているコードです:3秒ごとにラベルの値を減らす方法(VB)

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 

これを行うには他のより効率的な方法があります。私がしようとしていることを理解してくれることを願っています。

答えて

2

まず、スタックオーバーフローは実際にはチュートリアルサイトではありませんが、私はあなたの答えに抵抗することはできません。

OKコードには正直なところでいくつかの問題があります。しかし、まず、あなたの質問に。 TextBoxを使用する代わりに、ラベルを使用します。テキストボックスは、ユーザによって変更される可能性があります。これは問題の1つに私をもたらします。

まず、コントロールをデータのリポジトリとして使用することは、本当に悪いことです。あなたは変数healthで適切なアイデアを持っています。

秒。 Visual Studioの設定でOption Strictをオンにします。そこにいる間、Explicitがオンで、Compareがバイナリで、Inferがオフであることを確認してください。

this Stack Overflow answer

を見ては、これらのオプションを変更すると、少ないバグのあるコードを書くことを意味します持っていますが、下側に、あなたはもう少しを記述する必要があります。

最後に、変数とオブジェクトに意味のある名前を選ぶのに少し時間がかかりますが、それが何をするのかを覚えやすくなります。例えば、Timer2のようなものをTmrGameRunningと呼んでください。TmrGRのようなものではありません。 :-)

LblHealthというラベルを作成する必要があります。私は、TxtBoxコントロールは、単にタイマのダニをカウントするために破棄することができると仮定しています。あなたはそれを必要としません。また、タイマーコントロールとしてタイマーを追加したと仮定すると、タイマーのプロパティでは、ちょうど3秒間のミリ秒数である3000に間隔を設定します。

変更されたコードと説明を参照してください

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 

ヒント。ゲームを開始するには、ボタンコントロールとButton.Clickイベントハンドラメソッドが必要です(ピクチャボックスが表示されているときにこのタイマーを停止するのを忘れないでください)表示されます)、最後に画像をクリックして非表示にしたときに呼び出されるイベントハンドラ(健康を低下させるタイマーを停止)

関連する問題