Skip to content

Commit

Permalink
Merge pull request #28 from tomaustn/patch-1
Browse files Browse the repository at this point in the history
Create walkies.js
  • Loading branch information
mavi0 committed Jun 1, 2024
2 parents 65906e7 + e956382 commit 08d94c7
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions web/priv/js_effects/walkies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
return class MyEffect {
constructor(display) {
this.display = display;
this.frame = 0;
this.speed = 3; // speed
this.#clear();
}

#clear() {
for (let x = 0; x < this.display.width; x++) {
for (let y = 0; y < this.display.height; y++) {
if (y >= Math.floor(this.display.height * 0.75)) {
// grass
this.display.setPixel(x, y, [0, 255, 0]);
} else {
// sky
this.display.setPixel(x, y, [135, 206, 235]);
}
}
}

const sunRadius = 5;
const sunCenterX = this.display.width - sunRadius - 1;
const sunCenterY = sunRadius + 1;

// sun
for (let i = -sunRadius; i <= sunRadius; i++) {
for (let j = -sunRadius; j <= sunRadius; j++) {
if (i * i + j * j <= sunRadius * sunRadius) {
const targetX = sunCenterX + i;
const targetY = sunCenterY + j;
if (targetX >= 0 && targetX < this.display.width && targetY >= 0 && targetY < this.display.height) {
this.display.setPixel(targetX, targetY, [255, 233, 81]);
}
}
}
}

// Drawing the clouds
this.#drawCloud(10, 5);
this.#drawCloud(34, 15);
this.#drawCloud(70, 6);
this.#drawCloud(90, 18);
}

#drawCloud(x, y) {
const CLOUD_PIXEL_MAP = [
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]
];

const CLOUD_COLOR = [255, 255, 255]; // cloud

for (let i = 0; i < CLOUD_PIXEL_MAP.length; i++) {
for (let j = 0; j < CLOUD_PIXEL_MAP[i].length; j++) {
if (CLOUD_PIXEL_MAP[i][j]) {
const targetX = x + j;
const targetY = y + i;

if (targetX >= 0 && targetX < this.display.width && targetY >= 0 && targetY < this.display.height) {
this.display.setPixel(targetX, targetY, CLOUD_COLOR);
}
}
}
}
}

#drawCoco(x, y) {
const COCO_PIXEL_MAP_16 = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
];

const COCO_COLOR = [232, 190, 73];

for (let i = 0; i < COCO_PIXEL_MAP_16.length; i++) {
for (let j = 0; j < COCO_PIXEL_MAP_16[i].length; j++) {
if (COCO_PIXEL_MAP_16[i][j]) {
const targetX = x + j;
const targetY = y + i;

if (targetX >= 0 && targetX < this.display.width && targetY >= 0 && targetY < this.display.height) {
this.display.setPixel(targetX, targetY, COCO_COLOR);
}
}
}
}
}

update() {
this.#clear();

const y = Math.floor(this.display.height * 0.75) - 16; // dogheight = 16
const x = ((Math.floor(this.frame / this.speed)) % (this.display.width + 16)) - 16; // check head on screen whilst running

this.#drawCoco(x, y);

this.display.flush();
this.frame++;
}
}

0 comments on commit 08d94c7

Please sign in to comment.