Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
8.2 Give the player and enemies health
Class Game Extends App
' .............
Method OnCreate()
Self.engine = New CustomEngine
default_scene = engine.GetDefaultScene()
default_layer = engine.GetDefaultLayer()
Local box:ftObject = Self.engine.CreateBox(120, 20, engine.GetCanvasWidth()/2, engine.GetCanvasHeight()/2)
box.SetColor(0, 70, 70)
box.SetMaxSpeed(20.0)
box.SetMinSpeed(-20.0)
box.SetColGroup(PLAYER_GROUP)
box.SetColType(Self.engine.ctBox)
box.SetText("PLAYER")
' Give player 3 health
Self.player = New Character(box, 3, True)
Self.enemies = New List<Character>()
Self.last_enemy_time = Millisecs()
Self.next_enemy_interval = 3000
Seed = Millisecs()
End
' ..................
Method CreateEnemy()
Local rand_width:Float = Rnd(3, 7) * 10
Local rand_height:Float = Rnd(3, 7) * 10
Local rand_y:Float = Rnd(rand_height, Self.engine.GetCanvasHeight())
Local box:ftObject = Self.engine.CreateBox(rand_width, rand_height, Self.engine.GetCanvasWidth(), rand_y)
Local rand_color:Float = Rnd(0, 3)
' randomize enemy health to correspond to color
' red enemies have 1, green enemies have 2
' and blue enemies have 3
Local health = 1
Local colors:Int[] = [0, 0, 0]
If (rand_color <= 1.0)
colors = [255, 0, 0]
Else If (rand_color > 1.0 And rand_color <= 2.0)
colors = [0, 255, 0]
health = 2
Else
colors = [0, 0, 255]
health = 3
End
box.SetColor(colors[0], colors[1], colors[2])
box.SetSpeedX(Rnd(10, 30) * -1)
box.SetColGroup(ENEMY_GROUP)
box.SetColWith(PLAYER_GROUP, True)
box.SetText("ENEMY")
' give the enemy the health value based on their color
enemies.AddLast(New Character(box, health))
Self.next_enemy_interval = Rnd(0, 3) * 1000
End
' .................
End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment