Developing Asteroids: a series.

thanks! i think it went well.

@RonJeffries I was looking at your Space Invaders page where you were showing the hex strings to make the aliens. I thought I’d throw together some code to convert the hex strings into bitmaps/sprites. You can change their size using the parameter slider for the scale command.

displayMode(STANDARD)

function setup()
    parameter.integer("sc",1,15,5)
    
    hex1={0x00,0x00,0x39,0x79,0x7A,0x6E,0xEC,0xFA,0xFA,0xEC,0x6E,0x7A,0x79,0x39}
    hex2={0x00,0x00,0x00,0x78,0x1D,0xBE,0x6C,0x3C,0x3C,0x3C,0x6C,0xBE,0x1D,0x78}
    hex3={0x00,0x00,0x00,0x00,0x19,0x3A,0x6D,0xFA,0xFA,0x6D,0x3A,0x19,0x00,0x00}
    hex4={0x00,0x0F,0x1F,0x1F,0x1F,0x1F,0x7F,0xFF,0x7F,0x1F,0x1F,0x1F,0x1F,0x0F}
    
    map1=image(16,8)
    map2=image(16,8)
    map3=image(16,8)
    map4=image(16,8)
    
    bitMap(hex1,map1)
    bitMap(hex2,map2)
    bitMap(hex3,map3)
    bitMap(hex4,map4)
end

function bitMap(hexString,img)
    for a=1,#hexString do
        for z=0,7 do
            if (hexString[a]>>z)&1==1 then
                img:set(a,z+1,255,255,255,255)
            end
        end 
    end   
end

function draw()
    background(0)
    noSmooth()
    fontSize(30)
    fill(255)
    text("Scale  "..sc,WIDTH/2,HEIGHT-100)
    scale(sc,sc)
    sprite(map1,WIDTH/2/sc,HEIGHT*.7/sc)
    sprite(map2,WIDTH/2/sc,HEIGHT*.5/sc)
    sprite(map3,WIDTH/2/sc,HEIGHT*.3/sc)
    sprite(map4,WIDTH/2/sc,HEIGHT*.1/sc)
end

very nice, thanks!

@RonJeffries I added noSmooth() to the above code in the draw function, so the sprites are sharper as they’re scaled up.

Thanks! I’ll be writing about this in just a few minutes. :slight_smile:

@dave1707 https://ronjeffries.com/articles/020-invaders/i-03/
Thanks!

@RonJeffries Thanks for the mention in your Space Invaders write up.

thanks for the help!

Yes. For a one-off it seemed the prudent thing to do :slight_smile:

@RonJeffries Don’t know if you could use this or not. It allows you to resize a sprite/bitMap to whatever size you want. This might allow you to write your Space Invaders without having to use the scale command. It might make the calculations easier not having to account for scaling.

displayMode(FULLSCREEN)

function setup()
    size=4
    saveName="Invader1"
    img=readImage(asset.documents.Dropbox.inv1)
    
    fill(255)   
    fontSize(20)
    
    newImage=resize(size,img) 
    
    saveImage(asset.documents.Dropbox..saveName,newImage)
end

function draw()
    background(95, 70, 32)
    sprite(img,WIDTH/2,HEIGHT/2+200)
    sprite(newImage,WIDTH/2,HEIGHT/2)
    text("x  1",100,HEIGHT/2+200)
    text("x  "..size,100,HEIGHT/2)
end

function resize(size,bitMap)
    local iw=bitMap.width
    local ih=bitMap.height
    local img=image(iw*size,ih*size)
    for x=0,iw-1 do
        for y=0,ih-1 do
            local r,g,b,a=bitMap:get(x+1,y+1)
            for x1=1,size do
                for y1=1,size do
                    img:set(x1+x*size,y1+y*size,r,g,b,a)                    
                end
            end
        end
    end
    return(img)
end

@RonJeffries Posted the wrong code before. Here’s the correct code. Since the values are 16 bit instead of 8 bit, for my code to work right, every other value needs to be in front of the previous value to create the 16 bit value.

PS. Looked at your article again and noticed you were able to hack the original code.

displayMode(STANDARD)

function setup()
    parameter.integer("sc",1,15,5)

    hex5={0x0FFF,0x1FFF,0x3FFF,0x7FFF,0xFFFF,0xFFFC,0xFFF8,0xFFF0,0xFFF0,0xFFF0,0xFFF0, 
0xFFF0,0xFFF0,0xFFF0,0xFFF8,0xFFFC,0xFFFF,0xFFFF,0x7FFF,0x3FFF,0x1FFF,0x0FFF}

    map5=image(22,16)    
    bitMap(hex5,map5)
end

function bitMap(hexString,img)
    for a=1,#hexString do
        for z=0,15 do
            if (hexString[a]>>z)&1==1 then
                img:set(a,z+1,255,255,255,255)
            end
        end 
    end   
end

function draw()
    background(0)
    noSmooth()
    fontSize(30)
    fill(255)
    text("Scale  "..sc,WIDTH/2,HEIGHT-100)
    scale(sc,sc)
    sprite(map5,WIDTH/2/sc,HEIGHT*.5/sc)
end


--[[

Original values


1D20: FF 0F FF 1F FF 3F FF 7F FF FF FC FF F8 FF F0 FF F0 FF F0 FF F0 FF
1D36: F0 FF F0 FF F0 FF F8 FF FC FF FF FF FF FF FF 7F FF 3F FF 1F FF 0F
--]]

@RonJeffries I thought I was doing something wrong when I was creating the bitMap of the Shield. I noticed that it’s not symmetrical. The left base is smaller than the right base. The left base is 5 bytes wide while the right base is 6 bytes wide. I’m not sure if that was noticeable in the original game.

@RonJeffries The latest zip file you show for Space Invaders 9 takes me to an I’m sorry, I can’t find that page message.

Thanks for resize. I think scaling will allow me to do everything in raw historical coordinates 224x256. We’ll see. Zip file was named invaders not Invaders, will upload in a moment.

Didn’t notice the asymmetry, that’s interesting.

Version 10 has the code explicitly. I’ve not uploaded the images.

I’m not having as much fun as I was with asteroids. Early days tho. :slight_smile:

Ha! Bitmap images takes me back. That’s how we did fonts in Codea before they implemented text natively. I have a whole library of code for creating sprites for characters using a bitmap font.

i almost wish i could blit to an image fast enough to do it that way. almost.

@RonJeffries - blit? Are you showing your past encounters with the blitter chip on the Amiga?

blit predates even that :slight_smile:

@RonJeffries I was looking at you last Space Invaders article about hitting the Shields. I had nothing better to do so I thought I’d try something. Here’s some code I came up with.

displayMode(STANDARD)

function setup()
    fill(255,0,0)
    rectMode(CENTER)
    spriteMode(CENTER)
    mx,my=WIDTH//2,-20
    vy=0 
    
    -- shield sprite is 88 wide, 64 high  (4x normal size)
    -- shield edges, left, right, bottom, top
    shield1=readImage(asset.documents.Dropbox.shield)
    sl=WIDTH//2-44
    sr=WIDTH//2+44
    sb=HEIGHT//2-32
    st=HEIGHT//2+32
end

function draw()
    background(0)
    text("tap around here",WIDTH/2,HEIGHT/2-100)
    sprite(shield1,WIDTH//2,HEIGHT//2)
    rect(mx,my,10,20)   -- missle size 10x20 
    my=my+vy
    tot=0
    if mx+5>=sl and mx-5<=sr and my+10>sb and my-10<st then
        for x=-5,5 do
            xx=(mx-sl+x)//1
            yy=(my-sb+10)//1
            if xx>=1 and xx<=88 and yy>=1 and yy<=64 then
                r,g,b,a=shield1:get(xx,yy)
                tot=tot+r+g+b
                if tot>0 then
                    explode()
                    vy=0
                    my=-20                                        
                end
            end
        end
    end
end

function explode()
    for x=-5,5 do
        for y=1,10 do
            for z=1,20 do
                a=math.random(20)
                b=math.random(30)
                shield1:set((mx-sl+x+a)//1,(my-sb+y+b)//1,0,0,0,0)
            end
        end
    end    
end

function touched(t)
    if t.state==BEGAN then
        vy=1
        mx=t.x
        my=t.y
    end    
end

gives pixel out of bounds message. i’ll dig into it later
thanks