New at Codea - trying to code a checkerboard

I’m new at this Codea thing. I’m trying to code a game that uses a checkerboard but not having much luck.
Can somebody give me some code to get me started?
Just an 8x8 board. Thanks.

displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    col={}
    square=80
    boardSetup()
end
    
function draw()
    background(40, 40, 50)
    drawBoard()
end

function boardSetup()
    x=1
    for z=1,64 do
        if x==1 then
            col[z]=color(255,255,255)
        else
            col[z]=color(255,0,0)
        end
        x=x*-1
        if z%8 == 0 then
            x=x*-1
        end
    end
end

function drawBoard()
    z = 0
    for x=1,8 do
        for y=1,8 do
            z = z + 1
            fill(col[z])
            rect(x*square,y*square,square-1,square-1)
        end
    end
end

Here’s another version.

displayMode(FULLSCREEN)

function setup()
    square=80
end
    
function draw()
    background(40, 40, 50)
    drawBoard()
end

function drawBoard()
    for x=1,8 do
        for y=1,8 do
            if x%2==y%2 then
                fill(255,255,255)
            else
                fill(255,0,0)
            end
            rect(x*square,y*square,square-1,square-1)
        end
    end
end


Thanks a ton, Dave.

@theofani - you can use setContext as shown below to draw your board to an image in memory.

This means you can then just draw the board as a picture instead of redrawing it constantly, and also makes it easy to move the board around the screen.

displayMode(FULLSCREEN)

function setup()
    --draw chessboard to an image called board
    board=SetupBoard(60) 
end

function draw()
    background(40, 40, 50)
    sprite(board,WIDTH/2,HEIGHT/2)
end

function SetupBoard(square)
    local img=image(square*8,square*8)
    setContext(img) --draw to image instead of screen
    for x=1,8 do
        for y=1,8 do
            if x%2==y%2 then
                fill(255,255,255)
            else
                fill(255,0,0)
            end
            rect(x*square,y*square,square-1,square-1)
        end
    end
    setContext() --stop drawing to image
    return img
end

hello
you can use
Labo Shader / Patterns / Checker ( it use gpu )
and read the book of @Ignatz wich explain how to use very powerfull and fast shaders
bye

I don’t think you need shaders just yet, a simple picture is good enough

Thank you all for your help.

I have my checkerboard set up. Now, I want to have four squares off to the side with images on them that I can click on and drag to the board (but the squares will remain so more than one can be dragged).
Can someone refer me to a similar game with code that I can use?

Thanks again.

@theofani Try this. You can drag an image that’s been placed on the board to another square and you can double tap an image on the board to remove it. I guess this can be used for a checker or chess game.

EDIT: Changed code from 4 characters to 11.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    rectMode(CENTER)
    rec={vec3(800,600,1),vec3(800,500,2),vec3(800,400,3),vec3(800,300,4),
        vec3(800,200,5),vec3(800,100,6),vec3(900,600,7),vec3(900,500,8),
        vec3(900,400,9),vec3(900,300,10),vec3(900,200,11)}
    pic={readImage("Planet Cute:Character Boy"),readImage("Planet Cute:Character Cat Girl"),
        readImage("Planet Cute:Character Pink Girl"),readImage("Planet Cute:Character Horn Girl"),
        readImage("Planet Cute:Character Pink Girl"),readImage("Planet Cute:Character Horn Girl"),
        readImage("Planet Cute:Character Pink Girl"),readImage("Planet Cute:Character Horn Girl"),
        readImage("Planet Cute:Character Pink Girl"),readImage("Planet Cute:Character Horn Girl"),
        readImage("Planet Cute:Character Pink Girl")}
    move=0
    square=80
end

function draw()
    background(40, 40, 50)
    drawBoard()
    drawSq()
end

function touched(t)
    if t.state==BEGAN then
        if t.tapCount==2 then
            table.remove(rec,move)
        end
        for a,b in pairs(rec) do
            if t.x>b.x-square/2 and t.x<b.x+square/2 and t.y>b.y-square/2 and t.y<b.y+square/2 then
                if a>11 then
                    move=a
                else
                    table.insert(rec,vec3(t.x,t.y,a))
                    move=#rec
                end
                break
            end
        end        
    end
    if t.state==MOVING and move>0 then
        rec[move].x=t.x
        rec[move].y=t.y       
    end
end

function drawBoard()
    for x=1,8 do
        for y=1,8 do
            if x%2==y%2 then
                fill(255,255,255)
            else
                fill(255,0,0)
            end
            rect(x*square,y*square,square-1,square-1)
        end
    end
    fill(255)
    for a,b in pairs(rec) do
        if a<12 then
            rect(b.x,b.y,square)
            sprite(pic[b.z],b.x,b.y,square,square)
        end
    end
end

function drawSq()
    for a,b in pairs(rec) do
        if a>11 then
            sprite(pic[b.z],b.x,b.y,square,square)
        end
    end
end

Dave, once again thanks for your help. It is appreciated. If I could bother you one more time, I’d like to have a total of 11 “buttons” in the Setup function that are used in the game but I keep getting a syntax error(unexpected symbol) on the pic= line that reads "unexpected symbol near ‘</226>’

function setup()
rectMode(CENTER)
rec={vec3(800,600,1),vec3(800,500,2),vec3(800,400,3),vec3(800,300,4),
vec3(900,600,5),vec3(900,500,6),vec3(900,400,7),vec3(900,300,8),
vec3(900,700,9),vec3(900,800,10),vec3(900,900,11)}
pic={readImage(“Planet Cute:Character Boy”),readImage(“Planet Cute:Character Cat Girl”),readImage(“Planet Cute:Character Pink Girl”),readImage(“Planet Cute:Character Horn Girl”),readImage(“Planet Cute:Character Pink Girl”),readImage(“Planet Cute:Character Horn Girl”),readImage(“Planet Cute:Character Pink Girl”),readImage(“Planet Cute:Character Horn Girl”),readImage(“Planet Cute:Character Pink Girl”),readImage(“Planet Cute:Character Horn Girl”),readImage(“Planet Cute:Character Pink Girl”)}
move=0
square=80
end

I appreciate your help on this and I’m willing to compensate you for your time and effort. I can mail you a check. Just let me know.

Thanks

Do you try to make a chess aplication? The Engine mode will be the hardest point, without learning codea good, you can’t do that

No, something similar but not trying to make a chess application.
The board will be empty with 11 pieces the players can place on the board to race to achieve a goal.

@theofani I changed my code above to handle 11 characters.

Thank you so much, Dave. I’ve got it working. Couldn’t have done it without you. Good vibes coming your way.

@dave1707, there I found a bug in your project: attempt a call nil index value 43 and then index(?) - I tapped the screen where nobody is placed 100 times

@TokOut There’s things that I didn’t code for. For instance, if you double tap to remove a piece and there isn’t one, it’s an error. I’m sure there’s others, but usually when I post an example, I don’t add all of the error code. I usually leave that up to whoever uses the code because I don’t know exactly how they’re going to use It. So if you want to use this, it’s up to you to add error checking.