Log In  


I am struggling to wrap my head around creating a function to search though a table for matching positions and then replacing the matching value with a new value.

The idea is for a simple racing game. Every time an obstacle/car leaves the screen it is deleted from the table and new obstacle/car is generated with a new position.

The new position is randomly picked from a list of pre-chosen positions. But this means that there are occasions where the obstacles/cars overlap because they have randomly picked the same positions as an existing obstacle/car already in the table of obstacles.

My rough concept of the idea runs like this...

  • Create obstacle table
  • Create new obstacle
    • Check if obstacle table is empty - add new obstacle
    • If not empty
      -- Loop through table of obstacles and compare x/y pos values
      -- If a match is found(overlap) then change the x/y pos of the new
      obstacle BEFORE adding it to the table
      -- Maybe loop again to perform another check???


You mention the X/Y pos values of the obstacle, and that you want to check for overlap. This is a bit too vague :
Are objects all of the same size and on a grid or are their coordinates integers ? If so use a 2D array to keep track of what is in what cell.

If all the objects are on screen, you could simply test a pixel color. Note that in this case you might need to use two palette entries with the same color, say one black for the road and one black with another index for the black pixels of your obstacle, so your pixel test can distinguish between black background. and black object.

Please post a sample code of your data structure so we can help you better.

For example

positions = {{x=4,y=5},{x=9,y=4},{x=5,y=5},{x=11,y=9},{x=4,y=5},{x=8,y=4}}

is not handled the same as

positionsX={4,9,5,11,4,8}
positionsY={5,4,5, 9,5,4}

My apologies for being vague. The obstacles could be a variety of sizes in the future but for now it's the same size sprite.

Here's my code I have tried thus far:

--obstacles--

function i_obs()
	tmr_obs=180
	obs={}
	cars={8,24,40,56}
	gaps={-16,-40,-84,-312}
	colliding=0

	local o
	for o=1,4 do
		new_obs()
	end//for
end

function u_obs()
	//start timer
	local o

	if tmr_obs>0 then
		tmr_obs-=1
	else
		for o in all(obs) do
			o.dy+=scroll_spd-.5
		end//for
	end//if

	//del obs from obs if off screen
	for o in all(obs) do
		if (o.dy>124) then
			del(obs,o)
			new_obs()
		end//if
	end//for
end//u_obs()

function d_obs()
	//shadow
	local i,o

	for o in all(obs) do
		for i=1,15 do
			pal(i,0)
		end
		sspr(
			o.sx,
			o.sy,
			o.sw,
			o.sh,
			o.dx,
			o.dy+2
		)

		//reset shadow
		pal()
		sspr(o.sx,o.sy,
							o.sw,o.sh,
							o.dx,o.dy)
	end//for
end//d_obs()

function new_obs()
	//change lanes if overlap
	local i,o
	local dx=rnd(lanes)
	local dy=rnd(gaps)

	if #obs>0 then
		for i=1,#obs do
			if dy>=obs[i].dy-16 or
						dy<=obs[i].dy+obs[i].sh+16 then
				if dx==obs[i].dx then
					colliding+=1
				end//if
			end//if
		end//for i
	end//if

	add(obs,{
		dx=dx,
		dy=dy,
		sx=rnd(cars),
		sy=16,
		sw=10,
		sh=15
})
end//new_obs()

function check_overs()
local i,o
for i=1,#obs do
		for o in all(obs) do
			local x1=o.dx
			local x2=o.dx+o.sw
			local y1=o.dy
			local y2=o.dy+o.sh

			if obs[i].dx==x1 then
				if obs[i].dy<=y2 or 
							obs[i].dy+obs[i].sh>=y1 then
							obs[i].dx=rnd(lanes)
				end//if
			end//if
		end//for o
	end//for i
end//check_overs()

Thanks Pablo! I tried it out and it would make the game crash. I had a feeling it was because it became stuck in an endless loop by just continuing to increase the collision variable. So I tried by just assigning 1 to collision if there is an overlap. It has been working great so far now in my testing.


Sorry for deleting posts, i made a new account.

Just saw you on Mastodon, cool stuff!


Hey! Thanks. I'm hoping I actually finish that new game. So that I can build up the confidence to always finish my games.



[Please log in to post a comment]