Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Some flys here

edited August 2014 in Code Sharing Posts: 175

Hi guys, it's my first topic and this is my simple game (if I can say that) . I make it for 2-3 hours and i have some unsolved problems : 1. How can I spawn more flys then I tap this one? (I read ignatz book, but "balloon" example don't work for me. Maybe I'm dumb for programming) 2. Maybe there is some shorter way o make button on this fly ?(now I take roundrect and button classes from example projects) 3. Is there some elegant way to animate my sprites?

Ps. Sorry for my English . And wasting your time


  • IgnatzIgnatz Mod
    edited August 2014 Posts: 5,396

    @lupino8211 - welcome. And you're not wasting our time. We like helping people like you. It's too late at night for me, but I'm sure you'll get help here. :)

  • IgnatzIgnatz Mod
    Posts: 5,396

    @lupino8211 - and don't be sorry for your English. I can't speak any of your language! Also, we come from all around the world.

    As long as we can understand what you are saying, we can help you.

  • Posts: 175

    @Ignatz thank you. I'm very interested in making games, and you books are very helpful for me .

  • Jmv38Jmv38 Mod
    edited August 2014 Posts: 3,295

    @lupino8211 difficult to make a detailed answer to your question. You could look at this code: it draws 300 butterfies, which are touch sensitive, and the drawing is efficient (if not 'elegant'). Managing many buttefies is done by creatin a 'butterfly' class. Maybe this example can help you?

    --# Butterflies Butterflies = class() function Butterflies:init(n) self.colorList = {} self.colorList[1] = color(253, 255, 0, 255) self.colorList[2] = color(255, 188, 0, 255) self.colorList[3] = color(255, 75, 0, 255) self.colorList[4] = color(255, 0, 157, 255) self.colorList[5] = color(252, 0, 255, 255) self.colorList[6] = color(138, 0, 255, 255) self.colorList[7] = color(0, 159, 255, 255) self.colorList[8] = color(0, 247, 255, 255) self.colorList[9] = color(0, 255, 158, 255) self.colorList[10] = color(41, 255, 0, 255) self.n = n local id = {} local ms = mesh() print(ms) print(#ms.vertices) = id = ms for i =1,n do local c = self.colorList [rnd(10)] local j = (i-1)*12 id[i] = Butterfly(ms,c) end end function Butterflies:draw() for i,p in ipairs( do p:draw() end end function Butterflies:touched(touch) for i,p in ipairs( do p:touched(touch) end end --# Butterfly Butterfly = class() rnd = math.random cos = math.cos sin = math.sin pi = math.pi function Butterfly:init(ms,c) local x,y,z,w,h,o,r,j local xc,yc = WIDTH/2,HEIGHT/2 local shape = {} x = (rnd()-0.5)*WIDTH*0.9 + xc y = (rnd()-0.5)*HEIGHT*0.9 + yc z = 0 w = (rnd()+2)*4 h = w o = 1 -- this is the opening of the wings r = rnd()*2*math.pi j = #ms.vertices ms:addRect(x,y,w,h,r) ms:addRect(x,y,w,h,r) = ms local p =self p.j, p.shape, p.r, p.c, p.w, p.h, p.o, p.x, p.y = j,shape,r,c,w,h,o,x,y p.beat, p.maxBeat = 0,1 self:redraw() end function Butterfly:redraw() local p= self local j,shape,r,c0,w,h,o,x,y = p.j, p.shape, p.r, p.c, p.w, p.h, p.o, p.x, p.y local ms = shape[1]=vec2(0,0):rotate(r) shape[2]=vec2(w*o*1.3,h):rotate(r) shape[3]=vec2(w*o/2,-h):rotate(r) shape[4]=vec2(0,0):rotate(r) shape[5]=vec2(-w*o*1.3,h):rotate(r) shape[6]=vec2(-w*o/2,-h):rotate(r) c = color(0,0,0,128) local d,s if self.flying then d=10 s=1.1 else d=4 s=1 end for k=1,6 do ms:vertex(j+k,shape[k]*s+vec2(x+d,y-d)) ms:color(j+k,c) end for k=1,6 do ms:vertex(j+k+6,shape[k]*s+vec2(x,y)) ms:color(j+k+6,c0) end end function Butterfly:draw() local rnd = math.random if rnd(500)==1 then self:turn() end if rnd(500)==1 then self:wingBeat(rnd(20)+10) end if rnd(10000)==1 then self:fly("random") end self:wingBeat() if self.flying then self:fly() end end function Butterfly:wingBeat(n) local pap = self if n then -- this is a start pap.beat= n pap.maxBeat = n -- sound(SOUND_HIT, 39771) else -- continue beating if pap.beat ~=0 then local o = (1.5+ cos(pap.beat/pap.maxBeat*2*pi))/2.5 pap.o = o self:redraw() pap.beat = pap.beat - 1 end end end function Butterfly:turn() local pap = self local r = (rnd()-0.5)*pi/2 pap.r = pap.r + r self:redraw() end function Butterfly:fly(type) local alpha if type then self.flying = true self.flyType = type if self.flyType == "random" then self.flySpeed = 1+rnd(1) alpha = self.r + pi/2 self.flyDir = vec2(cos(alpha),sin(alpha)) end else if self.beat==0 then self:wingBeat(rnd(10)+10) end if self.flyType == "random" then if rnd(30)==1 then self:turn() alpha = self.r + pi/2 self.flyDir = vec2(cos(alpha),sin(alpha)) end if rnd(300)==1 then self.flying=false end end if self.x <0 then self.x = self.x + WIDTH end if self.x >WIDTH then self.x = self.x - WIDTH end if self.y<0 then self.y = self.y + HEIGHT end if self.y>HEIGHT then self.y = self.y - HEIGHT end local dir,speed = self.flyDir,self.flySpeed self.x = self.x + dir.x * speed self.y = self.y + dir.y * speed self:redraw() end end function Butterfly:touched(touch) if vec2(touch.x,touch.y):dist(vec2(self.x,self.y))<40 then self:fly("random") end end --# Main -- 0 papillon displayMode(FULLSCREEN) -- Use this function to perform your initial setup function setup() print("Hello World!") papillons = Butterflies(300) -- fps=FPS() end -- This function gets called once every frame function draw() -- This sets a dark background color background(68, 68, 68, 255) papillons:draw() -- fps:draw() end function touched(touch) papillons:touched(touch) end
  • Posts: 175

    @Jvm38 thank you very much. Looks very useful for me. I'l have a look on this more careful

  • dave1707dave1707 Mod
    Posts: 7,468

    @lupino8211 Here's a version of what I think you're after. It keeps track of your hits and misses. The score is (hits - misses) but doesn't go negative. The speed of the fly increases as your score increases. This doesn't use classes to keep it simple. Maybe this will help you some.

    displayMode(FULLSCREEN) function setup() fx=WIDTH/2 fy=HEIGHT/2 dx,dy=0,0 x1,y1=0,0 hit=0 miss=0 score=0 end function draw() background(40, 40, 50) if math.random(2)==1 then sprite("Platformer Art:Battor Flap 1",fx,fy) else sprite("Platformer Art:Battor Flap 2",fx,fy) end x1=x1-1 if x1<0 then dx=math.random(-1,1)*score -- change fly x direction x1=20 end y1=y1-1 if y1<0 then dy=math.random(-1,1)*score -- change fly y direction y1=20 end fx=fx+dx -- update fly position fy=fy+dy if fx>WIDTH then -- check if fly is off screen fx=0 end if fx<0 then fx=WIDTH end if fy>HEIGHT then -- check if fly is off screen fy=0 end if fy<0 then fy=HEIGHT end fontSize(20) fill(255) text("Hits "..hit,WIDTH/2,HEIGHT-50) text("Miss "..miss,WIDTH/2,HEIGHT-100) fontSize(40) score=hit-miss if score<0 then score=0 end text("Score "..score,WIDTH/2,HEIGHT-150) end function touched(t) if t.state==BEGAN then v1=vec2(fx,fy) -- center of fly d=v1:dist(vec2(t.x,t.y)) -- check touch distance to center of the fly if d<40 then hit=hit+1 -- fly was hit fx=math.random(50,WIDTH-50) fy=math.random(50,HEIGHT-50) else miss=miss+1 -- fly was missed end end end
  • edited August 2014 Posts: 175

    @dave1707 you make it very simple and more interesting, thanks . And I played for a 20 minutes in this game. Awesome

  • dave1707dave1707 Mod
    Posts: 7,468

    @lupino8211 It doesn't take a lot of coding to do things with Codea. You just have to think ahead of time what you want to do and how it needs to be done. Unless you totally understand how someone else's classes work and what they're intended for, you can be pulling in a lot of code you don't need. Also, the more programs you write, the more you'll understand how to do thing easier. And the programs don't have to be big to learn.

Sign In or Register to comment.