Author Topic: This is my first lua game  (Read 8562 times)

0 Members and 1 Guest are viewing this topic.

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
This is my first lua game
« on: August 02, 2011, 08:25:53 am »
Since l don't have enough posts to upload the file,l'll upload the code ,if you're interested in it, you can try it.
Code: [lua]
Code: [Select]
x=0 T,F=0,0  b="" u=0 t=0
aa=math.random(1,10)
bb=math.random(1,10)   ab=aa+bb
ts=0.06 life=10 level="learner"
function on.charIn(char)
mm=timer.getMilliSecCounter()
ch=char
if x>130 then u=1 t=t+1 end
end

function loop()
if x>220 then x=0 t=0 u=0
aa=math.random(1,10)
bb=math.random(1,10)   ab=aa+bb
draw1,draw2=0,0
return loop() end end

draw=class()
function draw:paint(gc)
gc:setPen("thick","dotted")
gc:drawRect(60,150,200,50)

if draw1==1 then
gc:setColorRGB(180,180,180)
gc:fillRect(60,150,200,50)
if mm+90<timer.getMilliSecCounter()
then draw1=0 end
end
if draw2==1 then
gc:setColorRGB(20,20,20)
gc:fillRect(60,150,200,50)
if mm+90<timer.getMilliSecCounter()
then draw2=0 end
end
end

function judge()
if x>130 and x<200 and  ab>10 and u==1 then
   if ch=="6"  then b="true"  
   elseif ch~="6" and ch~="a" and ch~="b" and ch~="c" and ch~="d" and ch~="p" then b="false" end
end
if x>130 and x<200  and  ab<10 and u==1 then
   if ch=="4"  then b="true"  
   elseif ch~="4" and ch~="a" and ch~="b" and ch~="c" and ch~="d" and ch~="p" then b="false" end
end
if x>130  and x<200 and  ab==10 and u==1 then
   if ch=="5"  then b="true"  
   elseif ch~="5" and ch~="a" and ch~="b" and ch~="c" and ch~="d" and ch~="p" then b="false" end
end
--if u==0 and t==0 then b="false" t=1 end
if b=="true" and t==1 then
T=T+1 b="" u=0 draw1=1 end
if b=="false" and t==1 then
F=F+1 b="" u=0 draw2=1 end
end
function on.paint(gc)
draw:paint(gc)
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","bi",30)
gc:drawString(aa.." + "..bb,110,x,"top")
--gc:setColorRGB(100,100,100)
--gc:drawString(aa.." + "..bb,110,x-30,"top")
gc:setFont("sansserif","bi",10)
gc:drawString("T: "..T.."  ".."F: "..F,0,15,"top")
gc:drawString("u: "..u,0,30,"top")
gc:drawString("t: "..t,0,45,"top")
gc:drawString("ab: "..ab,0,60,"top")
gc:drawString("x: "..x,0,75,"top")
gc:drawString("life: "..life,0,90,"top")
gc:drawString("level: "..level,0,105,"top")
if F>life then
highscore()
gc:fillRect(0,0,330,250)
gc:setColorRGB(255,255,255)
gc:setFont("sansserif","bi",30)
gc:drawString("You Lost",95,50,"top")
gc:setFont("sansserif","bi",15)
gc:drawString("your score: "..T,120,110,"top")
gc:drawString("highscore: "..var.recall("highscore"),120,130,"top")
gc:drawString("Press R to restart",170,180,"top")
if ch=="r" then x=300 T=0 F=0
return loop() end
end
timer.start(ts)
if ch=="p" then timer.stop() end
if ch=="a" then ts=0.05 life=8 level="easy" end
if ch=="b" then ts=0.04 life=6 level="medium"end
if ch=="c" then ts=0.03 life=5 level="hard" end
if ch=="d" then ts=0.02 life=3 level="extreme" end
end
function on.enterKey()
ch="xx"
timer.start(ts)
platform.window:invalidate()
end
function highscore()
if var.recall("highscore")==nil or T>var.recall("highscore")
then var.store("highscore",T)
end
var.monitor("highscore")
end

--[[function on.varChange(varlist)
  local highscore=var.recall("highscore")
 if not highscore then return -3 end
if type(highscore)~="number" then return -2 end
if highscore<0 then return -1 end
end]]

function on.timer()
local ss=0.45*T+3
if ss>15 then ss=15 end
x=x+(ss)
loop()
judge()
platform.window:invalidate()
end
And of course, l didn't finish it,if you have some good ideas,you can tell me.
[4]-----"<10"
[5]-----"=10"
[6]-----">10"
[a\b\c\d]------"change speed"
[p\enter]-------"pause\continue"  
« Last Edit: August 03, 2011, 06:48:04 am by cypressx1 »

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: This is my first lua game
« Reply #1 on: August 02, 2011, 11:55:06 am »
Hey,

This game is a great idea ! :)

At first I didn't realize I had to enter either 4/5/6 so I thought it didn't work :P

So, for people who like screenshots :


One thing I would like to tell you especially is that your code should be cleaner (it's better for us readers and especially for you).
For example, indentation (maybe that's just because the forum didn't get it right, though).

Also, you're making a lot of tests (if ... ~= ... etc.) that might be completely rewritten in a function to compare directly all the numbers/variables you're using. Thus, saving cpu, battery, time, game speed etc.
(the function judge() especially can be very optimized, I think)

In conclusion, you're saying it's your first Lua game, so I must say it's a very good start. You understood how the nspire lua API worked and your document can be used for educational purposes, so that's also a good point :)

Now, what about making the game more difficult with other calculations than basic additions ?

;)

NB : I just found a bug (idk if that's because I tested it on the computer software ant not a calculator...) : the text is not centered at the game over screen :
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: This is my first lua game
« Reply #2 on: August 02, 2011, 11:59:45 am »
I think instead of "true" and "false", you should use the actual boolean values true and false. You can use these directly in conditional statements without having to compare them to anything. Instead of b=="true" you can just use b, and instead of b=="false" you can use not b.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline zeldaking

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 197
  • Rating: +15/-0
    • View Profile
Re: This is my first lua game
« Reply #3 on: August 02, 2011, 12:16:15 pm »
That looks interesting, To bad I don't have ti-nspire.

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: This is my first lua game
« Reply #4 on: August 03, 2011, 06:33:49 am »
oh!! thanks for your advice. l'll fix it.

Offline chattahippie

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +27/-0
  • Super Member! :D
    • View Profile
Re: This is my first lua game
« Reply #5 on: August 03, 2011, 06:37:28 am »
Since l don't have enough posts to upload the file,l'll upload the code ,if you're interested in it, you can try it.

You can attach a file to the main post, I don't think that has a post limit

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: This is my first lua game
« Reply #6 on: August 03, 2011, 06:50:21 am »
What?! it really works.l have been uploaded it

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: This is my first lua game
« Reply #7 on: August 03, 2011, 06:59:45 am »
* Levak Lost the game ...
<_<
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: This is my first lua game
« Reply #8 on: August 03, 2011, 07:31:20 am »
what's the score?

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: This is my first lua game
« Reply #9 on: August 03, 2011, 07:55:37 am »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline cypressx1

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: This is my first lua game
« Reply #10 on: August 03, 2011, 08:17:56 am »
Er...it's off limits to me

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: This is my first lua game
« Reply #11 on: August 03, 2011, 12:50:17 pm »
For a first program, this is definitely very good. Nice job, cypress! :)