Missile Attack in Monkey X

Again, some nostalgia. In older blog post I presented a shortened version of my old implementation of Amiga’s “Missile Attack”. This night I made the game in Monkey X and the source can be directly compiled to Android target.

The game is quite simple one: Just shoot the missiles before they get to the bottom of the screen.

If a missile goes to the left or right side of the screen, you see the colors of the background changing — and also when you fire a shot. This gives the game more life. 🙂

Below is the source code:

Import mojo2

Function Main()
	New MyApp
End

Class Missile
	Field x:Float		' x-coordinate of a missile
	Field y:Float		' y-coordinate of a missile
	Field startx:Float
	Field addx:Float
	' the color of the missile
	Field red:Float
	Field green:Float
	Field blue:Float
End Class

Class MyApp Extends App

	Field bg:Image
	Global canvas:Canvas
	Global nrOfMissiles:Int
	Global mx:Float, my:Float
	Global shot:Bool
	Global gameOver:Bool = False	
	Global px:Float, py:Float, pr:Float
	Global scalex:Float, scaley:Float
	
	Global missileList := New List<Missile>	' build a missile list
	
	Method OnCreate()
		SetDeviceWindow(640,480,0)
		canvas = New Canvas
		shot = False
		nrOfMissiles = 0
		bg = Image.Load("bg.png",0,0)
		scalex = DeviceWidth() / 640.0
		scaley = DeviceHeight() / 480.0
		SetUpdateRate(60)
	End
	
	Method OnUpdate()

		If gameOver = False Then
			
			If nrOfMissiles < 8 Then
			
				Local mssl:Missile = New Missile
				mssl.x = Rnd(100,539)
				mssl.startx = mssl.x
				mssl.y = 0
				mssl.addx = Rnd(-2,2)
				mssl.red = Rnd(0.2,1)
				mssl.green = Rnd(0.2,1)
				mssl.blue = Rnd(0.2,1)
				missileList.AddLast(mssl)
			
				nrOfMissiles = nrOfMissiles + 1
			Endif

			If TouchHit(0) > 0 Then
				mx = TouchX() / scalex
				my = TouchY() / scaley
				pr = 4
				px = mx
				py = my
				shot = True
			Endif
		
		Endif
		
	End

	Method OnRender()

		canvas.PushMatrix()
		canvas.Scale(scalex, scaley)
		
		canvas.DrawImage bg,0,0
		
		If gameOver = False Then
			
			If nrOfMissiles > 0 Then
								
				For Local mssl:Missile = Eachin missileList
				
					If mssl <> Null Then
						canvas.SetColor mssl.red,mssl.green,mssl.blue	
						canvas.DrawLine(mssl.startx,0,mssl.x,mssl.y)
						mssl.y = mssl.y + 1
				
						If mssl.y >= 479 Then gameOver = True
						
						mssl.x = mssl.x + mssl.addx
					
						If mssl.x < 0 Or mssl.x > 639 Then
							missileList.Remove(mssl)
							nrOfMissiles = nrOfMissiles - 1
						Endif
					
					Endif
				Next
			Endif
		
			If shot = True Then
				canvas.SetColor 0,0,1
				canvas.DrawCircle(px,py,pr)
			Endif

			If shot = True And pr < 50 Then pr = pr + 1 Else shot = False
			
			For Local mssl:Missile = Eachin missileList
				Local removeMissile:Bool = False
				
				If mssl <> Null And shot = True Then
				
					' Mathematical way to determine if player's shot has hit the missile...
					For Local angle:Int = 0 To 360
						If Abs((px + Cos(angle) * pr) - mssl.x) <= 1 And Abs((py + Sin(angle) * pr) - mssl.y) <= 1 Then
							removeMissile = True
							score = score + 1
							Exit
						Endif
					Next
					
					If removeMissile = True Then
						missileList.Remove(mssl)
						nrOfMissiles = nrOfMissiles - 1
					Endif
					
				Endif

			Next

		Else
			canvas.SetColor 1,1,1
			canvas.DrawText "Game Over",(640 - 9 * 8) / 2, (480 - 8) / 2
		Endif
		
		canvas.Flush()
		canvas.PopMatrix()	
	End
	
End Class

Source code license: GNU General Public License 3.0.

Below is the video of the game on Android tablet and on computer screen:

I may make a better version of the game later on this summer…