Howdy, Stranger!

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

Physics for Air Hockey

Hi all, I have posted a discussion before about my air hockey project but now I am trying to use the built in Codea physics rather than fake the physics. This is what I have so far, but I am stuck and cannot figure out the collision detection and how to make the ball bounce off the players when the puck hits them...

    supportedOrientations(LANDSCAPE_ANY)--can only be used in landscape position
    function setup()
        displayMode(FULLSCREEN)--starts the game in full screen (for testing purposes)
        game=0--initial setup variable to show homescreen
        win=0--setting a nul value to bypass the variable test asking who wins
        physics.gravity(vec2(0,0))--removing the built in physics
        p_ball=physics.body(CIRCLE,20)--creating bodies for the built in variables(x y variables to save time when coding)
        r_ball=physics.body(CIRCLE,50)
        l_ball=physics.body(CIRCLE,50)
        r_ball.type=STATIC
        l_ball.type=STATIC
        p_ball.type=DYNAMIC
        p_ball.restituion=0.5
        wall1=physics.body(EDGE,vec2(30,30),vec2(30,HEIGHT-30))
        wall2=physics.body(EDGE,vec2(30,30),vec2(WIDTH-30,30))
        wall3=physics.body(EDGE,vec2(WIDTH-30,30),vec2(WIDTH-30,HEIGHT-30))
        wall4=physics.body(EDGE,vec2(WIDTH-30,HEIGHT-30),vec2(30,HEIGHT-30))
        wall1.type=STATIC wall2.type=STATIC wall3.type=STATIC wall4.type=STATIC
        wall1.restitution=0.1 wall2.restitution=0.1 wall3.restitution=0.1 wall4.restitution=0.1
        p_ball.bullet=true
        p_speedX=0 p_speedY=0
    end

    function touched (touch)
        if game==0 and touch.state==BEGAN then
            game=1 rightscore=0 leftscore=0--starting the game from the menu screen
        end
        if game==1 then--sensing the sides that are being touched and then setting the player positions if they touch on their side
            if touch.x > WIDTH/2+50 then
                r_ball.x=touch.x
                r_ball.y=touch.y
            end
            if touch.x < WIDTH/2-50 then
                l_ball.x=touch.x
                l_ball.y=touch.y
            end
        end
    end
    function draw()
    if game==0 and win==0 then--homescreen and other setup variables
            p_speedX=0--setting initial speed to 0
            p_speedY=0--setting initial speed to 0
            p_ball.x=WIDTH/2--placing the puck at the center of the screen
            p_ball.y=HEIGHT/2
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Air Hockey",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play",WIDTH/2, HEIGHT-270)
            text("First Player to 7 Wins",WIDTH/2, HEIGHT-370)
            font("ArialRoundedMTBold")
            fill(143, 131, 122, 255)
            fontSize(100)
            text("Air Hockey",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play",WIDTH/2+2, HEIGHT-270)
            text("First Player to 7 Wins",WIDTH/2+2, HEIGHT-370)
        end
    if game==0 and win==1 then --left side won menu screen
            background(249, 249, 249, 255)
            p_speedX=0
            p_speedY=0
            p_ball.x=WIDTH/2
            p_ball.y=HEIGHT/2
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Left Side Won",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2, HEIGHT-270)
            fill(0, 7, 255, 255)
            fontSize(100)
            text("Left Side Won",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
    end
    if game==0 and win==2 then --right side won menu screen
            background(249, 249, 249, 255)
            p_speedX=0
            p_speedY=0
            p_ball.x=WIDTH/2
            p_ball.y=HEIGHT/2
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Right Side Won",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2, HEIGHT-270)
            fill(255, 0, 0, 255)
            fontSize(100)
            text("Right Side Won",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
    end
    if game==1 then
            --drawing the background for gameplay
        background(249, 249, 249, 255)
        strokeWidth(5)
        stroke(0, 0, 0, 255)
        line(WIDTH/2,HEIGHT-30,WIDTH/2,30)
        fill(249,249,249,255)
        ellipse(WIDTH/2,HEIGHT/2,200)
        --drawing edges
        stroke(0, 0, 0, 255)
        line(30,30,30,HEIGHT-30)
        line(30,HEIGHT-30, WIDTH-30,HEIGHT-30)
        line(WIDTH-30,HEIGHT-30,WIDTH-30,30)
        line(30,30,WIDTH-30,30)
        --drawing goals
        stroke(0, 0, 0, 255)
        line(30,(HEIGHT/2)+100, 50, (HEIGHT/2)+100)
        line(30,(HEIGHT/2)-100,50,(HEIGHT/2)-100)
        line(WIDTH-30,(HEIGHT/2)+100, WIDTH-50, (HEIGHT/2)+100)
        line(WIDTH-30,(HEIGHT/2)-100,WIDTH-50,(HEIGHT/2)-100)
    --changing speeds       
            p_ball.linearVelocity=vec2(p_speedX,p_speedY)
            if p_speedX>0.01 or p_speedX<-0.01 then
            p_speedX=p_speedX-0.01
                end
            if p_speedY>0.01 or p_speedY<-0.01 then
            p_speedY=p_speedY-0.01
                end
            --drawing puck
            fill(162, 162, 162, 255)    
            ellipse(p_ball.x,p_ball.y,p_ball.radius*2) 

           --drawing two users 
        if r_ball.x >(WIDTH/2)+50 then --right side of screen
                fill(255, 0, 0, 255)
                ellipse(r_ball.x,r_ball.y,r_ball.radius*2) 
            end
          if l_ball.x <(WIDTH/2)-50 then --left side of screen
                fill(0, 7, 255, 255)
                ellipse(l_ball.x,l_ball.y,l_ball.radius*2)
            end  

            if p_ball.x <50 and p_ball.y < HEIGHT/2+100 and p_ball.y >HEIGHT/2-100 then
                rightscore=rightscore+1 p_ball.x=WIDTH/2 p_ball.y=HEIGHT/2 
            end
            if p_ball.x >WIDTH-50 and p_ball.y < HEIGHT/2+100 and p_ball.y >HEIGHT/2-100 then
                leftscore=leftscore+1 p_ball.x=WIDTH/2 p_ball.y=HEIGHT/2 
            end
    --detecting the win
            if leftscore==7 then game=0 win=1 end
            if rightscore==7 then game=0 win=2 end
    --printing score (on top of everything else)
            font("ArialRoundedMTBold")
            fill(136, 136, 136, 255)
            fontSize(30)
            text("Score:",WIDTH*0.25,HEIGHT-15)
            text(leftscore,(WIDTH*0.25)+65,HEIGHT-15)
            fill(0, 7, 255, 255)
            text("Score:",(WIDTH*0.25)+2,HEIGHT-14)
            text(leftscore,(WIDTH*0.25)+67,HEIGHT-14)
            fill(136, 136, 136, 255)
            fontSize(30)
            text("Score:",WIDTH*0.75,HEIGHT-15)
            text(rightscore,(WIDTH*0.75)+65,HEIGHT-15)
            fill(255, 0, 0, 255)
            text("Score:",(WIDTH*0.75)+2,HEIGHT-14)
            text(rightscore,(WIDTH*0.75)+67,HEIGHT-14)
    end

    sprite("Documents:namelogo",WIDTH-25,HEIGHT/2500)--my initials in the bottom left corner   
    end

    function  collide(contact)
        --when there is a collision do...
    end

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    You don't seem to be adjusting p_speedX and p_speedY for being pushed, so it's not surprising the puck isn't moving.

  • I know, because when I tried it didn't work right. I can't figure out how tochange the speeds according to the force of the hit and the speed it is already going as well as the angle it needs to bounce off at

  • Posts: 2,020

    You shouldn't directly set the positions of physics objects, because things like collision detection can get messed up. You should move them with applyForce.

  • Posts: 2,020

    Also, a simple state engine would greatly improve the readability/ maintainability of your code.

  • dave1707dave1707 Mod
    Posts: 7,924

    @kylewood Try this to give you help. Use the red circle to hit the blue circle.

    displayMode(FULLSCREEN)
    
    function setup()
        e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
        e2=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        e3=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
        e4=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    
        b1=physics.body(CIRCLE,50)
        b1.x=WIDTH/2
        b1.y=HEIGHT/2
        b1.gravityScale=0
        b1.restitution=.9
    
        b2=physics.body(CIRCLE,50)
        b2.x=300
        b2.y=100
        b2.gravityScale=0
        b2.type=KINEMATIC
    end
    
    function draw()
        background(0)
        fill(0,0,255)
        ellipse(b1.x,b1.y,100)
        fill(255,0,0)
        ellipse(b2.x,b2.y,100)
    end
    
    function touched(t)
        if t.state==MOVING then
            b2.x=t.x
            b2.y=t.y
            b2.linearVelocity=vec2(t.deltaX*40,t.deltaY*40)
        end
        if t.state==ENDED then
            b2.linearVelocity=vec2(0,0)
        end
    end
    
  • edited December 2015 Posts: 18

    Thank you @dave1707 ! This was extremely helpful! I can now use this as my code for the puck and get a working game. Will post a final video once I can incorporate this physics code into my project.

  • My pre-final code: (just needs a few touch ups, if you see anywhere that could use some cleaning up just inform me, please) Also, on another not I need to incorporate tables into this project somehow, if anyone has any ideas for incorporating tables and making the code more efficient in the process than all suggestions wouldbe helpful! Thank you in advance!

    --# Main
    -- Air Hockey
    -- Kyle Wood
    supportedOrientations(LANDSCAPE_ANY)--can only be used in landscape position
    function setup()
        displayMode(FULLSCREEN)--starts the game in full screen (for testing purposes)
        game=0--initial setup variable to show homescreen
        win=0--setting a nul value to bypass the variable test asking who wins
        leftscore=0
        rightscore=0
        --wall bodies
       w1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
        w2=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        w3=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
        w4=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
        --goalpoast bodies
        g1=physics.body(EDGE,vec2(30,(HEIGHT/2)+100),vec2(50, (HEIGHT/2)+100))
        g2=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)+100),vec2(WIDTH-50, (HEIGHT/2)+100))
        g3=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)+100), vec2(WIDTH-50, (HEIGHT/2)+100))
        g4=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)-100), vec2(WIDTH-50,(HEIGHT/2)-100))
        b1=physics.body(CIRCLE,50)
        b1.x=WIDTH/2
        b1.y=HEIGHT/2
        b1.gravityScale=0
        b1.type=KINEMATIC
        b2=physics.body(CIRCLE,50)
        b2.x=WIDTH/2
        b2.y=HEIGHT/2
        b2.gravityScale=0
        b2.type=KINEMATIC
        p=physics.body(CIRCLE,30)
        p.x=WIDTH/2
        p.y=HEIGHT/2
        p.gravityScale=0
        p.restitution=.9
    end
    
    function touched(t)
        if t.state==BEGAN and game==0 and win==0 then game=1 end
        if t.x<WIDTH/2 then
        if t.state==MOVING then
            b2.x=t.x
            b2.y=t.y
            b2.linearVelocity=vec2(t.deltaX*30,t.deltaY*30)
        end
        if t.state==ENDED then
            b2.linearVelocity=vec2(0,0)
        end
        end
    
        if t.x>WIDTH/2 then
        if t.state==MOVING then
            b1.x=t.x
            b1.y=t.y
            b1.linearVelocity=vec2(t.deltaX*30,t.deltaY*30)
        end
        if t.state==ENDED then
            b1.linearVelocity=vec2(0,0)
        end
        end
    end
    
    function draw()
    if game==0 and win==0 then--homescreen and other setup variables
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Air Hockey",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play",WIDTH/2, HEIGHT-270)
            text("First Player to 7 Wins",WIDTH/2, HEIGHT-370)
            font("ArialRoundedMTBold")
            fill(143, 131, 122, 255)
            fontSize(100)
            text("Air Hockey",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play",WIDTH/2+2, HEIGHT-270)
            text("First Player to 7 Wins",WIDTH/2+2, HEIGHT-370)
        end
    if game==0 and win==1 then --left side won menu screen
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Left Side Won",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2, HEIGHT-270)
            fill(0, 7, 255, 255)
            fontSize(100)
            text("Left Side Won",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
    end
    if game==0 and win==2 then --right side won menu screen
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Right Side Won",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2, HEIGHT-270)
            fill(255, 0, 0, 255)
            fontSize(100)
            text("Right Side Won",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
    end
    if game==1 then
            --drawing the background for gameplay
        background(0, 0, 0, 255)
        strokeWidth(5)
        stroke(24, 255, 0, 255)
        line(WIDTH/2,HEIGHT-30,WIDTH/2,30)
        fill(0, 0, 0, 255)
        ellipse(WIDTH/2,HEIGHT/2,200)
        --drawing edges
        stroke(24, 255, 0, 255)
        line(30,30,30,HEIGHT-30)
        line(30,HEIGHT-30, WIDTH-30,HEIGHT-30)
        line(WIDTH-30,HEIGHT-30,WIDTH-30,30)
        line(30,30,WIDTH-30,30)
        --drawing goals
        stroke(24, 255, 0, 255)
        line(30,(HEIGHT/2)+100, 50, (HEIGHT/2)+100)
        line(30,(HEIGHT/2)-100,50,(HEIGHT/2)-100)
        line(WIDTH-30,(HEIGHT/2)+100, WIDTH-50, (HEIGHT/2)+100)
        line(WIDTH-30,(HEIGHT/2)-100,WIDTH-50,(HEIGHT/2)-100)
    --puck drawing       
    
            fill(162, 162, 162, 255)    
            ellipse(p.x,p.y,60) 
           --drawing two users 
        if b1.x >(WIDTH/2) then --left side of screen
                fill(255, 0, 0, 255)
                ellipse(b1.x,b1.y,100) 
            end
          if b2.x <(WIDTH/2) then --right side of screen
                fill(0, 7, 255, 255)
                ellipse(b2.x,b2.y,100)
            end  
    
    
    --detecting if the puck scored
            if p.x <50 and p.y < HEIGHT/2+100 and p.y >HEIGHT/2-100 then
                rightscore=rightscore+1 p.x=WIDTH/2 p.y=HEIGHT/2 p.linearVelocity=vec2(0,0)
            end
            if p.x >WIDTH-50 and p.y < HEIGHT/2+100 and p.y >HEIGHT/2-100 then
                leftscore=leftscore+1 p.x=WIDTH/2 p.y=HEIGHT/2 p.linearVelocity=vec2(0,0)
            end
    --detecting the win
            if leftscore==7 then game=0 win=1 end
            if rightscore==7 then game=0 win=2 end
    --printing score (on top of everything else)
            font("ArialRoundedMTBold")
            fill(136, 136, 136, 255)
            fontSize(30)
            text("Score:",WIDTH*0.25,HEIGHT-15)
            text(leftscore,(WIDTH*0.25)+65,HEIGHT-15)
            fill(0, 7, 255, 255)
            text("Score:",(WIDTH*0.25)+2,HEIGHT-14)
            text(leftscore,(WIDTH*0.25)+67,HEIGHT-14)
            fill(136, 136, 136, 255)
            fontSize(30)
            text("Score:",WIDTH*0.75,HEIGHT-15)
            text(rightscore,(WIDTH*0.75)+65,HEIGHT-15)
            fill(255, 0, 0, 255)
            text("Score:",(WIDTH*0.75)+2,HEIGHT-14)
            text(rightscore,(WIDTH*0.75)+67,HEIGHT-14)
    end
    
    sprite("Documents:namelogo",WIDTH-25,HEIGHT/2500)--my initials in the bottom left corner   
    end
    
  • dave1707dave1707 Mod
    Posts: 7,924

    The game doesn't restart after a win.

  • Here is the fixed code @dave1707 . There are some more fixed problems but are there any more that need fixing and what else can I do to improve it? Also it is important that Iincorporate tables in my project so does anyonehave ideas for incorporating tables into this? Thanks

    -- Air Hockey
    -- Kyle Wood
    supportedOrientations(LANDSCAPE_ANY)--can only be used in landscape position
    function setup()
        displayMode(FULLSCREEN)--starts the game in full screen (for testing purposes)
        game=0--initial setup variable to show homescreen
        win=0--setting a nul value to bypass the variable test asking who wins
        leftscore=0
        rightscore=0
        --wall bodies
        w1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
        w2=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        w3=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
        w4=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
        --goalpoast bodies
        g1=physics.body(EDGE,vec2(30,(HEIGHT/2)+100),vec2(50, (HEIGHT/2)+100))
        g2=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)+100),vec2(WIDTH-50, (HEIGHT/2)+100))
        g3=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)+100), vec2(WIDTH-50, (HEIGHT/2)+100))
        g4=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)-100), vec2(WIDTH-50,(HEIGHT/2)-100))
        b1=physics.body(CIRCLE,50)--setting up one user for physics
        b1.x=WIDTH/2
        b1.y=HEIGHT/2
        b1.gravityScale=0--removing gravity
        b1.type=KINEMATIC--making it so the puck bounces off
        b2=physics.body(CIRCLE,50)--setting up the other user for physics(same as first user)
        b2.x=WIDTH/2
        b2.y=HEIGHT/2
        b2.gravityScale=0
        b2.type=KINEMATIC
        p=physics.body(CIRCLE,30)--setting up puck for physics
        p.x=WIDTH/2
        p.y=HEIGHT/2
        p.gravityScale=0--removing gravity
        p.restitution=.9--the bounciness
    end
    
    function touched(t)
        if t.state==BEGAN and game==0 and win==0 then game=1 end
        if t.x<WIDTH/2 then
        if t.state==MOVING then
            b2.x=t.x
            b2.y=t.y
            b2.linearVelocity=vec2(t.deltaX*30,t.deltaY*30)
        end
        if t.state==ENDED then
            b2.linearVelocity=vec2(0,0)
        end
        end
    
        if t.x>WIDTH/2 then
        if t.state==MOVING then
            b1.x=t.x
            b1.y=t.y
            b1.linearVelocity=vec2(t.deltaX*30,t.deltaY*30)
        end
        if t.state==ENDED then
            b1.linearVelocity=vec2(0,0)
        end
        end
    end
    
    function draw()
    if game==0 and win==0 then--homescreen and other setup variables
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Air Hockey",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play",WIDTH/2, HEIGHT-270)
            text("First Player to 7 Wins",WIDTH/2, HEIGHT-370)
            font("ArialRoundedMTBold")
            fill(143, 131, 122, 255)
            fontSize(100)
            text("Air Hockey",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play",WIDTH/2+2, HEIGHT-270)
            text("First Player to 7 Wins",WIDTH/2+2, HEIGHT-370)
        end
    if game==0 and win==1 then --left side won menu screen
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Left Side Won",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2, HEIGHT-270)
            fill(0, 7, 255, 255)
            fontSize(100)
            text("Left Side Won",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
    end
    if game==0 and win==2 then --right side won menu screen
            background(249, 249, 249, 255)
            font("ArialRoundedMTBold")
            fill(84, 62, 45, 255)
            fontSize(100)
            text("Right Side Won",WIDTH/2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2, HEIGHT-270)
            fill(255, 0, 0, 255)
            fontSize(100)
            text("Right Side Won",WIDTH/2+2, HEIGHT-200)
            fontSize(40)
            text("Tap to Play Again",WIDTH/2+2, HEIGHT-270)
    end
    if game==1 then
            --drawing the background for gameplay
        background(0, 0, 0, 255)
        strokeWidth(5)
        stroke(24, 255, 0, 255)
        line(WIDTH/2,HEIGHT-30,WIDTH/2,30)
        fill(0, 0, 0, 255)
        ellipse(WIDTH/2,HEIGHT/2,200)
        --drawing edges
        stroke(24, 255, 0, 255)
        line(30,30,30,HEIGHT-30)
        line(30,HEIGHT-30, WIDTH-30,HEIGHT-30)
        line(WIDTH-30,HEIGHT-30,WIDTH-30,30)
        line(30,30,WIDTH-30,30)
        --drawing goals
        stroke(24, 255, 0, 255)
        line(30,(HEIGHT/2)+100, 50, (HEIGHT/2)+100)
        line(30,(HEIGHT/2)-100,50,(HEIGHT/2)-100)
        line(WIDTH-30,(HEIGHT/2)+100, WIDTH-50, (HEIGHT/2)+100)
        line(WIDTH-30,(HEIGHT/2)-100,WIDTH-50,(HEIGHT/2)-100)
    --puck drawing       
    
            fill(162, 162, 162, 255)    
            ellipse(p.x,p.y,60) 
           --drawing two users 
        if b1.x >(WIDTH/2) then --left side of screen
                fill(255, 0, 0, 255)
                ellipse(b1.x,b1.y,100) 
            end
          if b2.x <(WIDTH/2) then --right side of screen
                fill(0, 7, 255, 255)
                ellipse(b2.x,b2.y,100)
            end  
    
    
    --detecting if the puck scored
            if p.x <50 and p.y < HEIGHT/2+100 and p.y >HEIGHT/2-100 then
                rightscore=rightscore+1 p.x=WIDTH/2 p.y=HEIGHT/2 p.linearVelocity=vec2(0,0)
            end
            if p.x >WIDTH-50 and p.y < HEIGHT/2+100 and p.y >HEIGHT/2-100 then
                leftscore=leftscore+1 p.x=WIDTH/2 p.y=HEIGHT/2 p.linearVelocity=vec2(0,0)
            end
    --detecting the win
            if leftscore==7 then game=0 win=1 end
            if rightscore==7 then game=0 win=2 end
    --printing score (on top of everything else)
            font("ArialRoundedMTBold")
            fill(136, 136, 136, 255)
            fontSize(30)
            text("Score:",WIDTH*0.25,HEIGHT-15)
            text(leftscore,(WIDTH*0.25)+65,HEIGHT-15)
            fill(0, 7, 255, 255)
            text("Score:",(WIDTH*0.25)+2,HEIGHT-14)
            text(leftscore,(WIDTH*0.25)+67,HEIGHT-14)
            fill(136, 136, 136, 255)
            fontSize(30)
            text("Score:",WIDTH*0.75,HEIGHT-15)
            text(rightscore,(WIDTH*0.75)+65,HEIGHT-15)
            fill(255, 0, 0, 255)
            text("Score:",(WIDTH*0.75)+2,HEIGHT-14)
            text(rightscore,(WIDTH*0.75)+67,HEIGHT-14)
    end
    
    sprite("Documents:namelogo",WIDTH-25,HEIGHT/2500)--my initials in the bottom left corner   
    end`
    
  • dave1707dave1707 Mod
    Posts: 7,924

    @kylewood I tried your new code, but the game still doesn't start after a win. I get the message, Left Side Won, Tap To Play Again, but tapping the screen does nothing. What do you want to put into a table. Your game seems OK right now.

  • A video of my final project in the link below, thanks to anyone and everyone that helped me (especially with the physics @dave1707 ) I've put a lot of time into this and I am happy with the outcome, but as always you can always make suggestions for me to improve it.

Sign In or Register to comment.