0
Module Module1
    Sub MoveLeft()
        Console.WriteLine("You Moved Left")
    End Sub
    Sub MoveRight()
        Dim Number As String
        Console.WriteLine("You Moved Right")
        Randomize()
        Number = Int(Rnd() * 10) + 1
        If Number = 1 To 2 Then 
            Console.WriteLine(" An Enemy Appeared")
        ElseIf  
            Console.WriteLine(" The Way Is Clear")
        End If
    End Sub
    Sub Shoot()
        Console.WriteLine("Pew")
    End Sub
    Sub Main()
        Dim quit As Boolean = False
        Dim key As String = ""
        Do While quit = False


            If Console.KeyAvailable Then
                key = Console.ReadKey(0).Key
                If key = 81 Then
                    quit = True
                End If


                Select Case key
                    Case 37
                        MoveLeft()
                    Case 39
                        MoveRight()
                    Case 32
                        Shoot()
                End Select
            End If
        Loop
    End Sub
End Module

How do I Get A random Number To roll and make certain values make an enemy appear and other values not.

stark
  • 2,246
  • 2
  • 23
  • 35

2 Answers2

1

Instead of

    Randomize()
    Number = Int(Rnd() * 10) + 1
    If Number = 1 To 2 Then 
        Console.WriteLine(" An Enemy Appeared")
    ElseIf  

    End If

Try

Dim rnd As New Random
Select Case rnd.Next(5)
    Case 1
        Console.WriteLine(" An Enemy Appeared")
    Case Else
        Console.WriteLine(" The Way Is Clear")
End Select

This code creates a new randomized randomnumber generator called rnd, generates a random number between 1 and 5 inclusive and if the random number is 1 then an enemy appears. If the random number is anything else, the way is clear.

You only need a range of 5 and a check to see if the result is 1 as this is the same as a range of 10 and checking for a result of 1 or 2

Although speed of computation isn't too important in your program, I think that using a Select Case statement may be quicker than your original code - and is more maintainable later on

David Wilson
  • 4,369
  • 3
  • 18
  • 31
0
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 10.
Dim number As Integer = CInt(Int((10 * Rnd()) + 1))
' Show enemy if the integer number is smaller than 3 (= 1, 2)
If number < 3 Then 
    'your method here to have enemy appear
Else 
    Console.WriteLine(" The Way Is Clear")
End If
display name
  • 4,165
  • 2
  • 27
  • 52