Traffic-Light Simulation

–Traffic-Light matox 2013/1

A traffic simulation. The 7 images can be downloaded from the following addresses. Then save in the Documents folder.

http://imageshack.us/a/img805/6471/ampel.png
http://imageshack.us/a/img826/3563/streetg.png
http://imageshack.us/a/img22/3160/59823085.png

http://imageshack.us/a/img443/4015/63174822.png
http://imageshack.us/a/img845/9625/77250382.png
http://imageshack.us/a/img199/5262/93768284.png
http://imageshack.us/a/img405/2300/76255633.png

--Traffic-Light matox 2013/2

-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    gox = 100
    speed = 10
    red=color(255,0,0)
    yellow=color(255,255,0)
    green=color(0,255,0)
    xt=0
    frameDelay = 10
    animation = Animation({"Documents:T1",
                           "Documents:T2",
                           "Documents:T3",
                           "Documents:T4"}, 10)
end

-- This function gets called once every frame
function draw()  
    gox = gox + speed
    if gox > 1200 then gox = -31 end 
    if gox > 400 and gox < 480 and s == true then speed = 0
    else speed = 10; end
    -- Do your drawing here
    --sprite("Documents:Street",WIDTH/2,HEIGHT/2,WIDTH*2,HEIGHT) --for iPad1
    sprite("Documents:Street",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT) --for iPad4 Retina
    sprite("Documents:VW",gox,100,320,140)
    sprite("Planet Cute:Tree Tall",1000,0,200,800)
    sprite("Documents:Ampel",495,200,116,375)
    xt=xt+1
    if xt > 1002 then xt = 0 end
    animation:draw(xt,600) 
  
 if (os.date("%S")) > ("00") and (os.date("%S")) < ("11") then fill(red) ellipse(500,340,40,40);
     noFill() ellipse(500,300,40,40);noFill() ellipse(500,260,40,40); s=true end
    
 if (os.date("%S")) > ("10") and (os.date("%S")) < ("12")then fill(red) ellipse(500,340,40,40);
     fill(yellow) ellipse(500,300,40,40);noFill() ellipse(500,260,40,40);  s=true end
    
if (os.date("%S")) > ("11") and (os.date("%S")) < ("26")then noFill() ellipse(500,340,40,40);
     noFill() ellipse(500,300,40,40); fill(green) ellipse(500,260,40,40) ; s=false end   
    
 if (os.date("%S")) > ("25") and (os.date("%S")) < ("27") then noFill() ellipse(500,340,40,40);
     fill(yellow) ellipse(500,300,40,40); noFill() ellipse(500,260,40,40); s=false end
    
if (os.date("%S")) > ("26") and (os.date("%S")) < ("41") then fill(red) ellipse(500,340,40,40);
     noFill() ellipse(500,300,40,40); noFill() ellipse(500,260,40,40) ; s=true end  
    
if (os.date("%S")) > ("40") and (os.date("%S")) < ("42") then fill(red) ellipse(500,340,40,40);
     fill(yellow) ellipse(500,300,40,40); noFill() ellipse(500,260,40,40) s=true end 
    
if (os.date("%S")) > ("41") and (os.date("%S")) < ("58") then noFill() ellipse(500,340,40,40);
     noFill() ellipse(500,300,40,40);fill(green) ellipse(500,260,40,40)  ; s=false end  
    
if (os.date("%S")) > ("57") and (os.date("%S")) < ("61") then noFill() ellipse(500,340,40,40);
     fill(yellow) ellipse(500,300,40,40);noFill() ellipse(500,260,40,40)  s=false end
    
if (os.date("%S")) < ("01") then fill(red) ellipse(500,340,40,40);
     noFill() ellipse(500,300,40,40); noFill() ellipse(500,260,40,40)   s=true end
end

-- Animation class
Animation = class()
 
function Animation:init(frames, delay)
    self.frames = frames -- table of sprite names
    self.currentFrame = 1 -- the current frame
    self.frameDelay = delay -- how many iterations of main draw loop before changing frame
    self.count = 1 -- keeps track of iterations passed 
end
 
function Animation:draw(x, y)
    -- Increment the counter
    self.count = self.count + 1
    -- Check if frame should be updated
    if self.count > self.frameDelay then
        -- Reset count and increment current frame
        self.count = 1
        self.currentFrame = self.currentFrame + 1
        -- Check if we've reached the last frame
        if self.currentFrame > table.maxn(self.frames) then
            -- Back to first frame
            self.currentFrame = 1
        end
    end
    -- Draw correct frame
    sprite(self.frames[self.currentFrame], x, y) 
end

That’s cool, but are all the lights supposed to be on together or is that a bug? Cause after second light switches on the first on doesn’t disappear.

Hallo Saurab // With me on the iPad4 retina, the signal sequence is correct

In Germany, the signal sequence:
1 red
2 red and yellow
3 green
4 yellow
5 red

I don’t know what’s going wrong cause i didn’t understand your code so I don’t know. by the way mines retina too and that shouldn’t be a problem cause codea handles changes from retina to non retina(maybe). I don’t think you’ll have to put different images for retina and non retina. The same one should work on both.

I have an iPad1 and an iPad4 Retina. In the program, I just need the background image to either switch for 1 or 4th Then it works just fine with me.
http://www.youtube.com/watch?v=m9KoPjpdBGA

Cool @matox. Btw how did you post the video here on the forum?

@Saurabh
in the current program at the bottom left click on the video icon. Then you can save the video on Youtube and get a fetch address.