finally finish the game its not perfect but it works
----pong game
------testing version
– Your code here
-----local backgroundMusic = audio.loadStream(“Stealth Music - Ninja.mp3”)
local creategamescreen
local startgame
local gameover
pongpoint = audio.loadSound ( “pongscore.wav” )
serverball = audio.loadSound ( “pongserverball.wav” )
--------------------------- score board
---- Player 1 score
player1score = 0
player1scoreTxt = display.newText( “Player 1 Score: 0”, 0, 0, “Helvetica”, 30 )
player1scoreTxt.x = display.contentCenterX
player1scoreTxt.y = 10
—CPU Score
cpuscore=0
cpuscoreTxt = display.newText( “CPU Player Score: 0”, 0, 0, “Helvetica”, 30 )
cpuscoreTxt.x = display.contentCenterX
cpuscoreTxt.y = 500
------------layout of room
----left side of room
local myleft = display.newLine( 10, -50, 10, 1000 )
----rightside of room
local myright = display.newLine( 310, 900, 310, -50 )
–center of room
local mycenter = display.newLine( 160, 550, 170, -50 )
—bottom of room
local mybottom =display.newLine( - 9899, 500, 495, 520 )
—top of room
–local mytop =display.newLine( -925, -97, 555, -1 )
local mytop =display.newLine( -925, -72, 555, -27 )
– Create a rectangle player one
myRect = display.newRect( 0, 0, 30, 100 )
myRect.x =40
myRect.y =125
— paddle for computer player
cpupaddle=display.newImage (“paddle1.png”)
cpupaddle.x =270
cpupaddle.y =325
------creates first screen a player sees
function creategamescreen()
startgametext = display.newText( “Tap here to start the game!”, 150, 250, “Helvetica”, 24 )
startgametext:setFillColor( 1, 0, 0 )
local function removetxt()
display.remove(startgametext)
startgame()
end
startgametext:addEventListener ( “tap”, removetxt )
end
creategamescreen()
---------------move paddle player#1
local function ontouch (event)
if event.phase == “ended” then
----moves it any where i click–
— transition.to (myRect,{x=event.x, y=event.y})
—just moves it up and down
transition.to (myRect,{y=event.y})
end
end
Runtime:addEventListener(“touch”, ontouch)
----this starts the game
function startgame(event)
print(“function startgame starts”)
local physics = require(“physics”)
physics.start()
---------- add physics to layout of room
----left side of room
physics.addBody( myleft, “static”, { isSensor=true })
myleft.isSensor = true
----rightside of room
physics.addBody( myright, “static”, { isSensor=true })
myright.isSensor = true
—bottom of room
local mybottom =display.newLine( - 9899, 500, 495, 520 )
physics.addBody( mybottom, “static” ,{density = 0, friction = 0})
—top of room
local mytop =display.newLine( -925, -97, 555, -1 )
physics.addBody( mytop, “static” ,{density = 0, friction = 0})
—add physics to paddles
physics.addBody( cpupaddle, “static” ,{density = 1.0, friction = .3, bounce = 0})
physics.addBody( myRect, “static” ,{density = 1.0, friction = .3, bounce = 0})
-----------------------score
player1score = 0
player1scoreTxt.text = "Player1 Score: " … player1score
print ("player 1 score is ",player1score)
cpuscore = 0
cpuscoreTxt.text = "CPU Score: " … cpuscore
print ("cpu score is ",cpuscore)
timer.performWithDelay ( 4000, moveball )
audio.play (serverball )
end
— ends the game
function gameover()
local physics = require(“physics”)
physics.stop()
local gameovertext = display.newText( “Game Over! click here to restart”, 150, 250, “Helvetica”, 24 )
gameovertext:setFillColor( 1, 0, 0 )
local function removegameovertext(event)
print (“game restarted”)
–gameovertext = nil
display.remove(gameovertext)
startgame()
end
gameovertext:addEventListener ( “tap”,removegameovertext)
end
function moveball ()
print (“server the ball”)
local physics = require(“physics”)
physics.start()
myCircle = display.newCircle( 200, 200, 30 )
myCircle:setFillColor( 0.5 )
myCircle.x = display.contentCenterX
myCircle.y = display.contentCenterY
physics.addBody(myCircle,“dynamic”,{density=0.1,bounce = .8, friction = 0})
----this works to make the ball bounce off the paddle but ball moves at random for x & y
myCircle:setLinearVelocity(math.random(-600,-300),math.random(-100,500))
—this moves cpupaddle
–transition.to (cpupaddle,{x=cpupaddle.x, y=cpupaddle.y + math.random(-230,180)})
– transition.to (cpupaddle,{x=cpupaddle.x, y=cpupaddle.y + math.random(-100,100)})
transition.to (cpupaddle,{x=cpupaddle.x, y=math.random(-200,100)})
transition.to (cpupaddle,{x=cpupaddle.x, y=math.random(10,250)})
end
------comment out for backup
—leftwall collision
—collsion notificatons when ball hits left wall
function leftwallcollison( self, event)
if ( event.phase == “began” ) then
print (“event left wall began”)
elseif ( event.phase == “ended” ) and cpuscore <11 then
print (“ball hit left wall”)
print (“cpu scored”)
cpuscore = cpuscore + 1
audio.play (pongpoint)
cpuscoreTxt.text = "CPU Score: " … cpuscore
print ("cpu score is ",cpuscore)
timer.performWithDelay ( 4000, moveball )
audio.play (serverball)
elseif cpuscore >10 then
print (“gameover CPU won game”)
gameover()
end
end
myleft.collision = leftwallcollison
myleft:addEventListener( “collision” )
function rightwallcollison( self, event)
----added
if ( event.phase == “began” ) then
print (“event right wall began”)
elseif ( event.phase == “ended” ) and player1score <11 then
print (“ball hit right wall”)
print (“player1 scored”)
player1score = player1score + 1
audio.play (pongpoint)
player1scoreTxt.text = "Player1 Score: " … player1score
print ("player1 score is ",player1score )
timer.performWithDelay ( 4000, moveball )
audio.play (serverball)
elseif player1score >10 then
print(“gameover player1 won game”)
gameover()
end
end
myright.collision = rightwallcollison
myright:addEventListener( “collision” )