It looks like you're new here. If you want to get involved, click one of these buttons!
I’m trying to set this code up so it displays 1 random image from my table, which holds 8 images ,on screen 1 at a time.
I’m halfway there. I got the images to display randomly but they are stacked on each other. Also I’m trying to set it up so if one image from the table gets displayed it won’t show up twice.
displayMode(FULLSCREEN)
function setup()
et=ElapsedTime
count=30
tab={ asset.builtin.Planet_Cute.Character_Princess_Girl,
asset.builtin.Planet_Cute.Character_Boy,
asset.builtin.Planet_Cute.Character_Horn_Girl,
asset.builtin.Planet_Cute.Character_Pink_Girl,
asset.builtin.Space_Art.UFO,
asset.builtin.Planet_Cute.Enemy_Bug,
asset.builtin.Planet_Cute.Key,
asset.builtin.Planet_Cute.Gem_Green, }
bTab={}
for x=-20,20 do
for y=-20,20 do
table.insert(bTab,{x=x,y=y,v=math.random(13)})
end
end
dx,dy=WIDTH/2,HEIGHT/2
end
function draw()
background(0)
if ElapsedTime-et>1 then
count = count - 1
et=ElapsedTime
end
text(count,WIDTH/2,HEIGHT/2)
if count<=29 then
for a,b in pairs(bTab) do
if b.v<7 then
sprite(tab[b.v],dx+200,dy)
else
if count<=28 then
sprite(tab[b.v-5],dx,dy+200)
end
if count<=27 then
sprite(tab[b.v],dx,dy-200)
end
end
end
end
end
Comments
@Jarc You have to be a little more specific with what you want. This shows a random sprite from the table with no duplicates. When all eight are shown, the word done is displayed. Not sure what this will accomplish.
@dave1707 I want to display them all on the screen, in different positions. Basically I’m going for a shuffling cards effect then the screen deals them 1 by 1. What’s in the table are the cards, math.random is the shuffle. Going to use Elapsed time to deal. In a card deck you don’t get the same card dealt twice, so that’s why I don’t want duplicates.
Here’s a card shuffle program. It just prints the cards. You can add the card images if you want. Try doing a forum search for things. Probably everything you’re looking for has been done and there’s probably an example I’ve written for it.
@dave1707 also want them to stay on screen after appearing.
@dave1707 My bad I didn’t see that you responded. I’ll check the code out, Thanks.
Here’s another version.
@dave1707 I shouldn’t have brought up playing cards, bad example on my part. I feel off subject. I want the code to display exactly like this but I want the sprites to be random & no duplicates.
Been trying to incorporate the shuffling program for hours but I feel like it’s tedious.
This displays random sprites in a circle. You can have as many sprites as you want. You will have to scale them if you have a lot so the don’t overlap.
@dave1707 Perfect! Thanks!
@dave1707 how would I go about giving each sprite a specific x,y coordinate to display at?
I think it’s here but not sure how to set up
In the first table, change all the x=200,y=200 to what you want them to be.
@dave1707 It’s not working. Right now the same sprites end up at the same locations every time. I want those specific locations and I want which sprite that shows in those specific locations to be random. To clarify I want a random sprite to appear in a random order in those specific locations every time I run the code.
Example pic below, I get this every time I run code. Pretty much I want everything to be random but the locations.
Sorry, I was in a hurry this morning and didn’t check what I was doing.
@dave1707 Works great! Thanks I appreciate it.
@dave1707 I’m trying to combine the code above with this code, that you previously helped me with.
The code above randomizes sprites then puts in specific locations.
The code below makes the sprites moveable and scale from small circle to big circle.
Posting my progress below
@dave1707 Here is my progress but I think my for, do for the sprite in draw is written wrong. I’ve been trying to make this work for hours.
@Jarc Here’s the other code with random added.
@dave1707 I already had the random added in my code progress. The problem I’m having is making the sprites appear 1 at a time randomly in those specific locations then making the sprites moveable and scale from small circle to big circle.
@dave1707 I have them all appearing after 1sec + random + moveable and scale to circles. All I need to do now is make them appear 1 by 1 every second. Is there a easy way?
@dave1707 I figured it out but I feel like there is a simpler method.
@dave1707 A delay loop function, nice! Thanks for the knowledge.
@Jarc There’s a problem in that delay function. Once all the sprites are shown, loop will add 1 to itself as long as the program is running. Even though that’s not a problem, here’s a better one. With the new one, once all the sprites are shown, the if statement is false and doesn’t execute any of the code inside it anymore.
@dave1707 I have some questions.
I want to add more sprites(3 for example) but keep the same number of small circle slots so once after scoring the first 3(round 1), it will repeat same beginning process but load the rest of the unused sprites so round 2 can began.
Also this is for visual effect but how do I get the sprites to score 1 by 1, left to right each round. Meaning how do I get the value scores to show up as text near the sprite for about 1 sec and simultaneously while the value text are being displayed on each sprite(1 by 1, left to right) I want it to update a onscreen scoreboard text that starts at 00 when game begins.
The two bottom ellipses I added are for round 2.
@Jarc You can create a table that has all your sprites in it. On the first round, you pull 3 random sprites out. On the second round, pull out the rest of them in random order. As for displaying the scores, you just need to have a variable that you set. As the count goes down, you display the value. If it’s between a certain range you display it in one spot, the next range in another spot and the third spot for the remaining range.
@dave1707 How do I tell the code to stop after 3.
@Jarc How many programs are you working on.
Since you’re adding more sprites, you need to change the way you randomly pick the sprites. The above routine isn’t going to work anymore. You still only pick 3 sprites from the full table. When a sprite is selected, remove it from the table. If you’re always picking 3 sprites at a time, the table should contain multiples of 3. Each time you pick a sprite, assign a small circle to it. I believe I mentioned somewhere that it’s easier to write code when you know in advance what you want to do, so you’re not always rewriting the code because you change something you already coded for.
@Jarc Heres an example of picking a random name from a table of 9 and assigning it 1 of 3 random small circles. This shows 3 rounds.
@dave1707 Hey, I added a coroutine to this. Basically every round after 1 has a time limit or touch event that starts the next. For some reason I can’t get round 3’s time limit to work no matter what I try. Can u take a look?
@Jarc I don’t know why you’re using a coroutine for a timer. You can just use a simple timer. As far as as I know, a coroutine is mostly used when you have a routine that takes a long time to complete. You yield that routine frequently to allow the other code to run while the coroutine takes however long it needs to run.
@dave1707 I feel like a coroutine would give me more control over each round especially when I later add more code to it. Not sure how a simple timer could give me as much control and be less code.
@Jarc I don’t know what you want to do with the code you show, but here’s some of you code with a simple timer.
@dave1707 basically that but can you add if the timer goes out the next round starts.
Here’s my version with coroutines but I’ll use yours. It does the same as yours but if the timer goes out the next round starts. (Btw the os clock in this doesn’t reset unless you close Codea
)
@Jarc Heres the updated code.
@dave1707 Thanks! This is a little of a backtrack but I was working on the visual for the game. Combining the two codes you helped me with. The problem I was having is the rest of the sprites loading in the far left corner. I fixed it by doing this
but not sure if that was the best way to fix it
Here’s the full code without the fix.
@Jarc Sorry, but I don’t know what you’re trying to do. I don’t see any difference between adding your fix or leaving it out.
(Code updated)
@dave1707 Sorry, here’s a better example with timer included.
How do I
1) make 3 sprites load every round from table, no duplicates. (Round 1 works)
2)Get the delay function to work every round (I had to delete to get this code to work)
3)Stop timer and next button from producing rounds after round 3
4)Keep the value score and update it every round (each round it adds)
5) make the sprites in the big circles stay every round.
@Jarc Your code seems to change each time you post it. You’re last code had 3 large circles. This code has six large circles. A lot of what you want fixed was working at some point in previous code. You seem to be going in circles as you change things. I don’t know if you’re trying to add more things on the fly or if this was all planned up front.
@dave1707 It was a lot to ask of you, sorry. I figured most of it out but I have one question. I have the code set to when I load 4 sprites into the first row slots and press ready the score appears. My question is how can I get just the sprites in that 1st row to move into another table(when I press ready). I know about table.move but I’m not sure how to only pick those sprites.
@Jarc Are you talking about the sprite information or do you want to move the sprites to a different position on the screen.
@dave1707 the sprites and the sprites info into a different table but just the sprites in the 1st row.
@Jarc You can have something like this when you press the Ready button. Any sprite that’s in the top row ( has a y2 value of 290 ), move that table entry into another table. Just make sure you set something so you’ll only do it once or you’ll move the info again.
This code goes in the touched function for the ready button.
@dave1707 Thanks!
@dave1707 how do I get the score value from the 1st row to appear 1 at time, left to right instead of the total amount when the ready button is pressed.
Like if the values in the 1st row were 1,3,4,5
Then the score text would display 1 then 4 then 8 then 13 (because it’s adding each slot)
@Jarc Instead of adding all the values, move each value into a table. Instead of displaying the total, display the sum as you go through the table. Use a counter to delay the adding.
@dave1707 Thanks, I figured it out but how do I get it to score in order. From left to right.
@Jarc I don’t know what you want to go from left to right. All you’re doing is displaying the sum of the values one at a time.
@dave1707 I know but I need the values to display from left to right in order. If the Sprites are positioned chest, bug, heart, star
And the values of the sprites are chest=1, bug=7, heart=5, star=3
I need it to show 1 then 8 then 13 then 16 so it’s in order with the positions.
Right now it’s displaying the values one at a time in a random order.
@Jarc To get them in order, you need to check for the x2,y2 value when you put them in tabB. Check x2,y2 for 320,290 then 445,290 then 570,290 then 695,290. You might have to loop through it a couple of times.
@Jarc Maybe something like this.