I’m very new to codea and lua I’ve been reading the codea and lua ebooks by ignatz ,sorry if the code is sloppy

All you do is drag your player and dodge the ball it gets faster and faster as you progress, and the ball moves randomly after awhile

If there’s anything I can do to make it neat please let me know

``````-- Game
function setup()
displayMode(LANDSCAPE_LEFT)
PlayerX = WIDTH/2
PlayerY = HEIGHT/2
PlayerWIDTH = 100
PlayerHEIGHT = 190
deathY = 0
deathX = math.random(10,100)
deathSpeedX = 5
deathSpeedY = 5
deathWIDTH = 10
deathHEIGHT = 10
passes = 0

end

function draw()
background(40, 40, 50)

if deathX>1000 then passes = passes + 1
end

ellipse(PlayerX,PlayerY,100)

deathX = deathX + deathSpeedX

if deathX>1100 then deathX = -50
deathY = math.random(0,800)
end

if passes == 30 then deathSpeedX = deathSpeedX + 5
passes = 0
end

if deathSpeedX>20 then deathY = deathY + math.random(-10,20)

end

if deathX>PlayerX-PlayerWIDTH/2 and deathX<=PlayerX+PlayerWIDTH/2 and
deathY>PlayerY-PlayerHEIGHT/2 and deathY<=PlayerY+PlayerHEIGHT/2 then  restart()

end

function touched(touch)

if touch.x then PlayerX = touch.x
if touch.y then PlayerY = touch.y
end

end
end
end
``````

Use three (~) before the beginning and after the ending of code to format it. I fixed your above post for you.

Ah I was wondering why my post did that. Thanks

@GLaDOS anytime! Cool little game! I really like when the balls speed up and zig zag. Add in something to keep track of a score. I’m having trouble seeing if I’m actually lasting longer then the previous attempt!

Keep it up!

You seem to have the touched function inside the draw function. I’m guessing you were got confused by all the if statements.

It is much easier to see what is going on if you lay the code out neatly, as below…

But otherwise, it looks fine. Just keep practising and learning!

``````-- Game
function setup()
displayMode(LANDSCAPE_LEFT)
PlayerX = WIDTH/2
PlayerY = HEIGHT/2
PlayerWIDTH = 100
PlayerHEIGHT = 190
deathY = 0
deathX = math.random(10,100)
deathSpeedX = 5
deathSpeedY = 5
deathWIDTH = 10
deathHEIGHT = 10
passes = 0
end

function draw()
background(40, 40, 50)

if deathX>1000 then passes = passes + 1 end

ellipse(PlayerX,PlayerY,100)
deathX = deathX + deathSpeedX

if deathX>1100 then
deathX = -50
deathY = math.random(0,800)
end

if passes == 30 then
deathSpeedX = deathSpeedX + 5
passes = 0
end

if deathSpeedX>20 then
deathY = deathY + math.random(-10,20)
end

if deathX>PlayerX-PlayerWIDTH/2 and deathX<=PlayerX+PlayerWIDTH/2
and deathY>PlayerY-PlayerHEIGHT/2 and
deathY<=PlayerY+PlayerHEIGHT/2 then
restart()
end
end

function touched(touch)
if touch.x then PlayerX = touch.x end
if touch.y then PlayerY = touch.y end
end
``````

I’m going to keep adding to it and make it better once I learn more. Thanks for all the support

Above all, it’s quite normal to feel stupid. I feel stupid at least 10 times a day.

Nice, it qualifies for the 50 lines challenge http://twolivesleft.com/Codea/Talk/discussion/4230/50-line-challenge#Item_24

Ah didn’t even notice that

@GLaDOS, If you want to add a score, here’s a sample of a method based on how long the player survives:

``````Score = ElapsedTime
text("Score: "..string.format("%.0f",Score))
``````

@CodeaNoob Oh Thanks I couldn’t figure how to add a score, I tried it your way but I wasn’t outting …string.format(“%.of” guess I was doing it wrong Thanks

Nice first game!

@CodeaNoob Or… `math.floor(ElapsedTime)` produces the same result.

@SkyTheCodef Thanks I’ll see which way I like better text or math

@clemantines A bit off topic, but nice avatar

If you get in an endless loop, you have to kill Codea. It happens sometimes if you have a bug.

You shouldn’t have that happen often, though, so don’t worry too much.

Hi, I’m sorry for the beginner’s problem, but my daughter and I tried out the code above (Ignatz December 12) and are in an infinite loop. Can someone please tell us how to get back to our code and tell us what to add to avoid the infinite loop? (We tried these instructions but didn’t get the red thing to tap: “Try exiting Codea and double tapping the home button. A little thing should come up at the bottom of the screen. Hold down on the Codea icon until a little red thing comes up in the corner. Tap the red thing.”
Thank you so much for helping us out. This is such a nice community, and we will try hard to search for our answers instead of asking whenever possible!

Welcome to our forum, which is very family friendly. We have users of all ages here.

I wouldn’t use this code as it is a beginner’s attempt, and buggy. Rather start with the tutorials on the wiki page at the top, or, if you want to see some simple games right away, look at the code on this thread:

http://twolivesleft.com/Codea/Talk/discussion/4230/50-line-challenge

We recommend starting with learning Lua, the language behind Codea, and there is an ebook on the wiki link for this. There is a learning curve, as with all programming languages, but many of us use Codea every day because we enjoy it so much.

Please feel free to ask questions if you get stuck. As long as you have tried to solve it yourself first, we enjoy helping to fix problems. (We all started where you are now).

Thank you! I will follow your advice on where to start and now bought & downloaded some LUA refs. But can you tell us how to escape the infinite loop without shutting down Codea app? We are stuck and can’t find instructions in the forum on how to “escape” to get back to our code. I apologize!

Go back to the main iPad screen, doubleclick to bring up the current running apps, if iOS7, swipe Codea upwards, if iOS6 press on the little x at the corner of Codea.

If all else fails, reboot (hold down both iPad buttons until it turns off and starts turning on again.

By the way, you don’t need to buy Lua refs. I’ve written lots of tutorials on the wiki link above, plus an ebook referenced there, all free.