Dodge- First Games/Experiment

Hello all, I am newish to the codea forums, but I’ve had codea for about half a year, and I must say it is the best app out there. I really like it so far! I’ve recently spent in total a day making and figuring out this game, Dodge. It’s a simple game where you just dodge rectangles. I have to say, since it is my first game from scratch (along with Ignatz’s tutorials) it is pretty buggy. It also, is not very good, and could use lots of improvement, which I wouldn’t mind at all! Thanks!

I’ll be splitting the code into to pieces, but they all belong in the same class:

-- Dodge
-- Developed By: Perscirious
-- Purposed for Experimental and Educational Usage
-- Free to use and edit at your will

-- [[Note: I am not a Codea or Lua proffesional, this is something I wrote for fun and as an experiment. It is probably badly written and/or organized to other more proffesional Codea/Lua users. There are probably many ways to improve these things so please feel free to do so.]]

--[[ Key:
    Rect- Rect is my abbreviation for rectangle.
    Ellipse and circle will be used interchangibly.
    Emy- Emy is my abbreviation for enemy
    ]]

function setup() --Here the values for the variables and other stuff will be set up
    displayMode(FULLSCREEN) --Set display mode
    
    --Ellipse Setup (initial location)
    ellipseX=WIDTH/2
    ellipseY=HEIGHT/1.1
    ellipseWidth=50
    ellipseHeight=50
    
    --Rect 1 Setup (initial location)
    emyrect1X=WIDTH/4
    emyrect1Y=HEIGHT/1000
    
    --Rect 2 Setup (initial location)
    emyrect2X=WIDTH/2
    emyrect2Y=-10
    
    --Rect 3 Setup (initial location)
    emyrect3X=WIDTH/1.1
    emyrect3Y=-10
    
    --Rect 4 Setup (intial location)
    emyrect4X=WIDTH/1000
    emyrect4Y=-10
    
    --Rect 5 Setup (initial location)
    emyrect5X=WIDTH/1.38
    emyrect5Y=-10
    
    --Side Rect 1 Setup (initial location to cause delay)
    siderect1X=-2000
    siderect1Y=HEIGHT/2
    
    --Side Rect 2 Setup (initial location to cause delay)
    siderect2X=-2020
    siderect2Y=HEIGHT/3
    
    --Side Rect 3 Setup (initial location to cause delay)
    siderect3X=-2080
    siderect3Y=HEIGHT/1.3
    --Side Rect 4 Setup (initial location to cause delay)
    siderect4X=-2000
    siderect4Y=HEIGHT/1000
    --Side Rect 5 Setup (initial location to cause delay)
    siderect5X=-2000
    siderect5Y=HEIGHT/6
    --Side Rect 6 Setup (initial location to cause delay)
    siderect6X=-2000
    siderect6Y=HEIGHT/1.1
    
    --Score Setup (location and initial value)
    score=0
    scoreWidth=WIDTH/2
    scoreHeight=HEIGHT/1.2
    
    --"You Died" Text (initial location to appear only when dead)
    dtextWidth=-1000
    dtextHeight=-1000   
end

function draw() --Here the drawings will be drawn (60 times a second)
    background(40, 40, 50)  --Set Background Color
    score=score+1
    --Rectangle Movements
    emyrect1Y = emyrect1Y+5 --Set the upward (Y-Axis) velocity for the enemy rectangles
    emyrect2Y=emyrect2Y+9.5
    emyrect3Y=emyrect3Y+7
    emyrect4Y=emyrect4Y+10.5
    emyrect5Y=emyrect5Y+11
    siderect1X=siderect1X+5 --Set the sideways (X-Axis) velocity for the enemy side rectangles
    siderect2X=siderect2X+7
    siderect3X=siderect3X+9
    siderect4X=siderect4X+10
    siderect5X=siderect5X+7
    siderect6X=siderect6X+11
    
    --Fills, Stroke, and Ellipse Drawings
    fill(0, 190, 255, 255)   --Set up the fill color
    strokeWidth(3) --Set stroke (line) width in pixels
    stroke(0, 255, 143, 255)   --Set stroke (line) color
    ellipse(ellipseX, ellipseY, ellipseWidth, ellipseHeight) --Draw the ellipse (circle or oval, depending on width and height)
    popStyle() --Pop the current fill and stroke settings
    fill(151, 26, 26, 255)   --Set the enemy rectangles fill colors
    strokeWidth(3) --Set the stroke (line) width for the enemy rectangles
    stroke(227, 106, 106, 255)   --Set the stroke (line) color for the enemy rectangles
    
    --Rectangle Drawings
    rect(emyrect1X, emyrect1Y, 60, 60) --Draw the enemy rectangles
    rect(emyrect2X,emyrect2Y, 60, 60)
    rect(emyrect3X, emyrect3Y, 60, 60)
    rect(emyrect4X, emyrect4Y, 60, 60)
    rect(emyrect5X, emyrect5Y, 60, 60)
    rect(siderect1X, siderect1Y, 40, 40)
    rect(siderect2X, siderect2Y, 40, 40)
    rect(siderect3X, siderect3Y, 40, 40)
    rect(siderect4X, siderect4Y, 40, 40)
    rect(siderect5X, siderect5Y, 40, 40)
    rect(siderect6X, siderect6Y, 40, 40)
    
    --Ellipse-Rectangle Collision Detection and Results
    if emyrect1X>ellipseX-ellipseWidth/2 and emyrect1X<=ellipseX+ellipseWidth/2 and emyrect1Y>ellipseY-ellipseHeight/2 and emyrect1Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if emyrect2X>ellipseX-ellipseWidth/2 and emyrect2X<=ellipseX+ellipseWidth/2 and emyrect2Y>ellipseY-ellipseHeight/2 and emyrect2Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end 
    if emyrect3X>ellipseX-ellipseWidth/2 and emyrect3X<=ellipseX+ellipseWidth/2 and emyrect3Y>ellipseY-ellipseHeight/2 and emyrect3Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end  
    if emyrect4X>ellipseX-ellipseWidth/2 and emyrect4X<=ellipseX+ellipseWidth/2 and emyrect4Y>ellipseY-ellipseHeight/2 and emyrect4Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end   
    if emyrect5X>ellipseX-ellipseWidth/2 and emyrect5X<=ellipseX+ellipseWidth/2 and emyrect5Y>ellipseY-ellipseHeight/2 and emyrect5Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if siderect1X>ellipseX-ellipseWidth/2 and siderect1X<=ellipseX+ellipseWidth/2 and siderect1Y>ellipseY-ellipseHeight/2 and siderect1Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if siderect2X>ellipseX-ellipseWidth/2 and siderect2X<=ellipseX+ellipseWidth/2 and siderect2Y>ellipseY-ellipseHeight/2 and siderect2Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if siderect3X>ellipseX-ellipseWidth/2 and siderect3X<=ellipseX+ellipseWidth/2 and siderect3Y>ellipseY-ellipseHeight/2 and siderect3Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if siderect4X>ellipseX-ellipseWidth/2 and siderect4X<=ellipseX+ellipseWidth/2 and siderect4Y>ellipseY-ellipseHeight/2 and siderect4Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if siderect5X>ellipseX-ellipseWidth/2 and siderect5X<=ellipseX+ellipseWidth/2 and siderect5Y>ellipseY-ellipseHeight/2 and siderect5Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    if siderect6X>ellipseX-ellipseWidth/2 and siderect6X<=ellipseX+ellipseWidth/2 and siderect6Y>ellipseY-ellipseHeight/2 and siderect6Y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
    
    --Rectangle Edge Pass Check
    if emyrect1Y>HEIGHT then --Rectangle 1 Check
        emyrect1Y=HEIGHT/1000 --We set the rectangle back to default first, then add its speed again after
        emyrect1Y=emyrect1Y+5
    end
    if emyrect2Y>HEIGHT then --Rectangle 2 Check
        emyrect2Y=-100
        emyrect2Y=emyrect2Y+9.5
    end
    if emyrect3Y>HEIGHT then --Rectangle 3 Check
        emyrect3Y=-10
        emyrect3Y=emyrect3Y+7 
    end
    if emyrect4Y>HEIGHT then --Rectangle 4 Check
        emyrect4Y=-10
        emyrect4Y=emyrect4Y+10.5
    end
    if emyrect5Y>HEIGHT then --Rectangle 5 Check
        emyrect5Y=-10
        emyrect5Y=emyrect5Y+11
    end
    if siderect1X>WIDTH then --Side Rectangle 1 Check
        siderect1X=-10
        siderect1X = siderect1X+5
    end
    if siderect2X>WIDTH then --Side Rectangle 2 Check
        siderect2X=-10
        siderect2X=siderect2X+7
    end

Ok, it’s just for fun anyways, but I’ll work on it. @Ignatz thanks for the better version

@Perscirious - this is one way to do it with tables. See how easy it is to change the number of rectangles or their positions.


function setup() --Here the values for the variables and other stuff will be set up
    displayMode(FULLSCREEN) --Set display mode
    
    --Ellipse Setup (initial location)
    ellipseX=WIDTH/2
    ellipseY=HEIGHT/1.1
    ellipseWidth=50
    ellipseHeight=50
    
    emyRects={}
    emyRects.size=vec2(60,60)
    emyRects.X={WIDTH/4,WIDTH/2,WIDTH/1.1,WIDTH/1000,WIDTH/1.38}
    emyRects.defaultY={HEIGHT/1000,-10,-10,-10,-10}
    emyRects.count=#emyRects.X
    emyRects.Y={} 
    for i=1,emyRects.count do emyRects.Y[i]=emyRects.defaultY[i] end
    emyRects.Move={5,9.5,7,10.5,11} 
    
    sideRects={}
    sideRects.size=vec2(40,40)
    sideRects.Y={HEIGHT/2,HEIGHT/3,HEIGHT/1.3,HEIGHT/1000,HEIGHT/6,HEIGHT/1.1}
    sideRects.count=#emyRects.Y
    sideRects.defaultX={-2000,-2020,-2080,-2000,-2000,-2000}
    sideRects.X={} 
    for i=1,sideRects.count do sideRects.X[i]=sideRects.defaultX[i] end  
    sideRects.Move={5,7,9,10,7,11}
    
    --Score Setup (location and initial value)
    score=0
    scoreWidth=WIDTH/2
    scoreHeight=HEIGHT/1.2
    
    --"You Died" Text (initial location to appear only when dead)
    dtextWidth=-1000
    dtextHeight=-1000   
end

function draw() --Here the drawings will be drawn (60 times a second)
    background(40, 40, 50)  --Set Background Color
    score=score+1
    --Rectangle Movements
    for i=1,emyRects.count do 
        emyRects.Y[i]=emyRects.Y[i]+emyRects.Move[i] 
    end  
    for i=1,sideRects.count do 
        sideRects.X[i]=sideRects.X[i]+sideRects.Move[i] 
    end  
    
    --Fills, Stroke, and Ellipse Drawings
    fill(0, 190, 255, 255)   --Set up the fill color
    strokeWidth(3) --Set stroke (line) width in pixels
    stroke(0, 255, 143, 255)   --Set stroke (line) color
    ellipse(ellipseX, ellipseY, ellipseWidth, ellipseHeight) --Draw the ellipse (circle or oval, depending on width and height)
    popStyle() --Pop the current fill and stroke settings
    fill(151, 26, 26, 255)   --Set the enemy rectangles fill colors
    strokeWidth(3) --Set the stroke (line) width for the enemy rectangles
    stroke(227, 106, 106, 255)   --Set the stroke (line) color for the enemy rectangles
    
    --Rectangle Drawings
    for i=1,emyRects.count do 
        rect(emyRects.X[i],emyRects.Y[i],emyRects.size.x,emyRects.size.y) 
    end  
    for i=1,sideRects.count 
        do rect(sideRects.X[i],sideRects.Y[i],sideRects.size.x,sideRects.size.y) 
    end  
    
    --Ellipse-Rectangle Collision Detection and Results
    for i=1,emyRects.count do 
        CheckCollision(emyRects.X[i],emyRects.Y[i])
    end  
    for i=1,sideRects.count do 
        CheckCollision(sideRects.X[i],sideRects.Y[i])
    end  
    
    --Rectangle Edge Pass Check
    for i=1,emyRects.count do 
        if emyRects.Y[i]>HEIGHT then emyRects.Y[i]=emyRects.defaultY[i]+emyRects.Move[i] end
    end  
    for i=1,sideRects.count do 
        if sideRects.X[i]>WIDTH then sideRects.X[i]=sideRects.defaultX[i]+sideRects.Move[i] end
    end  
    
    fill(86, 255, 0, 255)  --Score Draw and Proportions
    fontSize(100)
    textWrapWidth(10000)
    text(score, scoreWidth, scoreHeight)
    
    fill(255, 0, 0, 255)  --Death Text Draw and Proportions
    fontSize(100)
    textWrapWidth(1000)
    text("You Died", dtextWidth, dtextHeight)
end

function CheckCollision(x,y)
    if x>ellipseX-ellipseWidth/2 and x<=ellipseX+ellipseWidth/2 
        and y>ellipseY-ellipseHeight/2 and y<=ellipseY+ellipseHeight/2 then
        dtextWidth=WIDTH/2
        dtextHeight=HEIGHT/2
        scoreHeight=-1000
        scoreWidth=-1000
        ellipseX=-1000
        ellipseY=-1000
    end
end

--Touch Code and Effects
function touched(touch) --Setup touch function
    if touch.state==ENDED then --Check whether touch began, is moving, or ended.
       ellipseX=touch.x
       ellipseY=touch.y
    end
    if touch.state==MOVING then
        ellipseX=CurrentTouch.x
        ellipseY=CurrentTouch.y
    end
        if touch.state==BEGAN then
        ellipseX=CurrentTouch.x
        ellipseY=CurrentTouch.y
    end
end

    

this game is too easy because there are only five coord x and seven coord y, i beleave.

perhaps use sin cos or rand function to not have linear trajectory

@dave1707 and @Ignatz thanks for the replies, I work on it for extra practice.

This is the second part (remember: it all belongs on the same class/page)

    if siderect3X>WIDTH then --Side Rectangle 3 Check
        siderect3X=-10
        siderect3X=siderect3X+9
    end
    if siderect4X>WIDTH then --Side Rectangle 4 Check
        siderect4X=-10
        siderect4X=siderect4X+10
    end
    if siderect5X>WIDTH then --Side Rectangle 5 Check
        siderect5X=-10
        siderect5X=siderect5X+7
    end
    if siderect6X>WIDTH then --Side Rectangle 6 Check
        siderect6X=-10
        siderect6X=siderect6X+11
    end
    fill(86, 255, 0, 255)  --Score Draw and Proportions
    fontSize(100)
    textWrapWidth(10000)
    text(score, scoreWidth, scoreHeight)
    
    fill(255, 0, 0, 255)  --Death Text Draw and Proportions
    fontSize(100)
    textWrapWidth(1000)
    text("You Died", dtextWidth, dtextHeight)
end

--Touch Code and Effects
function touched(touch) --Setup touch function
    if touch.state==ENDED then --Check whether touch began, is moving, or ended.
       ellipseX=touch.x
       ellipseY=touch.y
    end
    if touch.state==MOVING then
        ellipseX=CurrentTouch.x
        ellipseY=CurrentTouch.y
    end
        if touch.state==BEGAN then
        ellipseX=CurrentTouch.x
        ellipseY=CurrentTouch.y
    end
end

Also, I added lots of comments to anyone who wants to know what each part does and how it is organized. Thanks, and I hope you all like it!

@Perscirious Interesting program. The part I have trouble with is my hand hides part of the screen as I try to avoid the rectangles. My suggestion for you if you want code practice is to rewrite the program using less if statements.

To reduce if statements, you can put your rectangles into tables, along with any information about them, then you can use a for loop to work through them all. This makes it very easy to add new objects.