Advertisement
Guest User

Untitled

a guest
Feb 10th, 2012
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. function love.load()
  2.     text = "Sandbox"
  3.     love.graphics.setBackgroundColor(0, 128, 55)
  4.     -- герой
  5.     hero = {
  6.     x = 16,
  7.     y = 16,
  8.     xstart = x,
  9.     ystart = y,
  10.     speed = 300,
  11.     hp = 100,
  12.     w = 32,
  13.     h = 32,
  14.     }
  15.     -- друг
  16.     friend = {
  17.     x = 320,
  18.     y = 240,
  19.     xstart = x,
  20.     ystart = y,
  21.     speed = 200,
  22.     hp = 100,
  23.     w = 16,
  24.     h = 16,
  25.     ai = 0
  26.     }
  27.     -- выход
  28.     quit = {
  29.     x = love.mouse.getX(),
  30.     y = love.mouse.getY(),
  31.     xstart = x,
  32.     ystart = y,
  33.     w = 16,
  34.     h = 16,
  35.     }
  36. end
  37.  
  38. -- step
  39.  
  40. function love.update(dt)
  41.     if love.keyboard.isDown("escape") then
  42.     love.event.push("q")    -- <  0.8
  43.     love.event.push("quit") -- >= 0.8
  44.     end
  45.     -- функции
  46.     hero_movement(dt)
  47.     friend_movement(dt)
  48.     -- рестарт
  49.     if love.keyboard.isDown("r") then
  50.     hero.x = hero.xstart
  51.     hero.y = hero.ystart
  52.     friend.x = friend.xstart
  53.     friend.y = friend.ystart
  54.     end
  55. end
  56.  
  57. -- draw
  58.  
  59. function love.draw()
  60.     love.graphics.setColor(255, 255, 255, 255)
  61.     love.graphics.print(text, 0, 0)
  62.     -- функции
  63.     hero_draw()
  64.     friend_draw()
  65.     quit_draw()
  66. end
  67.  
  68. -- коллизии
  69.  
  70. function CheckCollision(ax1, ay1, aw, ah, bx1, by1, bw, bh)
  71.     local ax2, ay2, bx2, by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  72.     return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
  73. end
  74.  
  75.     -- герой
  76.  
  77. -- движение
  78.  
  79. function hero_movement(dt)
  80.     -- вверх
  81.     if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
  82.     hero.y = hero.y - hero.speed * dt
  83.     end
  84.     -- вниз
  85.     if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
  86.     hero.y = hero.y + hero.speed * dt
  87.     end
  88.     -- влево
  89.     if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
  90.     hero.x = hero.x - hero.speed * dt
  91.     end
  92.     -- вправо
  93.     if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
  94.     hero.x = hero.x + hero.speed * dt
  95.     end
  96.     -- коллизии
  97.     -- друг
  98.     if CheckCollision(hero.x, hero.y, hero.w, hero.h, friend.x, friend.y, friend.w, friend.h) then
  99.     friend.ai = 1
  100.     end
  101.     -- выход
  102.     if CheckCollision(hero.x, hero.y, hero.w, hero.h, quit.x, quit.y, quit.w, quit.h) then
  103.         if friend.ai == 1 then
  104.         love.graphics.print("You completed the level!", love.mouse.getX(), love.mouse.getY())
  105.         end
  106.     end
  107. end
  108.  
  109. function hero_draw()
  110.     --love.graphics.draw(image, hero.x, hero.y)
  111.     love.graphics.setColor(255, 255, 255, 255)
  112.     love.graphics.rectangle("fill", hero.x, hero.y, hero.w, hero.h)
  113. end
  114.  
  115.     -- друг
  116.  
  117. -- движение
  118.  
  119. function friend_movement(dt)
  120.     -- герой взял к себе в компанию
  121.     if friend.ai == 1 then
  122.         -- влево
  123.         if friend.x > hero.x then
  124.         friend.x = friend.x - friend.speed * dt
  125.         end
  126.         -- вправо
  127.         if friend.x < hero.x then
  128.         friend.x = friend.x + friend.speed * dt
  129.         end
  130.         -- вверх
  131.         if friend.y > hero.y then
  132.         friend.y = friend.y - friend.speed * dt
  133.         end
  134.         -- вниз
  135.         if friend.y < hero.y then
  136.         friend.y = friend.y + friend.speed * dt
  137.         end
  138.     end
  139. end
  140.  
  141. -- рисование
  142.  
  143. function friend_draw()
  144.     --love.graphics.draw(image, hero.x, hero.y)
  145.     love.graphics.setColor(0, 112, 255, 255)
  146.     love.graphics.rectangle("fill", friend.x, friend.y, friend.w, friend.h)
  147.     love.graphics.setColor(255, 255, 255, 255)
  148.     love.graphics.print(friend.ai, 0, 10)
  149. end
  150.  
  151.     -- выход
  152.  
  153. -- рисование
  154.    
  155. function quit_draw()
  156. love.graphics.setColor(0, 0, 0, 255)
  157. love.graphics.rectangle("fill", quit.x, quit.y, quit.w, quit.h)
  158. -- выход
  159.     if CheckCollision(hero.x, hero.y, hero.w, hero.h, quit.x, quit.y, quit.w, quit.h) then
  160.         if friend.ai == 1 then
  161.         love.graphics.print("You completed the level!", love.mouse.getX(), love.mouse.getY() + 20)
  162.         end
  163.     end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement