Last active
December 9, 2016 14:11
-
-
Save codetravis/369eac05e1499e4513f9bcdb866725f8 to your computer and use it in GitHub Desktop.
7.5 Improve enemy generator to make it more interesting
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
#FantomX_UsePhysics = True | |
Import fantomX | |
Import character | |
Class Game Extends App | |
Field engine:ftEngine | |
Field default_layer:ftLayer | |
Field default_scene:ftScene | |
Field player:Character | |
Field enemies:List<Character> | |
Field last_enemy_time:Float | |
' Add field for next enemy random interval | |
Field next_enemy_interval:Float | |
Method OnCreate() | |
Self.engine = New ftEngine | |
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) | |
Self.player = New Character(box, True) | |
Self.enemies = New List<Character>() | |
Self.last_enemy_time = Millisecs() | |
' Starting interval for enemies is 3 seconds | |
Self.next_enemy_interval = 3000 | |
' Set the starting seed of the Pseudo Random number generator | |
' So our game does not start the same every time | |
Seed = Millisecs() | |
End | |
Method OnUpdate() | |
Local time_delta:Float = Float(engine.CalcDeltaTime())/60.0 | |
' put the next enemy interval here so we can randomize the time between enemies | |
If ((Millisecs() - Self.last_enemy_time) > Self.next_enemy_interval) | |
CreateEnemy() | |
Self.last_enemy_time = Millisecs() | |
End | |
player.Update(engine.GetCanvasWidth(), engine.GetCanvasHeight()) | |
If engine.GetPaused() = False | |
engine.Update(time_delta) | |
End | |
End | |
Method CreateEnemy() | |
' Get random size and starting position for our enemy | |
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) | |
' Get a random color for our enemy | |
Local rand_color:Float = Rnd(0, 3) | |
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] | |
Else | |
colors = [0, 0, 255] | |
End | |
box.SetColor(colors[0], colors[1], colors[2]) | |
' Get a random speed for our enemy | |
box.SetSpeedX(Rnd(10, 30) * -1) | |
enemies.AddLast(New Character(box)) | |
' Create a new random interval to the next enemy | |
Self.next_enemy_interval = Rnd(0, 3) * 1000 | |
End | |
Method OnRender() | |
' change to a white background | |
engine.Clear(255, 255, 255) | |
engine.Render() | |
engine.SetColor(0, 70, 70) | |
engine.RenderFlush() | |
End | |
End | |
Function Main() | |
New Game() | |
End |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment