Zodiak sign

My zodiak sign is Sagittarius. Predictable to the day and age, I created the following program.


function setup()
    coordx={0,215,430,645,860,1075,0,215,430,645,860,1075}
    coordy={240,240,240,240,240,240,0,0,0,0,0,0}
    large={205,205,205,205,205,205,205,205,205,205,205,205}
    maxspr=12
    sprites={}
    chargeimage=false
    continue=false
    http.request("http://img560.imageshack.us/img560/7135/stern12.jpg",didGetImage)
     iparameter("d",1,31,19)
    iparameter("m",1,12,12)
    iparameter("y",1900,2020,1930)
end

-- This function gets called once every frame
function draw()
    background(255, 255, 255, 0)
    fill(0, 0, 0, 255)
    textMode(CENTER)
    fontSize(28)
    text("YOUR SIGN",WIDTH/2,HEIGHT-50)
    text("Wait please. Loading Image from Http...",WIDTH/2,HEIGHT-90)
    text("Day:   "..d,WIDTH/2,600)
    text("Month: "..m,WIDTH/2,575)
    text("Year:  "..y,WIDTH/2,550)
    if chargeimage then 
        
        print("Load completed...")
            for i=1,maxspr do
                sprites[i]=matrice:copy(coordx[i],coordy[i],large[i],245)
            end 
        continue=true
        chargeimage=false
    end   
    if continue then
    if y<1902 then y=1902 end
    timeValue = os.time{year=y,month=m,day=d}
    timeValueString = string.format("%.1f", timeValue)
    dateString = os.date("%c", timeValue)
    
    t1 = os.time{day=d,month=m,year=y}
    t2 = os.time()
    k1 = os.date("*t",t1)
    k2 = os.date("*t",t2)
    text("Finished loading ...",WIDTH/2,HEIGHT-130)
    text("Which was on  "..k1.day.."."..k1.month.."."..k1.year,WIDTH/2,120)
    text("Today is  "..k2.day.."."..k2.month.."."..k2.year,WIDTH/2,90)
    if k2.month < k1.month then
        text("You are  "..(k2.year - k1.year -1).."  Years old.",WIDTH/2,60)
    else
         text("You are  "..(k2.year - k1.year).."  Years old.",WIDTH/2,60)
    end
    
    local weekday=(string.sub(dateString ,1,3))
    if weekday == "Sun" then weekday = "Sunday - Sonntag" end
    if weekday == "Mon" then weekday = "Monday - Montag" end
    if weekday == "Tue" then weekday = "Tuesday - Dienstag" end
    if weekday == "Wed" then weekday = "Wednesday - Mittwoch" end
    if weekday == "Thu" then weekday = "Thursday - Donnerstag" end
    if weekday == "Fri" then weekday = "Friday - Freitag" end
    if weekday == "Sat" then weekday = "Saturday - Samstag" end
    text("You are born on a " ..weekday,WIDTH/2,180)

        if m == 1 and d > 20 then
                w,h=spriteSize(sprites[1])
                text("Wassermann",WIDTH/2,470)
                text("Aquarius",WIDTH/2,420)
                sprite(sprites[1],WIDTH/2,300,200,200)
            end
        if m == 2 and d < 19 then
                w,h=spriteSize(sprites[1])
                text("Wassermann",WIDTH/2,470)
                text("Aquarius",WIDTH/2,420)
                sprite(sprites[1],WIDTH/2,300,200,200)
            end
        if m == 2 and d > 18 then
                w,h=spriteSize(sprites[8])
                text("Fische",WIDTH/2,470)
                text("Pisces",WIDTH/2,420)
                sprite(sprites[8],WIDTH/2,300,200,200)
            end
         if m == 3 and d < 21 then
                w,h=spriteSize(sprites[8])
                text("Fische",WIDTH/2,470)
                text("Pisces",WIDTH/2,420)
                sprite(sprites[8],WIDTH/2,300,200,200)
            end
           if m == 3 and d > 20 then
                w,h=spriteSize(sprites[2])
                text("Widder",WIDTH/2,470)
                text("Aries",WIDTH/2,420)
                sprite(sprites[2],WIDTH/2,300,200,200)
            end
         if m == 4 and d < 21 then
                w,h=spriteSize(sprites[2])
                text("Widder",WIDTH/2,470)
                text("Aries",WIDTH/2,420)
                sprite(sprites[2],WIDTH/2,300,200,200)
            end  
         if m == 4 and d > 20 then
                w,h=spriteSize(sprites[11])
                text("Stier",WIDTH/2,470)
                text("Taurus",WIDTH/2,420)
                sprite(sprites[11],WIDTH/2,300,200,200)
            end    
        if m == 5 and d < 21 then
                w,h=spriteSize(sprites[11])
                text("Stier",WIDTH/2,470)
                text("Taurus",WIDTH/2,420)
                sprite(sprites[11],WIDTH/2,300,200,200)
            end   
        if m == 5 and d > 20 then
                w,h=spriteSize(sprites[5])
                text("Zwillinge",WIDTH/2,470)
                text("Gemini",WIDTH/2,420)
                sprite(sprites[5],WIDTH/2,300,200,200)
            end  
           if m == 6 and d < 22 then
                w,h=spriteSize(sprites[5])
                text("Zwillinge",WIDTH/2,470)
                text("Gemini",WIDTH/2,420)
                sprite(sprites[5],WIDTH/2,300,200,200)
            end   
           if m == 6 and d > 21 then
                w,h=spriteSize(sprites[3])
                text("Krebs",WIDTH/2,470)
                text("Cancer",WIDTH/2,420)
                sprite(sprites[3],WIDTH/2,300,200,200)
            end  
           if m == 7 and d < 23 then
                w,h=spriteSize(sprites[3])
                text("Krebs",WIDTH/2,470)
                text("Cancer",WIDTH/2,420)
                sprite(sprites[3],WIDTH/2,300,200,200)
            end          
           if m == 7 and d > 22 then
                w,h=spriteSize(sprites[6])
                text("Löwe",WIDTH/2,470)
                text("Leo",WIDTH/2,420)
                sprite(sprites[6],WIDTH/2,300,200,200)
            end  
           if m == 8 and d < 23 then
                w,h=spriteSize(sprites[6])
                text("Löwe",WIDTH/2,470)
                text("Leo",WIDTH/2,420)
                sprite(sprites[6],WIDTH/2,300,200,200)
            end                  
            if m == 8 and d > 22 then
                w,h=spriteSize(sprites[12])
                text("Jungfrau",WIDTH/2,470)
                text("Virgo",WIDTH/2,420)
                sprite(sprites[12],WIDTH/2,300,200,200)
            end  
           if m == 9 and d < 24 then
                w,h=spriteSize(sprites[12])
                text("Jungfrau",WIDTH/2,470)
                text("Virgo",WIDTH/2,420)
                sprite(sprites[12],WIDTH/2,300,200,200)
            end                         
           if m == 9 and d > 23 then
                w,h=spriteSize(sprites[7])
                text("Waage",WIDTH/2,470)
                text("Libra",WIDTH/2,420)
                sprite(sprites[7],WIDTH/2,300,200,200)
            end  
           if m == 10 and d < 24 then
                w,h=spriteSize(sprites[7])
                text("Waage",WIDTH/2,470)
                text("Libra",WIDTH/2,420)
                sprite(sprites[7],WIDTH/2,300,200,200)
            end                                  
           if m == 10 and d > 23 then
                w,h=spriteSize(sprites[10])
                text("Skorpion",WIDTH/2,470)
                text("Scorpio",WIDTH/2,420)
                sprite(sprites[10],WIDTH/2,300,200,200)
            end  
           if m == 11 and d < 23 then
                w,h=spriteSize(sprites[10])
                text("Skorpion",WIDTH/2,470)
                text("Scorpio",WIDTH/2,420)
                sprite(sprites[10],WIDTH/2,300,200,200)
            end 
         if m == 11 and d > 22 then
                w,h=spriteSize(sprites[9])
                text("Schütze",WIDTH/2,470)
                text("Sagittarius",WIDTH/2,420)
                sprite(sprites[9],WIDTH/2,300,200,200)
            end  
           if m == 12 and d < 22 then
                w,h=spriteSize(sprites[9])
                text("Schütze",WIDTH/2,470)
                text("Sagittarius",WIDTH/2,420)
                sprite(sprites[9],WIDTH/2,300,200,200)
            end                                                    
          if m == 12 and d > 21 then
                w,h=spriteSize(sprites[4])
                text("Capricornus",WIDTH/2,420)
                text("Steinbock",WIDTH/2,470)
                sprite(sprites[4],WIDTH/2,300,200,200)
            end  
           if m == 1 and d < 21 then
                w,h=spriteSize(sprites[4])
                text("Steinbock",WIDTH/2,470)
                text("Capricornus",WIDTH/2,420)
                sprite(sprites[4],WIDTH/2,300,200,200)
            end   
    end                                                                                 
end

function didGetImage(image)
    matrice=image
    if spriteSize(matrice)==0 then
        print ("Download faulty.....")
        setup()
    end
    chargeimage=true
end

Very nice! Good to remind me german names for week days too :slight_smile: .But you could have avoided reminding me my age :frowning: .Do you want me to put it in my web repo for ‘polished apps’ so that it does not disappear in the limbs of Vanilla?

Interesting program, thanks for sharing. I haven’t played with the http.request very much, so I was playing around with the program to understand it and ran into a problem. I’m not sure if I’m doing something wrong, but if I run the program below which is a stripped down version of the original, it blows Codea away after I load the image 13 or 14 times. Just keep tapping the screen to load the image. Can someone explain what I’m doing wrong or why it blows up. I use an iPad1, 16gb.


displayMode(FULLSCREEN)

function setup()
    chargeimage=false
    count=0
    str1="tap screen for image"
end

function draw()
    background(255, 255, 255, 0)
    text(count,WIDTH/2,HEIGHT-100)
    text(str1,WIDTH/2,HEIGHT-150)
    if chargeimage then
        sprite(matrice,WIDTH/2,HEIGHT/2,w/2,h/2)
    end
end

function touched(t)
    if t.state==BEGAN then
        chargeimage=false
        str1="Requesting image"
        http.request("http://img560.imageshack.us/img560/7135/stern12.jpg",didGetImage,fail)
    end
end

function didGetImage(image)
    matrice=image
    w,h=spriteSize(matrice)
    if w+h>0 then
        str1="tap screen for image"
        chargeimage=true
        count = count + 1
    end
end

function fail(data)
    print(data)
end

Usually what blows up Codea is memory overload: you cant load too many images to the memory. If you load to the same variable, then old memory should be released, this is done automatically but not often enough if you tap tap fast. So you can add collectgarbage() to free memory just after the image load.

Thanks @Jmv38. That stopped the crashes. Just a note, collectgarbage doesn’t show in the auto-complete line as it’s being typed and I didn’t see it in the manual.

Hi matox, I see you’re interested in programming. I want to tell you some things about tables and loops to bring back the fun. Your code looks like a massive effort of copy and paste and has all the bad side effects like improper indentation. I want to help you to structure your data and reduce typing. (It’s enough if I do the typing to tell you where you did too much of it.)

Mini table introduction

Let’s start simple with your expansion of the names of weekdays:


    if weekday == "Sun" then weekday = "Sunday - Sonntag" end
    if weekday == "Mon" then weekday = "Monday - Montag" end
    . . .

Make good use of tables:


    local fullname = {
        ["Sun"] = "Sunday - Sonntag",
        ["Mon"] = "Monday - Montag",
	. . .
    }
    weekday = fullname[weekday]
    
    -- alternatively, if you find this more easy to understand:
    local fullname = {}
    fullname["Sun"] = "Sunday - Sonntag"
    fullname["Mon"] = "Monday - Montag"
    . . .
    weekday = fullname[weekday]
    -- end of alternativity

This was just to get you used to tables and to show you that data can be structured as … well … data. Data in the meaning of having them in data structures, not inlined into code. We’ll need tables later to beautify a lot of your code, not just 7 lines.

Doing the same thing once

This is a simplification that works without tables and already reduces a lot of code. Zodiacs occupy two months, you write it like this:


    if m == 1 and d > 20 then
        w,h=spriteSize(sprites[1])
        text("Wassermann",WIDTH/2,470)
        text("Aquarius",WIDTH/2,420)
        sprite(sprites[1],WIDTH/2,300,200,200)
    end
    if m == 2 and d < 19 then
        w,h=spriteSize(sprites[1])
        text("Wassermann",WIDTH/2,470)
        text("Aquarius",WIDTH/2,420)
        sprite(sprites[1],WIDTH/2,300,200,200)
    end

You’re doing exactly the same thing twice, better write this:


    if (m == 1 and d >= 21) or (m == 2 and d < 19) then
        w,h=spriteSize(sprites[1])
        text("Wassermann",WIDTH/2,470)
        text("Aquarius",WIDTH/2,420)
        sprite(sprites[1],WIDTH/2,300,200,200)
    end

Also observe that I adjusted the notion of the start day.

Doing similar things once or Putting data into structures

You place all your zodiac signs at WIDTH/2, 300, 200, 100, right? No? At WIDTH/2, 300, 200, 200 you say? Imagine you change your mind, you’ll have to edit 12 (or 24 without the prior change) places.

What are the properties of a zodiac sign? We have a start day/month, a last day/month, a name in two different languages and as a special property a sprite number.

Let’s start simple:


    -- Preparation (in setup)
    -- Abbr. to reduce typing efforts:
    -- sm, sd = starting month, day
    -- nm, nd = next zodiac's month, day
    aquarius = {
        sm =  1, sd = 21, nm =  2, nd = 19, spritenum = 1,
	german = "Wassermann", english = "Aquarius"
    }
    
    -- Making use of it (in draw)
    -- z = zodiac
    z = aquarius
    if (m == z.sm and d >= z.sd) or (m == z.nm and d < z.nd) then
        w, h = spriteSize(sprites[z.spritenum])
        text(z.german, WIDTH/2 ,470)
        text(z.english, WIDTH/2, 420)
        sprite(sprites[z.spritenum], WIDTH/2, 300, 200, 200)
    end
    

Now that all variable information is in a table, the code part looks quite generic. It can draw any zodiac, I simply used Aquarius as a starter. Go on, create tables for all zodiacs, then put them into a list:


    -- Preparation (in setup)
    aquarius = { . . . }
    pisces = { . . . }
    . . .
    zodiaclist = { aquarius, pisces, . . . }
    

Now loop over all zodiacs (eliminating some dozen lines of code at once), I’ll present to ways for you:


    -- Making use of it (in draw)
    for i = 1, 12 do
        -- z = zodiac
        local z = zodiaclist[i]
        if (m == z.sm and d >= z.sd) or (m == z.nm and d < z.nd) then
            w, h = spriteSize(sprites[z.spritenum])
            text(z.german, WIDTH/2 ,470)
            text(z.english, WIDTH/2, 420)
            sprite(sprites[z.spritenum], WIDTH/2, 300, 200, 200)
        end
    end

    -- alternatively; better choose this loop when geeking out
    for i, z in ipairs(zodiaclist) do
        if (m == z.sm and d >= z.sd) or (m == z.nm and d < z.nd) then
            w, h = spriteSize(sprites[z.spritenum])
            text(z.german, WIDTH/2 ,470)
            text(z.english, WIDTH/2, 420)
            sprite(sprites[z.spritenum], WIDTH/2, 300, 200, 200)
        end
    end

Well, that’s short and polished (even if not as much as it could be), I suggest Jmv38 should wait a bit if he wants to put a “polished” app into his repo.

Geeking out

“Ophiuchus” - “Schlangenträger” is missing you list of zodiacs.

I hope I didn’t repel you, I just want you to learn and put more work into creativity and follow the path of nice code.

Here are the new zodiac signs and dates. There are now 13 signs instead of 12. This can be seen by using any planetarium program and watch as the Sun travels thru the different constellations.


Capricorn: Jan. 20 - Feb. 16
Aquarius: Feb. 16 - March 11
Pisces: March 11- April 18
Aries: April 18 - May 13
Taurus: May 13 - June 21
Gemini: June 21 - July 20
Cancer: July 20 - Aug. 10
Leo: Aug. 10 - Sept. 16
Virgo: Sept. 16 - Oct. 30
Libra: Oct. 30 - Nov. 23
Scorpio: Nov. 23 - Nov. 29
Ophiuchus: Nov. 29 - Dec. 17
Sagittarius: Dec. 17 - Jan. 20