Skip to content

Instantly share code, notes, and snippets.

@luizbills
Created February 1, 2026 23:20
Show Gist options
  • Select an option

  • Save luizbills/0d60d571b845731532a74176640c0b70 to your computer and use it in GitHub Desktop.

Select an option

Save luizbills/0d60d571b845731532a74176640c0b70 to your computer and use it in GitHub Desktop.
Memory with Litecanvas
litecanvas({})
let cards = [], sprites, opens = [], timer = 0, score = 0
const
SIZE = 64,
OFFSET = SIZE/1.5,
CLOSED_CARD = paint(SIZE, SIZE, () => {
cls(2)
})
function init() {
textfont(PIXEL_FONT_MONOGRAM)
textsize(3)
for (let i = 0; i < 16; i++) {
const sprite = paint(SIZE, SIZE, () => {
cls(1)
text(SIZE/4,SIZE/3, lpad(i,2,'0'))
})
cards.push(
card(sprite),
card(sprite)
)
}
cards = shuffle(cards)
for (let i = 0; i < cards.length; i++) {
cards[i].sprite1.x = cards[i].sprite2.x = OFFSET/2 + (i % 8) * (SIZE + SIZE/4)
cards[i].sprite1.y = cards[i].sprite2.y = OFFSET + floor(i/8) * (SIZE + SIZE/4)
}
}
function update(dt) {
timer -= dt
if (timer <= 0 && opens.length === 2) {
opens[0].open = opens[1].open = false
opens.length = 0
}
}
function tapped(x, y) {
if (opens.length === 2) return
if (timer > 0) return
for (let i = 0; i < cards.length; i++) {
if (cards[i].sprite1.in(x, y)) {
if (!cards[i].open) {
cards[i].open = true
opens.push(cards[i])
}
if (opens.length > 1) {
if (opens[0].img === opens[1].img) {
timer = 0.25
opens.length = 0
score++
sfx()
} else {
timer = 1
}
}
break;
}
}
}
function draw() {
cls(0)
for (let i = 0; i < cards.length; i++) {
if (cards[i].open) {
cards[i].sprite1.draw()
} else {
cards[i].sprite2.draw()
}
}
text(OFFSET/2, 2, 'score: ' + score)
}
function card(img) {
const obj = {
img,
sprite1: new Actor(img),
sprite2: new Actor(CLOSED_CARD),
open: false
}
return obj
}
@luizbills
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment