Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How Can I Improve My Code?

I have a lot of code for this game I am making. So I guess this is a two part question. First of all how do I link lots of code to a forum (Here I worked around the issue by making a google doc with the code . . . link below)? Secondly I would love some simple feedback about this program. I'm afraid it is very repetitive. There is also a problem with the scoring which I cannot figure out. Thanks a lot!

link to code:



  • dave1707dave1707 Mod
    Posts: 8,725

    You need to learn about tables. That will cut down the size of the program a lot. What is the game supposed to do.

  • The game is a first person shooter (2d of course) with dots as the enemy. You have thirty seconds to eliminate as many as you can. As soon as you kill all 5 and new 5 pop up moving faster. You control your "gun" using the accelerometer.

  • dave1707dave1707 Mod
    Posts: 8,725

    Interesting game. I replaced the sprite I don't have with a small circle. One problem, if you tilt the iPad too far, the screen rotates. You need to lock the screen.

  • Thanks @dave1707 ! Do you have any idea how to fix the scoring?

  • IgnatzIgnatz Mod
    Posts: 5,396

    you can put your code on Github

  • dave1707dave1707 Mod
    Posts: 8,725

    @AIDENCODER123 Before worrying about why the scoring isn't working properly, I would spend the time learning about tables. You have so much repetitive code and tables could eliminate a lot of it. Once you reduce the code, it would be so much easier to fix the scoring. You have a very interesting game and you should continue to learn more coding. You could then make the code easier to update or fix errors. After learning tables, you should then look into classes. Classes can also eliminate a lot of repetitive coding.

  • Thank you both!

  • IgnatzIgnatz Mod
    edited January 2015 Posts: 5,396

    @AIDENCODER123 - I've written an ebook on Lua that tries to explain tables, among other things. I also have a bunch of blog posts on tables.

    You can find all of them here, ebooks at the top

  • dave1707dave1707 Mod
    edited January 2015 Posts: 8,725

    @AIDENCODER123 Here's an example of what tables can do. I took your example and wrote something similar. I think you should look at this example and learn what you can from it and then try to modify your code. Your code is a great start and I think you can learn a lot by trying to condense it. That's the quickest way to learn.

    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT) function setup() bullets=15 fontSize(40) score=0 rectMode(CENTER) ellipseMode(CENTER) createTargets() cx=WIDTH/2 cy=HEIGHT/2 dx,dy=0,0 gameOver=false end function createTargets() bullets=bullets+5 targets={} for z=1,5 do table.insert(targets,{x=math.random(WIDTH), y=math.random(HEIGHT),spX=math.random(-1,1),spY=math.random(-1,1)}) end end function draw() background(0, 255, 187, 255) fill(255,0,0) text("SCORE "..score,WIDTH/2,HEIGHT-50) text("BULLETS "..bullets,WIDTH/2,HEIGHT-100) fill(255) if gameOver then background(0) text(" GAME OVER\ntriple tap to restart",WIDTH/2,HEIGHT/2) return end for a,b in pairs(targets) do rect(b.x,b.y,10,10) b.x=b.x+b.spX b.y=b.y+b.spY if b.x>WIDTH or b.x<0 then b.spX=-b.spX end if b.y>HEIGHT or b.y<0 then b.spY=-b.spY end end cx=cx+Gravity.x*20 cy=cy+Gravity.y*20 if cx<0 then cx=0 end if cx>WIDTH then cx=WIDTH end if cy<0 then cy=0 end if cy>HEIGHT then cy=HEIGHT end noFill() stroke(255) strokeWidth(2) ellipse(cx,cy,30) end function touched(t) if t.state==BEGAN then if gameOver then if t.tapCount==3 then gameOver=false createTargets() bullets=20 end return end bullets=bullets-1 if bullets==0 then gameOver=true return end for a,b in pairs(targets) do v1=vec2(cx+dx,cy+dy) if v1:dist(vec2(b.x,b.y))<10 then table.remove(targets,a) score=score+1 end end if #targets==0 then createTargets() end end end
Sign In or Register to comment.