# simple ball paddle example

Here is a simple ball and paddle example for new coders. Slide your finger right/left to move paddle.

``````
displayMode(FULLSCREEN)    -- set full screen mode

function setup()
-- create screen edge
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))    -- left
e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))    -- top
e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))    -- right
e4=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))    -- bottom

-- create multiple balls
ballTable={}    -- ball table
for z=1,6 do    -- create 6 balls
b=physics.body(CIRCLE,20) -- create a circle physics body
b.position=vec2(math.random(WIDTH),math.random(HEIGHT)) -- random x,y
b.linearVelocity=vec2(500,400)    -- set initial speed
b.gravityScale=0    -- turn off gravity for balls
b.restitution=1    -- set bouncyness of balls
b.friction=0    -- set friction to 0
table.insert(ballTable,b)    -- put ball info ball table
end

-- create paddle with a size 100 x 10
p=physics.body(POLYGON,vec2(-50,5),vec2(50,5),vec2(50,-5),vec2(-50,-5))
p.position=vec2(WIDTH/2,HEIGHT/2)    -- set initial x,y position
p.gravityScale=0    -- turn off gravity for paddle
p.type=STATIC    -- set so paddle won't react to collision
end

function draw()
background(40, 40, 50)    -- clear screen to defined color
fill(255)    -- white fill color for balls
noStroke()    -- don't draw outline around ball or paddle
for nbr,ball in pairs(ballTable) do    -- loop thru ball table
ellipse(ball.x,ball.y,40)    -- draw balls at x,y position
end
fill(255,0,0)    -- red fill color for paddle
p.x=CurrentTouch.x    -- paddle position from touch
rect(p.x-50,p.y-5,100,10)    -- draw paddle
end

``````

@West - thanks man, I’ll give it a go when I get the chance. I’m already learning lots with the improvised version I’ve been playing around with. If you don’t mind, I might use some of this code for a future side scroller idea I have in mind. The funny thing is, I had a really similar idea a few weeks back before I’d even heard of Codea but with a fly instead of a bee. It even had descending spiders and everything. Not sure if I’ll do that now!

Ps it’s this kind of well documented template style game code (albeit with slightly easier graphics replacement) that I had in mind in the above comments…

Vry nice program @Dave1707 . Would fit the 50 line challenge too. It is a good starting point for people who want to improve it an make it a game by adding features.

@Jmv38 I think that simple examples are what the new coders need. They don’t need to be overwhelmed by a lot of fancy stuff, just basic code that does something to get them started. The smaller it is, the easier to understand.

@dave1707 - I’ve been saying since I joined that we need to replace most of the current demo projects with simpler versions, and move the fancy stuff to the wiki

@Ignatz I don’t know if we need to replace the current demos. Maybe we just need a place to put simple examples that the new coders can easily find. They can look thru them to find what they want to start with instead of constantly asking how to do the same thing over and over.

@dave1707 - I have simple examples like that all through my ebooks and blog posts, which answer many of the frequent questions we get here.

But unfortunately, some people don’t RT*M no matter what you do.

@Ignatz I know what you mean. Your ebooks contain a lot of information that I even use. The problem is some new coders don’t want to read anything. They want to copy something that they can use immediately. That’s why I think little working examples might give them what they want and simple enough that they might understand what’s happening.

@dave1707 - We already have some projects like that in the wiki, including what I think are the most useful - step by step projects, because they gradually increase in complexity over several tabs.

And I’m not sure anyone looks at those…

I’m a newbie so I feel I have a valid opinion on this.
I agree with Dave1707 when he said simple things like this are good for beginners, I personally find things like this really helpful and I can follow it line by line and understand how it works, whereas with a large complex example I cannot as I tend to ‘get lost’ in the middle of it.

let me also say, without meaning to be mean to the Codea creators, that I personally find/found the built-in examples to be too complex and large, I can only say for myself, but I would have preferred many tiny examples of Codea rather than the ones we have now. but its only my opinion

@Tyson I think the built in examples are more for coders who have a good understanding of programming and are looking for more complex examples. I sometimes find it hard to understand some of the code in the examples unless I really study what’s going on. That’s why I feel simple, small code is perfect for the new coders. They can always build on it as the gain speed.

I think the built-in examples are less of a ‘tutorial’ example and more of a showing something possible with Codea, like there are some for some of the cooler features just showing what can be done. Ex: physics, 3D (roller coaster, and cubes), animation

I agree with JakAttack. The examples are for showing what can be done. I have look at Ignatz awesome ebooks, but sometimes I want to do, and break, and create simple stuff without leaving Codea. Simple examples would have helped but as always RT*M

RT*M?

@JakAttak - Read The [insert expletive here] Manual.

Usually used with people who refuse to read instructions.

I’m also a new codea user and have just finished my first proper game after just a couple of weeks, and I can say without doubt that I wouldn’t have got anywhere without reading Ignatz’s books (both of them!) so big, big thanks for writing them. (I’m also someone who finds Dave1707’s code snippets hugely useful… side note: was it you that posted the maze generator once Dave? It’s been invaluable!)

But I also agree that new users could really benefit with a range of well-chosen, well documented generic game examples that they can read through and adapt to their own purposes. Not sure what other people think about this but I also made the point on another thread that providing some fairly complete generic game templates with Codea that non-programmers could easily adapt (dropping in their own graphics Construct2/Gamepress style and cutting and pasting code to create new levels) could really bring in some additional revenue and open up Codea beyond it’s relatively niche user base. A lot of iOS users are desperately looking for just such a tool and there isn’t a lot else out there. Certainly not of the same standard and versatility. I’d be interested to hear other people’s thoughts!

@ScottDafydd - the problem is that all of us are just like you - we are doing this for fun, and to learn. There are only a couple of developers, working on Codea part time, and they spend all their time working on improving Codea.

Creating templates of the kind you suggest is a great idea, but the problem with that, is that it’s a lot of work, and not much fun. You pretty much have to create a game before you know exactly what needs to be in the template. Who wants to do it?

What some of us began creating was a set of step-by-step projects, where they start simple in the first code tab, and each tab adds something extra. There are in the wiki, and I think they are a good way for people to learn.

I would be happy to help but I’m not doing that all on my own.

@Ignatz Fair enough. I only make the point because I think Codea is a truly amazing bit of kit for the price, and would love to see it both opened up to non-coders and bring in some well-deserved revenue for its devs. In your experience, how big a job do you think it would be? It seems like most of the code for a simple range of games is already out there (I’m thinking at the very least a vertical shooter, scrolling platformer, infinite runner and infinite jumper) so would probably just involve tidying up the code and writing some easy directions in the comments. Maybe it’s even something the community could do as a gift for the devs!

@ScottDafydd - many people have already contributed a lot of time and effort to Codea in various ways, to support it. We do see that as a way of thanking the developers.

But Codea will never be an app for non programmers. There are code-less drag and drop apps for those people. Codea requires programming skills, and it would take an enormous effort to parametrize it sufficiently that it could be used without coding.

I certainly think it is possible to create a game template, though. It’s difficult to estimate time and effort, but you may be talking about anything up to 50 hours. I’ve been toying with the idea of building and blogging a sidescroller, which I haven’t tried yet (and Simeon’s great Juice library that does little animations would be very useful), but I don’t really like playing them so I haven’t got round to it.

Yes, I know what you mean. A platform game has to be fairly exceptional to lift it from the average. Could the ‘foggy bummer’ code be any use perhaps? I tried to take a look at it last week but couldn’t get the sprites to work for some reason…

By the way, I didn’t mean any disrespect by what I said to either you or the Codea Community. On the contrary I’m hugely impressed by Codea and the community that has sprung up around it, and meant it as a compliment. Like I said, your books have been a massive help!