Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

starter game 22

dave1707dave1707 Mod
edited December 2017 in Code Sharing Posts: 6,828

I haven’t done this for awhile, but here’s another starter game for anyone who wants to take it and add whatever bells and whistles they want to it. This uses simple Craft graphics and draws 200 red balls and 200 green balls. The object is to run thru the green balls, but avoid the red balls. To move, place your finger on the screen. Slide up to go forward, down to reverse, left to go left and right to go right. The more you move up, the faster you go. There’s a count to show how many green balls remain. I didn’t add any checking, so a red and green ball can occupy the same spot. If you hit a red ball, game over. Double tap to restart. It takes a second or so to start the game. If you run thru all the green balls, nothing happens. You can add that if you want.

displayMode(FULLSCREEN)

function setup() 
    st=os.time()
    print(st)
    speed,ey,ang=0,45,0
    cameraX,cameraZ=-205,-205
    scene = craft.scene()
    scene.camera.position = vec3(cameraX,0,cameraZ)
    scene.camera.eulerAngles=vec3(ex,ey,ez)
    scene.sun.rotation = quat.eulerAngles(45,0,45)
    scene.ambientColor = color(90,90,90)
    skyMaterial = scene.sky.material
    skyMaterial.sky = color(252, 252, 6, 255)
    skyMaterial.horizon = color(0, 203, 255, 255)    
    count=200
    tab={}
    for z=1,count do
        createSphere(math.random(-200,200),math.random(-200,200),255,0,0)
    end
    for z=1,count do
        createSphere(math.random(-200,200),math.random(-200,200),0,255,0)
    end
    createFloor()
end

function update(dt)
    scene:update(dt)
    scene.camera.position = vec3(cameraX,1,cameraZ)
    scene.camera.eulerAngles=vec3(0,ey,0)
end

function draw()
    background(0)
    if gameOver then
        sprite("Tyrian Remastered:Explosion Huge",WIDTH/2,HEIGHT/2,500,500)
        fill(255,0,0)
        text("Double tap the screen to restart",WIDTH/2,HEIGHT/2)
        text(200-count.." hits in "..en-st.." seconds",WIDTH/2,HEIGHT/2-50)
        val=string.format("%5.2f",(200-count)/(en-st))
        text(val.." hits per second",WIDTH/2,HEIGHT/2-100)
        return
    end
    en=os.time()
    update(DeltaTime)
    scene:draw()
    ey=ey-ang
    x=speed*math.sin(math.rad(ey))
    z=speed*math.cos(math.rad(ey)) 
    cameraX=cameraX+x
    cameraZ=cameraZ+z
    for a,b in pairs(tab) do
        if cameraX>b.position.x-.5 and cameraX<b.position.x+.5 and 
                cameraZ>b.position.z-.5 and cameraZ<b.position.z+.5 then
            if b.material.diffuse.y==1 then
                b:destroy()
                table.remove(tab,a)
                count=count-1
                sound(SOUND_HIT, 19423)
            else
                gameOver=true
                sound(SOUND_EXPLODE, 27037)
            end
        end
    end
    translate(WIDTH/2,HEIGHT/2-100)
    rotate(ang*-30)
    sprite("Tyrian Remastered:Boss A",0,0,300)
    translate()
    fill(255)
    fontSize(40)
    text("Count  "..count,WIDTH/2,HEIGHT-50)
end

function touched(t)
    if t.state==BEGAN then
        if t.tapCount==2 and gameOver then
            restart()
        end
    elseif t.state==MOVING then
        ang=ang+t.deltaX/50
        speed=speed+t.deltaY/80
    end
end

function createFloor(x,z)
    c1=scene:entity()
    w=c1:add(craft.rigidbody,STATIC)
    c1.model = craft.model.cube(vec3(400,1,400))
    c1.position=vec3(x,-.5,z)
    c1.material = craft.material("Materials:Standard")
    c1.material.map = readImage("Blocks:Cactus Top")
    c1.material.offsetRepeat=vec4(0,0,50,50)
end

function createSphere(x,z,r,g,b)
    size=.5
    sphere1=scene:entity()
    s1=sphere1:add(craft.rigidbody,STATIC)
    sphere1.position=vec3(x,1,z)
    sphere1:add(craft.shape.sphere,size)
    sphere1.model = craft.model.icosphere(size,2)
    sphere1.material = craft.material("Materials:Specular")
    sphere1.material.diffuse=color(r,g,b)
    table.insert(tab,sphere1)
