Jump to content



Photo

Code Causes A Reboot Screen


  • Please log in to reply
14 replies to this topic

#1 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 25 May 2012 - 05:24 PM

This code gives me a reboot screen. why?

local rect = graydraw.rect




function  garageDoor(x, y, width, height, position, panelHeight)

	rect(x, y, x + width, y - height, 4, 0) --draw garage outine

	for i = y + height - position, y, panelHeight do
		rect(x, i - panelHeight, x + width, i, 4, 0)
		
		if i <= y then
			i = panelHeight1
			break
		end 
	end

	rect(x, y, x + width, y - panelHeight1, 4, 0) --draw first panel 

end


garageDoor(1, 1, 30, 30, 5, 6)


#2 veb

veb

    Casio Fan

  • Members
  • PipPip
  • 43 posts

Posted 25 May 2012 - 06:42 PM

Thank you for your bug tracking,

when reading my code, I see I forgot to test we are in the gray mode in this function (I didn't forgot it in the other functions)

Then you are writing in gray buffers, without beeing in gray mode....

#3 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 25 May 2012 - 08:27 PM

So are you going to upload a new version?

Also, I like how you made it so it doesn't reboot after a program executes. :)

#4 veb

veb

    Casio Fan

  • Members
  • PipPip
  • 43 posts

Posted 26 May 2012 - 06:39 AM

Yes I will correct this version, but you should first correct your code by setting the gray mode before calling graydraw.rect .

#5 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 27 May 2012 - 04:38 PM

ah, ok, I get it.

#6 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 29 May 2012 - 09:10 PM

OK, I used this code and I did not get a reboot but I got an error:

local rect = graydraw.rect
local setColor = graydraw.setcolor

function main()
setColor(1)
garageDoor(1, 1, 30, 30, 5, 6)
end

function  garageDoor(x, y, width, height, position, panelHeight)

	rect(x, y, x + width, y - height, 4, 0) --draw garage outine

	for i = y + height - position, y, panelHeight do
		rect(x, i - panelHeight, x + width, i, 4, 0)
		
		if i <= y then
			panelHeight1 = i
			break
		end 
	end

	rect(x, y, x + width, y - panelHeight1, 4, 0) --draw first panel 

end

main()

Error:

22: Attempt to perform arithmetic operation on 'panelHeight1' (a nil value)

Am I doing something wrong? Should line 16 be changed?

#7 veb

veb

    Casio Fan

  • Members
  • PipPip
  • 43 posts

Posted 30 May 2012 - 02:49 PM

for i = y + height - position, y, panelHeight do

with your values, height-position = 25,
then y + height-position > y

=> i>y, then you never define panelHeight1 (by default a value not defined is set to nil)

Moreover, since y + height-position > y and panelHeight>0, the for loop never executes

two reasons that explains why panelHeight1 is not defined

Moreover, I don't understant why you launch a for loop that will break when i <= y

#8 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 30 May 2012 - 03:41 PM

OK, I think I understand what you mean.

The reason I have a loop that breaks when i<=y is that it starts at a value larger than I want it to end at.
My program is supposed to draw a paneled garage door that can open and close.

x and y in the garage door function display the garage door at x, y.

width and height are the garage's width and height.

position is how much open space there is between the bottom panel and the bottom of the garage in pixels.

panelHeight is how large the panels should be.




So...

The way I am trying to get it to work is it draws all the panels that are fully visible. The top panel may not be fully visible because it is going up into the garage.

The for loop draws the visible panels.

I am trying to figure out how to draw the very top panel. I need to find out how tall it should be and I was using panelHeight1 to do that.

#9 veb

veb

    Casio Fan

  • Members
  • PipPip
  • 43 posts

Posted 30 May 2012 - 03:48 PM

then you want i to decrease:

for i = y + height - position, y, -panelHeight do


#10 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 30 May 2012 - 04:02 PM

Oh, ok. I will try that.:)

#11 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 01 June 2012 - 03:48 PM

OK, need some more help.

Code:
local rect = graydraw.rect
local setColor = graydraw.setcolor
local wait = misc.wait
local line = graydraw.line

screenWidth = 128
screenHeight = 64

key_left = 37
key_right = 40
key_exit = 36
gDoorPosition = {0,0,0}
gDoorActive = 1

function main()
	setColor(1)
	menu()
end

function menu()
	while not key(key_exit) do
		if key(key_left) then gDoorActive = gDoorActive - 1 end
		if key(key_right) then gDoorActive = gDoorActive + 1 end
		if gDoorActive < 1 then gDoorActive = 1 end
		if gDoorActive > 3 then gDoorActive = 3 end
		
		for i = 1, 3, 1 do
			if i == gDoorActive and gDoorPosition[i] < 30 then gDoorPosition[i] = gDoorPosition[i] + 1
			elseif gDoorPosition[i] > 0 then gDoorPosition[i] = gDoorPosition[i] - 1 end
		end
		
		for ix = 1, 3, 1 do
			garageDoor(ix * 20 + 5, y, 20, 30, gDoorPosition[ix], 5)
		end 
		
		refresh
	end
end

function  garageDoor(x, y, width, height, position, panelHeight)

	rect(x, y, x + width, y + height, 4, 4) --draw garage outine
	rect(x, y, x + width, y + height - position, 4, 0) --draw door
	
	--draw panels
	for iy = height - position, 0, -panelHeight do
		line(x, y + iy, x + width, y + iy, 4)
	end
end

main()

Error:
:42: Attempt to perform arithmetic on local 'y' (a nil value)


EDIT:

Never mind, fixed code. :)

#12 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 05 June 2012 - 01:39 PM

My error is weird...
:151: attempt to perform operation on field '?' (a nil value)

Here is the code on line 151:
buffer = ai.y[i] - player.y

It was working just before I saved the file. Here is the whole file:

local rect = graydraw.rect
local setColor = graydraw.setcolor
local wait = misc.wait
local line = graydraw.line
local graytext = graydraw.text
local fill = graydraw.fill
local pixel = graydraw.pixel
local random = misc.random


car = {height = 8, width = 8, sprite = "\000\000<((f^fz(<V<(jvNnr8<V\000"}


screenWidth = 128
screenHeight = 64

ai = {x = {}, y = {}, speed = {}, topspeed = {}, number = 5}
continue = 0
done = 0
gDoorPosition = {0,0,0}
gDoorText = {"Play", "Optn", "Help"}
gDoorActive = 1
i = 0
invert = 0
Key = 0
key_alpha = 33
key_left = 37
key_right = 40
key_down = 38
key_up = 39
key_exit = 36
key_shift = 41
player = {x = 0, y = 0, speed = 0, topspeed = 6}



function main()
	setColor(1)
	menu()
end

function menu()
	clear nil
	
	while done ~= 1 do
		if key(key_left) then gDoorActive = gDoorActive - 1 wait(3) end
		if key(key_right) then gDoorActive = gDoorActive + 1 wait(3) end
		if key(key_exit) then done = 1 end
		if key(key_shift) then selection = gDoorActive continue = 1 end
		if gDoorActive < 1 then gDoorActive = 1 end
		if gDoorActive > 3 then gDoorActive = 3 end
		
		if selection == 1 then play()
			elseif selection == 2 then optn()
			elseif selection == 3 then help()
		end
		
		selection = 0
		
		for i = 1, 3, 1 do
			if i == gDoorActive and gDoorPosition[i] < 30 then gDoorPosition[i] = gDoorPosition[i] + 1
			elseif gDoorPosition[i] > 0 then gDoorPosition[i] = gDoorPosition[i] - 1 end
		end
		
		for ix = 1, 3, 1 do
			garageDoor(ix * 35 - 20, 25, 30, 30, gDoorPosition[ix], 5, gDoorText[ix], 5)
		end 
		
		refresh2()
		wait(1)
	end
	
	continue = 0
end

function  garageDoor(x, y, width, height, position, panelHeight, text, margin)

	buffer = height/2
	graytext(x + margin, y + buffer, text)  --display text

	rect(x, y, x + width, y + height, 5, 5) --draw garage
	
	rect(x, y, x + width, y + height - position, 4, 0) --draw door
	
	--draw panels
	for iy = height - position, 0, -panelHeight do
		line(x, y + iy, x + width, y + iy, 4)
	end
end

function play()
	clear nil
	var = 0
	iy = 0
	for i=1, ai.number, 1 do
		--if i * 8 + 1 < 32 then ai.y[i] = 0 else ai.y[i] = ai.y[i] + 1 end
		ai.x[i] = 9+i*8+1
		ai.speed[i] = 5
		ai.topspeed[i] = 6
	end
	
	player.y = 0
	
	--[[--
	graytext(5, 5, "Please Wait...")
	refresh2()
	
	clear nil
	for iy=1, screenHeight, 1 do
		for ix = 1, screenWidth, 1 do
			if i == 1 then pixel(ix, iy, 4) i = 0 else i = i + 1 end
		end
		if i==0 then i = 1 else i = 0 end
	end
	--]]--
	
	line(42, 0, 42, 64, 4)
	line(84, 0, 84, 64, 4)
	
	refresh2()
	
	
	--game loop
	while not key(key_exit) do
		rect(43, 0, 83, 64, 0, 0) --clears only the road
		
		if key(key_left) and player.x > 0 then player.x = player.x - 1
			elseif key(key_right) and player.x < 40 - car.width + 1 then player.x = player.x + 1
			elseif key(key_shift) then player.speed = player.speed + 1
			elseif key(key_alpha) then player.speed = player.speed - 1
		end
		
		if player.speed > player.topspeed then player.speed = player.topspeed
			elseif player.speed < 0 then player.speed = 0
		end
		
		aiRace()
		
		spritexy player.x + 43, screenHeight - car.height, car.sprite
		player.y = player.y + player.speed
		refresh2()
		wait(3)
	end
	
	waitKey(key_exit)
	clear nil
end

function aiRace()
	for i=1, ai.number, 1 do
		buffer = ai.y[i] - player.y
		spritexy ai.x[i] + 43, screenHeight - car.height - buffer, car.sprite
		ai.y[i] = ai.y[i] + ai.speed[i]
		
	end
end

function optn()
	clear nil
	graytext(1, 1, "Options Menu")
	refresh2()
	waitKey(key_exit)
	clear nil
end

function help()
	clear nil
	graytext(1, 1, "How may I help you?")
	refresh2()
	waitKey(key_exit)
	clear nil
end

function bye()
	clear nil
	graytext(1, 1, "Bye!")
	refresh2()
	wait(100)
end

function waitKey(Key)
	while not key(Key) do wait(3) end
	wait(3)
end

function refresh2()
	--if invert == 1 then rect(0, 0, screenWidth, screenHeight) end
	if invert == 1 then fill(5) end
	refresh
end

main()


#13 veb

veb

    Casio Fan

  • Members
  • PipPip
  • 43 posts

Posted 05 June 2012 - 04:34 PM

you never set the case of ai.y, then their content are nil

#14 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 10 June 2012 - 01:42 AM

OK, new problem.

Error:
186: Attempt to index field speed (a number value)

Line 186:
ai.y[i] = ai.y[i] + ai.speed[i]

ai.speed[i] is given a value here (line 99):
if i == 1 then ai.speed[i] = ai.topspeed[i] else ai.speed = 1 + random(ai.topspeed[i]) end

Complete code:

Pastebin

Raw:
local rect = graydraw.rect
local setColor = graydraw.setcolor
local wait = misc.wait
local line = graydraw.line
local graytext = graydraw.text
local fill = graydraw.fill
local pixel = graydraw.pixel
local random = misc.random


car = {height = 8, width = 8, sprite = "\000\000<((f^fz(<V<(jvNnr8<V\000"}


screenWidth = 128
screenHeight = 64

ai = {x = {}, y = {}, speed = {}, topspeed = {}, number = 3}
continue = 0
done = 0
gDoorPosition = {0,0,0}
gDoorText = {"Play", "Optn", "Help"}
gDoorActive = 1
i = 0
invert = 0
Key = 0
key_alpha = 33
key_left = 37
key_right = 40
key_down = 38
key_up = 39
key_exit = 36
key_shift = 41
player = {x = 0, y = 0, speed = 0, topspeed = 6}



function main()
	setColor(1)
	menu()
end

function menu()
	clear nil
	
	while done ~= 1 do
		if key(key_left) then gDoorActive = gDoorActive - 1 wait(3) end
		if key(key_right) then gDoorActive = gDoorActive + 1 wait(3) end
		if key(key_exit) then done = 1 end
		if key(key_shift) then selection = gDoorActive continue = 1 end
		if gDoorActive < 1 then gDoorActive = 1 end
		if gDoorActive > 3 then gDoorActive = 3 end
		
		if selection == 1 then play()
			elseif selection == 2 then optn()
			elseif selection == 3 then help()
		end
		
		selection = 0
		
		for i = 1, 3, 1 do
			if i == gDoorActive and gDoorPosition[i] < 30 then gDoorPosition[i] = gDoorPosition[i] + 1
			elseif gDoorPosition[i] > 0 then gDoorPosition[i] = gDoorPosition[i] - 1 end
		end
		
		for ix = 1, 3, 1 do
			garageDoor(ix * 35 - 20, 25, 30, 30, gDoorPosition[ix], 5, gDoorText[ix], 5)
		end 
		
		refresh2()
		wait(1)
	end
	
	continue = 0
end

function  garageDoor(x, y, width, height, position, panelHeight, text, margin)

	buffer = height/2
	graytext(x + margin, y + buffer, text)  --display text

	rect(x, y, x + width, y + height, 5, 5) --draw garage
	
	rect(x, y, x + width, y + height - position, 4, 0) --draw door
	
	--draw panels
	for iy = height - position, 0, -panelHeight do
		line(x, y + iy, x + width, y + iy, 4)
	end
end

function play()
	clear nil
	var = 0
	iy = 0
	for i=1, ai.number, 1 do
		ai.y[i] = 0
		ai.x[i] = i*8+1
		ai.topspeed[i] = 6
		if i == 1 then ai.speed[i] = ai.topspeed[i] else ai.speed = 1 + random(ai.topspeed[i]) end
	end
	
	player.y = 0
	
	--[[--
	graytext(5, 5, "Please Wait...")
	refresh2()
	
	clear nil
	for iy=1, screenHeight, 1 do
		for ix = 1, screenWidth, 1 do
			if i == 1 then pixel(ix, iy, 4) i = 0 else i = i + 1 end
		end
		if i==0 then i = 1 else i = 0 end
	end
	--]]--
	
	line(42, 0, 42, 64, 4)
	line(84, 0, 84, 64, 4)
	
	refresh2()
	
	
	--game loop
	while not key(key_exit) do
		rect(43, 0, 83, 64, 0, 0) --clears only the road
		
		if key(key_left) and player.x > 0 then player.x = player.x - 1
			elseif key(key_right) and player.x < 40 - car.width + 1 then player.x = player.x + 1
			elseif key(key_shift) then player.speed = player.speed + 1
			elseif key(key_alpha) then player.speed = player.speed - 1
		end
		
		if player.speed > player.topspeed then player.speed = player.topspeed
			elseif player.speed < 0 then player.speed = 0
		end
		
		aiRace()
		
		spritexy player.x + 43, screenHeight - car.height, car.sprite
		player.y = player.y + player.speed
		refresh2()
	end
	
	waitKey(key_exit)
	clear nil
end

function aiRace()
	for i=1, ai.number, 1 do
		--ai and collisions with ai
		for j=1, ai.number do
			--if car i is not the same as car j
			if i~=j then
				--if car j is in front or behind car i
				if ai.x[j] > ai.x[i] - 16 and ai.x[j] < ai.x[i] + 16 then
					--if car j is in front
					if ai.y[j] > ai.y[i] and ai.y[j] < ai.y[i] + 16 then
						if ai.speed[i] > 0 then  ai.speed[i] = ai.speed[i] - 1 else ai.x[i] = ai.x[i] + 1 end
					end
					--if car j is behind
					if ai.y[j] < ai.y[i]  and ai.y[j] > ai.y[i] - 16 then
						if ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 else ai.x[i] = ai.x - 1 end
					end
				end
				--if car j is left or right of car i
				if ai.y[j] > ai.y[i] - 16 and ai.y[j] < ai.y[i] + 16 then
					--if car j is to the right
					if ai.x[j] > ai.x[i] and ai.x[j] < ai.x[i] + 16 then
						--if the car i is not against the left wall
						if ai.x[i] > 0 then ai.x[i] = ai.x[i] - 1 elseif ai.speed > 0 then ai.speed[i] = ai.speed[i] - 1 end
					end
					--if car j is to the left
					if ai.x[j] < ai.x[i] and ai.x[j] > ai.x[i] - 16 then
						--if car i is not against right wall
						if ai.x[i] < 40 then ai.x[i] = ai.x[i] + 1 elseif ai.speed > 0 then ai.speed[i] = ai.speed[i] - 1 end
					end
				end
			end
		end
	--collisions with player
	if ai.y[i] > player.y and ai.y[i] < player.y + 8 and ai.x[i] > player.x and ai.x[i] < player.x + 8 then crash() end
	
	--display
	buffer = ai.y[i] - player.y
	spritexy ai.x[i] + 43, screenHeight - car.height - buffer, car.sprite
	ai.y[i] = ai.y[i] + ai.speed[i]
	end
		
end

function crash()
	clear nil
	graytext(1, 1, "You Died!")
	refresh2()
	wait(100)
	menu()
end

function optn()
	clear nil
	graytext(1, 1, "Options Menu")
	refresh2()
	waitKey(key_exit)
	clear nil
end

function help()
	clear nil
	graytext(1, 1, "How may I help you?")
	refresh2()
	waitKey(key_exit)
	clear nil
end

function bye()
	clear nil
	graytext(1, 1, "Bye!")
	refresh2()
	wait(100)
end

function waitKey(Key)
	while not key(Key) do wait(3) end
	wait(3)
end

function refresh2()
	--if invert == 1 then rect(0, 0, screenWidth, screenHeight) end
	if invert == 1 then fill(5) end
	refresh
end

main()


#15 veb

veb

    Casio Fan

  • Members
  • PipPip
  • 43 posts

Posted 10 June 2012 - 01:25 PM

The error message explains that ai.speed contain a value and not a table,

In your line 99, there is a mistake in the 'else ...' : ai.speed is considerer not as a table, but as a number.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users