Advertisement
Guest User

Platform bros Codea

a guest
Aug 30th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1.  
  2. --# Main
  3. -- PlatformBros
  4.  
  5. -- Use this function to perform your initial setup
  6. function setup()
  7.    
  8.     ground = 100
  9.     x = 100
  10.     y = ground + 25
  11.     velocity = 0
  12.     gravity = -900
  13.    
  14.     speed = 200
  15.     onGround = false
  16.    
  17.     platforms = {}
  18.    
  19.     addPlatform(0, 200, ground)
  20.    
  21.     local sx1 = 200
  22.     local sx2 = 300
  23.     local sy = ground
  24.     for i = 0,100 do
  25.         addPlatform(sx1, sx2, sy)
  26.         sx1 = sx2 + math.random(100, 150)
  27.         sx2 = sx1 + math.random(50, 150)
  28.         sy = sy + math.random(-150, 150)
  29.     end
  30.    
  31. end
  32.  
  33. function addPlatform(x1, x2, y)
  34.     table.insert(platforms, Platform(x1, x2, y))
  35. end
  36.  
  37. -- This function gets called once every frame
  38. function draw()
  39.    
  40.     translate(-x  + 250, -y + 250)
  41.    
  42.     -- This sets a dark background color
  43.     background(40, 40, 50)
  44.  
  45.     -- This sets the line thickness
  46.     strokeWidth(5)
  47.  
  48.     -- Do your drawing here
  49.    
  50.     sprite("Planet Cute:Character Boy",x,y+50,100)
  51.     x = x + DeltaTime * speed
  52.  
  53.     onGround = false
  54.     for k,v in pairs(platforms) do
  55.         v:draw()
  56.        
  57.         if x > v.x1 - 25 and x < v.x2 + 25 and y >= v.y and y + DeltaTime * velocity <= v.y then
  58.             y = v.y
  59.             velocity = 0
  60.             onGround = true
  61.         end
  62.     end    
  63.    
  64.     if not onGround then
  65.         y = y + DeltaTime * velocity  
  66.         velocity = velocity + DeltaTime * gravity
  67.     end
  68.    
  69.    
  70. end
  71.  
  72. function jump()
  73.     if onGround then
  74.         velocity = velocity + 500
  75.     end
  76. end
  77.  
  78. function touched(touch)
  79.     if touch.state == BEGAN then
  80.         jump()
  81.     end
  82. end
  83.  
  84.  
  85. --# Platform
  86. Platform = class()
  87.  
  88. function Platform:init(x1, x2, y)
  89.     -- you can accept and set parameters here
  90.     self.x1 = x1
  91.     self.x2 = x2
  92.     self.y = y
  93. end
  94.  
  95. function Platform:draw()
  96.     -- Codea does not automatically call this method
  97.     stroke(255, 235, 0, 255)
  98.     line(self.x1, self.y, self.x2, self.y)
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement