To create a similar type game where users can add, remove, and move cars, motorcycles, and trucks of different sizes around, you can use HTML, CSS, and JavaScript. Below is an example implementation:
HTML (play.html):
<!DOCTYPE html>
<html>
<head>
<title>Parking Lot Game</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Parking Lot Game</h1>
<div id="parkingLot">
<!-- Dynamically generated vehicle elements will be added here -->
</div>
<script src="script.js"></script>
</body>
</html>
CSS (style.css):
#parkingLot {
width: 500px;
height: 300px;
border: 1px solid #000;
position: relative;
}
.vehicle {
position: absolute;
border: 1px solid #000;
text-align: center;
cursor: move;
}
.car {
width: 100px;
height: 40px;
background-color: #F44336;
}
.motorcycle {
width: 50px;
height: 30px;
background-color: #4CAF50;
}
.truck {
width: 150px;
height: 60px;
background-color: #2196F3;
}
JavaScript (script.js):
// Vehicle data
var vehicles = [];
// Vehicle class
function Vehicle(type, width, height, posX, posY) {
this.type = type;
this.width = width;
this.height = height;
this.posX = posX;
this.posY = posY;
}
// Add a new vehicle to the parking lot
function addVehicle(type) {
var width, height;
switch (type) {
case 'car':
width = 100;
height = 40;
break;
case 'motorcycle':
width = 50;
height = 30;
break;
case 'truck':
width = 150;
height = 60;
break;
default:
return;
}
var vehicle = new Vehicle(type, width, height, 0, 0);
vehicles.push(vehicle);
renderParkingLot();
}
// Remove a vehicle from the parking lot
function removeVehicle(index) {
vehicles.splice(index, 1);
renderParkingLot();
}
// Move a vehicle within the parking lot
function moveVehicle(index, deltaX, deltaY) {
vehicles[index].posX += deltaX;
vehicles[index].posY += deltaY;
renderParkingLot();
}
// Render the parking lot with vehicles
function renderParkingLot() {
var parkingLot = document.getElementById('parkingLot');
parkingLot.innerHTML = '';
vehicles.forEach(function(vehicle, index) {
var vehicleElem = document.createElement('div');
vehicleElem.classList.add('vehicle', vehicle.type);
vehicleElem.style.width = vehicle.width + 'px';
vehicleElem.style.height = vehicle.height + 'px';
vehicleElem.style.left = vehicle.posX + 'px';
vehicleElem.style.top = vehicle.posY + 'px';
vehicleElem.addEventListener('mousedown', function(e) {
if (e.button === 0) {
var startX = e.clientX;
var startY = e.clientY;
var moveHandler = function(e) {
var deltaX = e.clientX - startX;
var deltaY = e.clientY - startY;
moveVehicle(index, deltaX, deltaY);
startX = e.clientX;
startY = e.clientY;
};
var upHandler = function() {
document.removeEventListener('mousemove', moveHandler);
document.removeEventListener('mouseup', upHandler);
};
document.addEventListener('mousemove', moveHandler);
document.addEventListener('mouseup', upHandler);
}
});
parkingLot.appendChild(vehicleElem);
});
}
To begin, create a new directory on your system and save the above code in separate files: play.html, style.css, and script.js. Then, open the play.html file in a web browser, and you should see the Parking Lot Game.
The game allows users to add new vehicles (car, motorcycle, or truck) to the parking lot by clicking corresponding buttons outside the parking lot area. Users can also remove vehicles by clicking on them within the parking lot area, and move them around by clicking and dragging on the vehicles.
Disclaimer: This is a simplified example, and collision detection or advanced game rules like vehicles not being able to overlap completely have not been implemented.