Open-world game with only HTML tables and DOM
by MapleKitten
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Table Platformer</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div id="output"></div>
<table id="inventory">
<tbody>
<tr>
<td class="inventoryCell" id="slot1"></td>
<td class="inventoryCell" id="slot2"></td>
</tr>
<tr>
<td class="inventoryCell" id="slot3"></td>
<td class="inventoryCell" id="slot4"></td>
</tr>
<tr>
<td class="inventoryCell" id="slot5"></td>
<td class="inventoryCell" id="slot6"></td>
</tr>
</tbody>
</table>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
.platformerCell {
width: 6px;
height: 6px;
max-width: 6px;
max-height: 6px;
}
.inventoryCell {
border: 1px solid black;
width: 17px;
max-width: 17px;
height: 66px;
box-sizing: border-box;
}
#platformer {
border-collapse: collapse;
border: 1px solid black;
grid-area: 1/2/2/3;
}
#output{
padding: 8px;
background-color: lightgray;
color: black;
border: 1px solid black;
font-family: 'Roboto', sans-serif;
grid-area: 1/1/2/2;
}
#inventory{
padding: 8px;
text-align: center;
font-family: 'Roboto', sans-serif;
font-size: large;
border-spacing: 1em;
border: 1px solid black;
grid-area: 2/1/3/2;
}
#wrapper{
display: grid;
grid-template-columns: 216px 834px;
grid-template-rows: 267px 100px;
grid-column-gap: 0px;
grid-row-gap: 0px;
}
JavaScript
let rows = 69;
let columns = 104;
let matrix = [];
let arr = [];
let horizontalDirection = 1;
let verticalDirection = 0;
let slot = 0;
document.getElementById("slot1").style.border = "3px solid aqua";
let inventory = [
[],
[],
[],
[],
[],
[],
];
// Used in inventory functions to output block names to Inventory GUI
const blockIDs = {
"rgb(125, 231, 5)": "Leaves",
"rgb(66, 41, 26)": "Trunks",
"green": "Grass",
"rgb(101, 67, 33)": "Dirt"
}
function inventoryAddItem(item){
// Searches if item matches any item already in inventory. If not, adds item to first empty slot.
let foundSlot = false;
for(var i = 0; i < inventory.length; i++) {
if(inventory[i][0] == item){
inventory[i].push(item)
document.getElementById("slot"+(i+1)).style.backgroundColor = inventory[i][0] // Adds color of block to slot i
document.getElementById("slot"+(i+1)).innerHTML = blockIDs[inventory[i][0]] + " x"+inventory[i].length // Adds amount of block to slot i
foundSlot = true;
return;
}
}
if(foundSlot == false){
for(var i = 0; i < inventory.length; i++) {
if(inventory[i].length == 0){
inventory[i].push(item)
document.getElementById("slot"+(i+1)).style.backgroundColor = inventory[i][0]
document.getElementById("slot"+(i+1)).innerHTML = blockIDs[inventory[i][0]] + " x"+inventory[i].length
return;
}
}
}
}
function inventoryRemoveItem(item){
// Removes item from inventory.
// If there are still other items present of the same type, then subtract 1 from inventory GUI count of that item.
// If there are no more items of that type left, then remove all background color and text for that slot.
for(var i = 0; i < inventory.length; i++) {
if(inventory[i][0] == item) {
inventory[i].pop()
if(inventory[i].length !== 0){
document.getElementById("slot"+(i+1)).innerHTML =...