main.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. player = {}
  2. fish = {}
  3. cwide = 520
  4. chigh = 333
  5. love.window.setTitle(' Hello Game Wörld ')
  6. love.window.setMode(cwide, chigh)
  7. function love.load()
  8. fish.x = 0
  9. fish.y = 0
  10. fish.img = love.graphics.newImage( 'images/fish.png' )
  11. fish.wide = fish.img:getWidth()
  12. fish.high = fish.img:getHeight()
  13. player.x = 150
  14. player.y = 150
  15. player.img = love.graphics.newImage('images/tux.png')
  16. player.wide = player.img:getWidth()
  17. player.high = player.img:getHeight()
  18. player.speed = 10
  19. end
  20. function love.update(dt)
  21. if love.keyboard.isDown("right") then
  22. if player.x <= (love.graphics.getWidth() - player.img:getWidth()) then
  23. player.x = player.x+player.speed
  24. rotate_right()
  25. end
  26. elseif love.keyboard.isDown("left") then
  27. if player.x <= (love.graphics.getWidth() + player.wide - player.img:getWidth()) then
  28. player.x = player.x-player.speed
  29. rotate_left()
  30. end
  31. elseif love.keyboard.isDown("up") then
  32. player.y = player.y-player.speed
  33. elseif love.keyboard.isDown("down") then
  34. player.y = player.y+player.speed
  35. end
  36. if CheckCollision(fish.x,fish.y,151,61, player.x,player.y,player.img:getWidth(),player.img:getHeight() ) then
  37. fdead()
  38. else
  39. falive()
  40. end
  41. automove(fish,1,0,fish.wide,fish.high)
  42. end
  43. function love.draw()
  44. love.graphics.draw(player.img,player.x,player.y,0,1,1,0, 0)
  45. love.graphics.draw(fish.img, fish.x, fish.y, 0, 1, 1, 0, 0)
  46. end
  47. function automove(obj,x,y,ox,oy)
  48. if obj.x == cwide-fish.img:getWidth() then
  49. edgeright = 0
  50. elseif obj.x == 0 then
  51. edgeright = 1
  52. end
  53. if edgeright == 1 then
  54. obj.x = obj.x + x
  55. else
  56. obj.x = obj.x - x
  57. end
  58. end
  59. function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  60. return x1 < x2+w2 and
  61. x2 < x1+w1 and
  62. y1 < y2+h2 and
  63. y2 < y1+h1
  64. end
  65. function rotate_left()
  66. player.img = love.graphics.newImage('images/tuxleft.png')
  67. end
  68. function rotate_right()
  69. player.img = love.graphics.newImage('images/tux.png' )
  70. end
  71. function falive()
  72. fish.img = love.graphics.newImage('images/fish.png')
  73. end
  74. function fdead()
  75. fish.img = love.graphics.newImage('images/fishbones.png')
  76. end