Results 1 to 8 of 8

Thread: [2005] Go at random

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Posts
    331

    [2005] Go at random

    I am creating a game sort of a thing, and I need enemys to attack at random times. How would I do this? I can post my code if necessary.
    Katya Chehova is best!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Go at random

    You generate random numbers using the Random class. You could use a random number as the Interval for a Timer, then make your enemy attack from the Elapsed event handler. You might do something like this:
    VB Code:
    1. Private enemyTimers As New Dictionary(Of Timers.Timer, Enemy)
    2. Private attackTimeGenerator As New Random
    3.  
    4. Private Sub AddNewEnemy()
    5.     Dim t As New Timers.Timer
    6.  
    7.     t.AutoReset = True
    8.     Me.SetAttackInterval(t)
    9.     AddHandler t.Elapsed, AddressOf Timer_Elapsed
    10.  
    11.     Dim e As New Enemy
    12.  
    13.     Me.enemyTimers.Add(t, e)
    14. End Sub
    15.  
    16. Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    17.     Dim t As Timers.Timer = DirectCast(sender, Timers.Timer)
    18.  
    19.     'Set a new attack interval.
    20.     Me.SetAttackInterval(t)
    21.  
    22.     'Make the enemy associated with this timer attack.
    23.     Me.enemyTimers(t).Attack()
    24. End Sub
    25.  
    26. Private Sub SetAttackInterval(ByVal t As Timers.Timer)
    27.     'Make the associated enemy attack in a random amount of time from 1 to 10 seconds.
    28.     t.Interval = Me.attackTimeGenerator.NextDouble * 9000.0R + 1000.0R
    29. End Sub
    This assumes that you have declared an Enemy class with an Attack method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Posts
    331

    Re: [2005] Go at random

    It sounds like a good idea, though I have a question.
    Where in that code is the part where I tell the program what to do if there is a new ene,y. I need it to update a label with a new enemy.
    I have like
    Dim enemy As Integer

    Then on the Tick event (I think) I add,
    enemy = enemy + 1
    enemylbl.Text = enemy
    Katya Chehova is best!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Go at random

    Here's a hint:
    Quote Originally Posted by jmcilhinney
    You generate random numbers using the Random class. You could use a random number as the Interval for a Timer, then make your enemy attack from the Elapsed event handler. You might do something like this:
    VB Code:
    1. Private enemyTimers As New Dictionary(Of Timers.Timer, Enemy)
    2. Private attackTimeGenerator As New Random
    3.  
    4. Private Sub [U][COLOR=Red]AddNewEnemy[/COLOR][/U]()
    5.     Dim t As New Timers.Timer
    6.  
    7.     t.AutoReset = True
    8.     Me.SetAttackInterval(t)
    9.     AddHandler t.Elapsed, AddressOf Timer_Elapsed
    10.  
    11.     Dim e As New Enemy
    12.  
    13.     Me.enemyTimers.Add(t, e)
    14. End Sub
    15.  
    16. Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    17.     Dim t As Timers.Timer = DirectCast(sender, Timers.Timer)
    18.  
    19.     'Set a new attack interval.
    20.     Me.SetAttackInterval(t)
    21.  
    22.     'Make the enemy associated with this timer attack.
    23.     Me.enemyTimers(t).Attack()
    24. End Sub
    25.  
    26. Private Sub SetAttackInterval(ByVal t As Timers.Timer)
    27.     'Make the associated enemy attack in a random amount of time from 1 to 10 seconds.
    28.     t.Interval = Me.attackTimeGenerator.NextDouble * 9000.0R + 1000.0R
    29. End Sub
    This assumes that you have declared an Enemy class with an Attack method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Posts
    331

    Re: [2005] Go at random

    As of now my attack method is this:
    VB Code:
    1. enemy = enemy + 1
    2. enemylbl.Text = enemy
    Katya Chehova is best!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Go at random

    My code is an example of the principle. The principle is that you use a Dictionary to store Timers and the objects that represent the enemies that those Timers correspond to. What those objects are is up to you. When the Timer Elapses you set a new Interval and you use the Dictionary to get the object that represents the enemy that corresponds to the Timer that just Elapsed. You then use that object to do what is required to represent an attack from that enemy. That's the principle. Now it's up to you to adapt that principle to whatever specific circumstances you have.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Go at random

    Another alternative would be to use a single Timer object, probably with a smaller Interval. You could then use the Random object to choose a period to wait before the next attack and also to choose a random index. That random index would then be used to get an enemy from an array or collection, who would then attack.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Posts
    331

    Re: [2005] Go at random

    Quote Originally Posted by jmcilhinney
    Another alternative would be to use a single Timer object, probably with a smaller Interval. You could then use the Random object to choose a period to wait before the next attack and also to choose a random index. That random index would then be used to get an enemy from an array or collection, who would then attack.
    Can you show a code example for this? Because this seems like the best way to do it...
    Katya Chehova is best!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width