Skip to content

Instantly share code, notes, and snippets.

@flaki
Created March 10, 2018 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaki/df339839fa5a6510b3e342492ae72476 to your computer and use it in GitHub Desktop.
Save flaki/df339839fa5a6510b3e342492ae72476 to your computer and use it in GitHub Desktop.
Tamagotchi with MicroCanvas
"use strict";
let game = new MicroCanvas();
let gfxGotchi, gfxGotchiFlip, gfxBone;
game.setup(function(game) {
gfxGotchi = game.loadSprite(`! gotchi 16x16
##............##
.#.....###.#####
.##......###.#.#
.##.......#####.
..#......#####.#
..#.......##..#.
..##.....#####..
..##########....
.##..###.####...
##..#########...
.#####...###....
.####....###....
##.##...##.##...
##..##..##.###..
.##.............
................`);
gfxGotchiFlip = game.loadSprite(`! gotchiflip 16x16
..#...........##
.##.###.....####
.####........###
#.####.......##.
.##.##........##
#####.........#.
#####....#.###..
..##########....
.###.###...##...
.########.###...
.#####..#####...
.####...#####...
..###...##.##...
.##.##..##.##...
###.............
................`);
gfxBone = game.loadSprite(`! bone 10x10
..........
..###.....
##..#.....
#...#.....
.###.####.
...###..#.
....#.##..
..###.##..
...###....
..........`);
y = game.height - gfxGotchi.height;
});
let x = 0, y = 0;
let sx = 1, sy = 1;
let boney = 0;
let bonefallspeed = 0;
let hungry = 0;
let animationSpeed = 8;
game.loop(function() {
if (game.everyXFrames(10)) {
hungry = hungry + 1;
}
if (hungry < 100) {
x = x + sx;
}
if (bonefallspeed > 0) {
boney = boney + bonefallspeed;
}
if (x > game.width - gfxGotchi.width) {
x = game.width -gfxGotchi.width;
sx = -1;
}
if (x < 0) {
x = 0;
sx = 1;
}
if (game.detectCollision(gfxGotchi,x,y, gfxBone, 50, boney)) {
hungry = hungry - 20;
boney = 0;
bonefallspeed = 0;
}
// Clear display, redraw background text
game.clear();
game.centerText(`${hungry}`, 20,0);
// Draw gotchi
if (sx === 1) {
game.drawImage(gfxGotchi, x,y);
} else {
game.drawImage(gfxGotchiFlip, x,y);
}
game.drawImage(gfxBone, 50, boney);
if (game.buttonPressed('enter')) {
bonefallspeed = 2;
}
if (game.everyXFrames(120)) {
if (boney > game.height) {
boney = 0;
bonefallspeed = 0;
}
}
// Screen updates automatically at the end of loop()
});
console.log("MicroCanvas initialized");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment