Okay, with the help of the great people here, and the wonderful Smack It tutorial videos by Daniel Foreman, I got my game running with no bugs (as far as the Debug program can find, at least).
However, when I restart the game, it will set the score back to 0, but it still displays the previous score until I score a new point, then it will revert to 1. I can't seem to pinpoint the issue.
Another issue I'm having is at the end of the first game, it displays the Highscore as 0 no matter how many points I have, but at the end of the next game, it will show the current high score. So it's gathering the info, but not displaying it until the next play through.
UPDATE: I fixed the highscore problem. I was telling the endgame section to write the new highscore, but I didn't tell it to read it.
Because of the nature of my coding, I can't just post the pertinent code, so I'll post the whole code. I know it's still a little messy, but nothing like what it was before. It's my first game. Some of the code I don't use currently, but will later on.
Any help would be greatly appreciated!
//////TYPES
Type _Orb
spr as Integer
spd as Float
Endtype
Type _Side
spr as Integer
Endtype
Type _Rock
spr as Integer
Endtype
Type _Player
spd as Float
Endtype
//////GLOBAL IDENTIFIERS
Global GameMode
Global Score
Global FinalScore
Global Highscore
Global MainMenu
Global EndMenu
Global Waterfall
Global Player
Global plpfall
//Global y# as Float
Global initialScroll#
Global defaultScroll#
Global increment
Global gameStart#
//////ARRAYS
Dim OrbImage [ 0 ]
Dim Orbs [ 2 ] as _Orb
Dim OrbDustImage [ 0 ]
Dim OrbDust [ 0 ]
Dim WaterfallImage[ 0 ]
Dim SideImages [ 3 ]
Dim Sides [ 3 ] as _Side
Dim RockImage [ 0 ]
Dim Rocks [ 5 ] as _Rock
Dim Text [ 3 ]
// set display properties
SetVirtualResolution( 768, 1024 )
SetOrientationAllowed( 1, 1, 0, 0 )
EnableClearColor ( 0 )
// set physics
//SetPhysicsGravity( 0, 300 )
SetPhysicsWallTop ( 0 )
SetPhysicsWallBottom ( 0 )
SetPhysicsWallLeft ( 0 )
SetPhysicsWallRight ( 0 )
// SetPhysicsDebugOn ()
// call functions
CreateBackground ()
CreateMainMenu ()
CreateEndMenu ()
LoadLevelImages ()
LoadOrbImages ()
LoadPlayerImage ()
LoadFont ()
CreateTextLines ()
LoadSounds ()
CreateLevel ()
CreateOrbSprites ()
CreateJoystick ()
ActivateMenu ()
SetSpriteOrder ()
// set game mode
GameMode = 0
do
Select GameMode
Case 0
if GetPointerPressed () = 1
Score = 0
ActivateGame ()
GameMode = 1
ReadHighscoreFile ()
endif
Endcase
Case 1
IncreaseScrollingSpeed ()
Movement ()
CheckPlayerAgainstRocks ()
CheckPlayerWithSides ()
CheckPlayerWithOrbs ()
CheckRocksAgainstEachOther ()
Endcase
Endselect
Sync()
loop
//////FUNCTIONS
function CreateBackground ()
background = CreateSprite ( LoadImage ( "bg1.png" ))
endfunction
function CreateMainMenu ()
mainmenu = CreateSprite ( LoadImage ( "mainmenu.png" ))
endfunction
function CreateEndMenu ()
endmenu = CreateSprite (LoadImage ( "gameover.png" ))
endfunction
function LoadLevelImages ()
WaterfallImage[ 0 ] = LoadImage ( "waterfall.png" )
SideImages[ 0 ] = LoadImage ( "tower.png" )
SideImages[ 1 ] = LoadImage ( "tower2.png" )
RockImage[ 0 ] = LoadImage ( "rock.png" )
endfunction
function LoadOrbImages ()
OrbImage [ 0 ] = LoadImage ( "orb.png" )
endfunction
function LoadPlayerImage ()
Plp = LoadImage ( "fishspriteg.png" )
endfunction
function LoadFont ()
defaultfontimage = LoadImage ( "ascii.png" )
SetTextDefaultFontImage ( defaultfontimage )
endfunction
function CreateTextLines ()
Text[ 0 ] = CreateText ( "SCORE" + chr ( 10 ) + str ( Score ) )
Text[ 1 ] = CreateText ( "HIGHSCORE" + chr ( 10 ) + str ( highscore ) )
Text[ 2 ] = CreateText ( "" )
SetTextPosition ( Text [0], 50, 25 )
SetTextSize ( Text [0], 24)
SetTextAlignment ( Text [0], 0 )
SetTextVisible ( Text[ 1 ], 1 )
SetTextPosition ( Text [ 1 ], 384, 622 )
SetTextSize ( Text [ 1 ], 24)
SetTextAlignment ( Text [1], 1 )
SetTextPosition ( Text [ 2 ], 384, 300 )
SetTextSize ( Text [ 2 ], 36)
SetTextAlignment ( Text [ 2 ], 1 )
endfunction
function LoadSounds ()
// scoring sounds
LoadSound ( 1, "hit1.wav" )
LoadSound ( 2, "hit2.wav" )
LoadSound ( 3, "hit3.wav" )
LoadSound ( 4, "hit4.wav" )
LoadSound ( 5, "hit5.wav" )
//dying sounds
LoadSound ( 6, "hit1.wav" )
LoadSound ( 7, "hit2.wav" )
LoadSound ( 8, "hit3.wav" )
LoadSound ( 9, "hit4.wav" )
LoadSound ( 10, "hit5.wav" )
endfunction
/*
function LoadPlayMusic ()
LoadMusic = ( 1, "maintheme.mp3" )
PlayMusic = ( 1, 1 )
endfunction
*/
function CreateLevel ()
// create waterfall
Waterfall = LoadImage ( "waterfall.png" )
// create sides
Sides[ 0 ].spr = CreateSprite(LoadImage ("tower.png"))
setSpriteSize(Sides[0].spr,610,3143)
setSpritePosition(Sides[0].spr,-480,-2119)
SetSpriteVisible (Sides[0].spr, 1)
SetSpriteShape( Sides[0].spr, 3 )
SetSpriteDepth ( Sides[0].spr, 9 )
SetSpriteCategoryBit (Sides[0].spr, 3, 1)
Sides[ 1 ].spr = createSprite(LoadImage ("tower2.png"))
setSpriteSize(Sides[1].spr,610,3143)
setSpritePosition(Sides[1].spr,-480,-5262)
SetSpriteVisible (Sides[1].spr, 0)
SetSpriteShape( Sides[1].spr, 3 )
SetSpriteDepth ( Sides[1].spr, 9 )
Sides[ 2 ].spr = createSprite(LoadImage ("tower.png"))
setSpriteSize(Sides[2].spr,610,3143)
setSpritePosition(Sides[2].spr,638,-2119)
SetSpriteVisible (Sides[2].spr, 1)
SetSpriteShape( Sides[2].spr, 3 )
SetSpriteDepth ( Sides[2].spr, 9 )
Sides[ 3 ].spr = createSprite(LoadImage ("tower2.png"))
setSpriteSize(Sides[3].spr,610,3143)
setSpritePosition(Sides[3].spr,638,-5262)
SetSpriteVisible (Sides[3].spr, 1)
SetSpriteShape( Sides[3].spr, 3 )
SetSpriteDepth ( Sides[3].spr, 9 )
// create rocks
for i = 0 to 5
Rocks [ i ] .spr= CreateSprite (LoadImage ("rock.png"))
setSpritePosition(Rocks [ i ].spr,Random (100, 568), Random2 (-3600, -200))
SetSpriteDepth(Rocks [ i ].spr,1)
SetSpriteCategoryBit (Rocks [ i ].spr, 3, 1)
SetSpriteCollideBit (Rocks [ i ].spr, 4, 1)
SetSpritePhysicsOn (Rocks [ i ].spr, 1)
SetSpriteVisible (Rocks [ i ].spr, 1)
next i
endfunction
function CreateOrbSprites ()
for i = 0 to 2
Orbs [ i ].spr = CreateSprite ( LoadImage ("orb.png"))
SetSpriteSize (Orbs [ i ].spr, 40, 40)
SetSpritePosition (Orbs [ i ].spr, Random (110, 630), Random2 (-1358, 0))
SetSpriteCategoryBit (Orbs [ i ].spr, 3, 1)
SetSpriteCollideBit (Orbs [ i ].spr, 4, 1)
SetSpritePhysicsOn (Orbs [ i ].spr, 3)
SetSpriteVisible (Orbs [ i ].spr, 1)
SetSpritePhysicsVelocity (Orbs [ i ].spr, 0, Random (50,500))
next i
endfunction
function CreateJoystick ()
// Create joystick
LoadImage (50, "joystick.png")
AddVirtualJoystick (1, 650, 950, 125 )
SetVirtualJoystickImageOuter (1,50)
SetVirtualJoystickImageInner (1,50)
endfunction
function CreateCharacterSprite ()
spd = 15
Player = CreateSprite (LoadImage ("fishspriteg.png"))
SetSpritePosition(Player, 300, 600)
SetSpriteCategoryBit(Player, 4, 1)
SetSpriteCollideBit (Player, 3, 1)
SetSpritePhysicsOn(Player, 1)
SetSpriteShapeBox ( Player, -1*(GetSpriteWidth(Player)/20), -1*(GetSpriteHeight(Player)/4), GetSpriteWidth(Player)/20, GetSpriteHeight(Player)/4, 0 )
SetSpriteAnimation ( Player, 56, 75, 8 )
PlaySprite ( Player, spd, 1, 8, 1 )
endfunction
function ActivateMenu ()
SetSpriteVisible ( endmenu, 0 )
SetSpriteVisible ( MainMenu, 1 )
CreateCharacterSprite ()
SetSpriteVisible ( Player, 0 )
for i = 0 to 2
SetTextVisible ( Text[ i ], 0 )
next i
endfunction
function SetSpriteOrder ()
SetTextDepth ( Text [ 0 ], 0 )
SetTextDepth ( Text [ 1 ], 0 )
SetTextDepth ( Text [ 2 ], 0 )
SetSpriteDepth ( mainmenu, 1 )
SetSpriteDepth ( endmenu, 1 )
SetSpriteDepth ( Player, 2 )
SetSpriteDepth ( Orbs [ 0 ].spr, 3 )
SetSpriteDepth ( Orbs [ 1 ].spr, 3 )
SetSpriteDepth ( Orbs [ 2 ].spr, 3 )
SetSpriteDepth ( Sides[0].spr, 4 )
SetSpriteDepth ( Sides[1].spr, 4 )
SetSpriteDepth ( Sides[2].spr, 4 )
SetSpriteDepth ( Sides[3].spr, 4 )
SetSpriteDepth ( Rocks [ 0 ] .spr, 5 )
SetSpriteDepth ( Rocks [ 1 ] .spr, 5 )
SetSpriteDepth ( Rocks [ 2 ] .spr, 5 )
SetSpriteDepth ( Rocks [ 3 ] .spr, 5 )
SetSpriteDepth ( Rocks [ 4 ] .spr, 5 )
SetSpriteDepth ( Rocks [ 5 ] .spr, 5 )
SetSpriteDepth ( waterfall, 6 )
endfunction
function ActivateGame ()
CreateScrolling ()
SetTextVisible ( Text[0], 1 )
SetTextPosition ( Text [0], 50, 25 )
SetTextSize ( Text [0], 24)
SetTextAlignment ( Text [0], 0 )
SetSpriteVisible ( mainmenu, 0 )
SetSpriteVisible ( Player, 1 )
endfunction
function CreateScrolling ()
initialScroll# = 75
defaultScroll# = 75
increment = 0
gameStart# = timer()
endfunction
function ResetScrolling ()
initialScroll# = 0
defaultScroll# = 0
increment = 0
gameStart# = timer()
setSpritePosition( Sides[ 0 ].spr, -480, -2119 )
setSpritePosition( Sides[ 1 ].spr, -480, -5262 )
setSpritePosition(Sides[ 2 ].spr, 638, -2119)
setSpritePosition(Sides[ 3 ].spr, 638, -5262)
for i = 0 to 5
setSpritePosition(Rocks [ i ].spr,Random (100, 568), Random2 (-3600, -200))
next i
for i = 0 to 2
SetSpritePosition (Orbs [ i ].spr, Random (110, 630), Random2 (-1358, 0) )
next i
endfunction
function IncreaseScrollingSpeed ()
// Increase scrolling speed
y# = defaultScroll# * getFrameTime ()
gameTime# = timer () - gameStart#
elapsedTime = gameTime# / 10
if elapsedTime > increment
increment = elapsedTime
defaultScroll# = initialScroll# + ( (initialScroll# / 2 ) * increment )
endif
SetSpritePosition ( Sides[ 0 ].spr, getSpritex (Sides[ 0 ].spr ), GetSpriteY ( Sides[ 0 ].spr ) + y# )
if getSpriteY ( Sides [ 0 ].spr ) > GetVirtualHeight ()
SetSpritePosition ( Sides[ 0 ].spr, getSpritex (Sides[ 0 ].spr ), GetSpriteY ( Sides[ 0 ].spr ) - 6286 )
endif
SetSpritePosition (Sides[1].spr, getSpritex(Sides[1].spr), GetSpriteY (Sides[1].spr) + y#)
if getSpriteY(Sides[1].spr) > GetVirtualHeight()
SetSpritePosition (Sides[1].spr, getSpritex(Sides[1].spr), GetSpriteY (Sides[1].spr) - 6286)
endif
SetSpritePosition (Sides[2].spr, getSpritex(Sides[2].spr), GetSpriteY (Sides[2].spr) + y#)
if getSpriteY(Sides[2].spr) > GetVirtualHeight()
SetSpritePosition (Sides[2].spr, getSpritex(Sides[2].spr), GetSpriteY (Sides[2].spr) - 6286)
endif
SetSpritePosition (Sides[3].spr, getSpritex(Sides[3].spr), GetSpriteY (Sides[3].spr) + y#)
if getSpriteY(Sides[3].spr) > GetVirtualHeight()
SetSpritePosition (Sides[3].spr, getSpritex(Sides[3].spr), GetSpriteY (Sides[3].spr) - 6286)
endif
for i = 0 to 5
SetSpritePosition (Rocks [ i ].spr, getSpritex(Rocks [ i ].spr), GetSpriteY (Rocks [ i ].spr) + y#)
if getSpriteY(Rocks [ i ].spr) > GetVirtualHeight()
SetSpritePosition (Rocks [ i ].spr, Random (100, 568), Random2 (-3600, -200))
endif
next i
for i = 0 to 2
SetSpritePhysicsVelocity (Orbs [ i ].spr, 0, (Random (50,500)) + y#)
if getSpriteY(Orbs [ i ].spr) > GetVirtualHeight ()
SetSpritePosition (Orbs [ i ].spr, Random (110, 630), Random2 (-1358, 0))
endif
next i
speed = 15
SetSpriteSpeed (Player, speed + y#)
endfunction
function CheckPlayerAgainstRocks ()
// Collision with rocks
for i = 0 to 5
if GetSpriteCollision ( Player, Rocks [ i ].spr ) = 1
KillPlayer ( )
PlaySound ( Random ( 6, 10 ) )
exit
endif
next i
endfunction
function CheckPlayerWithSides ( )
for i = 0 to 3
if GetSpriteCollision ( Player, Sides [ i ].spr ) = 1
KillPlayer ( )
PlaySound ( Random ( 6, 10 ) )
exit
endif
next i
if GetSpriteY (Player) < 0
PlaySound ( Random ( 6, 10 ) )
KillPlayer ()
endif
if GetSpriteY (Player) > 949
PlaySound ( Random ( 6, 10 ) )
KillPlayer ()
endif
endfunction
function CheckPlayerWithOrbs ( )
for i = 0 to 2
if ( GetSpriteCollision ( Player, Orbs [ i ].spr ) = 1 )
Score = Score + 1
SetTextString ( Text[ 0 ], "SCORE" + chr( 10 ) + str( Score) )
SetSpritePosition (Orbs [ i ].spr, Random (110, 630), Random2 (-1358, 0))
PlaySound ( Random ( 1, 5 ) )
exit
endif
next i
endfunction
function CheckRocksAgainstEachOther ()
// collision between rocks
if GetSpriteCollision (Rocks [ 0 ].spr, Rocks[1].spr) = 1
SetSpritePosition (Rocks[0].spr, Random (110, 630), (GetSpriteY (Rocks[0].spr)) - 500)
endif
if GetSpriteCollision (Rocks[0].spr, Rocks[2].spr) = 1
SetSpritePosition (Rocks[0].spr, Random (110, 630), (GetSpriteY (Rocks[0].spr)) - 500)
endif
if GetSpriteCollision (Rocks[0].spr, Rocks[3].spr) = 1
SetSpritePosition (Rocks[0].spr, Random (110, 630), (GetSpriteY (Rocks[0].spr)) - 500)
endif
if GetSpriteCollision (Rocks[0].spr, Rocks[4].spr) = 1
SetSpritePosition (Rocks[0].spr, Random (110, 630), (GetSpriteY (Rocks[0].spr)) - 500)
endif
if GetSpriteCollision (Rocks[0].spr, Rocks[5].spr) = 1
SetSpritePosition (Rocks[0].spr, Random (110, 630), (GetSpriteY (Rocks[0].spr)) - 500)
endif
if GetSpriteCollision (Rocks[1].spr, Rocks[2].spr) = 1
SetSpritePosition (Rocks[1].spr, Random (110, 630), (GetSpriteY (Rocks[1].spr)) - 500)
endif
if GetSpriteCollision (Rocks[1].spr, Rocks[3].spr) = 1
SetSpritePosition (Rocks[1].spr, Random (110, 630), (GetSpriteY (Rocks[1].spr)) - 500)
endif
if GetSpriteCollision (Rocks[1].spr, Rocks[4].spr) = 1
SetSpritePosition (Rocks[1].spr, Random (110, 630), (GetSpriteY (Rocks[1].spr)) - 500)
endif
if GetSpriteCollision (Rocks[1].spr, Rocks[5].spr) = 1
SetSpritePosition (Rocks[1].spr, Random (110, 630), (GetSpriteY (Rocks[1].spr)) - 500)
endif
if GetSpriteCollision (Rocks[2].spr, Rocks[3].spr) = 1
SetSpritePosition (Rocks[2].spr, Random (110, 630), (GetSpriteY (Rocks[2].spr)) - 500)
endif
if GetSpriteCollision (Rocks[2].spr, Rocks[4].spr) = 1
SetSpritePosition (Rocks[2].spr, Random (110, 630), (GetSpriteY (Rocks[2].spr)) - 500)
endif
if GetSpriteCollision (Rocks[2].spr, Rocks[5].spr) = 1
SetSpritePosition (Rocks[2].spr, Random (110, 630), (GetSpriteY (Rocks[2].spr)) - 500)
endif
if GetSpriteCollision (Rocks[3].spr, Rocks[4].spr) = 1
SetSpritePosition (Rocks[3].spr, Random (110, 630), (GetSpriteY (Rocks[3].spr)) - 500)
endif
if GetSpriteCollision (Rocks[3].spr, Rocks[5].spr) = 1
SetSpritePosition (Rocks[3].spr, Random (110, 630), (GetSpriteY (Rocks[3].spr)) - 500)
endif
if GetSpriteCollision (Rocks[4].spr, Rocks[5].spr) = 1
SetSpritePosition (Rocks[4].spr, Random (110, 630), (GetSpriteY (Rocks[4].spr)) - 500)
endif
endfunction
function Movement ()
// move player using joystick
joystickX# = GetVirtualJoystickX ( 1 )
joystickY# = GetVirtualJoystickY ( 1 )
SetSpritePosition ( Player, GetSpriteX ( Player ) + ( joystickX# * 16), GetSpriteY ( Player ) + ( joystickY# * 16 ) )
// move player left, right, up, and down using keyboard
if GetRawKeyState (68) = 1
SetSpritePosition (Player, GetSpriteX (Player) + 25, GetSpriteY (Player))
endif
if GetRawKeyState (65) = 1
SetSpritePosition (Player, GetSpriteX (Player) - 25, GetSpriteY (Player))
endif
if GetRawKeyState (87) = 1
SetSpritePosition (Player, GetSpriteX (Player), GetSpriteY (Player) - 25 )
endif
if GetRawKeyState (83) = 1
SetSpritePosition (Player, GetSpriteX (Player), GetSpriteY (Player) + 25 )
endif
endfunction
function KillPlayer ( )
//snarky = Random (5, 15)
//PlaySound (snarky)
// create plpfall
plpfall = CreateSprite ( LoadImage ( "plpfall.png" ) )
SetSpriteCollideBit ( plpfall, 2, 0 )
SetSpritePhysicsOn ( plpfall,2)
SetSpriteDepth ( plpfall,0 )
SetSpriteVisible ( plpfall, 1 )
SetSpritePosition ( plpfall, GetSpriteX ( Player ), GetSpriteY ( Player ) )
SetSpritePhysicsCanRotate ( plpfall, 1 )
SetSpritePhysicsVelocity ( plpfall, GetSpritePhysicsVelocityX ( plpfall ), -200 )
SetSpritePhysicsVelocity ( plpfall, GetSpritePhysicsVelocityY ( plpfall ), -50 )
SetSpritePhysicsAngularVelocity ( plpfall, -20 )
DeleteSprite ( Player )
if Score > Highscore
Highscore = Score
File = OpenToWrite ( "highscore.dat", 0 )
WriteInteger ( File, Highscore )
CloseFile ( File )
endif
GameOver ()
endfunction
function GameOver ()
SetSpriteVisible (endmenu, 1)
SetTextPosition ( Text [0], 384, 572 )
SetTextSize ( Text [0], 24)
SetTextAlignment ( Text [0], 1 )
SetTextVisible ( Text [ 1 ], 1 )
Repeat
SetTextVisible ( Text[ 2 ], 1 )
SetTextString ( Text[ 2 ], "" )
Sync ()
Until GetPointerPressed () = 1
DeleteSprite ( plpfall )
ResetScrolling ()
GameMode = 0
ActivateMenu ()
endfunction
function ReadHighscoreFile ()
if GetFileExists ( "highscore.dat" ) = 1
File = OpenToRead ( "highscore.dat" )
Highscore = ReadInteger ( File )
CloseFile ( File )
SetTextString ( Text[ 1 ], "HIGHSCORE" + chr( 10 ) + str ( Highscore ) )
endif
endfunction
[b][u]