Friday, April 23, 2021

Air Hockey game

Air Hockey Game 

in code.org (gamelab)



Codes:

// Creating Player's Mallet
var playerMallet = createSprite(200,325,100,15);
playerMallet.shapeColor="black";

// Creating Computer's Mallet
var computerMallet = createSprite(200,75,100,15);
computerMallet.shapeColor="black";

// Creating Puck
var puck = createSprite(200,200,15,15);
puck.shapeColor="white";

// Creating Computer's Gole Post
var golePost1 = createSprite(200,35,120,25)
golePost1.shapeColor = "yellow"

// Creating Player's Gole Post
var golePost2 = createSprite(200,365,120,25)
golePost2.shapeColor = "yellow";

// Creating left Line
var lElement = createSprite(25,200,10,400);
lElement.shapeColor = "white";

// Creating right Line
var rElement = createSprite(375,200,10,400);
rElement.shapeColor = "white";

// Creating top Line
var tElement = createSprite(200,25,400,10);
tElement.shapeColor = "white";

// Creating bottom Line
var bElement = createSprite(200,375,400,10);
bElement.shapeColor = "white";

// Creating top middle Line
var mElement1 = createSprite(200,125,400,10);
mElement1.shapeColor = "white";

// Creating bottom middle Line
var mElement2 = createSprite(200,275,400,10);
mElement2.shapeColor = "white";

//Creating Game State
var gameState = "serve"

// Creating Scores
var playerScore = 0;
var computerScore=0;

//Creating Serve Function
function Serve(){
  
  if(keyDown("space") && gameState=="serve"){
    
    puck.velocityX = 6;
    puck.velocityY  = 6;
    gameState = "play";
  }
}

// Creating line between the screen
function Net(){
  
 for(var i = 5; i<400; i = i+20)
{
  stroke("White");
  strokeWeight(3);
  line(i,200,i+10,200);
  }
}

// Increasing Score
function Score(){
  
  if(puck.isTouching(golePost1)){
    
    puck.x = 200;
    puck.y = 200;
    puck.velocityX = 0;
    puck.velocityY = 0;
    
    playerScore = playerScore+1;
    
    gameState="serve"
  }
  
  if(puck.isTouching(golePost2)){
    
    puck.x = 200;
    puck.y = 200;
    puck.velocityX = 0;
    puck.velocityY = 0;
    
    computerScore = computerScore+1;
    
    gameState="serve"
  }
}

// Displaying Texts through Text Function
function Text(){
  
  if(gameState=="serve"){
    
    fill("White");
    textSize(25)
    text("Press Space to Serve",75,180)
  };
  
  if(gameState=="play"){
    
    fill("white");
    textSize(25);
    text(computerScore,50,190)
    text(playerScore,50,230)
  }
  
  if(gameState=="over"){
    fill("white");
    textSize(30);
    text("Game Over",120,150)
    textSize(20)
    text("Press R to Restart",75,250)
    
  }
}

// Creating Reset Function
function Restart(){
  
  if(gameState=="over" && keyDown("r")){
    
    computerScore = 0;
    playerScore = 0;
    gameState="serve";
  }
}

// Creating Game Over Condition
function gameOver(){
  
  if(computerScore==5 || playerScore==5){
    
    gameState="over"
  }
}

function draw() {
  
  // Changing background color
  background("green");
  
  // Creating Edges
  createEdgeSprites();
  
  // Set playerMallet velocities to 0
  playerMallet.velocityX=0;
  playerMallet.velocityY=0;
  
  // Changing computerMallet X position with puck's X position
  computerMallet.x = puck.x;
  
  // Player Mallet Movement
  
  if(keyDown("UP_ARROW")){
    
  playerMallet.y = playerMallet.y - 6;
   }
   
  if(keyDown("DOWN_ARROW")){

  playerMallet.y = playerMallet.y + 6;
   }
   
  if(keyDown("RIGHT_ARROW") && gameState=="play"){
    
    playerMallet.velocityX = 6
  }
  
  if(keyDown("LEFT_ARROW") && gameState=="play"){
    
    playerMallet.velocityX = -6
  }
  
  // Adding Sounds
  if(puck.isTouching(playerMallet) || puck.isTouching(computerMallet)){
    
    playSound("sound://category_bell/vibrant_game_correct_answer_1.mp3")
  }
  
  if(puck.isTouching(golePost1) || puck.isTouching(golePost2)){
    
    playSound("sound://category_notifications/vibrant_tone_zip.mp3")
  }
  
  if(puck.isTouching(rElement) || puck.isTouching(lElement) || puck.isTouching(tElement) || puck.isTouching(bElement)){
    
    playSound();
  }
  
  // Making Puck Bounceoff
  puck.bounceOff(lElement)
  puck.bounceOff(rElement)
  puck.bounceOff(tElement)
  puck.bounceOff(bElement)
  puck.bounceOff(playerMallet)
  puck.bounceOff(computerMallet);
  
  // Making player Mallet and computer Mallet collide wirh edges
  playerMallet.collide(edges)
  computerMallet.collide(edges)
  
// Displaying Sprites
  drawSprites();
  
// Calling all Functions
  Serve();
  Net();
  Score();
  gameOver();
  Restart();
  
  Text();
  
}


Video:






Previous Post
Next Post

post written by:

0 Comments: