Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateMap(width, height, monsters)
- map = {} -- map content
- map_visibility = {} -- map visibility
- for y=1, height do
- table.insert(map, {})
- table.insert(map_visibility, {})
- for x=1, width do
- table.insert(map[#map], 0)
- table.insert(map_visibility[#map_visibility], 0)
- end
- end
- for id=1, 2 do -- for player id 1 and 2
- for n=1, monsters do -- for n monsters
- while true do -- find random position
- x = math.random(width)
- y = math.random(height)
- if map[y][x] == 0 then break end
- end
- map[y][x] = id -- set it
- end
- end
- end
- function love.load()
- generateMap(5, 5, 2) -- 5x5 map with 2 monsters per player
- turn = math.random(2) -- random start
- nextTurn() -- for message
- end
- function nextTurn()
- turn = turn + 1
- if turn > 2 then
- turn = 1
- end
- selection = { x = -1, y = -1 }
- print("PLAYER " .. tostring(turn) .. "'s TURN NOW")
- end
- function love.draw()
- for y=1, #map do
- for x=1, #map[y] do
- if map_visibility[y][x] == 0 then
- love.graphics.setColor(255, 255, 255, 255)
- love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
- else
- love.graphics.setColor(255, 255, 255, 255) -- empty field (white)
- if map[y][x] == 1 then -- player 1 monster (red)
- love.graphics.setColor(255, 0, 0, 255)
- elseif map[y][x] == 2 then -- player 2 monster (blue)
- love.graphics.setColor(0, 0, 255, 255)
- end
- love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
- end
- end
- end
- end
- function love.mousepressed(x, y, button)
- x = math.floor(x / 32) -- round down to get "real" coordinates
- y = math.floor(y / 32)
- if y >= 1 and y <= #map and x >= 1 and x <= #map[y] then -- check if valid
- if button == "l" then -- left mouse button
- if selection.x ~= -1 and map[y][x] ~= turn and map_visibility[y][x] == 1 then
- if x >= selection.x - 1 and x <= selection.x + 1 and y >= selection.y - 1 and y <= selection.y + 1 then
- map[y][x] = map[selection.y][selection.x] -- moved if a monster was selected
- map[selection.y][selection.x] = 0
- nextTurn()
- else
- print("Illegal move!")
- end
- elseif map_visibility[y][x] == 0 then
- map_visibility[y][x] = 1 -- set visibile if hidden
- nextTurn()
- elseif map[y][x] ~= 0 then
- if map[y][x] == turn then
- selection.x = x
- selection.y = y
- else
- print("You cannot move another player's monster")
- end
- end
- elseif button == "r" then -- right mouse button
- if map_visibility[y][x] == 0 then
- if map[y][x] == 0 then
- print("Field: EMPTY")
- else
- print("Field: PLAYER " + tostring(map[y][x]) + " MONSTER")
- end
- nextTurn()
- end
- end
- else
- selection = { x = -1, y = -1 }
- end
- end
Add Comment
Please, Sign In to add comment