{{ message }}

Instantly share code, notes, and snippets.

# juaxix/Car.lua

Created Jul 21, 2012
2D physic puzzle / sandbox game made in Codea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 Car = class() -- 2D Physics game made in Codea by @juaxix -- More games: xixgames.com -- 21/07/2012 -- inspired by Codea Physics tutorial and Crayon Physics :) function Car:init(x,y) local car = physics.body(POLYGON, vec2(-50,10), vec2(-50,-10), vec2(50,-10), vec2(50,10), vec2(30,10),vec2(25,25),vec2(-25,25),vec2(-30,10)) car.position = vec2(x, y) debugDraw:addBody(car) car.density = 2.0 local piston = physics local leftPos = vec2(x - 30, y-10) local leftWheel = createCircle(leftPos.x, leftPos.y, 20) leftWheel.friction = 1 self.leftJoint = physics.joint(REVOLUTE, car, leftWheel, leftWheel.position) self.leftJoint.maxMotorTorque = 10 local rightPos = vec2(x + 30, y-10) local rightWheel = createCircle(rightPos.x, rightPos.y, 15) rightWheel.friction = 1 self.rightJoint = physics.joint(REVOLUTE, car, rightWheel, rightWheel.position) end function Car:cleanup() self.leftJoint = nil self.rightJoint = nil end function Car:touched(touch) -- Codea does not automatically call this method if touch.state == BEGAN or touch.state == MOVING then self.leftJoint.enableMotor = true if touch.x > WIDTH/2 then self.leftJoint.motorSpeed = -1666 else self.leftJoint.motorSpeed = 3333 end elseif touch.state == ENDED then self.leftJoint.motorSpeed = 0 end end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 -- Firework Class -- 2D Physics game made in Codea by @juaxix -- More games: xixgames.com -- 21/07/2012 -- inspired by Codea Physics tutorial and Crayon Physics :) Firework_Parts = 33 Life = 11 Life_Variation = 100 Air_Resistance = 0.1 PartSize = 60 PartSize_Variation = 166 Velocity = 6 Velocity_Variation = 11 Color_Variation = 50 Firework = class() function Firework:init(position) -- you can accept and set parameters here self.p = {} self.position = position local colour = color(243, 255, 0, 255) self.numParticles = Firework_Parts for i=1,self.numParticles do local psize = genNumber(PartSize,PartSize_Variation) local v = vec2(math.random(-100,100),math.random(-100,100)) v = v:normalize() v = v * genNumber(Velocity,Velocity_Variation) --[[local c = color(genNumber(colour.r,Color_Variation), genNumber(colour.g,Color_Variation), genNumber(colour.b,Color_Variation), colour.a )]]-- self.p[i] = Particle( --posx,posy,size,life,colour, velocity,mass,angle,sprite) position.x, position.y, 130, genNumber(Life,Life_Variation), color(255, 255, 255, 255), v, 1, 0, "SpaceCute:Star") end end function Firework:draw() local resistance = 1/(Air_Resistance + 1) --local g = vec2(Gravity.x,Gravity.y) local g = vec2(0.040731, 0.1) for i=1,self.numParticles do local p = self.p[i] p.x = p.x + p.v.x p.y = p.y + p.v.y p.v = p.v + g p.v = p.v * resistance local size = math.random(PartSize) * (p.lifeLeft/p.life) p.width = size p.height = size p:draw() end end function Firework:isDead() for i=1,self.numParticles do p = self.p[i] if p.lifeLeft ~= 0 and (p.x>0 and p.x0 and p.y 0 then self.lifeLeft = self.lifeLeft - 1 end if self.lifeLeft ~= 0 then if self.sprite == nil then fill(self.color) ellipse(self.x,self.y,self.width,self.height) else pushMatrix() translate(self.x,self.y) rotate(self.angle) tint(self.color) sprite(self.sprite,0,0,self.width,self.height) popMatrix() end end end function genNumber(number,variation) ret = variation*0.01*number ret = number + math.random(-ret,ret) return ret end Smoke = class() function Smoke:init(x,y,n) self.numparts = n -- color used to tint the particle local c = color(73, 73, 73, 69) -- name of the sprite of each smoke particle local s = "Tyrian Remastered:Dark Orb" self.parts = {} for i=0,n do -- initial size of the smoke particle local sz = genNumber(60,100) self.parts[i] = Particle(genNumber(x,20), genNumber(y,20),sz,-7,c,nil,-1,genNumber(180,100),s) end -- rotation speed self.rSpeed = 0.5 self.windX = 1 self.windY = 1 end function Smoke:draw() for i=1,self.numparts do local p = self.parts[i] if p.color.a > 0 then p.angle = p.angle + self.rSpeed p.width = p.width + 3 p.height = p.height + 3 p.color.a = p.color.a - 0.2 p.x = p.x + self.windX p.y = p.y + self.windY p:draw() end end end function Smoke:isDead() for i=1,self.numparts do local p = self.parts[i] if p.color.a > 0 then return false end end return true end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 -- Level Class Level = class() -- 2D Physics game made in Codea by @juaxix -- More games: xixgames.com -- 21/07/2012 -- inspired by Codea Physics tutorial and Crayon Physics :) current_level = 1 winner = false loser  = false levels = { -- levels { -- level 1 { -- ground body list type = POLYGON,v1=vec2(0,20),v2=vec2(0,0),v3=vec2(WIDTH,0), v4=vec2(WIDTH,20), ball = vec2(166,110), star = vec2(WIDTH - 60, 110) },{ -- left wall type = POLYGON,v1=vec2(0,HEIGHT),v2=vec2(0,0),v3=vec2(-2,0), v4=vec2(-2,HEIGHT) } ,{ -- right wall type = POLYGON,v1=vec2(WIDTH+1,HEIGHT),v2=vec2(WIDTH+1,0), v3=vec2(WIDTH,0), v4=vec2(WIDTH,HEIGHT) } }, {-- level 2 {-- first platform type = POLYGON,v1=vec2(WIDTH/2,HEIGHT/3),v2=vec2(WIDTH/2,(HEIGHT/3)-11), v3=vec2((WIDTH/3),(HEIGHT/3)-11), v4=vec2(WIDTH/3,(HEIGHT/3)), ball = vec2(WIDTH/2 - 33,HEIGHT/3), star = vec2(WIDTH / 1.3, HEIGHT/4), }, { -- second platform type = POLYGON,v1=vec2(WIDTH/1.1,HEIGHT/5),v2=vec2(WIDTH/1.1,(HEIGHT/5)-11), v3=vec2((WIDTH/1.6),(HEIGHT/5)-11), v4=vec2(WIDTH/1.6,(HEIGHT/5)), } } , { -- level 3 { -- ground body list type = POLYGON,v1=vec2(0,20),v2=vec2(0,0),v3=vec2(WIDTH,0), v4=vec2(WIDTH,20), ball = vec2(166,110), star = vec2(WIDTH - 60, 110), car = vec2(60,50) },{ -- left wall type = POLYGON,v1=vec2(0,HEIGHT),v2=vec2(0,0),v3=vec2(-2,0), v4=vec2(-2,HEIGHT) } ,{ -- right wall type = POLYGON,v1=vec2(WIDTH+1,HEIGHT),v2=vec2(WIDTH+1,0), v3=vec2(WIDTH,0), v4=vec2(WIDTH,HEIGHT) }, { -- obstacle type = POLYGON,v1=vec2(WIDTH/1.5,66),v2=vec2(WIDTH/1.5,20),v3=vec2(WIDTH/2,20), v4=vec2(WIDTH/2,66) } },{ -- level { -- ground body list type = POLYGON,v1=vec2(0,0),v2=vec2(0,10),v3=vec2(WIDTH,10), v4=vec2(WIDTH,0), ball = vec2(66,HEIGHT), star = vec2(WIDTH - 60, HEIGHT), spring = true, -- star platform --v1=vec2(WIDTH-111,166),v2=vec2(WIDTH-111,120),v3=vec2(WIDTH-33,120), --   v4=vec2(WIDTH-33,166) door = {v1=WIDTH-66,v2=111,v3=33,v4=46} }, { -- ball platform type = POLYGON,v1=vec2(33,166),v2=vec2(33,120),v3=vec2(120,120), v4=vec2(120,166) },{ -- right wall type = POLYGON,v1=vec2(WIDTH+1,HEIGHT/2),v2=vec2(WIDTH+1,0), v3=vec2(WIDTH,-6), v4=vec2(WIDTH,HEIGHT/2) }, { -- obstacle type = POLYGON,v1=vec2(WIDTH/1.3-11,66),v2=vec2(WIDTH/1.3-11,20),v3=vec2(WIDTH/1.2-11,20), v4=vec2(WIDTH/1.2-11,66) } } } function Level:init() self.car = nil self.spring = nil self:load(current_level) end function Level:load(levelnum) if levelnum > #levels then levelnum = 1 end self.car = nil self.spring = nil for i,l in ipairs(levels[levelnum]) do local ground = physics.body(l.type, l.v1, l.v2,l.v3,l.v4) ground.type = STATIC debugDraw:addBody(ground) ground.friction = 1 end -- create game elements self.ball = createCircle( levels[levelnum][1].ball.x,levels[levelnum][1].ball.y ,30) self.star = createCircle( levels[levelnum][1].star.x,levels[levelnum][1].star.y, 30) if levels[levelnum][1].car~=nil then self.car = Car(levels[levelnum][1].car.x, levels[levelnum][1].car.y) end if levels[levelnum][1].spring~=nil then self.spring = Spring(self.ground) if levels[levelnum][1].door then self.spring.door = createBox( levels[levelnum][1].door.v1, levels[levelnum][1].door.v2, levels[levelnum][1].door.v3, levels[levelnum][1].door.v4 ) self.spring.door.type = STATIC debugDraw:addBody(self.spring.door) self.spring.door.friction = 1 end end self.time = os.clock() end function Level:createFirework(pos) self.stars= Firework(vec2(self.star.x,self.star.y)) end function Level:draw() if self.stars then self.stars:draw() if self.stars:isDead() then self.stars:init(self.stars.position) end end if self.spring ~= nil then self.spring:draw() end noTint() pushMatrix() translate(self.ball.x,self.ball.y) rotate(self.ball.angle) sprite("Tyrian Remastered:Dark Orb", 0,0,60) popMatrix() pushMatrix() translate(self.star.x, self.star.y) rotate(self.star.angle) sprite("SpaceCute:Star") popMatrix() end function Level:touched(touch) --createRandPoly(touch.x, touch.y) if self.car ~= nil then self.car:touched(touch) end end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters