Challenges?

I’m new-ish to Codea and I’ve started about a million projects that start off with a certain goal (The one I’ve gone farthest in is having a virtual pet, where you name it and make sure it gets fed or it dies) but I always end up wondering what new additions to add to those projects and I work on the additions so much they become their own projects.

Basically an endless cycle of me never truly completeing anything. I really enjoyed learning with those little challenges that Simeon had written in a discussion ( this one: http://twolivesleft.com/Codea/Talk/discussion/comment/9791#Comment_9791) and I was wondering if you guys could kinda come up with a list of increasingly difficult little projects in order to go from noob to moderately well. Projects that teaches noobs new ways of doing things like using meshes instead of sprites, in order to use in future projects :slight_smile:

Just being hopeful.

I had this idea to issue a weekly programming challenge, with a sort of deadline for entry. Everyone who was interested could complete it and help each other through the week. Then on the final day everyone could post their code and you could see how others tackled the same problem you did, and maybe it would teach you how to think like an advanced programmer.

The reason I never have done so is that I’ve wondered if anyone would care or even want to. It’s not like a programming contest where I could really offer a prize, and who would judge the entries? Maybe I will try sometime anyway and see if anyone tries it.

Sounds great! You don’t need prizes, it sounds like it would be the perfect hands-on learning experience that some people need! I suggest you get a trial run working as soon as you can because I’m itiching to see this in action :slight_smile:

You know I’m kinda in a Codea coding mode so if you have a small challenge off the top of your head (keep in mind that I still sometimes struggle with basic stuff like multi-touch) that would be great for today XP

Alright, here are two challenge paths for people who are newer to Codea:

Dice Roller-
Challenge1: When the player touches the screen, make a random number between 1 and 6 and print it using the print function.
Challenge 2: Now make it draw to the screen when the player touches the die, and update the die to the show the new face number. You can either use shapes like ellipse and rectangle to draw them, or import custom dice drawings using Dropbox.
Challenge 3: Make that code into a dice CLASS, which will allow you to easily add as many dice as you want to your project (or any future project.). Make it so the dice only roll if you touch on the die.

Whack-A-Mole (er bug):
Challenge 1: Use sprite to draw a bug (you can use the enemy bug sprite) to the screen. When the screen is touched, make the bug disappear.
Challenge 2: Now make it so that the bug appears in a random location on screen. Make it so that if you touch the screen the bug will move to a new random location.
Challenge 3: Now make multiple bugs that move when you touch them (maybe 5 on screen a t once.) It may be a good idea to use a CLASS for bugs now to store their locations, but it can be done without classes. Program it so that the user has to touch directly on the bug to make it disappear and move.
Challenge 4: Create a score variable, and add points to it every time a bug is touched. Display the score on screen.
Challenge 5: Create a timer and make it count down, when 0 is reached, display a new screen with the score. If the user touches the screen from here it should start the game over. You may need to see the state machine tutorial by @Reefwing.
Challenge 6: Add sounds. When the game starts, a bug is touched, or the game is over, make it play a sound.
Challenge 7: make it so that the bugs move automatically if not touched with 1 or 2 seconds. This should make the game more fun and challenging.

If you decide to try one of those, let me know how it goes. Good luck.

Just woke up. On it

Darn it I am so code challenged. I’m having trouble with Dice Roller’s Challenge 3.
To touch a certain part of the screen only I would always use this one code that I learned in another Discussion:

//the position of my dice as defined in Dice:init() is a vec2 called SELF.POS so...
function point(x,y)
    if x < (self.pos.x + self.width/2) and
    x > (self.pos.x - self.width/2) and
    y < (self.pos.y + self.height/2) and
    y > (self.pos.y - self.height/2) then
        return true
    end
    return false
end

function Dice:touched(touch)
    if point(touch.x, touch.y) and touch.state == ENDED then
        self.number = math.random(1,6)
        print(self.number)
    end
end

If there’s something easier than that plz let me know.

But this code only works with the most recent dice and when touched all the dice have the same number.

So how do I go about doing Challenge 3? (God I suck at this >< )

How do I make multiple instances of a Class but give them different locations? Can it only be done using tables or something?

Tables are the best way to accomplish this. Look up ipairs to iterate through them.

Don’t beat yourself up, that code looks correct and should work. Maybe the issue is with how you are storing / calling the dice. I would guess that your main setup has some code that looks like:

dice = {}
newDie = Dice(100,100) --Or however your init looks 
table.insert(dice, newDie)
newDie = Dice(300,100)
table.insert(dice, newDie)
-- Now we have a table of dice that contains 2 dice.

And then in your main touched function you might have

touched(touch)
    for _,die in pairs(dice) do
        die:touched(touch)
	--That calls the touched function of each die.
    end
end

What do I put in the draw function to get the table drawn?
Because Dice:draw() only draws one dice


function setup()
    print("Hello World!")
    dice = {}
    newDie = Dice:init(math.random(1,WIDTH),math.random(1,HEIGHT))
    table.insert(dice,newDie)
    newDie = Dice:init(math.random(1,WIDTH),math.random(1,HEIGHT))
    table.insert(dice,newDie)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    for _,v in ipairs(dice) do
        v:draw()
    end
end
for _,v in ipairs(dice) do
    v:draw()
end

I did it exactly as you said and nothing’s appearing on screen anymore :frowning:

The issue is probably in your Dice:draw() function. You might need to post that code so we can see whats going on.

It shouldn’t be :confused: it’s uber simple:


Dice = class()

function Dice:init(x,y)
    -- you can accept and set parameters here
    self.pos = vec2(x,y)
end

function Dice:draw()
    -- Codea does not automatically call this method
    stroke(0,0);strokeWidth(10)
    fill(255);rectMode(CENTER)
    rect(self.pos.x,self.pos.y,150,150)
end

function Dice:touched(touch)
    -- Codea does not automatically call this method
end

Okay, sorry, I see it now, small syntax error. In your main, you shouldn’t call init directly, it is the constructor. All you have to do is delete

:init

Twice from the setup function. So the code should look like:

    print("Hello World!")
    dice = {}
    newDie = Dice(math.random(1,WIDTH),math.random(1,HEIGHT))
    table.insert(dice,newDie)
    newDie = Dice(math.random(1,WIDTH),math.random(1,HEIGHT))
    table.insert(dice,newDie)

And voila.

It worked! I had added the “:init” because I thought you had simply forgotten to put it in :slight_smile:
I don’t understand, though. If you don’t tell the code to put the two math.randoms in the initiate then how does it know that’s where the variables go?

Init is a special function called a “constructor.” The constructor is automatically called when an object is created and receives the parameters that you pass in upon object creation.

Thanks! Now that i’m armed with this new piece of coding I can also tackle part 3 of the Whack-A-Mole (er, bug) challenge!

@Vega Lol, can’t get my High Score past 97 whacks :stuck_out_tongue:
Another challenge please :slight_smile: