Author Topic: [Lua] CubeField  (Read 37714 times)

0 Members and 1 Guest are viewing this topic.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [Lua] CubeField
« Reply #15 on: October 19, 2011, 02:47:58 am »
Probablye due to the settings. They all need to be to 0 I think (contrast and stuff).

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: [Lua] CubeField
« Reply #16 on: October 19, 2011, 03:28:39 am »
In case anyone wants to know how I got the source code (in Linux) :p
I opened his file with nspire_emu, and copied the source to the clipboard (of the device), and then use gcore to dump the memory of the entire nspire_emu process (fun).
Then I searched for some lua code and found the position in the dump. All I then did was copy and paste :D

(The built in memory dump function does not work for me)

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] CubeField
« Reply #17 on: October 19, 2011, 11:17:26 am »
Thank you for the code !
And yeah, I need to fix 2 or 3 things.

DJ_O :
I really don't understand that bug...
NVM, fixed
And I'll try to make cubes more diagonal when you turn. It shouldn't be hard, but It will decrease the framerate a bit.

Jonius7 :
Thanks for the screenshot !
But it looks kinda weird... The framerate is really low and the grey squares don't show up...

Anyway, I'm going to add the TNS document and Jonius7's screenshot to the first post.
Could someone tell me if the document works for OS>=3.0.2 ?
« Last Edit: October 19, 2011, 12:44:07 pm by Chockosta »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [Lua] CubeField
« Reply #18 on: October 19, 2011, 03:28:14 pm »
I acutally made a screenshot, but it's in AVI format and doesn't run at the original frame rate. It's still the exact same speed as the game, though.
« Last Edit: October 19, 2011, 03:30:18 pm by DJ_O »

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: [Lua] CubeField
« Reply #19 on: October 19, 2011, 03:28:31 pm »
I'm going to update the screenshot quite soon:
« Last Edit: October 19, 2011, 03:37:51 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [Lua] CubeField
« Reply #20 on: October 19, 2011, 03:32:33 pm »
I appended my avi to the previous post. It's 2.21 MB. On calc the game runs at about 20-25 FPS, but the video runs at 15.

Btw can Lua display rotated cubes? If not, then it might be best to forget about my screen rotation idea lol. Else, maybe you can cheat like in Squidgetx game and simply display sprites of squares and display alternate ones when they're far away or rotating?

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: [Lua] CubeField
« Reply #21 on: October 19, 2011, 03:42:02 pm »
Also another thing that could be added. In the original cubefield and the TI-84 version shown earlier, it starts with a corridor made out of cubes that the player travels through before entering the cubefield. Could this be implemented in the Lua version so it's not so easy to die instantly?

PS: I've uploaded the moving screenshot above.
« Last Edit: October 19, 2011, 06:13:46 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] CubeField
« Reply #22 on: October 19, 2011, 05:18:10 pm »
DJ_O :
The function gc:fillPolygon() could be used, it would be quite easy.
Thanks for the avi btw !

Jonius7 :
The corridor would be harder... Well, I would have to create levels instead of random cubes. But I could do this.
And your latest screenie is great ! Thanks ! :)

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: [Lua] CubeField
« Reply #23 on: October 19, 2011, 06:12:27 pm »
the corridor will only have to appear at certain times so from 0 - 150 score the corridor appears. Then maybe another one at 700 or something. You might have to extend where the black cubes come in from 1000 to maybe 1400. But the rest will be the same random cubes. I'm sure you can do this.
« Last Edit: October 19, 2011, 06:12:57 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: [Lua] CubeField
« Reply #24 on: October 19, 2011, 06:30:40 pm »
you can cheat like in Squidgetx game and simply display sprites of squares and display alternate ones when they're far away or rotating?

Heh heh. Basically, yeah XD
« Last Edit: October 19, 2011, 06:30:55 pm by squidgetx »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [Lua] CubeField
« Reply #25 on: October 22, 2011, 10:57:53 pm »
DJ_O :
The function gc:fillPolygon() could be used, it would be quite easy.
Thanks for the avi btw !

Jonius7 :
The corridor would be harder... Well, I would have to create levels instead of random cubes. But I could do this.
And your latest screenie is great ! Thanks ! :)
Yeah but can Lua actually display rotated polygons? If so, then that's epically awesome O.O

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: [Lua] CubeField
« Reply #26 on: October 23, 2011, 04:01:13 am »
DJ_O :
The function gc:fillPolygon() could be used, it would be quite easy.
Thanks for the avi btw !

Jonius7 :
The corridor would be harder... Well, I would have to create levels instead of random cubes. But I could do this.
And your latest screenie is great ! Thanks ! :)
Yeah but can Lua actually display rotated polygons? If so, then that's epically awesome O.O

gc:drawPoly({x1, y1, x2, y2, ... })
You can make your own rotation matrix, apply it and give the x, y couple result to drawPoly
« Last Edit: October 23, 2011, 04:01:38 am by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] CubeField
« Reply #27 on: October 23, 2011, 07:31:18 am »
Update !

-Now when you turn, the cubes become diagonal.
-The bug which caused some blocks that are farther to appear on top of the closer ones is fixed.
-Before the speed increases, a "Speed Up!" sign appears.
-Between 2 "levels", there is a corridor made of cubes.

Should I keep my little square for the player or should I create a sprite ?

As usual, I've attached a TNS file which contains the code. Could someone please extract the code ?
Thanks !

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: [Lua] CubeField
« Reply #28 on: October 23, 2011, 07:38:20 am »
Here :

Code: [Select]

cubesX={1,5,7,6,4,6,4,1,3,2,3,7,8,9,1,2,3,4,2,6,7,1,9,1,2,3,4}
cubesY={1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9}
lastCube=1
player=0
speed=0.08
moving=false
dir=1
moveStart=0
menu=true
score=0
level=1
color={225,170,0}
rotation=0

function render(gc)
 drawHorizon(gc)
 local size,x,y,i,polygon
 for j=1,27 do
  i=(j+lastCube-2)%27+1
  size=cubesY[i]*cubesY[i]/1.5+8
  x=(cubesX[i]-7-player)*20*(cubesY[i]*cubesY[i]/20+0.5)-size/2
  y=cubesY[i]*cubesY[i]*2-152
  polygon=rotate({x,y,x+size,y,x+size,y+size,x,y+size,x,y})
  if cubesY[i]>1 then
   gc:setColorRGB(color[1],color[2],color[3])
   gc:fillPolygon(polygon)
   gc:setColorRGB(0,0,0)
   gc:drawPolyLine(polygon)
  end
  if cubesY[i]>7.5 and cubesY[i]<8.5 then
   if x<7 and x+size>-7 then
    on.escapeKey()
   end
  end
 end
end

function rotate(p)
 local s,c=math.sin(rotation),math.cos(rotation)
 return {p[1]*c-p[2]*s+159,p[1]*s+p[2]*c+202,p[3]*c-p[4]*s+159,p[3]*s+p[4]*c+202,p[5]*c-p[6]*s+159,p[5]*s+p[6]*c+202,p[7]*c-p[8]*s+159,p[7]*s+p[8]*c+202,p[9]*c-p[10]*s+159,p[9]*s+p[10]*c+202}
end

function updateCubes()
 local step=menu and 0.06 or speed
 for i=1,27 do
  cubesY[i]=cubesY[i]+step
  if cubesY[i]>10 then
   if score%1000<800 then
    cubesX[i]=math.random(10,140)/10+player
   else
    cubesX[i]=math.random(10,140)/100+player+(i-1)%2*5+4
   end
   cubesY[i]=1
   lastCube=lastCube==1 and 27 or lastCube-1
  end
 end
end

function updateMove()
 if moving then
  player=player+0.07*dir
  if math.abs(rotation)<0.1 then
   rotation=rotation-0.005*dir
  end
  if timer.getMilliSecCounter()-moveStart>=150 then
   moving=false
  end
 else
  if math.abs(rotation)>0.005 then
   rotation=rotation-0.005*rotation/math.abs(rotation)
  end
 end
end

function drawPlayer(gc)
 if not menu then
  --gc:drawImage(playerImg,149,192)
  gc:setColorRGB(100,100,100)
  gc:fillRect(149,192,20,20)
 end
end

function drawHorizon(gc)
 local s,c=math.sin(rotation),math.cos(rotation)
 gc:setColorRGB(0,0,0)
 gc:drawLine(s*142-c*200+159,202-c*142-s*200,c*200+s*142+159,s*200-c*142+202)
end

function drawMenu(gc)
 if menu then
  gc:setColorRGB(0,0,0)
  gc:setFont("sansserif","r",30)
  gc:drawString("CubeField",80,10,"top")
  gc:setFont("sansserif","b",12)
  gc:drawString("Press Enter",115,150,"top")
  local highscore=var.recall("highscore")
  if not highscore then
   var.store("highscore",0)
   highscore=0
  end
  if score>highscore then
   var.store("highscore",score)
   document.markChanged()
  end
  local str="Score : "..tostring(score).."              Highscore : "..tostring(highscore)
  gc:drawString(str,159-gc:getStringWidth(str)/2,100,"top")
 end
end

function drawScore(gc)
 if not menu then
  gc:setColorRGB(0,0,0)
  gc:setFont("sansserif","r",12)
  gc:drawString(tostring(score),2,0,"top")
  if score%1000>950 then
   gc:setFont("sansserif","r",30)
   gc:drawString("Speed Up !",80,10,"top")
  end
 end
end

function levelUp()
 speed=speed+0.02
 level=level+1
 if (level-1)%5==0 then
  color={225,170,0}
 elseif (level-1)%5==1 then
  color={0,0,255}
 elseif (level-1)%5==2 then
  color={100,100,100}
 elseif (level-1)%5==3 then
  color={90,255,0}
 elseif (level-1)%5==4 then
  color={255,255,255}
 end
end

function on.paint(gc)
 render(gc)
 drawPlayer(gc)
 drawMenu(gc)
 drawScore(gc)
 gc:setColorRGB(0,0,0)
 gc:setFont("sansserif","r",8)
 gc:drawString("Lua CubeField - Par Loic Pujet",10,200,"top")
 timer.start(0.01)
end

function on.timer()
 timer.stop()
 updateCubes()
 updateMove()
 if not menu then
  score=score+1
  if score%1000==0 then
   levelUp()
  end
 end
 platform.window:invalidate()
end

function on.escapeKey()
 menu=true
end

function on.enterKey()
 if menu then
  menu=false
  speed=0.08
  moving=false
  score=0
  level=1
  color={225,170,0}
  for i=1,27 do
   cubesY[i]=cubesY[i]-5
  end
 end
end

function on.arrowLeft()
 if not menu then
  dir=-1
  moving=true
  moveStart=timer.getMilliSecCounter()
 end
end

function on.arrowRight()
 if not menu then
  dir=1
  moving=true
  moveStart=timer.getMilliSecCounter()
 end
end

function on.charIn(ch)
 if ch=="6" then
  on.arrowRight()
 elseif ch=="4" then
  on.arrowLeft()
 end
end


Very smooth game :)

and ...



but wait... you did all that oncalc ?
(NO WAY ?)

if so, I definitely can do something for you .... ( ;-)  )
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] CubeField
« Reply #29 on: October 23, 2011, 07:40:46 am »
Thanks a lot !
And yes, I entirely did that on-calc. (Just love on-calc programming)

What can you do for me ? :)