Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.08 KB | None | 0 0
  1. Gamestate = require "gamestate"
  2.  
  3. menu = {}
  4. game = {}
  5. roll = {}
  6. move = {}
  7.  
  8. local isRolling = true
  9. --local canGo = true
  10. local current_player = 2
  11.  
  12.  
  13. function love.load(args)
  14.    
  15.     love.graphics.setDefaultFilter('nearest', 'nearest')
  16.     tileset = love.graphics.newImage("map_tileset.png")
  17.     black = love.graphics.newImage("black.png")
  18.     white = love.graphics.newImage("white.png")
  19.     font = love.graphics.newFont("zx_spectrum-7.ttf", 64)
  20.     font:setFilter('nearest', 'nearest')
  21.     largefont = love.graphics.newFont("zx_spectrum-7.ttf", 256)
  22.     largefont:setFilter('nearest', 'nearest')
  23.     love.graphics.setFont(font)
  24.     scr_width = love.graphics.getWidth()
  25.    
  26.  
  27.     local tileset_width = tileset:getWidth()
  28.     local tileset_height = tileset:getHeight()
  29.     player_width = black:getWidth()
  30.     player_height = black:getHeight()
  31.  
  32.     love.graphics.setBackgroundColor(102, 153, 153)
  33.  
  34.     width = tileset_width / 2
  35.     height = tileset_height / 2
  36.  
  37.     p1_x = 1
  38.     p1_y = 1
  39.     p2_x = 20
  40.     p2_y = 11
  41.  
  42.     -- 8,8 160,88
  43.  
  44.     quads = {}
  45.  
  46.     for i=0,1 do
  47.         for j=0,1 do
  48.             table.insert( quads, love.graphics.newQuad(width * j, height * i, width, height, tileset_width, tileset_height))
  49.         end
  50.     end
  51.            
  52.     tilemap = {
  53.         {2,2,2,2,2,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0},
  54.         {2,0,0,0,2,0,0,2,2,2,1,1,1,0,1,1,1,1,1,1},
  55.         {2,0,0,0,2,0,2,2,0,0,1,0,1,0,1,0,1,0,1,0},
  56.         {2,2,2,0,2,2,2,0,0,0,1,1,1,1,1,0,1,1,1,0},
  57.         {0,0,2,0,0,2,0,0,0,0,1,0,0,0,1,0,1,0,1,0},
  58.         {0,0,2,0,0,2,2,2,2,2,3,1,1,0,1,0,1,0,1,1},
  59.         {2,2,2,2,2,2,0,2,0,0,2,0,1,0,1,1,1,1,1,0},
  60.         {0,0,2,0,0,0,0,0,0,0,2,0,1,1,1,0,0,0,1,0},
  61.         {0,0,2,0,0,0,2,0,0,2,2,0,0,0,1,0,0,0,1,1},
  62.         {0,2,2,2,2,0,2,0,0,2,0,0,1,1,1,0,0,1,0,1},
  63.         {0,0,0,0,2,2,2,2,2,2,0,1,1,0,1,1,1,1,1,1}
  64.     }
  65.  
  66.     fogofwar = {
  67.         {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  68.         {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  69.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  70.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  71.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  72.         {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
  73.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  74.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  75.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  76.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
  77.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1}
  78.        
  79.        
  80.     }
  81.    
  82.     Gamestate.switch(menu)
  83. end
  84.  
  85. function love.update(dt)
  86.     Gamestate:update(dt)
  87. end
  88.  
  89. function love.keypressed(key)
  90.     Gamestate.keypressed(key)
  91. end
  92.  
  93. function love.draw()
  94.     Gamestate:draw()
  95. end
  96.  
  97. function menu:keypressed(key, code)
  98.     if key == "return" then
  99.         Gamestate.switch(game)
  100.     end
  101. end
  102.  
  103. function menu:draw()
  104.     love.graphics.push("all")
  105.     flash = (love.timer.getTime() % 1) > 1 / 2
  106.     if flash == true then
  107.         love.graphics.setColor(0, 0, 0)
  108.         love.graphics.rectangle("fill", 140, 478, 520, 28)
  109.         love.graphics.setColor(255, 255, 255)
  110.         love.graphics.printf("PRESS ENTER TO START", 0, 450, scr_width, "center")
  111.     elseif flash == false then
  112.         love.graphics.setColor(0, 0, 0)
  113.         love.graphics.printf("PRESS ENTER TO START", 0, 450, scr_width, "center")
  114.     end
  115.     love.graphics.setColor(0, 0, 0)
  116.     love.graphics.printf("NEGATIVE SPACES", 0, 400, scr_width, "center")
  117.     --love.graphics.printf( "PRESS START BUTTON", 0, 500, scr_width, "center")
  118.     love.graphics.pop()
  119.  
  120.     love.graphics.scale(4, 4)
  121.     for i,row in ipairs(tilemap) do  
  122.         for j,tile in ipairs(row) do
  123.                 local v = tilemap[i][j] + 1
  124.                 if fogofwar[i][j] == 1 then
  125.                     love.graphics.draw(tileset, quads[v], j * width, i * height)
  126.                 end
  127.         end
  128.     end
  129.  
  130.    
  131.     love.graphics.draw(white, player_width * p1_x, player_height * p1_y)
  132.     love.graphics.draw(black, player_width * p2_x, player_height * p2_y)
  133. end
  134.  
  135. function game:keypressed(key, code)
  136.     if key == "return" then
  137.         Gamestate.switch(roll)
  138.     end
  139.  
  140.    
  141. end
  142.  
  143. function game:draw()
  144.         love.graphics.push("all")
  145.         love.graphics.setColor(0, 0, 0)
  146.        
  147.         love.graphics.printf("PLAYER " .. current_player .. " - YOUR TURN", 0, 400, scr_width, "center")
  148.         flash = (love.timer.getTime() % 1) > 1 / 2
  149.         if flash == true then
  150.             love.graphics.setColor(0, 0, 0)
  151.             love.graphics.rectangle("fill", 65, 458, 670, 28)
  152.             love.graphics.setColor(255, 255, 255)
  153.             love.graphics.printf("PRESS ENTER TO ROLL A DICE", 0, 430, scr_width, "center")
  154.         elseif flash == false then
  155.             love.graphics.setColor(0, 0, 0)
  156.             love.graphics.printf("PRESS ENTER TO ROLL A DICE", 0, 430, scr_width, "center")
  157.         end
  158.        
  159.         love.graphics.pop()
  160.  
  161.         love.graphics.scale(4, 4)
  162.         for i,row in ipairs(tilemap) do  
  163.             for j,tile in ipairs(row) do
  164.                     local v = tilemap[i][j] + 1
  165.                     if fogofwar[i][j] == 1 then
  166.                         love.graphics.draw(tileset, quads[v], j * width, i * height)
  167.                     end
  168.             end
  169.         end
  170.  
  171.        
  172.         love.graphics.draw(white, player_width * p1_x, player_height * p1_y)
  173.         love.graphics.draw(black, player_width * p2_x, player_height * p2_y)
  174. end
  175.  
  176. function roll:keypressed(key, code)
  177.     if key == 'return' and isRolling then
  178.         isRolling = false
  179.     elseif key == 'return' and isRolling == false then
  180.         Gamestate.switch(move)
  181.     end
  182. end
  183.  
  184. function roll:draw()
  185.         love.graphics.push("all")
  186.         love.graphics.setColor(0, 0, 0)
  187.        
  188.         --love.graphics.printf("ROLL A DICE!", 0, 400, scr_width, "center")
  189.         love.graphics.setFont(largefont)
  190.  
  191.         if isRolling then
  192.             number = love.math.random(6)
  193.         end
  194.         love.graphics.printf(number, 0, 290, scr_width, "center")
  195.        
  196.  
  197.         if isRolling == false then
  198.             love.graphics.setFont(font)
  199.             flash = (love.timer.getTime() % 1) > 1 / 2
  200.         if flash == true then
  201.             love.graphics.setColor(0, 0, 0)
  202.             love.graphics.rectangle("fill", 100, 528, 600, 28)
  203.             love.graphics.setColor(255, 255, 255)
  204.             love.graphics.printf("PRESS ENTER TO CONTINUE", 0, 500, scr_width, "center")
  205.         elseif flash == false then
  206.             love.graphics.setColor(0, 0, 0)
  207.             love.graphics.printf("PRESS ENTER TO CONTINUE", 0, 500, scr_width, "center")
  208.         end
  209.         end
  210.  
  211.         love.graphics.pop()
  212.        
  213.         love.graphics.scale(4, 4)
  214.         for i,row in ipairs(tilemap) do  
  215.             for j,tile in ipairs(row) do
  216.                     local v = tilemap[i][j] + 1
  217.                     if fogofwar[i][j] == 1 then
  218.                         love.graphics.draw(tileset, quads[v], j * width, i * height)
  219.                     end
  220.             end
  221.         end
  222.  
  223.        
  224.         love.graphics.draw(white, player_width * p1_x, player_height * p1_y)
  225.         love.graphics.draw(black, player_width * p2_x, player_height * p2_y)
  226. end
  227.  
  228. function move:keypressed(key, code)
  229.     if current_player == 1 and number > 0 then
  230.         local nextX = p1_x
  231.         local nextY = p1_y
  232.      
  233.         if key == "w" then
  234.             nextY = nextY - 1
  235.         elseif key == "s" then
  236.             nextY = nextY + 1
  237.         elseif key == "a" then
  238.             nextX = nextX - 1
  239.         elseif key == "d" then
  240.             nextX = nextX + 1
  241.         end
  242.      
  243.         if tilemap[nextY] and tilemap[nextY][nextX] and (tilemap[nextY][nextX] == 2 or tilemap[nextY][nextX] == 3) then -- not a wall, move the player
  244.             p1_x = nextX
  245.             p1_y = nextY
  246.             number = number - 1
  247.             fow()
  248.      
  249.             if tilemap[p1_y][p1_x] == 3 then --finish!
  250.                 print("Player 1 - You win!")
  251.             end
  252.         end
  253.     elseif current_player == 2 and number > 0 then
  254.             local p2nextX = p2_x
  255.             local p2nextY = p2_y
  256.          
  257.             if key == "up" then
  258.                 p2nextY = p2nextY - 1
  259.             elseif key == "down" then
  260.                 p2nextY = p2nextY + 1
  261.             elseif key == "left" then
  262.                 p2nextX = p2nextX - 1
  263.             elseif key == "right" then
  264.                 p2nextX = p2nextX + 1
  265.             end
  266.          
  267.             if tilemap[p2nextY] and tilemap[p2nextY][p2nextX] and (tilemap[p2nextY][p2nextX] == 1 or tilemap[p2nextY][p2nextX] == 3) then -- not a wall, move the player
  268.                 p2_x = p2nextX
  269.                 p2_y = p2nextY
  270.                 number = number - 1
  271.                 fow()
  272.          
  273.                 if tilemap[p2_y][p2_x] == 3 then --finish!
  274.                     print("Player 2 - You win!")
  275.                 end
  276.             end
  277.     end
  278. end
  279.  
  280. function move:draw()
  281.     love.graphics.push("all")
  282.         flash = (love.timer.getTime() % 1) > 1 / 2
  283.         if flash == true and number == 0 then
  284.             love.graphics.setColor(0, 0, 0)
  285.             love.graphics.rectangle("fill", 180, 528, 440, 28)
  286.             love.graphics.setColor(255, 255, 255)
  287.             love.graphics.printf("IS THAT OKAY? Y/N", 0, 500, scr_width, "center")
  288.         elseif flash == false and number == 0 then
  289.             love.graphics.setColor(0, 0, 0)
  290.             love.graphics.printf("IS THAT OKAY? Y/N", 0, 500, scr_width, "center")
  291.         end
  292.    
  293.         love.graphics.setColor(0, 0, 0)
  294.         love.graphics.setFont(largefont)
  295.         love.graphics.printf(number, 0, 290, scr_width, "center")
  296.  
  297.    
  298.     love.graphics.pop()
  299.    
  300.     love.graphics.scale(4, 4)
  301.         for i,row in ipairs(tilemap) do  
  302.             for j,tile in ipairs(row) do
  303.                     local v = tilemap[i][j] + 1
  304.                     if fogofwar[i][j] == 1 then
  305.                         love.graphics.draw(tileset, quads[v], j * width, i * height)
  306.                     end
  307.             end
  308.         end
  309.  
  310.        
  311.         love.graphics.draw(white, player_width * p1_x, player_height * p1_y)
  312.         love.graphics.draw(black, player_width * p2_x, player_height * p2_y)
  313.        
  314. end
  315.  
  316.  
  317. function fow()
  318.     -- updating fog of war
  319.     fogofwar[p1_y][p1_x - 1] = 1 -- while moving left
  320.     fogofwar[p1_y][p1_x + 1] = 1 -- while moving right
  321.     if p1_y > 1 then
  322.     fogofwar[p1_y - 1][p1_x] = 1 -- while moving up
  323.     end
  324.     if p1_y < 11 then
  325.     fogofwar[p1_y + 1][p1_x] = 1 -- while moving down
  326.     end
  327.    
  328.  
  329.     fogofwar[p2_y][p2_x - 1] = 1 -- while moving left
  330.     fogofwar[p2_y][p2_x + 1] = 1 -- while moving right
  331.     if p2_y > 1 then
  332.     fogofwar[p2_y - 1][p2_x] = 1 -- while moving up
  333.     end
  334.     if p2_y < 11 then
  335.     fogofwar[p2_y + 1][p2_x] = 1 -- while moving down
  336.     end
  337.    
  338. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement