Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

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

Text collision?

A topic by OsmineeNitro created May 05, 2020 Views: 346 Replies: 2
Viewing posts 1 to 3

I want to make text shooter game, where you are supposed to shoot asteroids, but I don't know how to make if player is in collision with asteroid, game ends. I tried to do it with while...do loop, but I don't know how you can make "zone collision" or something like that. Here's my code snippet:

r=math.random
t,x,y,ex,ey,blt,bltx,blty,sc,isblt=0,239/2,127,r(239),0,false,0,119,0
function TIC()
while do --here!
if t%0.5==0 then ey=ey+3 end if ey>248 then ey=-8 ex=r(239) end
cls()
chkbtns()
if blt==true or isblt==true then bltx=x+2 blty=blty-5print("|",bltx,blty)blt=false end
if blty<-8 then isblt=false blty=119 end
print("*",x,y,11)
print("o",ex,ey,4,0,2)
print("Score "..sc,0,0)
t=t+1
end
trace("GAME OVER",9)
end
function chkbtns()if btn(2) then x=x-3 end if btn(3) then x=x+3 end if btn(4) then blt=true isblt=true end end

(1 edit)

For “zone collision” I did something like this in Virus Defender:

collision_distance=10
if math.abs(asteroid_x - player_x) < collision_distance and math.abs(asteroid_y - player_y) < collision_distance then
    -- game over, man! game over!
end

Basically take the absolute value of the difference between the x’s and y’s of each center. The downside is that this is a square collision area which often doesn’t fit the need.

You could always do some trigonometry to get the direct distance. Something like if math.sqrt((ex-px)^2 + (ey-py)^2) < collision_distance. Actually that doesn’t look as hard as I thought, but I’m doing trig from memory so I might have done it wrong.

Edit: yeah, I think I got it right, and it wasn’t as complex as I thought. I just tested against a 3-4-5 right triangle in an interactive lua session:

> print(math.sqrt(3^2+4^2))                                                                                                               5.0

Thanks!