How I start to write my first Codea program

Hi, I have seen a few questions about How to start in Codea.
When I started with Codea I had the same question.
I can only tell what I have done maybe someone can use it.
First I started with running the example programs.
Reading the whole forum from question 1, that will keep you busy for a few days.
I have tried almost every example in the forum and was wondering will I ever understand this.
I can tell you I have seen great things some of them I have not figured out yet.
There are also some great tutorials around they are very helpful.
I have reed Lua-manuals because Codea is based on Lua.
After all this I was time to write a program from scratch.
I had found a way to make backups of my code, a structure/template I could use.
And a simple game I know very well, an old pen and paper game I played when I was a kid :slight_smile:
When I was programming the game I found out that I had to use a lot of ‘tricks’
to make a working game. So my advice is start simple it will be hard enough to complete your program.
On the end I had used tables ,keys, sound, buttons and even a shader and more.
The game is now playable you play against the computer, I only have to made the way the computer plays more intelligent.
I like to know what you think of the game/program, maybe there are things I can improve.

Here is the dropbox link:

I used dropbox , because I have not figured out how works :slight_smile:

@Dreamdancer, to use Github, check out this awesome utility that @Briarfox created:


Install it using the auto installer and then add it as a dependency to your project. Make sure you have Github account. Run AutoGist and sync it to your Github account. Add the necessary lines of code at the top of your main tab and in setup(). Run your project with the new code in it and AutoGist takes care of all your backup needs. You can also create an auto installer for your project using AutoGist.

@Slashin8r that sounds great I will look into it. thanks

@Dreamdancer nice battleships game! You definitely have a good basis :slight_smile: I have a few suggestions which I hope you find useful :slight_smile:

Firstly from a UI (user interface) perspective the fonts, game board and general look and feel is very good! :slight_smile:

However make sure you include a menu so your users can start a new game or exit if they want too.

When your playing more of a delay/transition is required between the switch from computer to human and back again its a little disorientating going between blue and bright yellow and orange if your playing fast :wink: and perhaps some sprites for the ships and water for finishing touches (which I’m sure you’ve thought of already!)

From a code perspective, during play appears very robust and there’s no immeadiately apparent bugs :).

Viewing in the editor some code appears duplicated partly duplicated in places. Consider, a computer player can do everything a human player can do just the difference is the computer does these things automatically. This is where classes are really useful :slight_smile: you could create 3 like so:

Player base class. Has all the common functionality between players like shooting etc.


function Player:init()

function Player:setup()
-- get notification of new game

function Player:place(ship)

function Player:fireat(player,square)

function Player:moveStarted()
-- get notification of turn
function Player:moveFinished()
-- notify turn finished

function Player:touched(touch)

HumanPlayer - has all the common functions from Player and uses them with the touched method and would use setup and moveStarted to know when to accept ship placements or moves.

HumanPlayer = class(Player)
function HumanPlayer:init()

function HumanPlayer:touched(touch)

Computer player - still has all the common functions from Player but uses them differently. ignores touch input, places ships in the setup function and makes moves in the moveStarted function

ComputerPlayer = class(Player)
function ComputerPlayer:init()
function ComputerPlayer:setup()
-- choose strategy 
-- place ships
function ComputerPlayer:moveStarted()
-- calculate move / best move
--  do move 

This is called inheritance and polymorphism, which you have probably seen mentioned in your extensive reading :slight_smile: It allows you to share functionality between different objects by it being “inherited” but also have it do things differently (polymorphism) where needed without having to rewrite potentially lots of code:)

You could then apply if further with Players to go on to create difficulties e.g EasyComputerPlayer =class(ComputerPlayer),MediumComputerPlayer =class(ComputerPlayer)etc :). It’s also something you could use in other areas of your game but it should also be noted that just like anything there are times where you should and shouldn’t use it :slight_smile:

Very nice game. I beat the computer. Just barely. I look the game over screen, and the button tints.

@XanDDemoX - thank you, you gave me my next blog post

@Ignatz wow! thank you :slight_smile: