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

[lua] Tetris Problem

Started by Tron, 28 March 2012 - 03:42 PM
Tron #1
Posted 28 March 2012 - 05:42 PM
This was just a test, not the actual tetris. and Im not getting a error im just not seeing anything happen, here is code:
Spoiler

term.clear()
sleep(0.1)
wt = 0
width, height = term.getSize()
width = width/2
height = height/2
os.pullEvent = os.pullEventRaw
function blockstart()
 -- Telling it to draw the shape
 term.setCursorPos(spw1, sph1) 
 write("#")
 term.setCursorPos(spw2, sph2)
 write("#")
 term.setCursorPos(spw3, sph3) 
 write("#")
 term.setCursorPos(spw4, sph4)
 write("#")
 end
function newblock()
 --Setting Rotation to 1
 rot = 1
 -- Shape
 local bshape = math.ceil(math.random(0,3))
 if bshape == 0 then
  shape = "square" 
  end
 if bshape == 1 then
  shape = "line"
  end
 if bshape == 2 then
  shape = "l"
  end
 if bshape == 3 then
  shape="hill"
  end
 startposw = width/2
 startposh = 1
 -- setting what shapes look like
 if shape == "hill" then
  spw1 = startposw
  spw2 = spw1 -1
  spw3 = spw1 +1
  spw4 = spw1
  sph1 = startposh
  sph2 = sph1 -1
  sph3 = sph1 -1
  sph4 = sph1 -1
  blockstart()
  end
 if shape == "l" then
  spw1 = startposw
  spw2 = spw1
  spw3 = spw1
  spw4 = spw3 +1
  sph1 = startposh
  sph2 = sph1 -1
  sph3 = sph2 -1
  sph4 = sph3
  blockstart()
  end
 if shape == "line" then
  spw1 = startposw
  spw2 = spw1
  spw3 = spw1
  spw4 = spw1
  sph1 = startposh
  sph2 = sph1 -1
  sph3 = sph2 -1
  sph4 = sph3 -1
  blockstart()
  end
 if shape == "square" then
  spw1 = startposw
  spw2 = spw1 +1
  spw3 = spw1
  spw4 = spw2
  sph1 = startposh
  sph2 = sph1
  sph3 = sph1 -1
  sph4 = sph1 -1
  blockstart()
  end
 end
function updateblock() 
 if wt ~= 4 then
  wt = wt +1
  end
 if wt == 4 then
  sph1 = sph1 -1
  sph2 = sph2 -1
  sph3 = sph3 -1
  sph4 = sph4 -1
  blockstart()
  end
 end
function rotl()
 if rot == 1 then
  rot = 4
  if shape == "hill" then
  spw3 = spw1
  spw1 = spw2
  spw4 = spw4
  spw2 = spw4
  sph3 = sph1
  sph1 = sph2
  sph4 = sph4
  sph2 = sph4 -1
  end
 if shape == "square" then
  end
 if shape == "line" then
  spw3 = spw4 -1
  spw2 = spw3 -1
  spw1 = spw2 -1
  spw4 = spw4
  sph1 = sph4
  sph2 = sph4
  sph3 = sph4
  sph4 = sph4
  end
 if shape == "l" then
  sph4 = sph2
  sph2 = sph3
  sph1 = sph3
  sph3 = sph3
  spw2 = spw3 -1
  spw1 = spw2 -1
  spw4 = spw3
  spw3 = spw3
  end
 end
 if rot == 4 then
  rot = 3
  if shape == "hill" then
  spw3 = spw1
  spw1 = spw2
  spw4 = spw4
  spw2 = spw4 +1
  sph3 = sph1
  sph1 = sph2
  sph2 = sph4
  sph4 = sph4
  end
 if shape == "square" then 
  end
 if shape == "line" then
  spw1 = spw4
  spw2 = spw4
  spw3 = spw4
  spw4 = spw4
  sph3 = sph4 -1
  sph2 = sph3 -1
  sph1 = sph2 -1
  sph4 = sph4
  end
 if shape == "l" then
  spw4 = spw4
  spw3 = spw4 +1 
 spw2 = spw3
 spw1 = spw3
 sph4 = sph4
 sph3 = sph4
 sph2 = sph4 -1
 sph1 = sph2 -1
 end
end
 if rot == 3 then
  rot = 2
 if shape == "l" then
  sph4 = sph2
  sph2 = sph3
  sph1 = sph3
  sph3 = sph3
  spw2 = spw3 +1
  spw1 = spw2 +1
  spw4 = spw4
  spw3 = spw3
  end
 if shape == "square" then 
  end
 if shape == "line" then
  spw3 = spw4 +1
  spw2 = spw3 +1
  spw1 = spw2 +1
  spw4 = spw4
  sph3 = sph4
  sph2 = sph4
  sph1 = sph4
  sph4 = sph4
  end
 if shape == "hill" then
  spw3 = spw1
  spw1 = spw2
  spw2 = spw4
  spw4 = spw4
  sph3 = sph1
  sph1 = sph4
  sph2 = sph4 +1
  sph4 = sph4
  end
 end
 if rot == 2 then
  rot = 1
 if shape == "square" then
  end
 if shape == "line" then
  spw1 = spw4
  spw2 = spw4
  spw3 = spw4
  spw4 = spw4
  spw3 = spw4 +1
  spw2 = spw3 +1
  spw1 = spw2 +1
  end
 if shape == "hill" then
  spw3 = spw1
  spw1 = spw2
  spw2 = spw4 -1
  spw4 = spw4
  sph3 = sph4
  sph2 = sph4
  sph1 = sph4 +1
  end
 if shape == "l" then
  end
 end
end
function update()
 updateblock()
 os.startTimer(.1)
 end
newblock()
timer1 = os.startTimer(.1)
while true do
 event , varx = os.pullEvent()
 if event == "timer" then
  if varx == "timer1" then
   update()
   end
  end
 if event == "char" then
  if varx == "a" then
   rotl()
   end
 if varx == "e" then
  break
  end
 end
end
Thanks :o/>/>
Cloudy #2
Posted 28 March 2012 - 06:20 PM
What does the error message say? I doubt anyone is going to scour your code for errors without knowing where it is.
Advert #3
Posted 28 March 2012 - 06:37 PM
From my exmaination:

At line 2-3, bad global variables.

wt = 0
width, height = term.getSize()
-- + more all over the code
function blockstart() -- at line 8, etc.

At line 7, you are making ANY program run on the computer un-terminatable, I really don't recommend doing this, especially seeing as this program is only a game, and not a password protection or anything of that sort.

os.pullEvent = os.pullEventRaw

You are also using the global space to pass varaibles between functions, which is also very bad, because you can't see the flow of the program, and it's harder to track down errors.

I'd recommend taking a look at tables, as they'll save you a lot of trouble, especially when defining shapes, and perhaps reading through this book, while trying the stuff it explains in the interactive shell (type 'lua' in the shell): http://www.lua.org/pil/
Tron #4
Posted 28 March 2012 - 07:46 PM
From my exmaination:

At line 2-3, bad global variables.

wt = 0
width, height = term.getSize()
-- + more all over the code
function blockstart() -- at line 8, etc.

At line 7, you are making ANY program run on the computer un-terminatable, I really don't recommend doing this, especially seeing as this program is only a game, and not a password protection or anything of that sort.

os.pullEvent = os.pullEventRaw

You are also using the global space to pass varaibles between functions, which is also very bad, because you can't see the flow of the program, and it's harder to track down errors.

I'd recommend taking a look at tables, as they'll save you a lot of trouble, especially when defining shapes, and perhaps reading through this book, while trying the stuff it explains in the interactive shell (type 'lua' in the shell): http://www.lua.org/pil/
yeah I guess it doesnt need to be terminate proof, also I have made a game with tables to make walls but I dont understad how it will work in tetris :/
I dont really understand what global variables do, but I will try to remove them( I know how ) and what is global space?
What does the error message say? I doubt anyone is going to scour your code for errors without knowing where it is.
No error , just nothing happens.
Advert #5
Posted 28 March 2012 - 07:54 PM
From my exmaination:

At line 2-3, bad global variables.

wt = 0
width, height = term.getSize()
-- + more all over the code
function blockstart() -- at line 8, etc.

At line 7, you are making ANY program run on the computer un-terminatable, I really don't recommend doing this, especially seeing as this program is only a game, and not a password protection or anything of that sort.

os.pullEvent = os.pullEventRaw

You are also using the global space to pass varaibles between functions, which is also very bad, because you can't see the flow of the program, and it's harder to track down errors.

I'd recommend taking a look at tables, as they'll save you a lot of trouble, especially when defining shapes, and perhaps reading through this book, while trying the stuff it explains in the interactive shell (type 'lua' in the shell): http://www.lua.org/pil/
yeah I guess it doesnt need to be terminate proof, also I have made a game with tables to make walls but I dont understad how it will work in tetris :/
I dont really understand what global variables do, but I will try to remove them( I know how ) and what is global space?
What does the error message say? I doubt anyone is going to scour your code for errors without knowing where it is.
No error , just nothing happens.

The global space is the enviorment:

print() -- you are getting 'print' from the global space/enviorment, then calling it and passing any values to it inside the parenthesis ()
string -- A library/API in the enviorment
a = 2 -- a is now equal to 2 in the enviorment

The reason why you shouldn't modify it (especially to send values between functions) is because it's very easy to create conflicts. (you use something here, then you change it somewhere else, then you expect it to remain the original value it was)

In addition to that, if you want to call your function in different places, it might not work, and you'll have to re-write it.
Ian-Moone #6
Posted 28 March 2012 - 09:35 PM
when your done…
please release?