Just want to apologize in advance to anyone taking the time to look at this snippet. I'm still learning and havent work out arrays and functions well enough to do this any easier yet.
I have been working on a game for my children to help with coin counting. Everything seems to work fine but sometimes the right answer triggers a wrong answer.
// Project: Keep The Change
// Created: 2017-07-14
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Keep The Change" )
SetWindowSize( 768, 1024, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 768, 1024 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 0, 0 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
penny = loadimage("penny.png") //load penny image
quarter = loadimage("quarter.png") //load quarter image
nickel = LoadImage("nickel.png") //load nickel image
dime = LoadImage("dime.png") //load dime image
buttons = loadimage("keys.png") //load atlas image(aka spritesheet)
//don't forget text file called "keys subimages.txt" placed in the media folder with "keys.png"
global keys as integer [64] //array to load subimages from keys.png
global time as integer //time is the counter to move from setup to play
global setup as integer //counter to check if initial sprites have been created already
time = 1 //set time to 1 to initialize interface building
global gameclock as float //time remaining before gameover
global coins as integer[6] //the 6 sprites that will be our random coins
type coin
value as integer
endtype
global cointotal as float
global onesinput as float
global tensinput as float
global hundinput as float
global inputtotal as float
global input as integer
global grandtotal as float
global clocktime as integer
LoadSound(1,"wrong.wav")
LoadSound(2,"right.wav")
for i = 1 to 64
keys[i] = LoadSubImage(buttons, str(i)+".png")
next i
cursor = CreateDummySprite()
do
SetSpritePosition(cursor, getpointerx(), getpointery())
if time = 1
AddVirtualButton(1, 768/2, 1024/2, 200)
CreateText(1, "Start!")
settextsize(1, 80)
SetTextPosition(1, (768/2)-75, (1024/2) + 100)
CreateText(3, "0")
SetTextSize(3, 80)
SetTextPosition(3, 450,10)
SetTextColor(3, 0, 255, 0, 255)
time = 2
endif
if time = 2
if getvirtualbuttonpressed(1)
DeleteText(1)
DeleteVirtualButton(1)
gameclock = 30
setup = 1
time = 3
roll = 1
grandtotal = 0
endif
endif
if time = 3
if setup = 1 //create answer box
hund = createsprite(keys[36]) //sprite for the dollar place value
setspritesize(hund, 128, -1) //set sprite larger
SetSpritePositionByOffset(hund, (768/2)-192, 500) //used byoffest and screen resolution to center these sprites
deci = createsprite(keys[49]) //sprite for the decimal point
setspritesize(deci, 128, -1)
SetSpritePositionByOffset(deci, (768/2)-64, 500)
tens = createsprite(keys[36]) //sprite for tenths place input
setspritesize(tens, 128, -1)
SetSpritePositionByOffset(tens, (768/2)+ 64, 500)
ones = CreateSprite(keys[38]) //sprite for hundredths place input
setspritesize(ones, 128, -1)
SetSpritePositionByOffset(ones, (768/2)+ 192, 500)
//making our keypad
onekey = createsprite(keys[27])
setspritesize(onekey, 128, -1)
setspriteposition(onekey, 128, 640)
twokey = createsprite(keys[28])
setspritesize(twokey, 128, -1)
setspriteposition(twokey, 256, 640)
threekey = CreateSprite(keys[29])
setspritesize(threekey, 128, -1)
SetSpritePosition(threekey, 384, 640)
fourkey = createsprite(keys[30])
setspritesize(fourkey, 128, -1)
setspriteposition(fourkey, 128, 768)
fivekey = createsprite(keys[31])
setspritesize(fivekey, 128, -1)
setspriteposition(fivekey, 256, 768)
sixkey = CreateSprite(keys[32])
setspritesize(sixkey, 128, -1)
SetSpritePosition(sixkey, 384, 768)
sevenkey = createsprite(keys[33])
setspritesize(sevenkey, 128, -1)
setspriteposition(sevenkey, 128, 896)
eightkey = createsprite(keys[34])
setspritesize(eightkey, 128, -1)
setspriteposition(eightkey, 256, 896)
ninekey = CreateSprite(keys[35])
setspritesize(ninekey, 128, -1)
SetSpritePosition(ninekey, 384, 896)
delkey = createsprite(keys[43])
setspritesize(delkey, 128, -1)
setspriteposition(delkey, 512, 640)
zerokey = createsprite(keys[36])
setspritesize(zerokey, 128, -1)
setspriteposition(zerokey, 512, 768)
enterkey = CreateSprite(keys[56])
setspritesize(enterkey, 128, -1)
SetSpritePosition(enterkey, 512, 896)
setup = 2
endif
if roll = 1 //counter for random generated coins
cointotal = 0.00 //resets data fields after each round
inputtotal = 0
SetSpriteImage(ones, keys[37]) //resets our data entry sprites
setspriteimage(tens, keys[38])
setspriteimage(hund, keys[36])
onesinput = 0
tensinput = 0
hundinput = 0
for i = 1 to 6 //deletes sprites before placing new ones
if GetSpriteExists(coins[i]) then deletesprite(coins[i])
next i
for i = 1 to 6 //fills 6 coins with sprites and coin value
rdmcoin = random(0,3) // I wanted to create a coin type with a coin.value but was having issues with coin[1].value not working.
if rdmcoin = 0 //0 is penny
cointotal = cointotal + 0.01 //we add the value of a penny to cointotal
newsprite = createsprite(penny) //we give our coin its image
coins[i] = newsprite //add our new coin to the coins array
endif
if rdmcoin = 1 //If random(0,3) is 1 our coin is nickel
cointotal = cointotal + 0.05
newsprite = createsprite(nickel)
coins[i] = newsprite
endif
if rdmcoin = 2 //if random(0,3) is 2 our coin is dime
cointotal = cointotal + 0.10
coins[i] = createsprite(dime)
endif
if rdmcoin = 3 //if random(0,3) is 3 our coin is quarter
cointotal = cointotal + 0.25
coins[i] = createsprite(quarter)
endif
SetSpritePositionByOffset(coins[i], 34 + (i * 100), 300) //set each coin in its place from left to right
next i
input = 1 //sets input to 1. input is used for data entry
roll = 2 //prevents coins from rerolling by changing roll counter, and initiates data entry
endif
if cointotal > .99 then roll = 1 //this function rerolls our coins if the total value is a dollar or more.
//eventually we can add coins and remove this for harder levels.
if roll = 2 //once roll(roll = 1) is complete we can begin entering data(roll = 2)
if input = 1 //first we want to input tenths place. once we enter a number input comes 2 so we can enter data in hundredths place
if click = 0 //click counter is used to prevent clicking multiple buttons on accident
if GetSpriteCollision(cursor, onekey) and getpointerpressed() //if mouse is over the one button and pointer is pressed
setspriteimage(tens, keys[27]) //we change the image in the tenths place to 1
input = 2 //moves us to next step of data entry (hundredths)
click = 1 //click = 1 will prevent hitting another button (other button requires click to be 0)
tensinput = .1 //we enter 0.1 into our input
endif
if GetSpriteCollision(cursor, twokey) and getpointerpressed()
setspriteimage(tens, keys[28])
input = 2
click = 1
tensinput = .2
endif
if GetSpriteCollision(cursor, threekey) and getpointerpressed()
setspriteimage(tens, keys[29])
input = 2
click = 1
tensinput = .3
endif
if GetSpriteCollision(cursor, fourkey) and getpointerpressed()
setspriteimage(tens, keys[30])
input = 2
click = 1
tensinput = .4
endif
if GetSpriteCollision(cursor, fivekey) and getpointerpressed()
setspriteimage(tens, keys[31])
input = 2
click = 1
tensinput = .5
endif
if GetSpriteCollision(cursor, sixkey) and getpointerpressed()
setspriteimage(tens, keys[32])
input = 2
click = 1
tensinput = .6
endif
if GetSpriteCollision(cursor, sevenkey) and getpointerpressed()
setspriteimage(tens, keys[33])
input = 2
click = 1
tensinput = .7
endif
if GetSpriteCollision(cursor, eightkey) and getpointerpressed()
setspriteimage(tens, keys[34])
input = 2
click = 1
tensinput = .8
endif
if GetSpriteCollision(cursor, ninekey) and getpointerpressed()
setspriteimage(tens, keys[35])
input = 2
click = 1
tensinput = .9
endif
if GetSpriteCollision(cursor, zerokey) and getpointerpressed()
setspriteimage(tens, keys[36])
input = 2
click = 1
tensinput = .0
endif
if GetSpriteCollision(cursor, delkey) and getpointerpressed()
setspriteimage(ones, keys[37])
setspriteimage(tens, keys[38])
input = 1
click = 1
tensinput = 0
endif
if GetSpriteCollision(cursor, enterkey) and getpointerpressed()
inputtotal = tensinput + onesinput
if inputtotal = cointotal
roll = 1
input = 1
click = 1
gameclock = gameclock + 10
PlaySound(2)
endif
if inputtotal <> cointotal
roll = 1
input = 1
click = 1
playsound(1)
gameclock = gameclock - 5
endif
endif
endif
endif
if input = 2 //basically repeating above statement but for hundreths instead of tenths
if click = 0 //pointer needs to be released before we can click again
if GetSpriteCollision(cursor, onekey) and getpointerpressed()
setspriteimage(ones, keys[27])
input = 3
click = 1
onesinput = .01
endif
if GetSpriteCollision(cursor, twokey) and getpointerpressed()
setspriteimage(ones, keys[28])
input = 3
click = 1
onesinput = .02
endif
if GetSpriteCollision(cursor, threekey) and getpointerpressed()
setspriteimage(ones, keys[29])
input = 3
click = 1
onesinput = .03
endif
if GetSpriteCollision(cursor, fourkey) and getpointerpressed()
setspriteimage(ones, keys[30])
input = 3
click = 1
onesinput = .04
endif
if GetSpriteCollision(cursor, fivekey) and getpointerpressed()
setspriteimage(ones, keys[31])
input = 3
click = 1
onesinput = .05
endif
if GetSpriteCollision(cursor, sixkey) and getpointerpressed()
setspriteimage(ones, keys[32])
input = 3
click = 1
onesinput = .06
endif
if GetSpriteCollision(cursor, sevenkey) and getpointerpressed()
setspriteimage(ones, keys[33])
input = 3
click = 1
onesinput = .07
endif
if GetSpriteCollision(cursor, eightkey) and getpointerpressed()
setspriteimage(ones, keys[34])
input = 3
onesinput = .08
click = 1
endif
if GetSpriteCollision(cursor, ninekey) and getpointerpressed()
setspriteimage(ones, keys[35])
input = 3
onesinput = .09
click = 1
endif
if GetSpriteCollision(cursor, zerokey) and getpointerpressed()
setspriteimage(ones, keys[36])
input = 3
onesinput = .00
click = 1
endif
if GetSpriteCollision(cursor, delkey) and getpointerpressed() //deletekey
setspriteimage(ones, keys[37]) //makes ones image blank
setspriteimage(tens, keys[38]) //makes tenths image "_"
click = 1
tensinput = 0 //remove data from tenths place (in
input = 1 //return us to first step of data entry (tenths)
endif
if GetSpriteCollision(cursor, enterkey) and getpointerpressed() //It's redundant to have this here
inputtotal = tensinput + onesinput //if the player hits enter
if inputtotal = cointotal
gameclock = gameclock + 10
PlaySound(2)
endif
if inputtotal <> cointotal
playsound(1)
gameclock = gameclock - 5
endif
click = 1
input = 1
roll = 1
endif
endif
endif
if input = 3 //at input 3 we have entered an integer for tenths (input 1) and for hundredths(input w2) and now can delete or submit data
if click = 0
if GetSpriteCollision(cursor, delkey) and getpointerpressed() //if we hit delete it will remove the data for hundreths place and return to input 2
SetSpriteImage(ones, keys[38]) //return the hundredths data to "_"
input = 2 //returns to step 2 of data entry (input hundredths)
click = 1
onesinput = 0 //dont ask why I used both 'ones and tens' and 'hundredths and tenths' to describe the same numbers...
endif //maybe I just like making things harder for myself...
if GetSpriteCollision(cursor, enterkey) and getpointerpressed() //if we submit our answer (hit equals button)
inputtotal = tensinput + onesinput //calculate total input by combining what we have entered in both fields
if inputtotal = cointotal //check if total input = coin total
gameclock = gameclock + 10 //gives player 10 more play seconds
grandtotal = grandtotal + inputtotal //add the input total to our score. Hence "Keep The Change"
PlaySound(2) //sound 2 is correct answer sound
endif
if inputtotal <> cointotal //if our total input amount DOES NOT EQUAL(<>) coin total
playsound(1) //sound 1 is wrong answer sound
gameclock = gameclock - 5 //player loses 5 seconds of game time for a wrong answer
endif // ^discourages guessing^
roll = 1 //reroll our coins and reset fields
input = 1 //returns us to step one of data entry
click = 1
endif
endif
endif
if input = 5 //input will never be five, this is here for later programming
if click = 0
if GetSpriteCollision(cursor, onekey) and getpointerpressed()
setspriteimage(hund, keys[27])
input = 3
click = 1
hundinput = 1
endif
if GetSpriteCollision(cursor, twokey) and getpointerpressed()
setspriteimage(hund, keys[28])
input = 3
click = 1
hundinput = 2
endif
if GetSpriteCollision(cursor, threekey) and getpointerpressed()
setspriteimage(hund, keys[29])
input = 3
click = 1
hundinput = 3
endif
if GetSpriteCollision(cursor, fourkey) and getpointerpressed()
setspriteimage(hund, keys[30])
input = 3
click = 1
hundinput = 4
endif
if GetSpriteCollision(cursor, fivekey) and getpointerpressed()
setspriteimage(hund, keys[31])
input = 3
click = 1
hundinput = 5
endif
if GetSpriteCollision(cursor, sixkey) and getpointerpressed()
setspriteimage(hund, keys[32])
input = 3
click = 1
hundinput = 6
endif
if GetSpriteCollision(cursor, sevenkey) and getpointerpressed()
setspriteimage(hund, keys[33])
input = 3
click = 1
hundinput = 7
endif
if GetSpriteCollision(cursor, eightkey) and getpointerpressed()
setspriteimage(hund, keys[34])
input = 3
click = 1
hundinput = 8
endif
if GetSpriteCollision(cursor, ninekey) and getpointerpressed()
setspriteimage(hund, keys[35])
input = 3
click = 1
hundinput = 9
endif
if GetSpriteCollision(cursor, zerokey) and getpointerpressed()
setspriteimage(hund, keys[36])
input = 3
click = 1
hundinput = 0
endif
if GetSpriteCollision(cursor, delkey) and getpointerpressed()
setspriteimage(tens, keys[38])
SetSpriteImage(hund, keys[36])
input = 2
click = 1
tensinput = 0
endif
if GetSpriteCollision(cursor, enterkey) and getpointerpressed()
inputtotal = hundinput + tensinput + onesinput
if inputtotal = cointotal
roll = 1
input = 1
click = 0
gameclock = gameclock + 10
grandtotal = grandtotal + inputtotal
PlaySound(2)
endif
if inputtotal <> cointotal
roll = 1
playsound(1)
gameclock = gameclock - 5
endif
endif
endif
endif
endif
if GetPointerReleased() then click = 0 //releasing mouse or removing finger from device witll reset click to hit next key
// inputtotal = onesinput + tensinput
// print(str(inputtotal, 2)) //shows your input
// print(str(input))
print(str(cointotal, 2)) //cheater command (for testing)
if gameclock > 0 then gameclock = gameclock - .02 //this command makes gameclock count down
if gameclock < 0
gameclock = 0
time = 2
AddVirtualButton(1, 768/2, 1024/2, 200)
CreateText(1, "Start!")
settextsize(1, 80)
SetTextPosition(1, (768/2)-75, (1024/2) + 100)
endif
// trunc(grandtotal)
SetTextString(3, str(grandtotal, 2))
print(str(gameclock, 2))
// clocktime = gameclock //gameclock is float for more precise calculating. we can convert to integer to clean up
// Print(clocktime) //this was my initial setup before i discovered how to format the float with str()
setprintcolor(255,0,0)
SetPrintSize(100)
endif //ends time = 3
// Print( ScreenFPS() )
Sync()
loop
Again, its plum full or redundancies and quite volumnus. I am totally open to criticism, and hope soon to be able to recreate this program with 100 lines or less =)
I just can't figure out why I am getting wrong answers when the input value is correct.