Piecing together a couple of forum posts I managed to get a scratch card effect working in AppGameKit using Memblocks. Hadn't used Memblocks before so took a little while to get it right.
In this example I'm using two coloured sprites, a yellowy/brown sprite for the background of the card and a grey one on top that acts as the silver part that you scratch. A white rectangle (the coin) follows the mouse and when you press down it rubs out the grey sprite. I've commented out lines that would place images on the sprites and one that rotates the coin. If you uncomment the rotation you will see that it scratches underneath the coin at all angles (using GetSpriteInBox).
It doesn't seem slow when running on the desktop (not tested on a device). It is a rough example and one I may work on some more but wanted to share it in-case anyone else finds it useful as a starting point.
The coin that follows the mouse does lag a little when you move the mouse fast. Also when scratching with a coin only part of the coin would actually scratch the silver in real life because the coin is round so you may want to have two sprites for the coin, one small one that scratches (have it invisible) and one sprite that shows the coin, and then constrain both sprites to the mouse pointer. You'd also want the coin to center on the mouse point with SetSpritePositionByOffset (I got lazy)
silver = CreateSprite(0)
SetSpriteSize(silver, 400, 200)
SetSpriteColor(silver, 186, 182, 183, 255)
//SetSpriteImage(silver, LoadImage("scratch-card-silver.png"))
Render()
silverImage = GetImage(0,0,400,200)
memblock = CreateMemblockFromImage(silverImage)
DeleteSprite(silver)
card = CreateSprite(0)
SetSpriteSize(card, 400, 200)
SetSpriteColor(card, 219, 208, 144, 255)
//SetSpriteImage(card, LoadImage("scratch-card.jpg"))
coin = CreateSprite(0)
SetSpriteSize(coin, 20, 100)
SetSpriteDepth(coin, 1)
CreateImageFromMemblock(1, memblock)
silverSprite = CreateSprite(1)
do
SetSpritePosition(coin, GetPointerX(), GetPointerY())
//SetSpriteAngle(coin, GetSpriteAngle(coin) + 1)
if (GetPointerState() = 1)
for byte = 15 to GetMemblockSize(memblock) Step 4
x = floor(mod(((byte - 12) / 4), GetSpriteWidth(card)))
y = floor((byte - 12) / (GetSpriteWidth(card) * 4))
if (GetSpriteInBox(coin, x, y, x, y))
SetMemblockByte(memblock, byte, 0)
endif
next
if (GetImageExists(1)) then DeleteImage(1)
CreateImageFromMemblock(1, memblock)
if (GetSpriteExists(silverSprite)) then DeleteSprite(silverSprite)
silverSprite = CreateSprite(1)
endif
Sync()
loop
I should probably mention that I've only tested this on a 400x200 image/sprite. I don't know how much it slows down when the sprites are much larger.
Happy to receive any feedback/suggestions on how it can be improved.
EDIT:
A working version of the code using virtual resolution can be found in this comment below:
https://forum.thegamecreators.com/thread/219503#msg2602008
A working version of the code using percentages can be found in this comment below:
https://forum.thegamecreators.com/thread/219503#msg2602021