Unsure on how i can edit my SpawnRate.

12wwood

New Member
Joined
Mar 5, 2019
I need to edit My variable called spawn rate in my code. I need it to drop by 1 a second but it doesn't seem to work anyway i do it. anyway i do it, it just takes it away at the speed of the Update rate.
Cerberus X:
Import mojo


Class MyGame Extends App
    ' timer variables
        Field GameTime:Int
        Field StartTime:Int
    'EndGameStats variables
        Field Accuracy:Int
        Field PlaneShootNum:Int
        Field EndGameTime:Int
    'Lists
        Field Swarm_List:List<Swarm>
        Field Strafe_List:List<Strafe>
        Field Bullet_List:List<Bullet>
        Field Life_List:List<Life>
        Field Player_List:List<Plane>
    'GameState variables
        Global GameState:String = "Menu"
        Field Menu:Image
        Field Instruction1:Image
        Field Instruction2:Image
        Field Background:Image
        Field Paused:Image
    'InGame Variables
        Global PlayerLife:Int
        Global PlayerScore:Int
        Global HiScore:Int
        Field PlayerHit:Bool
        Field RandomX:Int
        Field RandomY:Int
        Field SpawnRate:Int
        Field SpawnChance:Int
'---------------------------------------------------------------------------------------------------------------------
    Method OnUpdate()
    RandomY = Int(Rnd(1,420))
    RandomX = Int(Rnd(1,560))
        Select GameState
            Case "Menu"
                If KeyHit (KEY_1) Then GameState = "Initialise"
                If KeyHit (KEY_2) Then GameState = "Instruction1"
                If KeyHit (KEY_3) Then GameState = "Scoreboard"
                If KeyHit (KEY_4) Then GameState = "Exit"
            Case "Exit"
                Error("you have exited")
            Case "Initialise"
                SpawnRate = 500
                StartTime = Millisecs()
                Player_List.AddLast(New Plane)
                PlayerScore = 0
                PlayerLife = 3
                GameState = "Playing"
                'Initialise EndGameState calculating variables
                'Hi-Score = highest stored value in Array
            Case "Playing"
                SpawnChance = Int(Rnd(SpawnRate))
                GameTime = (Millisecs - StartTime)/1000
                If KeyHit(KEY_P) Then GameState = "Paused"
                'control player
                For Local Player := Eachin Player_List
                    If KeyHit(KEY_SPACE) Then Bullet_List.AddLast(New Bullet(Player.X,Player.Y))
                    If KeyDown(KEY_A) Then Player.Movement(-5,0)
                    If KeyDown(KEY_D) Then Player.Movement(5,0)
                    If KeyDown(KEY_W) Then Player.Movement(0,-4)
                    If KeyDown(KEY_S) Then Player.Movement(0,4)
                Next
                'move bullet
                For Local Bullet:=Eachin Bullet_List
                    Bullet.Move(10)
                    If Bullet.Y < 5 Then Bullet_List.Remove Bullet
                Next
                'move strafe
                For Local Strafe:=Eachin Strafe_List
                    Strafe.Move(4)
                    If Strafe.Y > 440 Then Strafe_List.Remove Strafe
                Next
                For Local Swarm:=Eachin Swarm_List
                    Swarm.Move(5)
                    If Swarm.X > 640 Then Swarm_List.Remove Swarm
                Next
                'Bullet collide with swarm
                For Local Bullet := Eachin Bullet_List
                    For Local swarm := Eachin Swarm_List
                        If Collide(Bullet.X,Bullet.Y,4,20,swarm.X,swarm.Y,40,65)
                            Swarm_List.Remove swarm
                            Bullet_List.Remove Bullet
                            'new explosion(swarm.X,swarm.Y) quick explosion
                            PlayerScore = PlayerScore + 100
                        Endif
                    Next
                Next
                'bullet collide with Strafe
                For Local Bullet:= Eachin Bullet_List
                    For Local Strafe := Eachin Strafe_List
                        If Collide(Bullet.X,Bullet.Y,4,20,Strafe.X,Strafe.Y,40,65)
                            Strafe_List.Remove Strafe
                            Bullet_List.Remove Bullet
                            'quick explode
                            PlayerScore = PlayerScore + 100
                        Endif
                    Next
                Next
                'player colide with Extra life
                For Local Player := Eachin Player_List
                    For Local ExtraLife := Eachin Life_List
                        If Collide(Player.X,Player.Y,85,50,ExtraLife.X,ExtraLife.Y,1,1)'X,Y,W,H
                            Life_List.Remove ExtraLife
                            If PlayerLife < 3
                                PlayerLife = PlayerLife + 1
                            Else PlayerScore = PlayerScore + 1000
                            Endif
                        Endif
                    Next
                Next
                'player colide with strafe
                For Local Player := Eachin Player_List
                    For Local Strafe := Eachin Strafe_List
                        If Collide(Player.X,Player.Y,70,22,Strafe.X,Strafe.Y,30,55)'correct values
                            PlayerLife = PlayerLife - 1
                            Player_List.Remove Player
                            Strafe_List.Remove Strafe
                            ' explode and add time until new spawn
                            PlayerHit = True
                        Endif
                    Next
                Next   
                ' player colide with swarm
                For Local Player := Eachin Player_List
                    For Local swarm := Eachin Swarm_List
                        If Collide(Player.X,Player.Y,62,40,swarm.X,swarm.Y,31,52) ' correct values
                            PlayerLife = PlayerLife - 1
                            Player_List.Remove Player
                            Swarm_List.Remove swarm
                            ' explode and add time until new spawn
                            PlayerHit = True
                        Endif
                    Next
                Next
            If PlayerHit = True And PlayerLife <= 0
                PlayerHit = False
                GameState = "EndScore"
            Elseif PlayerHit = True And PlayerLife <=3 And PlayerLife > 0
                PlayerHit = False
                Player_List.AddLast(New Plane)
            End
            
            If EnemySpawn(SpawnChance) = True
                Strafe_List.AddLast(New Strafe(RandomX,-40))
            End
            If EnemySpawn(SpawnChance) = True
                Swarm_List.AddLast(New Swarm(-40,RandomY))
            End
            
            Case "EndScore"
                ' remove all objects in this state also.
                If KeyHit(KEY_ENTER) Then GameState = "Scoreboard"
            Case "Scoreboard"
                If KeyHit(KEY_ENTER) Then GameState = "Menu"
            Case "Instruction1"
                If KeyHit(KEY_ENTER) Then GameState = "Instruction2"
            Case "Instruction2"
                If KeyHit(KEY_ENTER) Then GameState = "Menu"
            Case "Paused"
                If KeyHit(KEY_2) Then GameState = "Playing"
                If KeyHit(KEY_1)
                    For Local Player:=Eachin Player_List
                        Player_List.Remove Player
                    Next
                    ' place code removing all bullets and enemies too
                    GameState = "Menu"
                End
        End       
    End
    
'------------------------------------------------------------------------------------------------------------------------   
    Method OnCreate()   
        Player_List = New List<Plane>
        Life_List = New List<Life>
        Life_List.AddLast (New Life(100,300))
        Bullet_List = New List<Bullet>
        Swarm_List = New List<Swarm>
        Strafe_List = New List<Strafe>
        SetUpdateRate(60)
        Menu = LoadImage("MenuScreen.png")
        Instruction1 = LoadImage("Instruction1.png")
        Instruction2 = LoadImage("Instruction2.png")
        Background = LoadImage("sea_background.png")
        Paused = LoadImage("PausedMenu.png")
    End
    
    
    Method OnRender()
        Cls(0,0,0)
        Select GameState
            Case "Menu"
                DrawImage(Menu,0,0)
            Case "Playing"
                DrawImage(Background,0,0)
                For Local Bullet:= Eachin Bullet_List
                    DrawImage(Bullet.BulletImage,Bullet.X,Bullet.Y)
                Next
                For Local Player := Eachin Player_List
                    DrawImage(Player.PlayerImage,Player.X,Player.Y)
                Next
                For Local enemy:=Eachin Swarm_List
                    DrawImage (enemy.SwarmImage,enemy.X,enemy.Y)
                Next
                For Local enemy:=Eachin Strafe_List
                    DrawImage (enemy.StrafeImage,enemy.X,enemy.Y)
                Next
                For Local Life := Eachin Life_List
                    DrawImage(Life.LifeImage,Life.X,Life.Y)
                next
                DrawText("SCORE"+" "+PlayerScore,8,15,0,0.5)
                DrawText("HI-SCORE"+" "+ HiScore, 320,15,0.5,0.5)
                DrawText("LIVES"+" "+PlayerLife,610,15,0.5,0.5)
            Case "EndScore"
                DrawText("GAME OVER",320,160,0.5,0.5)
                DrawText("SCORE"+" "+PlayerScore, 320,200,0.5,0.5)
                DrawText("HI-SCORE"+" "+HiScore,320,220,0.5,0.5)
                DrawText("PLANES SHOT DOWN"+" "+PlaneShootNum,320,240,0.5,0.5)
                DrawText("% ACCURACY"+" "+Accuracy+"%",320,260,0.5,0.5)
                DrawText("GAME TIME"+" "+EndGameTime,320,280,0.5,0.5)
            Case "Scoreboard"
                DrawText("This Is ScoreBoard",320,240,0.5,0.5)
            Case "Instruction1"
                DrawImage(Instruction1,0,0)
            Case "Instruction2"
                DrawImage(Instruction2,0,0)
            Case "Paused"
            DrawImage(Paused,0,0)
        End
    End
End


Class Plane
    Field PlayerImage:Image = LoadImage("Player.png")
    Field X:Int = 265
    Field Y:Int = 320
    
    Method Movement(X_distance:Int,Y_distance:Int)
    X = X + X_distance
    Y = Y + Y_distance
    If X <= -15 Then X = -15
    If X >= 535 Then X = 535
    If Y <=  60 Then Y = 60
    If Y >= 410 Then Y = 410
    End
End


Class Swarm
    Field SwarmImage:Image = LoadImage("SwarmPlane.png")
    Field X:Int
    Field Y:Int
    
    Method New(X_spawn:Int,Y_spawn:Int)
        X = X_spawn
        Y = Y_spawn
    End
    
    Method Move(X_distance:Int)
        X = X + X_distance
    End
End


Class Strafe
    Field StrafeImage:Image = LoadImage("StrafePlane.png")
    Field X:Int
    Field Y:Int
    
    Method New(X_Spawn:Int,Y_Spawn:Int)
        X = X_Spawn
        Y = Y_Spawn
    End
    Method Move(Y_distance:Int)
        Y = Y + Y_distance
    End
End


Class Life
    Field X:Int
    Field Y:Int
    Field LifeImage:Image = LoadImage("ExtraLife.png")
    
    Method New(X_Spawn:Int,Y_Spawn:Int)
        X = X_Spawn
        Y = Y_Spawn
    End
End


Class Bullet
    Field X:Int
    Field Y:Int
    Field BulletImage:Image = LoadImage("Bullet.png",Image.MidHandle)
    
    Method New(X_spawn:Int,Y_spawn:Int)
        X = X_spawn + 37
        Y = Y_spawn - 5
    End
    
    Method Move(Y_distance:Int)
        Y = Y - Y_distance
    End
End

Function EnemySpawn(Chance:Int)
    If Chance = 1
        Return True
    Else
        Return False
    End
End


Function Collide:Bool(x1:Int,y1:Int,w1:Int,h1:Int,x2:Int,y2:Int,w2:Int,h2:Int)
    If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
    If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
    Return True
End

Function Main()
    New MyGame
End
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
For better control you need to store the last time used and compare it.
Cerberus X:
If time>=last_time+time_to_wait
    last_time = time
    ' do updates
endif
 

magic

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Mar 5, 2018
Cerberus X:
Import mojo
Function Main();New MyApp;end   
Class MyApp Extends App
    Field milli:Int
    Field spawn:Int=1000
    Method OnCreate()
        milli=Millisecs()
    End
    Method OnUpdate()
        If Millisecs()-milli>1000
            milli=Millisecs()
            spawn = spawn -1
        endif
    End
    Method OnRender()
        Cls
        DrawText("Spawn:"+spawn,10,10)
    End
End
 
Top Bottom