Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- text = "Sandbox"
- love.graphics.setBackgroundColor(0, 128, 55)
- -- герой
- hero = {
- x = 16,
- y = 16,
- xstart = x,
- ystart = y,
- speed = 300,
- hp = 100,
- w = 32,
- h = 32,
- }
- -- друг
- friend = {
- x = 320,
- y = 240,
- xstart = x,
- ystart = y,
- speed = 200,
- hp = 100,
- w = 16,
- h = 16,
- ai = 0
- }
- -- выход
- quit = {
- x = love.mouse.getX(),
- y = love.mouse.getY(),
- xstart = x,
- ystart = y,
- w = 16,
- h = 16,
- }
- end
- -- step
- function love.update(dt)
- if love.keyboard.isDown("escape") then
- love.event.push("q") -- < 0.8
- love.event.push("quit") -- >= 0.8
- end
- -- функции
- hero_movement(dt)
- friend_movement(dt)
- -- рестарт
- if love.keyboard.isDown("r") then
- hero.x = hero.xstart
- hero.y = hero.ystart
- friend.x = friend.xstart
- friend.y = friend.ystart
- end
- end
- -- draw
- function love.draw()
- love.graphics.setColor(255, 255, 255, 255)
- love.graphics.print(text, 0, 0)
- -- функции
- hero_draw()
- friend_draw()
- quit_draw()
- end
- -- коллизии
- function CheckCollision(ax1, ay1, aw, ah, bx1, by1, bw, bh)
- local ax2, ay2, bx2, by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
- return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
- end
- -- герой
- -- движение
- function hero_movement(dt)
- -- вверх
- if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
- hero.y = hero.y - hero.speed * dt
- end
- -- вниз
- if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
- hero.y = hero.y + hero.speed * dt
- end
- -- влево
- if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
- hero.x = hero.x - hero.speed * dt
- end
- -- вправо
- if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
- hero.x = hero.x + hero.speed * dt
- end
- -- коллизии
- -- друг
- if CheckCollision(hero.x, hero.y, hero.w, hero.h, friend.x, friend.y, friend.w, friend.h) then
- friend.ai = 1
- end
- -- выход
- if CheckCollision(hero.x, hero.y, hero.w, hero.h, quit.x, quit.y, quit.w, quit.h) then
- if friend.ai == 1 then
- love.graphics.print("You completed the level!", love.mouse.getX(), love.mouse.getY())
- end
- end
- end
- function hero_draw()
- --love.graphics.draw(image, hero.x, hero.y)
- love.graphics.setColor(255, 255, 255, 255)
- love.graphics.rectangle("fill", hero.x, hero.y, hero.w, hero.h)
- end
- -- друг
- -- движение
- function friend_movement(dt)
- -- герой взял к себе в компанию
- if friend.ai == 1 then
- -- влево
- if friend.x > hero.x then
- friend.x = friend.x - friend.speed * dt
- end
- -- вправо
- if friend.x < hero.x then
- friend.x = friend.x + friend.speed * dt
- end
- -- вверх
- if friend.y > hero.y then
- friend.y = friend.y - friend.speed * dt
- end
- -- вниз
- if friend.y < hero.y then
- friend.y = friend.y + friend.speed * dt
- end
- end
- end
- -- рисование
- function friend_draw()
- --love.graphics.draw(image, hero.x, hero.y)
- love.graphics.setColor(0, 112, 255, 255)
- love.graphics.rectangle("fill", friend.x, friend.y, friend.w, friend.h)
- love.graphics.setColor(255, 255, 255, 255)
- love.graphics.print(friend.ai, 0, 10)
- end
- -- выход
- -- рисование
- function quit_draw()
- love.graphics.setColor(0, 0, 0, 255)
- love.graphics.rectangle("fill", quit.x, quit.y, quit.w, quit.h)
- -- выход
- if CheckCollision(hero.x, hero.y, hero.w, hero.h, quit.x, quit.y, quit.w, quit.h) then
- if friend.ai == 1 then
- love.graphics.print("You completed the level!", love.mouse.getX(), love.mouse.getY() + 20)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement