0

I am making a game similar to pong in small basic. What I want to do is when the ball hits the wall the game ends and shows an error message.

What I did was use a while loop as such below:

While (hits right wall) or (hits left wall) or (hits top wall) or (hits bottom wall) = "True" 
    GraphicsWindow.ShowMessage("you lost", "game over")
Endwhile

What that actually does is keep repeating the error message and I have to quit the program. How can I get it to just show the message once when the conditions for it hitting either wall is true?

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
dstar19
  • 25
  • 1
  • 5

2 Answers2

0

Use break once the condition is met and once you show the error message.

kawadhiya21
  • 2,458
  • 21
  • 34
0

This is a job for a flag!

Before your loop you set a flag to true. Your loop is based on just that flag. Then for each exit condition that you need you have a separate if statement that handles that logic. When that condition is met, you set the flag to false.

This keeps the code simple, and the logic easy to follow.

Example:

gameOn = "true"

While gameOn = "true"
  'Code to control your game

  If (hitsLeftWall) Then
    gameOn = "false"
  ElseIf (hitsRightWall) then
    gameOn = "false"
  ElseIf (hitsLTopWall) then
    gameOn = "false"
  ElseIf (hitsBottomWall) then
    gameOn = "false"
  EndIf
EndWhile
codingCat
  • 2,396
  • 4
  • 21
  • 27