Author Topic: [Lua] Space Invaders  (Read 25755 times)

0 Members and 1 Guest are viewing this topic.

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
[Lua] Space Invaders
« on: June 03, 2011, 02:56:02 pm »
Hello everybody !

That's my first post here, so I'll try to introduce myself.
First, I'm French. I am a very bad English speaker (well, I'm fifteen and our English teacher sucks) so I apologize for all the nonsenses and mistakes that I'll do.
I was a member of TI-Bank, and I left this website because of all the troubles (fights between admins, etc)
So the most interesting website about calculators was Omnimaga, and I registered.
I have a TI-83+ and a TI-Nspire (Non-CAS & clickpad), and I code in Axe, in C for Nspire and in Lua. I also program in C with OpenGL on my PC, but that's not really interesting.
Well, I think I've enough talked about myself.

I made this post because I wanted feedbacks (not sure about this word) for my newest project, a Space Invaders game in Lua.
It looks like the original game, excepted that there's only one kind of enemies, and there's no "houses". Also, the enemies doesn't go faster as they go down, but the speed increases with the level.

The player can shoot with the up arrow, but that's not really convenient. If you had any better idea...
Also, I've never tried it on a calc. I don't have any CX and I use the OS 2.0.1 on my clickpad. If you could try it on your calc (and post your hi-score !)
EDIT : final version, updated.

Thank you,
Chockosta (Loic Pujet)

Lua code (Edited, final version):
Code: [Select]
function on.charIn(ch)
if ch=="r" then
blocks={5,5,5,5,5,5,5,5,5,5,5,5}
blockAttacked=0
game="notfinished"
score=0
player=100
shotX=0
shotY=0
level=1
wait=0.04
aliensDir="right"
down="no"
aliensX={}
aliensY={}
shotsX={}
shotsY={}
lives=3
for i=1,21,1 do
aliensX[i]=(i-1)%7*31+1
aliensY[i]=math.floor((i-1)/7)*25+10
shotsX[i]=aliensX[i]
shotsY[i]=200
end
end
if ch=="c" then
originalColors=not originalColors
end
end

function blocktest(x,y)
blockAttacked=0
if y>170 and y<177 then
if x>30 and x<40 and blocks[1]>0 then
blockAttacked=1
elseif x>40 and x<=50 and blocks[2]>0 then
blockAttacked=2
elseif x>50 and x<=60 and blocks[3]>0 then
blockAttacked=3
elseif x>60 and x<=70 and blocks[4]>0 then
blockAttacked=4
elseif x>140 and x<=150 and blocks[5]>0 then
blockAttacked=5
elseif x>150 and x<=160 and blocks[6]>0 then
blockAttacked=6
elseif x>160 and x<=170 and blocks[7]>0 then
blockAttacked=7
elseif x>170 and x<=180 and blocks[8]>0 then
blockAttacked=8
elseif x>250 and x<=260 and blocks[9]>0 then
blockAttacked=9
elseif x>260 and x<=270 and blocks[10]>0 then
blockAttacked=10
elseif x>270 and x<=280 and blocks[11]>0 then
blockAttacked=11
elseif x>280 and x<=290 and blocks[12]>0 then
blockAttacked=12
end
end
end


on.charIn("r")


function on.arrowLeft()
if player>0 then
player=player-10
end
end
function on.arrowRight()
if player<290 then
player=player+10
end
end
function on.tabKey()
if shotY<=0 then
shotY=195
shotX=player+13
end
end


function on.paint(gc)
if originalColors then
gc:setColorRGB(0,0,0)
gc:fillRect(0,0,platform.window:width(),platform.window:height())
end

if not originalColors then gc:setColorRGB(0,0,0)
else gc:setColorRGB(255,255,255) end
gc:setPen("thin","smooth")
gc:setFont("sansserif","r",8)
gc:drawString("Lua Alien Invaders - Par Loic Pujet",10,200,"top")

if game=="notfinished" then
if not originalColors then gc:setColorRGB(200,0,0)
else gc:setColorRGB(255,255,255) end
for i=1,21,1 do
if aliensY[i]~=0 then
gc:drawArc(aliensX[i]+4,aliensY[i]-9,10,10,0,360)
gc:fillArc(aliensX[i]-5,aliensY[i]-4,28,8,0,360)
end
end

