Hello,
I’m a beginner with using lua and codea. The code shown below is a game that I made(with random images so you can view it) and I am having trouble with the score. As you will see, as the star passes through the little “people” the score counts a point 60 times per second, not just once as I would like it to. The intention of the game is to create the different “people” to be worth different amounts (so some would add to the score and some take away from score), but I do not want the score to go crazy when doing so. Is there a way of doing this? My attempt to fix this was to make a new function so that it would not be in the draw function. Any suggestions would be much appreciated!
– Final Project
function setup()
B={}
displayMode=(FULLSCREEN)
fish=readImage("SpaceCute:Star")
fishx=200
fishy=350
fishspeed=0
fishw=100
fishh=60
jellyfish=readImage("Planet Cute:Character Cat Girl")
dolphin=readImage("Planet Cute:Character Horn Girl")
crab=readImage("Planet Cute:Character Princess Girl")
octopus=readImage("Planet Cute:Character Pink Girl")
gamestate="start"
fishFrequency=1
collision=1
timer=0
score=1
NewFish()
One()
Two()
Three()
Four()
end
function NewFish()
b={}--a table to hold information about pipes
b.w=150
b.h=150
b.x=750
b.r=75--"radius"
b.y=math.random(100,700)
b.s=5--speed pixels per second
b.g=100
b.rsc=math.random(1,4)--randomly decides which sea creature comes on screen
table.insert(B,b)--add to our table of balloons
end
function draw()
if gamestate=="start" then
background(0, 36, 255, 255)
fill(246, 245, 245, 255)
font("AmericanTypewriter")
fontSize(45)
text ("Tap to start Game!", WIDTH/2,HEIGHT/2)
fontSize(20)
text("Directions: GET the octopus and dolphin, AVOID the jellyfish and crab!", WIDTH/2,HEIGHT/2-100)
end
if gamestate=="play" then
background(0, 135, 255, 255)--background
sprite(fish,fishx,fishy,fishw)--draw the fish
fishy=fishy+fishspeed
fishspeed=fishspeed-.15
fill(166, 167, 120, 255)
rect(0,0,WIDTH,25)--bottom of ocean rectangle
pushStyle()
timer=timer+DeltaTime
if timer>1/fishFrequency then
NewFish()--run through this function
timer=0--reset timer
end
popStyle()
pushStyle()
for i,b in pairs(B) do--make everything run through the table
b.x=b.x-b.s
if b.x-b.w/2<=-b.w then
table.remove(B,i)--remove when off screen
end
if b.rsc==1 then--determines what sea creature comes on screen based on roll
sprite(jellyfish,b.x,b.y,b.w,b.h)
elseif b.rsc==2 then
sprite(dolphin,b.x,b.y,b.w,b.h)
elseif b.rsc==3 then
sprite(crab,b.x,b.y,b.w,b.h)
elseif b.rsc==4 then
sprite(octopus,b.x,b.y,b.w,b.h)
end
if fishx+(fishw/2)>=b.x-(b.w/2) and --collision detection
fishx-(fishw/2)<=b.x+(b.w/2) and
fishy-(fishh/2)<=b.y+(b.h/2) and
fishy+(fishh/2)>=b.y-(b.h/2) and
b.rsc==1 then
One()
end
if fishx+(fishw/2)>=b.x-(b.w/2) and --collision detection
fishx-(fishw/2)<=b.x+(b.w/2) and
fishy-(fishh/2)<=b.y+(b.h/2) and
fishy+(fishh/2)>=b.y-(b.h/2) and
b.rsc==2 then
Two()
end
if fishx+(fishw/2)>=b.x-(b.w/2) and --collision detection
fishx-(fishw/2)<=b.x+(b.w/2) and
fishy-(fishh/2)<=b.y+(b.h/2) and
fishy+(fishh/2)>=b.y-(b.h/2) and
b.rsc==3 then
Three()
end
if fishx+(fishw/2)>=b.x-(b.w/2) and --collision detection
fishx-(fishw/2)<=b.x+(b.w/2) and
fishy-(fishh/2)<=b.y+(b.h/2) and
fishy+(fishh/2)>=b.y-(b.h/2) and
b.rsc==4 then
Four()
end
if fishy-fishh<=0 or fishy+fishh>=HEIGHT then--game is over if the fish touches the sides of screen
gamestate="dead"
end
end
font("AmericanTypewriter")
fill(243, 242, 243, 255)
fontSize(50)
text(score, WIDTH/2,HEIGHT/2+275)
end
if gamestate=="dead" then
fishspeed=0
b.s=0
fishFrequency=0
timer=0
background(0, 119, 255, 255)
fill(235, 235, 236, 255)
font("AmericanTypewriter-CondensedBold")
fontSize(45)
text("You Died! You got "..score.." points.", WIDTH/2,HEIGHT/2)
text("Try Again", WIDTH/2,HEIGHT/2-100)
end
end
function One()
score=score-1
end
function Two()
score=score+3
end
function Three()
score=score-5
end
function Four()
score=score+2
end
function touched(touch)
if touch.state==BEGAN then
fishspeed=5--if the screen is touched then the fish moves up(instead of down)at a speed of 5
if gamestate=="start" and touch.state==BEGAN then
gamestate="play"
end
end
end