Skip to content

Instantly share code, notes, and snippets.

@trikyle
Last active January 13, 2016 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trikyle/8d3108d9a846f12701b2 to your computer and use it in GitHub Desktop.
Save trikyle/8d3108d9a846f12701b2 to your computer and use it in GitHub Desktop.
-- Air Hockey
-- Kyle Wood
supportedOrientations(LANDSCAPE_LEFT)--can only be used in landscape position
function setup()
displayMode(FULLSCREEN_NO_BUTTONS)--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--setting scores to 0 for start
w={}--setting a table for the wall bodies and goalpoast bodies
w[1]=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))--w[1]-w[8] set the wall and goalpost physic bodies
w[2]=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w[3]=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
w[4]=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
w[5]=physics.body(EDGE,vec2(30,(HEIGHT/2)+100),vec2(50, (HEIGHT/2)+100))
w[6]=physics.body(EDGE, vec2(30,(HEIGHT/2)-100),vec2(50, (HEIGHT/2)-100))
w[7]=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)+100), vec2(WIDTH-50, (HEIGHT/2)+100))
w[8]=physics.body(EDGE, vec2(WIDTH-30,(HEIGHT/2)-100), vec2(WIDTH-50,(HEIGHT/2)-100))
for i=1,#w do w[i].type=KINEMATIC end--setting all the walls to KINEMATIC type so they don't move with collisions
b={}--setting a table for the player physic bodies
b[1]=physics.body(CIRCLE,50)--b[1] and b[2] set the physics bodies for players
b[2]=physics.body(CIRCLE,50)
for i=1, #b do --this for loop sets the setup variables for the player physics bodies
b[i].x=WIDTH/2--setting starting x position
b[i].y=HEIGHT/2--hiding the players in the beginning
b[i].gravityScale=0--removing built in gravity
b[i].type=KINEMATIC--setting the players to kinematic so they move the puck but the puck doesn't move them
end
p=physics.body(CIRCLE,30)--setting puck physics body
p.x=WIDTH/2--setting puck x position
p.y=HEIGHT/2--setting puck y position
p.gravityScale=0--removing gravity
p.restitution=.9--making the ball bouncy
p.bullet=true--sets the body to a bullet so the physics detect faster
p.interpolate=true--making the puck interpolate (smooths out motion)
physics.continuous=true--always detecting physics
music("Game Music One:Sporting Arena", true)--playing music
end
function touched(t)--touch function
if t.state==BEGAN and game==0 and win==0 then game=1 end--starting the initial game
if t.state==BEGAN and game==0 and win==1 then game=1 win=0 leftscore=0 rightscore=0 end--startingn the game after a win
if t.state==BEGAN and game==0 and win==2 then game=1 win=0 leftscore=0 rightscore=0 end--starting the game after a win
if t.x<WIDTH/2 then
if t.state==MOVING then
b[2].x=t.x
b[2].y=t.y
b[2].linearVelocity=vec2(t.deltaX*30,t.deltaY*30)
end
if t.state==ENDED then
b[2].linearVelocity=vec2(0,0)
end
end
if t.x>WIDTH/2 then
if t.state==MOVING then
b[1].x=t.x
b[1].y=t.y
b[1].linearVelocity=vec2(t.deltaX*30,t.deltaY*30)
end
if t.state==ENDED then
b[1].linearVelocity=vec2(0,0)
end
end
if t.x <80 and t.y > HEIGHT-30 then
game=0 rightscore=0 leftscore=0 p.x=WIDTH/2 p.y=HEIGHT/2 p.linearVelocity=vec2(0,0)
end
end
function draw()
if game==0 and win==0 then--homescreen and other setup variables
background(0, 0, 0, 255)
font("ArialRoundedMTBold")
fill(255, 0, 0, 255)
fontSize(100)
text("Air Hockey",WIDTH/2, HEIGHT-200)
fontSize(40)
fill(24, 255, 0, 255)
text("Tap to Play",WIDTH/2, HEIGHT-270)
fontSize(30)
text("First Player to 7 Wins",WIDTH/2, HEIGHT-370)
font("ArialRoundedMTBold")
end
if game==0 and win==1 then --left side won menu screen
background(0, 0, 0, 255)
font("ArialRoundedMTBold")
fill(255, 0, 0, 255)
fontSize(100)
text("Left Side Won",WIDTH/2, HEIGHT-200)
fill(24, 255, 0, 255)
fontSize(40)
text("Tap to Play Again",WIDTH/2, HEIGHT-270)
end
if game==0 and win==2 then --right side won menu screen
background(0, 0, 0, 255)
font("ArialRoundedMTBold")
fill(255, 0, 0, 255)
fontSize(100)
text("Right Side Won",WIDTH/2, HEIGHT-200)
fill(24, 255, 0, 255)
fontSize(40)
text("Tap to Play Again",WIDTH/2, HEIGHT-270)
end
if game==1 then
--drawing the background for gameplay
background(0, 0, 0, 255)
strokeWidth(15)
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
stroke(24, 255, 0, 255)
strokeWidth(10)
fill(0, 0, 0, 255)
ellipse(p.x,p.y,60)
--drawing two users
stroke(0, 0, 0, 255)
strokeWidth(0)
if b[1].x>WIDTH/2 then
fill(255, 0, 0, 255)
ellipse(b[1].x,b[1].y,100) --left side
end
if b[2].x<WIDTH/2 then
fill(255, 0, 0, 255)--right side of screen
ellipse(b[2].x,b[2].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) sound("Game Sounds One:Female Cheer 1")
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) sound("Game Sounds One:Female Cheer 1")
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")
fontSize(25)
fill(255, 0, 0, 255)
text("Score:",(WIDTH*0.25)+2,HEIGHT-10)
text(leftscore,(WIDTH*0.25)+67,HEIGHT-10)
fill(255, 0, 0, 255)
text("Score:",(WIDTH*0.75)+2,HEIGHT-10)
text(rightscore,(WIDTH*0.75)+67,HEIGHT-10)
--printing the back button
fill(185, 185, 185, 255)
text("Back",50,HEIGHT-10)
end
sprite("Documents:namelogo",WIDTH-25,HEIGHT/2500)--my initials in the bottom left corner
end
--playing sounds
function collide (c)
if c.state==BEGAN then sound("Game Sounds One:Block 2") end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment