JSFiddle - React, Tailwind, and code Playground
HTML
<html lang="en">
<style>
#textScreen { border: 1px solid #000; width: 400px; height: 300px; white-space: pre; font-family: monospace; }
#graphicScreen { border: 1px solid #000; }
</style>
<div id="textScreen"></div>
<canvas id="graphicScreen" width="400" height="300"></canvas>
<script>
(function () {
class Dog {
constructor(strength, name) {
this.strength = strength;
this.name = name;
}
bark() {
return 'b' + 'a'.repeat(this.strength) + 'rk';
}
getName() {
return this.name;
}
}
class Message {
constructor(text) {
this.text = text;
}
}
class ObjectPosition {
constructor(objectReference, x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.objectReference = objectReference;
}
}
class World {
constructor(width, height) {
this.width = width;
this.height = height;
this.objectPositions = [];
this.currentDogDescriptor = null;
this.dogCounter = 0;
this.step = 8;
}
addNewDog() {
this.dogCounter++;
let dog = new Dog(this.rand(1, 3), 'dog ' + this.dogCounter);
let objectPos = new ObjectPosition(
dog,
this.rand(0, Math.floor(this.width / this.step)) * this.step,
this.rand(0, Math.floor(this.height / this.step)) * this.step,
90,
80,
);
this.ensureCoords(objectPos);
this.objectPositions.push(objectPos);
this.currentDogDescriptor = objectPos;
}
rand(min, max) {
return Math.floor(min + Math.random() * (max - min + 1));
}
...