This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

Stacker Game Help

Started by KingofGamesYami, 18 December 2014 - 02:40 AM
KingofGamesYami #1
Posted 18 December 2014 - 03:40 AM
I'm currently making a game called 'stacker', which I first encountered on miniclip. However, I've got a glitch that I cannot get rid of, and I have no idea why it happens. It only happens on the second line, once I'm above that I cannot reproduce the behavior. Basically, if you manage to get it perfectly aligned, it cuts off the last pixel on the right:
[attachment=2013:Screen Shot 2014-12-17 at 9.33.24 PM.png]

My messey and very much unfinished code

local stack = {}
local maxx, maxy = term.getSize()

local nSize = 8
local bNew = true

stack[ 0 ] = { maxx/2 - 4, maxx/2 + 4 }

function renderStack()
	term.setBackgroundColor( colors.white )
	for i = 0, #stack do
		term.setCursorPos( stack[ i ][ 1 ], maxy - i )
		term.write( string.rep( " ", stack[ i ][2] - stack[ i ][1] ) )
	end
	term.setBackgroundColor( colors.black )
end

function getInput()
	local new = bNew and { 1, nSize + 1 } or stack[ #stack ]
	local id = os.startTimer( 0.1 )
	while true do
		local event = { os.pullEvent() }
		if event[ 1 ] == "timer" and event[ 2 ] == id then
			new[ 1 ] = new[ 1 ] + 1
			new[ 2 ] = new[ 2 ] + 1
			if bNew then
				stack[ #stack + 1 ] = new
				bNew = false
			end
			break
		elseif event[ 1 ] == "key" and event[ 2 ] == keys.space then
			if new[ 1 ] < ( bNew and stack[ #stack ][ 1 ] or stack[ #stack - 1 ][ 1 ] ) then
				new[ 1 ] = ( bNew and stack[ #stack ][ 1 ] or stack[ #stack - 1 ][ 1 ] )
			end
			if new[ 2 ] > ( bNew and stack[ #stack ][ 2 ] or stack[ #stack - 1 ][ 2 ] ) then
				new[ 2 ] = ( bNew and stack[ #stack ][ 2 ] or stack[ #stack - 1 ][ 2 ] )
			end
			if new[ 2 ] - new[ 1 ] <= 0 then
				error( "Game Over", 0 )
			end
			nSize = new[ 2 ] - new[ 1 ]
			if bNew then
				stack[ #stack + 1 ] = new
			else
				stack[ #stack ] = new
				bNew = true
			end
			break
		end
	end
end
while true do
	term.clear()
	renderStack()
	getInput()
end

PSIf anyone knows a good way to increase the speed at a reasonable rate, please tell me. My attempts end up being exponential decline in the wait time… so it gets really, really fast
Bomb Bloke #2
Posted 18 December 2014 - 05:51 AM
stack[ 0 ] = { math.floor(maxx/2) - 4, math.floor(maxx/2) + 4 }

If anyone knows a good way to increase the speed at a reasonable rate, please tell me. My attempts end up being exponential decline in the wait time… so it gets really, really fast

Hrm. Timers will always wait for a multiple of server ticks, minimum of one. Currently you're asking it to wait 0.1 seconds (two server ticks). It won't actually go any faster until you get down to 0.05 seconds (one server tick, a twentieth of a second), and then after that, you can't really speed it up further without ditching the timer system completely and running "full speed" - even a requested delay of 0 seconds will get you 0.05.

The return values from os.clock() also happen to only increment in multiples of a twentieth of a second, so you really can't measure time any more finely than that.

But, let's imagine for a moment that this wasn't a problem. What I'd do is multiply the current timer value by 0.9 or something as each layer of the stack is placed. The first few layers would get faster at a faster pace than the last few layers. If I wanted the rate of change to get slower as you went up the stack, I'd start out with a lower multiplier, and multiply that by 1.1 or something as each layer went on as well.
KingofGamesYami #3
Posted 18 December 2014 - 01:39 PM
stack[ 0 ] = { math.floor(maxx/2) - 4, math.floor(maxx/2) + 4 }

If anyone knows a good way to increase the speed at a reasonable rate, please tell me. My attempts end up being exponential decline in the wait time… so it gets really, really fast

Hrm. Timers will always wait for a multiple of server ticks, minimum of one. Currently you're asking it to wait 0.1 seconds (two server ticks). It won't actually go any faster until you get down to 0.05 seconds (one server tick, a twentieth of a second), and then after that, you can't really speed it up further without ditching the timer system completely and running "full speed" - even a requested delay of 0 seconds will get you 0.05.

The return values from os.clock() also happen to only increment in multiples of a twentieth of a second, so you really can't measure time any more finely than that.

But, let's imagine for a moment that this wasn't a problem. What I'd do is multiply the current timer value by 0.9 or something as each layer of the stack is placed. The first few layers would get faster at a faster pace than the last few layers. If I wanted the rate of change to get slower as you went up the stack, I'd start out with a lower multiplier, and multiply that by 1.1 or something as each layer went on as well.

*facepalm* I should know by now that maxx doesn't divide by two…

And yeh, thats what I tried… The problem is I want it to increase linearly, not exponentially.
Bomb Bloke #4
Posted 19 December 2014 - 06:17 AM
Why does it matter, so long as it doesn't hit max speed too soon? But I guess if you really want a linear system, start out with a one second delay and subtract 0.05s as each layer goes on. Easy.

Personally I'd find it more interesting if it got faster at a faster rate during the first few levels, but that might just be me.