Created
December 14, 2016 13:42
-
-
Save codetravis/528e8fd3271d451e62b88afccde4ff2a to your computer and use it in GitHub Desktop.
8.2 Give the player and enemies health
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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