end


Comments

  • dave1707dave1707 Mod
    Posts: 6,828

    I added some counts plus a ship and I tilt the ship when you turn.

  • Posts: 815

    @dave1707 - neat, interesting and engrossing. Also in so few lines !!! Impressed. No time to play at moment but can see a few variants on this.

  • dave1707dave1707 Mod
    edited December 2017 Posts: 6,828

    Here’s an updated version. The flying direction is now controlled by tilting the iPad. I tried to control the speed by tilt also, but I didn’t like the way it worked so there's a slider to limit the max speed. To control the speed, slide your finger up or down but most of the time you’ll just keep it at the max speed you set. Before starting or restarting the game, make sure the iPad is level side to side or the ships tilt will be off. You might have trouble controlling the steering, but after awhile it will be easy. If you want, you can change the max speed while the game is in progress, just hit the > icon to show the parameter panel.

    displayMode(FULLSCREEN)
    
    function setup() 
        parameter.number("maxSpeed",.1,1.2,.5)
        setup1()
    end
    
    function setup1()
        gameOver=false
        hgx=Gravity.x
        st=os.time()
        speed,ey,ang=0,45,0
        cameraX,cameraZ=-205,-205
        count=200
        scene = craft.scene()
        scene.camera.position = vec3(cameraX,0,cameraZ)
        scene.camera.eulerAngles=vec3(ex,ey,ez)
        scene.sun.rotation = quat.eulerAngles(45,0,45)
        scene.ambientColor = color(90,90,90)
        skyMaterial = scene.sky.material
        skyMaterial.sky = color(252, 252, 6, 255)
        skyMaterial.horizon = color(0, 203, 255, 255)    
        tab={}
        for z=1,count do
            createSphere(math.random(-200,200),math.random(-200,200),255,0,0)
        end
        for z=1,count do
            createSphere(math.random(-200,200),math.random(-200,200),0,255,0)
        end
        createFloor()
    end
    
    function update(dt)
        scene:update(dt)
        scene.camera.position = vec3(cameraX,1,cameraZ)
        scene.camera.eulerAngles=vec3(0,ey,0)
    end
    
    function draw()
        background(0)
        if gameOver then
            sprite("Tyrian Remastered:Explosion Huge",WIDTH/2,HEIGHT/2,500,500)
            fill(255,0,0)
            text("Hold the ipad level then",WIDTH/2,HEIGHT/2+50)
            text("Double tap the screen to restart",WIDTH/2,HEIGHT/2)
            text(200-count.." hits in "..en-st.." seconds",WIDTH/2,HEIGHT/2-50)
            val=string.format("%5.2f",(200-count)/(en-st))
            text(val.." hits per second",WIDTH/2,HEIGHT/2-100)
            return
        end
        en=os.time()
        update(DeltaTime)
        scene:draw()
        if speed>maxSpeed then
            speed=maxSpeed
        elseif speed<=0 then
            speed=0
        end
        ey=ey-ang
        x=speed*math.sin(math.rad(ey))
        z=speed*math.cos(math.rad(ey)) 
        cameraX=cameraX+x
        cameraZ=cameraZ+z
        for a,b in pairs(tab) do
            if cameraX>b.position.x-.5 and cameraX<b.position.x+.5 and 
                    cameraZ>b.position.z-.5 and cameraZ<b.position.z+.5 then
                if b.material.diffuse.y==1 then
                    b:destroy()
                    table.remove(tab,a)
                    count=count-1
                    sound(SOUND_HIT, 19423)
                else
                    gameOver=true
                    sound(SOUND_EXPLODE, 27037)
                end
            end
        end
        pushMatrix()
        translate(WIDTH/2,HEIGHT/2-100)
        rotate(ang*-30)
        sprite("Tyrian Remastered:Boss A",0,0,300)
        fill(255)
        text(count.."  "..string.format("%2.1f",speed),0,0)
        translate()
        popMatrix()
        ang=ang+(Gravity.x-hgx)*4
        hgx=Gravity.x
        if speed==0 then
            text("Slide your finger up/down to + or - the speed",WIDTH/2,HEIGHT-50)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            if t.tapCount==2 and gameOver then
                setup1()
                return
            end
        elseif t.state==MOVING then
            speed=speed+t.deltaY/80
        end
    end
    
    function createFloor(x,z)
        c1=scene:entity()
        w=c1:add(craft.rigidbody,STATIC)
        c1.model = craft.model.cube(vec3(400,1,400))
        c1.position=vec3(x,-.5,z)
        c1.material = craft.material("Materials:Standard")
        c1.material.map = readImage("Blocks:Cactus Top")
        c1.material.offsetRepeat=vec4(0,0,50,50)
    end
    
    function createSphere(x,z,r,g,b)
        size=.5
        sphere1=scene:entity()
        s1=sphere1:add(craft.rigidbody,STATIC)
        sphere1.position=vec3(x,1,z)
        sphere1:add(craft.shape.sphere,size)
        sphere1.model = craft.model.icosphere(size,2)
        sphere1.material = craft.material("Materials:Specular")
        sphere1.material.diffuse=color(r,g,b)
        table.insert(tab,sphere1)
    end
    
  • Posts: 148

    Very cool and hypnotic game :)
    I only stopped playing when my son wrestled the iPad from me

  • dave1707dave1707 Mod
    Posts: 6,828

    Here’s another updated version. There’s a problem where the ship and the tilt of the iPad seem to get out of sync when doing a lot of fast turns. To correct this while the game is still in progress, with the iPad level, just tap the screen and the ships tilt will level out. Other changes are the green balls are half the size and the red balls are twice the size. Also, when you hit a green ball, I create a yellow ball that you also have to avoid. So as the game progresses, the green balls get harder to find and you have more larger balls to avoid. There isn’t any end game because I haven’t added any code so that the red and yellow balls don’t occupy the same space as a green ball. You might see a green ball sticking out of the side of a red or yellow ball. You could try skimming the green ball without hitting the larger ones, but odds are you’ll lose.

    displayMode(FULLSCREEN)
    
    function setup() 
        assert(craft, "Please include Craft as a dependency")
        parameter.number("maxSpeed",.1,1.5,.5)
        setup1()
    end
    
    function setup1()
        gameOver=false
        hgx=Gravity.x
        st=os.time()
        speed,ey,ang=0,45,0
        cameraX,cameraZ=-205,-205
        count=200
        scene = craft.scene()
        scene.camera.position = vec3(cameraX,0,cameraZ)
        scene.camera.eulerAngles=vec3(ex,ey,ez)
        scene.sun.rotation = quat.eulerAngles(45,0,45)
        scene.ambientColor = color(90,90,90)
        skyMaterial = scene.sky.material
        skyMaterial.sky = color(252, 252, 6, 255)
        skyMaterial.horizon = color(0, 203, 255, 255)    
        tab={}
        for z=1,count do
            createSphere(math.random(-200,200),math.random(-200,200),255,0,0,2)
        end
        for z=1,count do
            createSphere(math.random(-200,200),math.random(-200,200),0,255,0,.25)
        end
        createFloor()
    end
    
    function update(dt)
        scene:update(dt)
        scene.camera.position = vec3(cameraX,1,cameraZ)
        scene.camera.eulerAngles=vec3(0,ey,0)
    end
    
    function draw()
        fps=1/DeltaTime//1
        background(0)
        if gameOver then
            sprite("Tyrian Remastered:Explosion Huge",WIDTH/2,HEIGHT/2,500,500)
            fill(255,0,0)
            text("Hold the ipad level then",WIDTH/2,HEIGHT/2+50)
            text("Double tap the screen to restart",WIDTH/2,HEIGHT/2)
            text(200-count.." hits in "..en-st.." seconds",WIDTH/2,HEIGHT/2-50)
            val=string.format("%5.2f",(200-count)/(en-st))
            text(val.." hits per second",WIDTH/2,HEIGHT/2-100)
            return
        end
        en=os.time()
        update(DeltaTime)
        scene:draw()
        if speed>maxSpeed then
            speed=maxSpeed
        elseif speed<=0 then
            speed=0
        end
        ey=ey-ang
        x=speed*math.sin(math.rad(ey))
        z=speed*math.cos(math.rad(ey)) 
        cameraX=cameraX+x
        cameraZ=cameraZ+z
        for a,b in pairs(tab) do
            if b.material.diffuse.x~=1 then
                if cameraX>b.position.x-.25 and cameraX<b.position.x+.25 and 
                        cameraZ>b.position.z-.25 and cameraZ<b.position.z+.25 then
                    b:destroy()
                    table.remove(tab,a)
                    count=count-1
                    sound(SOUND_HIT, 19423)
                    createSphere(math.random(-200,200),math.random(-200,200),255,255,0,2)
                end
            elseif cameraX>=b.position.x-2 and cameraX<=b.position.x+2 and 
                        cameraZ>=b.position.z-2 and cameraZ<=b.position.z+2 then
                gameOver=true
                sound(SOUND_EXPLODE, 27037)
            end
        end
        gx=Gravity.x
        ang=ang+(gx-hgx)*4
        hgx=gx
        pushMatrix()
        translate(WIDTH/2,HEIGHT/2-100)
        rotate(ang*-30)
        sprite("Tyrian Remastered:Boss A",0,0,300)
        fill(255)
        text(count.."  "..string.format("%2.1f",speed),0,0)
        translate()
        popMatrix()
        if speed==0 then
            text("Slide your finger up/down to + or - the speed",WIDTH/2,HEIGHT-50)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            if t.tapCount==2 and gameOver then
                setup1()
                return
            else
                ang=0
            end
        elseif t.state==MOVING then
            speed=speed+t.deltaY/80
        end
    end
    
    function createFloor(x,z)
        c1=scene:entity()
        w=c1:add(craft.rigidbody,STATIC)
        c1.model = craft.model.cube(vec3(400,1,400))
        c1.position=vec3(x,-.5,z)
        c1.material = craft.material("Materials:Standard")
        c1.material.map = readImage("Blocks:Cactus Top")
        c1.material.offsetRepeat=vec4(0,0,50,50)
    end
    
    function createSphere(x,z,r,g,b,size)
        sphere1=scene:entity()
        s1=sphere1:add(craft.rigidbody,STATIC)
        sphere1.position=vec3(x,1,z)
        sphere1:add(craft.shape.sphere,size)
        sphere1.model = craft.model.icosphere(size,1)
        sphere1.material = craft.material("Materials:Specular")
        sphere1.material.diffuse=color(r,g,b)
        table.insert(tab,sphere1)
    end
    
  • dave1707dave1707 Mod
    edited December 2017 Posts: 6,828

    Here’s an updated version. Things added. Parameter.watch for Gravity.x and FPS. It looks like Gravity.x gets out of sync with the iPad after a lot of turns and twists which affects the tilt of the ship. To correct the ship tilt while the game is running, you can tap the screen when the iPad is level or you can swipe down to to make the speed 0. Then tap the > icon to show the parameters to see the value of Gravity.x when the iPad is level. Other things changed are yellow spheres are created when a green sphere is destroyed. Instead of having to avoid the yellow spheres, you need to hit them like the green spheres to destroy them. But unlike the green spheres, the yellow spheres are in constant motion which makes them harder to hit. On the back of the ship is a count of green spheres, yellow spheres, and ship speed. There is still no end game, but I think it will take a long time to hit all the green and yellow spheres.

    displayMode(FULLSCREEN)
    
    function setup() 
        assert(craft, "Please include Craft as a dependency")
        parameter.number("maxSpeed",.1,1.2,.5)
        parameter.watch("Gravity.x")
        parameter.watch("fps")
        setup1()
    end
    
    function setup1()
        mx,mz=0,0
        gameOver=false
        hgx=Gravity.x
        st=os.time()
        speed,ey,ang=0,45,0
        cameraX,cameraZ=-205,-205
        count=200
        yellow=0
        scene = craft.scene()
        scene.camera.position = vec3(cameraX,0,cameraZ)
        scene.camera.eulerAngles=vec3(ex,ey,ez)
        scene.sun.rotation = quat.eulerAngles(45,0,45)
        scene.ambientColor = color(90,90,90)
        skyMaterial = scene.sky.material
        skyMaterial.sky = color(252, 255, 0, 255)
        skyMaterial.horizon = color(0, 203, 255, 255)    
        tab={}
        for z=1,count do    -- 1=red
            createSphere(math.random(-200,200),math.random(-200,200),2,1,255,0,0,0,0)
        end
        for z=1,count do    -- 2=green
            createSphere(math.random(-200,200),math.random(-200,200),.25,2,0,255,0,0,0)
        end
        createFloor()
    end
    
    function update(dt)
        scene:update(dt)
        scene.camera.position = vec3(cameraX,1,cameraZ)
        scene.camera.eulerAngles=vec3(0,ey,0)
    end
    
    function draw()
        fps=1/DeltaTime//1
        background(0)
        if gameOver then
            sprite("Tyrian Remastered:Explosion Huge",WIDTH/2,HEIGHT/2,500,500)
            fill(255,0,0)
            text("Hold the ipad level then",WIDTH/2,HEIGHT/2+50)
            text("Double tap the screen to restart",WIDTH/2,HEIGHT/2)
            text(200-count.." hits in "..en-st.." seconds",WIDTH/2,HEIGHT/2-50)
            val=string.format("%5.2f",(200-count)/(en-st))
            text(val.." hits per second",WIDTH/2,HEIGHT/2-100)
            return
        end
        en=os.time()
        update(DeltaTime)
        scene:draw()
        if speed>maxSpeed then
            speed=maxSpeed
        elseif speed<=0 then
            speed=0
        end
        ey=ey-ang
        x=speed*math.sin(math.rad(ey))
        z=speed*math.cos(math.rad(ey)) 
        cameraX=cameraX+x
        cameraZ=cameraZ+z
        for a,b in pairs(tab) do
            if b.type==1 then
                if cameraX>=b.ent.position.x-b.size and cameraX<=b.ent.position.x+b.size and 
                        cameraZ>=b.ent.position.z-b.size and cameraZ<=b.ent.position.z+b.size then
                    gameOver=true
                    sound(SOUND_EXPLODE, 27037)
                end
            end
            if b.type==2 then
                if cameraX>b.ent.position.x-b.size and cameraX<b.ent.position.x+b.size and 
                        cameraZ>b.ent.position.z-b.size and cameraZ<b.ent.position.z+b.size then
                    b.ent:destroy()
                    table.remove(tab,a)
                    count=count-1
                    sound(SOUND_HIT, 19423)
                    createSphere(math.random(-200,200),math.random(-200,200),.5,3,255,255,0,.2,.2)
                    yellow=yellow+1
                end
            end
            if b.type==3 then
                if cameraX>=b.ent.position.x-b.size and cameraX<=b.ent.position.x+b.size and 
                        cameraZ>=b.ent.position.z-b.size and cameraZ<=b.ent.position.z+b.size then
                    b.ent:destroy()
                    table.remove(tab,a)
                    yellow=yellow-1
                    sound(SOUND_POWERUP, 19422)
                else
                    xx=b.ent.position.x
                    zz=b.ent.position.z
                    xx=xx+b.xv
                    zz=zz+b.zv
                    if xx<-200 or xx>200 then
                        b.xv=-b.xv
                    end
                    if zz<-200 or zz>200 then
                        b.zv=-b.zv
                    end
                    b.ent.position=vec3(xx,1,zz)
                end
            end
        end
        gx=Gravity.x
        ang=ang+(gx-hgx)*4
        hgx=gx
        pushMatrix()
        translate(WIDTH/2,HEIGHT/2-100)
        rotate(ang*-30)
        sprite("Tyrian Remastered:Boss A",0,0,300)
        fill(255)
        text(count.."  ".."  "..yellow.."  "..string.format("%2.1f",speed),0,0)
        translate()
        popMatrix()
        if speed==0 then
            text("Slide your finger up/down to + or - the speed",WIDTH/2,HEIGHT-50)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            if t.tapCount==2 and gameOver then
                setup1()
                return
            else
                ang=0
            end
        elseif t.state==MOVING then
            speed=speed+t.deltaY/80
        end
    end
    
    function createFloor(x,z)
        c1=scene:entity()
        w=c1:add(craft.rigidbody,STATIC)
        c1.model = craft.model.cube(vec3(400,1,400))
        c1.position=vec3(x,-.5,z)
        c1.material = craft.material("Materials:Standard")
        c1.material.map = readImage("Blocks:Cactus Top")
        c1.material.offsetRepeat=vec4(0,0,50,50)
    end
    
    function createSphere(x,z,size,type,r,g,b,xv,zv)
        sphere1=scene:entity()
        s1=sphere1:add(craft.rigidbody,KINEMATIC)
        sphere1.position=vec3(x,1,z)
        sphere1:add(craft.shape.sphere,size)
        sphere1.model = craft.model.icosphere(size,1)
        sphere1.material = craft.material("Materials:Specular")
        sphere1.material.diffuse=color(r,g,b)
        table.insert(tab,{ent=sphere1,xv=xv,zv=zv,size=size,type=type})
    end
    
Sign In or Register to comment.