if not originalColors then gc:setColorRGB(0,0,200)
else gc:setColorRGB(255,255,0) end
for i=1,21,1 do
if shotsY[i]<200 then
gc:fillRect(shotsX[i],shotsY[i],2,5)
end
end
if shotY>0 then
gc:fillRect(shotX,shotY,4,6)
end

if not originalColors then gc:setColorRGB(100,0,255)
else gc:setColorRGB(0,255,0) end
gc:fillRect(player,195,30,10)
gc:fillRect(player+13,191,4,4)
gc:setFont("sansserif","r",10)
gc:drawString("Lives:"..tostring(lives).." Score:"..tostring(score).." Level:"..tostring(level),0,0,"top")

if not originalColors then add=3
else add=0 end
for i=1,12,1 do
if blocks[i]>0 then
gc:fillRect(i*10+math.floor((i-1)/4)*70+20,180-blocks[i]*2,10,blocks[i]*2)
end
end
else
if var.recall("highscore")==nil then
var.store("highscore",0)
else
if var.recall("highscore")<score then
var.store("highscore",score)
document.markChanged()
end
end
if not originalColors then gc:setColorRGB(0,0,0)
else gc:setColorRGB(255,255,255) end
gc:setFont("sansserif","b",15)
gc:drawString("Score : "..tostring(score).."        High score : "..tostring(var.recall("highscore")),50,50, "top")
if not originalColors then gc:setColorRGB(0,0,255) end
if locale.name()=="fr" then
gc:drawString("Appuyez sur R pour rejouer",50,150,"top")
else
gc:drawString("Press R to try again",80,150,"top")
end
end

if wait>0.01 then
timer.start(wait)
else
timer.start(0.01)
end

end


function on.timer()
timer.stop()

if game=="notfinished" then
if aliensDir=="right" then
for i=1,21,1 do
if aliensY[i]>0 then
aliensX[i]=aliensX[i]+1
end
end
else
for i=1,21,1 do
if aliensY[i]>0 then
aliensX[i]=aliensX[i]-1
end
end
end

if aliensDir=="right" then
for i=1,21,1 do
if aliensX[i]>295 then
down="yes"
end
end
else
for i=1,21,1 do
if aliensX[i]<5 then
down="yes"
end
end
end

if down=="yes" then
down="no"
for i=1,21,1 do
if aliensY[i]>0 then
aliensY[i]=aliensY[i]+25
end
end
if aliensDir=="left" then
aliensDir="right"
else
aliensDir="left"
end
end

for i=1,21,1 do
if aliensY[i]>180 then
game="finished"
end

if shotsY[i]>=200 and aliensY[i]~=0 then
if math.random(1,100)==5 then
shotsY[i]=aliensY[i]+math.random(1,10)
shotsX[i]=aliensX[i]
end
else
shotsY[i]=shotsY[i]+3
end

blocktest(shotsX[i],shotsY[i])
if blockAttacked>0 then
shotsY[i]=201
blocks[blockAttacked]=blocks[blockAttacked]-1
end

if shotsY[i]<200 then
if shotsY[i]>195 and shotsX[i]>player and shotsX[i]<player+30 then
lives=lives-1
shotsY[i]=200
end
end

if shotY>0 and shotY>aliensY[i]-5 and shotY<aliensY[i]+5 and shotX>aliensX[i]-10 and shotX<aliensX[i]+21 then
aliensY[i]=0
aliensX[i]=100
shotY=0
score=score+1
end
end

blocktest(shotX,shotY)
if blockAttacked>0 then
shotY=-1
blocks[blockAttacked]=blocks[blockAttacked]-1
end

if shotY>=0 then
shotY=shotY-5
end

if lives<=0 then
game="finished"
end


wintest=0
for i=1,21,1 do
wintest=wintest+aliensY[i]
end

if wintest==0 then
if wait>0.01 then
wait=wait-0.005
end
level=level+1
aliensDir="right"
down="no"
for i=1,21,1 do
aliensX[i]=(i-1)%7*31+1
aliensY[i]=math.floor((i-1)/7)*25+10
shotsX[i]=aliensX[i]
shotsY[i]=200
end
end
end

platform.window:invalidate()
end

I attached the .tns It works on OS 3.0.1 and OS 3.0.2 (edited : bug fixed)
« Last Edit: June 29, 2011, 03:12:50 am by Chockosta »

Offline JosJuice

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1344
  • Rating: +66/-14
    • View Profile
Re: [Lua] Space Invaders
« Reply #1 on: June 03, 2011, 03:00:02 pm »
Welcome to Omnimaga! Have you uploaded your game yet so that others can play it? I think you can't upload it as an attachment in an Omnimaga post before you've made a certain amount of posts, so maybe you could try a file sharing site like mediafire.com.

Also, your English seems to be fine. :)

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: [Lua] Space Invaders
« Reply #2 on: June 03, 2011, 03:06:12 pm »
Hey, and welcome Chockosta!
Its nice to see another Lua programmer :)

I just tried your game on my touchpad, but for some reason I immediately die :/

Quote
The player can shoot with the up arrow, but that's not really convenient. If you had any better idea...
Maybe the TAB key is better?

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: [Lua] Space Invaders
« Reply #3 on: June 03, 2011, 03:09:12 pm »
Hello everybody !
Hi there :)

That's my first post here, so I'll try to introduce myself.
Welcome!

First, I'm French. I am a very bad English speaker (well, I'm fifteen and our English teacher sucks) so I apologize for all the nonsenses and mistakes that I'll do.
That ain't a problem. There are people who can't speak English at all. We have a French board for those n00bs :)

I was a member of TI-Bank, and I left this website because of all the troubles (fights between admins, etc)
Oh God. They play The Game too?

So the most interesting website about calculators was Omnimaga, and I registered.

I have a TI-83+ and a TI-Nspire (Non-CAS & clickpad), and I code in Axe, in C for Nspire and in Lua. I also program in C with OpenGL on my PC, but that's not really interesting.
I have a TI 84 + myself.

Well, I think I've enough talked about myself.

I made this post because I wanted feedbacks (not sure about this word) for my newest project, a Space Invaders game in Lua.
Feedback (It's already plural)

It looks like the original game, excepted that there's only one kind of enemies, and there's no "houses". Also, the enemies doesn't go faster as they go down, but the speed increases with the level.
You did that because it was easier to make or because you wanted to alter these things?

The player can shoot with the up arrow, but that's not really convenient. If you had any better idea...
Also, I've never tried it on a calc. I don't have any CX and I use the OS 2.0.1 on my clickpad. If you could try it on your calc (and post your hi-score !)
I don't have a Nspire :(

Thank you,
Chockosta (Loic Pujet)
:)
If you like my work: why not give me an internet?








Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: [Lua] Space Invaders
« Reply #4 on: June 03, 2011, 03:18:00 pm »
Sounds good I use OS 2.0.1 so unfortunately I won't be able to play this. I refuse to upgrade to 3 till downgrading and Ndless become possible.
You should start an introduction topic in the intro board so you can get your peanuts to make your calc run faster.
Your English seems almost as good as mine and I live in America and thats my first language.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: [Lua] Space Invaders
« Reply #5 on: June 03, 2011, 03:18:46 pm »
I have looked for the bug, and this is what I found

Code: [Select]
         for i=1,21,1 do
            if aliensY>0 then
               aliensY=aliensY+25
            end
         end

For some reason you put for loops arround every check, while it is not necessary.
This piece of code will loop 21 times, which means that aliensY=aliensY+525 .
And that is why you die.

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Space Invaders
« Reply #6 on: June 03, 2011, 03:37:34 pm »
Strange... It works perfectly on my emulator. (Nspire_emu)
Oooooh i understood !
The "spoiler" option suppress all the '[i ]'  of tables ! Let's try it with "code"
Code: [Select]
function on.charIn(ch)
if ch=="r" then
game="notfinished"
score=0
player=100
shotX=0
shotY=0
wait=0.04
aliensDir="right"
down="no"
aliensX={}
aliensY={}
shotsX={}
shotsY={}
lives=3
for i=1,21,1 do
aliensX[i]=(i-1)%7*31+1
aliensY[i]=math.floor((i-1)/7)*25+10
shotsX[i]=aliensX[i]
shotsY[i]=200
end
end
end


on.charIn("r")


function on.arrowLeft()
if player>0 then
player=player-10
end
end
function on.arrowRight()
if player<290 then
player=player+10
end
end
function on.arrowUp()
if shotY<=0 then
shotY=195
shotX=player+13
end
end


function on.paint(gc)
gc:setColorRGB(0,0,0)
gc:setPen("thin","smooth")
gc:setFont("sansserif","r",8)
gc:drawString("Lua Alien Invaders - Par Loic Pujet",10,200,"top")

if game=="notfinished" then
gc:setColorRGB(200,0,0)
for i=1,21,1 do
if aliensY[i]~=0 then
gc:drawArc(aliensX[i]+4,aliensY[i]-9,10,10,0,360)
gc:fillArc(aliensX[i]-5,aliensY[i]-4,28,8,0,360)
end
end

gc:setColorRGB(0,0,200)
for i=1,21,1 do
if shotsY[i]<200 then
gc:fillRect(shotsX[i],shotsY[i],2,5)
end
end
if shotY>0 then
gc:fillRect(shotX,shotY,4,6)
end

gc:setColorRGB(100,0,255)
gc:fillRect(player,195,30,10)
gc:fillRect(player+13,191,4,4)
gc:setFont("sansserif","r",10)
gc:drawString("vies:"..tostring(lives).." score:"..tostring(score),0,0,"top")
else
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","b",15)
gc:drawString("Score : "..tostring(score),100,50, "top")
gc:setColorRGB(0,0,255)
gc:drawString("Appuyez sur R pour rejouer",50,150,"top")
end

timer.start(wait)
end


function on.timer()
timer.stop()

if game=="notfinished" then
if aliensDir=="right" then
for i=1,21,1 do
if aliensY[i]>0 then
aliensX[i]=aliensX[i]+1
end
end
else
for i=1,21,1 do
if aliensY[i]>0 then
aliensX[i]=aliensX[i]-1
end
end
end

if aliensDir=="right" then
for i=1,21,1 do
if aliensX[i]>305 then
down="yes"
end
end
else
for i=1,21,1 do
if aliensX[i]<15 then
down="yes"
end
end
end

if down=="yes" then
down="no"
for i=1,21,1 do
if aliensY[i]>0 then
aliensY[i]=aliensY[i]+25
end
end
if aliensDir=="left" then
aliensDir="right"
else
aliensDir="left"
end
end

for i=1,21,1 do
if aliensY[i]>200 then
game="finished"
end
end

for i=1,21,1 do
if shotsY[i]>=200 and aliensY[i]~=0 then
if math.random(1,100)==5 then
shotsY[i]=aliensY[i]+math.random(1,10)
shotsX[i]=aliensX[i]
end
else
shotsY[i]=shotsY[i]+3
end
end
if shotY>=0 then
shotY=shotY-5
end

for i=1,21,1 do
if shotsY[i]<200 then
if shotsY[i]>195 and shotsX[i]>player and shotsX[i]<player+30 then
lives=lives-1
shotsY[i]=200
end
end
end
if lives==0 then
game="finished"
end

if shotY>0 then
for i=1,21,1 do
if shotY>0 and shotY>aliensY[i]-5 and shotY<aliensY[i]+5 and shotX>aliensX[i]-15 and shotX<aliensX[i]+15 then
aliensY[i]=0
aliensX[i]=100
shotY=0
score=score+1
end
end
end

wintest=0
for i=1,21,1 do
wintest=wintest+aliensY[i]
end
if wintest==0 then
if wait>0.02 then
wait=wait-0.005
end
aliensDir="right"
down="no"
for i=1,21,1 do
aliensX[i]=(i-1)%7*31+1
aliensY[i]=math.floor((i-1)/7)*25+10
shotsX[i]=aliensX[i]
shotsY[i]=200
end
end
end

platform.window:invalidate()
end


function on.arrowKey(key)
platform.window:invalidate()
end

Yay, it works !
I join a screenie :

Translate it to English if you want.

BTW, I saw that we could join pictures. How can I do that ?


« Last Edit: June 03, 2011, 03:39:01 pm by Chockosta »

Ashbad

  • Guest
Re: [Lua] Space Invaders
« Reply #7 on: June 03, 2011, 03:38:54 pm »
I looked through some of your code, and I noticed a few things wrong with it:

- unoptimized math commands.  While it's not huge, using player+=10 is a lot better than player=player+10.
- what's the deal with all of the for loops?  I don't see the point in many of them.
- you should chain if statements together more, it'll remove the need for much of the redundancies and might even half your code size.
- why do you assign different functions to keys instead of just making a checkKeys() function or just putting all checks inline?  Or are you overriding previously existing functions provided by an NLua module?

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: [Lua] Space Invaders
« Reply #8 on: June 03, 2011, 03:40:20 pm »
you use the drawimage function with the image screen. Thats the limit of my knowledge unfortunately
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] Space Invaders
« Reply #9 on: June 03, 2011, 03:41:19 pm »
I looked through some of your code, and I noticed a few things wrong with it:

- unoptimized math commands.  While it's not huge, using player+=10 is a lot better than player=player+10.
- what's the deal with all of the for loops?  I don't see the point in many of them.
- you should chain if statements together more, it'll remove the need for much of the redundancies and might even half your code size.
- why do you assign different functions to keys instead of just making a checkKeys() function or just putting all checks inline?  Or are you overriding previously existing functions provided by an NLua module?

I just started in LUA...
The '+=' works ? Omg I was fed up with all the 'player=player+10'
I will check all the other things tomorrow...

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: [Lua] Space Invaders
« Reply #10 on: June 03, 2011, 03:44:05 pm »
It looks like the original game, excepted that there's only one kind of enemies, and there's no "houses". Also, the enemies doesn't go faster as they go down, but the speed increases with the level.
Only one thing (don't have the time) : In the real game, the ennemy speed increases when they were less only because it was a performance lack. More ennemy there were, more the game was laggy !
« Last Edit: June 03, 2011, 05:04:36 pm 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

Ashbad

  • Guest
Re: [Lua] Space Invaders
« Reply #11 on: June 03, 2011, 03:44:18 pm »
I looked through some of your code, and I noticed a few things wrong with it:

- unoptimized math commands.  While it's not huge, using player+=10 is a lot better than player=player+10.
- what's the deal with all of the for loops?  I don't see the point in many of them.
- you should chain if statements together more, it'll remove the need for much of the redundancies and might even half your code size.
- why do you assign different functions to keys instead of just making a checkKeys() function or just putting all checks inline?  Or are you overriding previously existing functions provided by an NLua module?

I just started in LUA...
The '+=' works ? Omg I was fed up with all the 'player=player+10'
I will check all the other things tomorrow...

I can help you rewrite if you want, I think I could probably cut out ~60 lines of code if I do it correctly.  Also, for a beginning Lua programmer, it's actually not bad, and you should be proud ;)

Most double math commands like +=, -=, and the like usually work in lua, with exceptions of a few good ones like ++, --, and depending on the platform and what version was ported, the bitwise shifting ops.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: [Lua] Space Invaders
« Reply #12 on: June 03, 2011, 03:58:05 pm »
I believe this code should run a little faster. I haven't tested it yet though
Spoiler For Hopefully faster non buggy code:
Code: [Select]
function on.charIn(ch)
   if ch=="r" then
      game="notfinished"
      score=0
      player=100
      shotX=0
      shotY=0
      wait=0.04
      aliensDir="right"
      down="no"
      aliensX={}
      aliensY={}
      shotsX={}
      shotsY={}
      lives=3
      for i=1,21,1 do
         aliensX=(i-1)%7*31+1
         aliensY=math.floor((i-1)/7)*25+10
         shotsX=aliensX
         shotsY=200
      end
   end
end


on.charIn("r")


function on.arrowLeft()
   if player>0 then
      player=player-10
   end
end
function on.arrowRight()
   if player<290 then
      player=player+10
   end
end
function on.arrowUp()
   if shotY<=0 then
      shotY=195
      shotX=player+13
   end
end


function on.paint(gc)
   gc:setColorRGB(0,0,0)
   gc:setPen("thin","smooth")
   gc:setFont("sansserif","r",8)
   gc:drawString("Lua Alien Invaders - Par Loic Pujet",10,200,"top")

   if game=="notfinished" then
      gc:setColorRGB(200,0,0)
      if aliensY~=0 then
         gc:drawArc(aliensX+4,aliensY-9,10,10,0,360)
         gc:fillArc(aliensX-5,aliensY-4,28,8,0,360)
      end

      gc:setColorRGB(0,0,200)
      if shotsY<200 then
         gc:fillRect(shotsX,shotsY,2,5)
      end
      if shotY>0 then
         gc:fillRect(shotX,shotY,4,6)
      end

      gc:setColorRGB(100,0,255)
      gc:fillRect(player,195,30,10)
      gc:fillRect(player+13,191,4,4)
      gc:setFont("sansserif","r",10)
      gc:drawString("vies:"..tostring(lives).." score:"..tostring(score),0,0,"top")
   else
      gc:setColorRGB(0,0,0)
      gc:setFont("sansserif","b",15)
      gc:drawString("Score : "..tostring(score),100,50, "top")
      gc:setColorRGB(0,0,255)
      gc:drawString("Appuyez sur R pour rejouer",50,150,"top")
   end

   timer.start(wait)
end


function on.timer()
   timer.stop()
   
   if game=="notfinished" then
      if aliensDir=="right" then
         if aliensY>0 then
            aliensX+=1
         end
      else
         if aliensY>0 then
            aliensX-=1
         end
      end

      if aliensDir=="right" then
         if aliensX>305 then
            down="yes"
         end
      else
         if aliensX<15 then
            down="yes"
         end
      end

      if down=="yes" then
         down="no"
         if aliensY>0 then
            aliensY+=25
         end
         if aliensDir=="left" then
            aliensDir="right"
         else
            aliensDir="left"
         end
      end

      if aliensY>200 then
         game="finished"
      end

      if shotsY>=200 and aliensY~=0 then
         if math.random(1,100)==5 then
            shotsY=aliensY+math.random(1,10)
            shotsX=aliensX
         end
      else
          shotsY+=3
      end
      if shotY>=0 then
         shotY-=5
      end

      if shotsY<200 then
         if shotsY>195 and shotsX>player and shotsX<player+30 then
            lives-=1
            shotsY=200
         end
      end
      if lives==0 then
         game="finished"
      end
     
      if shotY>0 then
         if shotY>0 and shotY>aliensY-5 and shotY<aliensY+5 and shotX>aliensX-15 and shotX<aliensX+15 then
            aliensY=0
            aliensX=100
            shotY=0
            score+=1
         end
      end

      wintest=0
      wintest=wintest+aliensY
      if wintest==0 then
         if wait>0.02 then
            wait-=0.005
         end
         aliensDir="right"
         down="no"
         for i=1,21,1 do
            aliensX=(i-1)%7*31+1
            aliensY=math.floor((i-1)/7)*25+10
            shotsX=aliensX
            shotsY=200
         end
      end
   end

function on.arrowKey(key)
   platform.window:invalidate()
end
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Ashbad

  • Guest
Re: [Lua] Space Invaders
« Reply #13 on: June 03, 2011, 04:08:29 pm »
I went through really quickly and tried to cut off as much as I could in ~5 minutes.  Here's what I did in a nutshell:

-I put together the X and Y lists for less loops in the for statements, should double their speed
-I optimized math
-I took out a few logic things that weren't nessicary

I think there's still huge potential to optimize with this further, but make the rest of your game before it becomes unreadable! ;)

Code: [Select]
function on.charIn(ch)
if ch=="r" then
game="notfinished"
score=0
player=100
shotX=0;shotY=0
wait=0.04
aliensDir="right"
down="no"
aliensXY={}
shotsXY={}
lives=3
for i=1,21,2 do
aliensXY[i]=(math.ceil(i/2)-1)%7*31+1
aliensXY[i+1]=math.floor((math.ceil(i+1/2)-1)/7)*25+10
shotsXY[i]=aliensX[i]
shotsXY[i+1]=200
end
end
end


on.charIn("r")


function on.arrowLeft()
if player~=0 then
player-=10
end
end
function on.arrowRight()
if player<290 then
player+=10
end
end
function on.arrowUp()
if shotY <= 0 then
shotY=195
shotX+=13
end
end


function on.paint(gc)
gc:setColorRGB(0,0,0)
gc:setPen("thin","smooth")
gc:setFont("sansserif","r",8)
gc:drawString("Lua Alien Invaders - Par Loic Pujet",10,200,"top")

if game=="notfinished" then
gc:setColorRGB(200,0,0)
for i=1,21,2 do
if aliensXY[i+1]~=0 then
gc:drawArc(aliensXY[i]+4,aliensXY[i+1]-9,10,10,0,360)
gc:fillArc(aliensXY[i]-5,aliensXY[i+1]-4,28,8,0,360)
end
end

gc:setColorRGB(0,0,200)
for i=1,21,2 do
if shotsXY[i+1]<200 then
gc:fillRect(shotsXY[i],shotsxY[i+1],2,5)
end
end
if shotY>0 then
gc:fillRect(shotX,shotY,4,6)
end

gc:setColorRGB(100,0,255)
gc:fillRect(player,195,30,10)
gc:fillRect(player+13,191,4,4)
gc:setFont("sansserif","r",10)
gc:drawString("vies:"..tostring(lives).." score:"..tostring(score),0,0,"top")
else
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","b",15)
gc:drawString("Score : "..tostring(score),100,50, "top")
gc:setColorRGB(0,0,255)
gc:drawString("Appuyez sur R pour rejouer",50,150,"top")
end

timer.start(wait)
end


function on.timer()
timer.stop()

if game=="notfinished" then
if aliensDir=="right" then
for i=1,21,2 do
if aliensXY[i+1]>0 then
aliensXY[i]+=1
end
end
else
for i=1,21,2 do
if aliensXY[i+1]>0 then
aliensXY[i]-=1
end
end
end

if aliensDir=="right" then
for i=1,21,2 do
if aliensXY[i]>305 then
down="yes"
end
end
else
for i=1,21,2 do
if aliensXY[i]<15 then
down="yes"
end
end
end

if down=="yes" then
down="no"
for i=1,21,2 do
if aliensXY[i+1]>0 then
aliensXY[i+1]+=25
end
end
if aliensDir=="left" then
aliensDir="right"
else
aliensDir="left"
end
end

for i=1,21,2 do
if aliensXY[i+1]>200 then
game="finished"
end
end

for i=1,21,2 do
if shotsXY[i+1]>=200 and aliensXY[i+1]~=0 then
if math.random(1,100)==5 then
shotsXY[i+1]=aliensXY[i+1]+math.random(1,10)
shotsXY[i]=aliensXY[i]
end
else
shotsXY[i+1]+=3
end
end
if shotY>=0 then
shotY-=5
end

for i=1,21,2 do
if shotsXY[i+]<200 then
if shotsXY[i+1]>195 and shotsXY[i]>player and shotsXY[i]<player+30 then
lives-=1
shotsXY[i+1]=200
end
end
end
if lives==0 then
game="finished"
end

if shotY ~= 0 then
for i=1,21,2 do
if shotY~=0 and shotY>aliensXY[i+1]-5 and shotY<aliensXY[i+1]+5
   and shotX>aliensXY[i]-15 and shotX<aliensXY[i]+15 then
aliensXY[i+1]=0
aliensXY[i]=100
shotY=0
score+=1
end
end
end

wintest=0
for i=1,21,2 do
wintest+=aliensXY[i+1]
end
if wintest==0 and
if wait>0.02 then
wait-=0.005
end
aliensDir="right"
down="no"
for i=1,21,2 do
aliensXY[i]=(math.ceil(i/2)-1)%7*31+1
aliensXY[i+1]=math.floor((math.ceil(i+1/2)-1)/7)*25+10
shotsXY[i]=aliensX[i]
shotsXY[i+1]=200
end
end
end

platform.window:invalidate()
end


function on.arrowKey(key)
platform.window:invalidate()
end


EDIT: suggested new optimizations:

- there are some areas where do in pairs loops would be beneficial.
- using numbers for directions rather than strings.
- instead of using built in shape methods to draw your objects, I'm sure using sprite data would be a faster choice.
« Last Edit: June 03, 2011, 04:11:13 pm by Ashbad »

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: [Lua] Space Invaders
« Reply #14 on: June 03, 2011, 04:31:48 pm »
Most double math commands like +=, -=, and the like usually work in lua, with exceptions of a few good ones like ++, --, and depending on the platform and what version was ported, the bitwise shifting ops.

Good try, but ... you're wrong.
On the TI-Nspire Lua langage you can't do that.
Example of the given error :
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua