Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs

TIC-80

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

TIC-80 problem with game

A topic by Koziorek created Dec 17, 2017 Views: 467 Replies: 3
Viewing posts 1 to 3
(2 edits)

I have a problem with my simple game.
The concept of the game is character that shoots bullets. The player can move the character in four directions using arrows and shoot (only shooting right for now) with "x" key. Everything looks fine when I press "x" key the object is created and it travels right, but by pressing the key to shoot again the bullets are overlaping and the more bullets are created the faster all of the bullets that are overlapped into one go. 

If someone can help me figure out what is wrong with this "game" I'd be very happy.

Link to download this game: http://www.mediafire.com/file/0q6z3ng0875uag7/game.tic

Here's the code:

--------------------------------------------------------------------
-- author: Koziorek
-- desc:   simple game
-- script: lua
xpos=96 --position x of the character
ypos=24 --position y of the character
bullet = {x = 96, y = 24, tick = 0, anim = 0} --entity "class" (pattern for "object") 
entity = {} -- table that contains all of the "objects"
function create_bullet(xp, yp) --function that creates bullet 
local obj = {} --object is local because it is used as temporary table and is used only in thi function 
obj = bullet --setting new objcet (bullet) to pattern of the bullet -> table "bullet"
obj.x = xp --changing position x of the newly created bullet to xp (1st parameter of this function) 
obj.y = yp --changing position y of the newly created bullet to yp (2nd parameter of this function) 
obj.anim = 1 --changing "variable": "anim" of the newly created bullet to 1 (it is done in order to enable moving the bullet) 
table.insert(entity, #entity+1, obj) --insert new changed bullet to table of all the objects (bullets)
end
function fire() -- after pressing "x" key this function will activate
    create_bullet(xpos, ypos) -- create new "object"(bullet) with position of the character controled by the player 
end
function animate() --move right all of the objects (bullets)  
    for i=0,#entity-1 do --loops through every object (bullet)
        if entity[i+1].anim == 1 then --if the particular object marked by iterator "i" has "variable" (anim) that is 1 then...
            entity[i+1].x = entity[i+1].x + 1 --...increase every object"s x position by 1
        end
    end
end
function TIC() 
    --movement controls (arrows)
    if btn(0) then ypos=ypos-1 end 
    if btn(1) then ypos=ypos+1 end
    if btn(2) then xpos=xpos-1 end
    if btn(3) then xpos=xpos+1 end
    
    cls(0)   
    spr(3, xpos, ypos, 0, 1, 0, 0, 2, 2) --draw character controled by the player    
    
    if btnp(5,60,60) then --fire bullet by pressing "x" key
        fire() 
    end
    if #entity > 0 then --if there is at least one "object" in a table, draw those objects (bullets)
        animate() 
        for i=0,#entity-1 do --draw every object (bullet)
            --draw bullet      
            spr(2, entity[i+1]["x"], entity[i+1]["y"], 0, 1, 0, 0, 1, 1) --[[on the first iteration the "i" has the value of 0, so using i+1 is the correctly way to tell the number of iteration--]]
        end 
    end
end
--------------------------------------------------------------

you can publish it as it is, and then keep improving it - from there it might be far easier to debug it

Okay, but could you help me figure out what is wrong with this simple mechanic (that should create objects and draw them)?

The problem is that you are actually using the same object, not creating a new one.

Quick solution would be to clone your template bullet object

Add this function on the top:

--clone table
function clone(t)
local r={}
for k,v in pairs(t) do r[k]=v end
return r
end

Then, in your function, instead of doing:

local obj = {} 
obj = bullet

do

local obj = clone(bullet)