I’m attempting to make a little game where you have a character, which is a sprite, and by using the accelerometer you tilt the iPad to collect little stars that appear, which will also be sprites. Once you’ve touched the star it disappears.
There are 5 different coloured stars that I want to appear in random places, when you collect them you get points and more start appearing.
I think I need to use a table to hold the different stars and have them appear on the screen, but I can’t find any really easy to understand tutorials or examples on how to use tables in this way.
Could any of you link me to a really good tutorial, or quickly write up an example table that does this?
Please say if you need any more information, I’d post all my current code but I don’t know if it would really be relevant.
displayMode(FULLSCREEN)
function setup()
tab={}
for z=1,5 do
table.insert(tab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))
end
sp={"Planet Cute:Character Boy",
"Planet Cute:Character Cat Girl",
"Planet Cute:Character Horn Girl",
"Planet Cute:Character Pink Girl",
"Planet Cute:Character Princess Girl"}
end
function draw()
background(40,40,50)
for a,b in pairs(tab) do
sprite(sp[a],b.x,b.y)
end
end
@joshlong93 The z,a,b are just variables that I use. z is a variable in the “for” loop that goes from 1 to 5. The a and b are variables in another “for” loop. The “a” is a counter that goes from 1 to the number of entries in the table “tab”. “b” is the values in the table. Since I created the entries with a vec2, that means there are 2 values per entry. The way to access them is with b.x and b.y . You could put the different sprites in a table and use the “a” variable to access that table. I’ll give you an example soon.
that’s been really helpful thank you @dave1707! If I could just ask one more question, how would I randomly make more than one of a particular star show up?
@joshlong93 I’m not sure if this is what you’re after. I show 4 sprites and the 5th one will be a duplicate of one of the 4.
displayMode(FULLSCREEN)
function setup()
tab={}
for z=1,5 do
table.insert(tab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))
end
sp={"Planet Cute:Character Boy",
"Planet Cute:Character Cat Girl",
"Planet Cute:Character Horn Girl",
"Planet Cute:Character Pink Girl"}
r=math.random(4)
end
function draw()
background(40,40,50)
for a,b in pairs(tab) do
if a==5 then
sprite(sp[r],b.x,b.y)
else
sprite(sp[a],b.x,b.y)
end
end
end
This code allows for didpfferent types of stars with different colors, values and sizes etc.
-- Game
displayMode(OVERLAY)
supportedOrientations(CurrentOrientation)
function setup()
player={x=WIDTH/2,y=HEIGHT/2,radius=50,speed=10}
stars={}
starTypes={{value=1,col=color(255),radius=20},{value=2,col=color(255,100,0),radius=30}}
spawnStar()
score=0
end
function draw()
background(0)
player.x = player.x + Gravity.x*player.speed--move the player in both axis
player.y = player.y + Gravity.y*player.speed
for i=#stars,1,-1 do
local playerPos=vec2(player.x,player.y)
local starPos=vec2(stars[i].x,stars[i].y)
local distance=playerPos:dist(starPos)--distance between star and player
if distance<player.radius+starTypes[stars[i].Type].radius then--if the distance between player and star is less than the sum if their radii then they have collided
score = score + starTypes[stars[i].Type].value--update cpscore based on type of star
print("you've got "..score.." points\
Well done")
table.remove(stars,i)--delete this star
spawnStar(1)--spawn 1 new star
--make a sound ir something here:
end
end
noTint()
spriteMode(RADIUS)
sprite("Planet Cute:Character Boy",player.x,player.y,player.radius)--draw the player
for k,s in ipairs(stars) do
tint(starTypes[s.Type].col)
sprite("Planet Cute:Star",s.x,s.y,starTypes[s.Type].radius)--draw the star
end
end
function spawnStar(n)
local num
if n then num=n else num=1 end
for i=1,num do
local star={x=math.random(50,WIDTH-50),y=math.random(50,HEIGHT-50),num=#stars+1,
Type=math.random(1,2)}
table.insert(stars,star)
end
end