so I can't even math today. I am tinkering with a little game for my kids to quickly identify quantities (an early math skill). I have used the following code to create my sprites with a random color and position.
for i = 1 to answer[1]
sprite[i] = createsprite(0)
setspritesize(sprite[i], random(50,100), random(50,100))
SetSpriteColor(sprite[i], random(10,255), random(10,255), random(10,255), 255)
setspriteposition(sprite[i], random(24, 1000), random(24, 500))
next i
The problem is that sometimes the sprites cover eachother. I tried modifying it to test for collisions and rearrange, but its still not 100 percent.
function generateimage()
if imgsteps = 1
for i = 1 to answer[1]
sprite[i] = createsprite(0)
setspritesize(sprite[i], random(50,100), random(50,100))
SetSpriteColor(sprite[i], random(10,255), random(10,255), random(10,255), 255)
setspriteposition(sprite[i], random(24, 1000), random(24, 500))
for j = 1 to i
if getspritecollision(sprite[j], sprite[i]) = 1 then setspriteposition(sprite[i], random(24, 1000), random(24, 500))
if getspritecollision(sprite[j], sprite[i]) = 1 then setspriteposition(sprite[i], random(24, 1000), random(24, 500))
next j
next i
answersteps = 1
imgsteps = 2
endif
endfunction
I know that if I had to, I could type out a bunch of nested if's to do the job, but im sure there is a much more efficient way. I would like to use a "for i =" statement with the "if spritecollision=0" condition required before next i can happen. Thanks for the help in advance!
if anyone is interested in the game, its built with no supporting files in the media folder, just copy, paste and Run!
// Project: numberID
// Created: 2018-01-09
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "numberID" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // 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
global answer as integer[3]
global sprite as integer [9]
global imgsteps as integer
global steps as integer
global answersteps as integer
global randspot as integer [3]
global timeclock as integer
global score as integer
steps = 1
answersteps = 0
highscore = 0
do
if score > highscore then highscore = score
generatemath()
generateimage()
generatebuttons()
print("Score: " + str(score))
print("Highscore: " + str(highscore))
Sync()
loop
function generatemath ()
if steps = 1
answer[1] = random(1,9)
steps = 2
endif
if steps = 2
answer[2] = random(1,9)
steps = 3
endif
if steps = 3
if answer[2] = answer[1]
steps = 2
elseif answer[2] <> answer[1]
steps = 4
endif
endif
if steps = 4
answer[3] = random(1,9)
steps = 5
endif
if steps = 5
if answer[3] = answer[2]
steps = 4
elseif answer[3] = answer[1]
steps = 4
endif
if answer[3] <> answer[2] and answer[3] <> answer[1]
imgsteps = 1
steps = 6
endif
endif
if steps = 6
//print(answer[1])
//print(answer[2])
//print(answer[3])
//print("steps " + str(steps))
//print("imgsteps " + str(imgsteps))
//print("answersteps " + str(answersteps))
endif
endfunction
function generateimage()
if imgsteps = 1
for i = 1 to answer[1]
sprite[i] = createsprite(0)
setspritesize(sprite[i], random(50,100), random(50,100))
SetSpriteColor(sprite[i], random(10,255), random(10,255), random(10,255), 255)
setspriteposition(sprite[i], random(24, 1000), random(24, 500))
for j = 1 to i
if getspritecollision(sprite[j], sprite[i]) = 1 then setspriteposition(sprite[i], random(24, 1000), random(24, 500))
if getspritecollision(sprite[j], sprite[i]) = 1 then setspriteposition(sprite[i], random(24, 1000), random(24, 500))
next j
next i
answersteps = 1
imgsteps = 2
endif
endfunction
function generatebuttons ()
if answersteps = 1
randspot[1] = random(1,3)
answersteps = 2
endif
if answersteps = 2
randspot[2] = random(1,3)
answersteps = 3
endif
if answersteps = 3
if randspot[2] = randspot[1]
answersteps = 2
elseif randspot[2] <> randspot[1] then answersteps = 4
endif
if answersteps = 4
randspot[3] = random(1,3)
answersteps = 5
endif
if answersteps = 5
if randspot[3] = randspot[1]
answersteps = 4
elseif randspot[3] = randspot[2]
answersteps = 4
endif
if randspot[3] <> randspot[1] and randspot[3] <> randspot[2] then answersteps = 6
endif
if answersteps = 6
for i = 1 to 3
CreateText(i, str(answer[i]))
SetTextSize(i, 200)
SetTextPosition(i, randspot[i] * 256, 600)
settextcolor(i, random(30,255), random(30,255), random(30,255), 255)
next i
answersteps = 7
endif
if answersteps = 7
timeclock = timeclock + 1
if timeclock = 20
for i = 1 to 9
if GetSpriteExists(sprite[i]) = 1 then DeleteSprite(sprite[i])
next i
answersteps = 8
endif
endif
if answersteps = 8
if GetPointerPressed() = 1
if GetTextHitTest(1, GetPointerX(), getpointery()) = 1
score = score + 1
answersteps = 9
endif
if GetTextHitTest(2, getpointerx(), getpointery()) = 1
score = 0
answersteps = 9
endif
if GetTextHitTest(3, getpointerx(), getpointery()) = 1
score = 0
answersteps = 9
endif
endif
if answersteps = 9
clear()
endif
endif
endfunction
function clear()
steps = 1
answersteps = 0
imgsteps = 0
timeclock = 0
for i = 1 to 9
if getspriteexists(sprite[i]) = 1 then DeleteSprite(sprite[i])
next i
for i = 1 to 3
DeleteText(i)
next i
endfunction
One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...