Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

Fantasy computer for making, playing and sharing tiny games. · By Nesbox

Some code snippets

A topic by Raidez created Feb 21, 2017 Views: 3,423 Replies: 5
Viewing posts 1 to 6
(+2)

I have a passion to create useless code snippets for me, so I decide to share the most useful for others (or not ;) ).

-- script: moon

export width=240
export height=136
export tilesize=8
export fontsize=6

btnAxis=(a,b)->
    if btn(a) and btn(b)
        return 0
    elseif btn(a)
        return -1
    elseif btn(b)
        return 1
    else
        return 0


class Menu
    new:(x=0,y=0,index=1)=>
        @x=x
        @y=y
        @index=index
        @items={}
    
    add:(text)=>
        table.insert(@items,text)
        
    prev:=>
        i=@index-1
        if i<1
            @index=#@items
        else
            @index=i
    
    next:=>
        i=@index+1
        if i>#@items
            @index=1
        else
            @index=i
    
    draw:(sel="> ",usel="  ",spc=10,col=15)=>
        for k,v in ipairs(@items)
            dy=@y+(k-1)*spc
            if @index==k
                print(sel..v,@x,dy,col)
            else
                print(usel..v,@x,dy,col)


class Spr
    new:(id=0,x=0,y=0,alpha=-1,scale=1,flip=0,rotate=0)=>
        @id=id
        @x=x
        @y=y
        @alpha=alpha
        @scale=scale
        @flip=flip
        @rotate=rotate
    
    draw:=>
        spr(@id,@x,@y,@alpha,@scale,@flip,@rotate)


class RectCollider
    new:(x=0,y=0,w=1,h=1)=>
        @x=x
        @y=y
        @w=w
        @h=h
    
    draw:(col=8)=>
        rectb(@x,@y,@w,@h,col)
    
    collide:(B)=>
        if @x>(B.x+B.w) or (@x+@w-1)<B.x or @y>(B.y+B.h) or (@y+@h-1)<B.y
            return false
        else
            return true


class CircCollider
    new:(x=0,y=0,r=1)=>
        @x=x
        @y=y
        @r=r
    
    draw:(col=8)=>
        circb(@x,@y,@r,col)
    
    collide:(B)=>
        d=(@x-B.x)^2+(@y-B.y)^2
        r=(@r+B.r)^2
        
        if @x>=(B.x-B.r) and @y>=(B.y-B.r)
            r=(@r+B.r+1)^2
        
        if d>r
            return false
        else
            return true
--------------------
cls!
t=Spr(1,104,24,2,4)

export TIC=->
    --update
    t.y+=btnAxis(0,1)
    t.x+=btnAxis(2,3)
    t.id=time()%1000//500+1
    
    --draw
    cls(12)
    t\draw!
    print("HELLO WORLD!",84,64)

Very interesting, thank you!

Developer

You can call it like "moon bootstrap template" or smth else and add it to the wiki snippets I think :)

(+2)

I wrote a separate static class "Input" on the basis of your ) Very comfortably.

-- script: moon

-- static class, not need instantiate it
class Input
    UP=0
    DOWN=1
    LEFT=2
    RIGHT=3

    GetHorAxis:=> @btnAxis(LEFT,RIGHT)
    GetVerAxis:=> @btnAxis(UP,DOWN)

    btnAxis:(a,b)=>
        if btn(a) and btn(b)
            return 0
        elseif btn(a)
            return -1
        elseif btn(b)
            return 1
        else
            return 0

class Spr
    new:(id=0,x=0,y=0,alpha=-1,scale=1,flip=0,rotate=0)=>
        @id=id
        @x=x
        @y=y
        @alpha=alpha
        @scale=scale
        @flip=flip
        @rotate=rotate
    
    draw:=>
        spr(@id,@x,@y,@alpha,@scale,@flip,@rotate)

--------------------

logo=Spr(1,104,24,2,4)

export TIC=->
    --update
    logo.x+=Input\GetHorAxis()
    logo.y+=Input\GetVerAxis()   
    logo.id=time()%1000//500+1
    
    --draw
    cls(12)
    logo\draw()
    print("HELLO WORLD!",84,64)
(+1)

I create a code snippet for animations :) Hope its useful for someone https://tic.computer/play?cart=12

-- title:  Animation Testing
-- author: Ahmed Khalifa
-- desc:   Function to draw sprite animation
-- script: lua
-- input:  gamepad


-- create animation object
-- frames: array of the upper left corner of each frame
-- size: 0 means 8x8, 1 means 16x16, etc
-- fps: frames per seconds
function createAnimation(frames,size,fps)
    return {cf=0,frames=frames,size=size,t=1/fps*1000,spf=1/fps*1000}
end


-- reset the animation to the original frame
-- anim: animation object to reset
function resetAnimation(anim)
    anim.cf=0 anim.t=anim.spf
end


-- Draw the current animation object
-- anim: animation object to draw
-- dt: delta time since last update
-- x,y: position of upper left corner
-- ts: transparent color
-- flip: 0 no flip 1 flip horizontally
function drawAnimation(anim,dt,x,y,ts,flip)
    anim.t=anim.t-dt
    if anim.t<=0 then 
        anim.t=anim.t+anim.spf
        anim.cf=anim.cf+1
        if anim.cf>=#anim.frames then
            anim.cf=0
        end
    end
    local f=anim.frames[anim.cf+1]
    for i=0,anim.size do
        for j=0,anim.size do
            if flip==0 then
                spr(f+i+j*16,x+i*8,y+j*8,ts,1,flip)
            else
             spr(f+i+j*16,x+(anim.size-i)*8,y+j*8,ts,1,flip)
            end
        end
    end
end


-- initialize the required data
function init()
    pt=time()
    dt=0
    x=84
    y=64
    flip=0
    -- 16x16 animation
    big=createAnimation({256,258,260,258,256,262,264,262},1,20)
    -- 8x8 animation
    small=createAnimation({266,267,268,267,266,269,270,269},0,20)
    -- the current activated animation
    anim=big
end
init()


-- update function
function TIC()
    -- calculate delta time
    dt=time()-pt
    pt=time()
    
    -- change the animation state and direction based on controls
    state=0
    if btn(2) then state=1 flip=1 x=x-1 end
    if btn(3) then state=1 flip=0 x=x+1 end
    if btnp(4,60,6) then
        if anim==big then
            anim=small
        else
            anim=big
        end
    end
    
    -- clear screen
    cls(12)
    -- reset the current animation when no btns pressed
    if state==0 then resetAnimation(anim) end
    -- draw the screen
    drawAnimation(anim,dt,x,y,15,flip)
    print("Press Z to change Animation",50,100,0)
end

yep this is crazy hard to make a game, the only thing I can do is connect my brain up and edit memory and data/pixel manipulation which should work better