Tutorial: Space Shooter in 240 Lines!

Hello again everybody :slight_smile:

After having some positive feedback for the tutorial i did yesterday, i thought i would go ahead and make a second one today before i get too bogged down in work next week!

It’s based off the code i wrote in the last tutorial, but this time in space and we’re adding are own images, a scrolling background, a draggable player and a bit more of an advanced wave system.

Check it out here:
Tutorial: Space Shooter in 240 Lines!
I would once again recommend watching the video initially to see if the tutorial will be of any use :smiley:
Also for anyone who missed yesterdays more basic tutorial you can have a peak at it here:
Tutorial: Defense Game in 200 Lines!

The project files for both are available to download from the tutorial, so please feel to use them in whatever way you wish.
**Edit: Heres the latest tutorial as well :slight_smile:
Tutorial: Making a Slot Machine!**
Thanks again,

Jamie From T and G Apps Ltd.
[import]uid: 69826 topic_id: 24801 reply_id: 324801[/import]

Nice! Thank you for sharing!

Naomi [import]uid: 67217 topic_id: 24801 reply_id: 100590[/import]

Wow man, awesome tutorials! I loved both this one and your “defence game” tutorial. I’ve been learning so much by just reading through theese tutorials. Once again, thx man youre awesome!:smiley: [import]uid: 119384 topic_id: 24801 reply_id: 100606[/import]

Took me a little longer to get through this one, about 45 minutes.

Awesome. A person could apply this to so many genres, I’ve already thought about one piece that I thought could be a game in itself.

:slight_smile:

I think it’s important for people to look and try EVERY example that exists out there and read forum posts alot, even if it’s something you aren’t interested in. I find so many gems that way!

ng

Oh and yes, epic tutorial. I’m looking forward to more!!! Mmmmmm. [import]uid: 61600 topic_id: 24801 reply_id: 100614[/import]

+1 man! I’ve start reading the forum daily. Or actually im addicted to the forum. I think i check it atleast one time per hour XD But i’ve learned alot by just checking samples, tutorials and read the forum. I think i’ve gone through all of the tutorials at learningcorona.com :slight_smile:

Once again, Nice tutorial! [import]uid: 119384 topic_id: 24801 reply_id: 100620[/import]

Thanks for all the lovely comments guys. I’m glad that your finding them useful :slight_smile:

I will have to start doing some more advanced ones soon! Or just another genre of game… If any of you have a particular style of game that you’d like a tutorial for, I’d be more than happy to give it a go when I have some time :slight_smile:

All the positive comments definitely make me want to do more! Thanks again :slight_smile: [import]uid: 69826 topic_id: 24801 reply_id: 100626[/import]

Hey, TandG, it is so nice of you to offer. How about slot machine? I haven’t looked around the code-exchange or anywhere else, but I’m interested in seeing how slot-machine type game is put together.

Naomi [import]uid: 67217 topic_id: 24801 reply_id: 100633[/import]

Hmm that’s an interesting idea! Never even thought about making a slot machine. I will have a think about it and see if I can get it into a tutorial. Thanks for the suggestion :slight_smile: [import]uid: 69826 topic_id: 24801 reply_id: 100641[/import]

Good Tutorials.

Thank you for your good job.

Mila [import]uid: 83418 topic_id: 24801 reply_id: 100643[/import]

Ok so I’ve spent a crap ton of time with this code today.
I’m completely changing it to something else, but I’m trying to get a concept down and I am not sure what is up.

Here is what I would like to do:

In addition to shooting enemies with the lasers, how would I attach a listener to the enemies so I can remove them via touch?

The reason why I want to know how to do that as I see that method you are using on other samples, and spawning random objects. It’s learning how to get ACCESS to those damn objects that is making me mad.

I know if I have 2 local variables on screen and they both have the same name, and I attach a touch listener to do a display.remove(object) only one will disappear. That leads me to believe that what I am doing won’t work in the way I am trying. Of course, I can’t just remove the group as that removes them all! Arrrrgh.

Hmmmmmmmmmmmmm?

Ok on with the show, so here on on line 153

  
local function spawnEnemy()  
 local imageInt = mr(1,4) --\< use one of the 4 images)  
 local enemy = display.newImageRect("images/ship\_"..imageInt..".png",50,54) --imageInt is replaced with the # of the png, mmmk gotcha.   
  
 enemy.x = mr( 20, 300 ); enemy.y = -30; enemy.rotation = 180  
 enemy.name = "enemy"; physics.addBody( enemy, { isSensor = true } )  
 enemyGroup:insert( enemy )  

I tried to place this at the bottom of code, but seems like I can’t remove enemy, as it’s randomized between the 4 png’s in the images. So I know it’s something like removing a target, or doing some array or index or some other fancy pants term I don’t know that well. I understand YOUR code, but now I’m trying to learn and change things around a bit.

local function removeShip(event)  
 if event.phase == "began" then  
 print ("enemy ship touch test")  
---display.remove(enemy)  
 end  
 end  
enemy:addEventListener ("touch", removeShip)  

Of course that spits out can’t reference the “enemy” bla bla bla. I thought the enemy was a local variable in the function, but I can’t see it? I even tried putting

local enemy at the top so that it was a local variable.
Yes, I am still new. June 2011, seems a while ago but some of these things throw me off and at times I don’t “get it” haha.
So what you think? Is this a possible thing to do and remove enemies by touch, using your current code or what fancy pants things does a person have to do to get that to work?
-Nick [import]uid: 61600 topic_id: 24801 reply_id: 100657[/import]

Thanks Mila!

So basically you want to be able to click each enemy to remove them? Yes that shouldn’t be a problem :slight_smile:

You pretty much had it working above, but it was how you were accessing the enemy that was wrong. Fortunately it is an easy fix. In a touch event like that you can use “event.target” to access whichever object is linked to the function.

So heres the whole spawnEnemy function from before but with the remove code in:

local function spawnEnemy()  
 local imageInt = mr(1,4)  
  
 local function destoryMe( event )  
 if event.phase == "began" then  
 display.remove(event.target) --Reference to the enemy.  
 event.target = nil --Then nil the reference out.  
 end  
 return true --Remember to return true as well.  
 end  
  
 local enemy = display.newImageRect("images/ship\_"..imageInt..".png",50,54)  
 enemy.x = mr( 20, 300 ); enemy.y = -30; enemy.rotation = 180  
 enemy.name = "enemy"; physics.addBody( enemy, { isSensor = true } )  
 enemyGroup:insert( enemy )  
 enemy:addEventListener("touch", destoryMe) --New Listener  
  
 if spawned == spawnedMax then   
 wave = wave + 1 --Increase the wave.  
 if wave \<= 18 then --Limit max speed/spawn  
 enemySpeed = enemySpeed + 1   
 spawnIntMax = math.round(spawnIntMax \* 0.9)  
 end  
 spawned = 0 --Reset so that the next wave starts from 0  
 end  
 spawnInt = 0  
end  

If you just replace the spawnEnemy() function in yours with that it should all work fine :slight_smile:
It would probably be better if you were to put that destroy code outside of the spawn function, but for simplicities sake i just put it there as it works :smiley:

Let me know how it works for you! [import]uid: 69826 topic_id: 24801 reply_id: 100743[/import]

Oh and also you would probably have to add points to the overall game score in that function, as its not taking place in the collision code. [import]uid: 69826 topic_id: 24801 reply_id: 100745[/import]

ANOTHER one? You’re on fire! [import]uid: 52491 topic_id: 24801 reply_id: 100772[/import]

Haha :smiley:

It’s actually quite fun making tutorials, it also gives me a chance to improve my coding with things that I’ve forgotten.

Oh and I’m making another one now, thanks to Naomi’s it will be a slot machine demo :slight_smile: [import]uid: 69826 topic_id: 24801 reply_id: 100780[/import]

Thanks a lot, really great tutorial.

I’m currently developing my first game so seeing examples of other games and the coding frameworks are helping me a lot.

Keep up the good work.

Cheers

Nick [import]uid: 50787 topic_id: 24801 reply_id: 100797[/import]

Nice job! What do you use for a code viewer on your blog? [import]uid: 21331 topic_id: 24801 reply_id: 100804[/import]

Thanks Nick, I’m glad you enjoyed it :slight_smile:

Hi Tony, i use “Developer Formatter” which is a plugin for wordpress. Im not much of a fan of the colours it chooses for lua… but ohwell it does the job! [import]uid: 69826 topic_id: 24801 reply_id: 100807[/import]

Oh and I’m making another one now, thanks to Naomi’s it will be a slot machine demo :slight_smile:

Awesome! Can’t wait!

Naomi [import]uid: 67217 topic_id: 24801 reply_id: 100819[/import]

These are very good Tutorials !! I am a seasoned programmer and getting by with LUA. Something really stood out to me that I don’t think I have noticed anywhere else.

I would like to point this out because I makes so much sense and this really opens my eyes to making games even easier in Corona, and LUA.

When working with objects / groups moving them around in your gameLoop you spin thru the group using displayobject.numChildren ie (enemyGroup.numChildren)

That opens my eyes to know I don’t have to use global arrays etc to hold my enemy’s. Man that’s going to be a life saver!!
Larry
DoubleSlashDesign.com [import]uid: 11860 topic_id: 24801 reply_id: 100866[/import]

Thanks for the kind words larry! :slight_smile:

I’m glad you found them helpful! Looping through groups can be really helpful at times! [import]uid: 69826 topic_id: 24801 reply_id: 100941[/import]