Microsoft Small Basic

Program Listing: HJJ420
' Balloon Shooting Game
' original by Strawhatter
' modified by Nonki Takahashi

'Initialisation Window
gw = 800
gh = 400
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
fpsTarget= 30

'Initialise Key events
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

keyUp = 0
keyDown = 0

'Initialise Cannon
Cannon = Shapes.AddRectangle(50,50)
Boom = Shapes.AddRectangle(50,20)
balloon = Shapes.AddEllipse(50,50)
balloonDelay=5

StartY=10
dir=-1

CannonX = gw
CannonY = gh
speed = 3
Shapes.Move(Cannon,CannonX-25,CannonY-25)
Shapes.Move(Boom,CannonX-73,CannonY-8)

'Main loop - just move the ball
t = Clock.ElapsedMilliseconds
While ("True")
processKey()
If bullet <> "" Then
bx = Shapes.GetLeft(bullet)
by = Shapes.GetTop(bullet)
Shapes.Move(bullet, bx - 10, by)
EndIf

Collison()
Shapes.Move(Cannon,CannonX-25,CannonY-25)
Shapes.Move(Boom,CannonX-73,CannonY-8)

Program.Delay(10)
EndWhile

'Key press event subroutines
'A separate Down and Up is checked for each key, this tells us the state for any key
'And isn't affected by auto-repeat delays for keys
Sub OnKeyDown
lastKey = GraphicsWindow.LastKey

If(lastKey = "Up") Then
keyUp = 1
ElseIf(lastKey = "Down") Then
keyDown = 1
ElseIf(lastKey="Space")Then
KeySpace =1
EndIf
EndSub

Sub OnKeyUp
lastKey = GraphicsWindow.LastKey

If(lastKey = "Up") Then
keyUp = 0
ElseIf(lastKey = "Down") Then
keyDown = 0
Elseif (lastKey="Space")Then
KeySpace=0
EndIf
EndSub

Sub processKey
randNum = Math.GetRandomNumber(400)

If(keyUp = 1) Then
CannonY = CannonY-speed
BoomY=BoomY - speed
EndIf
If(keyDown = 1) Then
CannonY = CannonY+speed
BoomY= BoomY+speed
EndIf

If KeySpace=1 And bullet = "" Then
bullet=Shapes.AddEllipse(20,20)
Shapes.Move(bullet,gw,CannonY-6)
EndIf
'Check for hitting edges
If (CannonY < 0) Then
CannonY = -CannonY
BoomY=-BoomY
EndIf
If (Cannon > gh) Then
CannonY = -CannonY
BoomY=-BoomY
EndIf

If t + 500 < Clock.ElapsedMilliseconds Then
Shapes.Animate(balloon,10,randNum,500)
t = Clock.ElapsedMilliseconds
EndIf

EndSub

Sub Collison
Ball_Amount=2
If bullet <> "" Then
Bullet_X=Shapes.GetLeft(bullet) + 10
Bullet_Y=Shapes.GetTop(bullet) + 10
Balloon_X=Shapes.GetLeft(balloon) + 25
Balloon_Y=Shapes.GetTop(balloon) + 25
dx = Balloon_X - Bullet_X
dy = Balloon_Y - Bullet_Y
d = Math.SquareRoot(dx * dx + dy * dy)
If d < 35 Then
Sound.PlayClick()
GraphicsWindow.DrawText(Bullet_X, Bullet_Y, "Hello")
Shapes.Remove(bullet)
bullet = ""
ElseIf Bullet_X < 0 Then
Shapes.Remove(bullet)
bullet = ""
EndIf
EndIf
EndSub