0% found this document useful (0 votes)
119 views10 pages

WHR Cod

This document contains code for two pong games. The first game (section 1) creates sprites for the ball and two paddles controlled by the player and computer. It detects collisions with the paddles and edges to bounce the ball and keeps score. The second game (section 2) creates a side-scrolling game where the player controls a character to jump over obstacles and clouds that scroll from right to left. It detects collisions to end the game and resets when restart is pressed.

Uploaded by

Dr Rupali Arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views10 pages

WHR Cod

This document contains code for two pong games. The first game (section 1) creates sprites for the ball and two paddles controlled by the player and computer. It detects collisions with the paddles and edges to bounce the ball and keeps score. The second game (section 2) creates a side-scrolling game where the player controls a character to jump over obstacles and clouds that scroll from right to left. It detects collisions to end the game and resets when restart is pressed.

Uploaded by

Dr Rupali Arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

PONG GAME

//create the ball, playerPaddle and computerPaddle as sprite objects

var ball = createSprite(200,200,10,10);

var playerPaddle = createSprite(380,200,10,70);

var computerPaddle = createSprite(10,200,10,70);

var playerscore = 0;

var computerscore = 0;

//variable to store different state of game

var gameState = "serve";

function draw() {

//clear the screen

background("white");

text(playerscore, 220, 20);

text(computerscore, 180, 20);

//place info text in the center

if (gameState === "serve") {

text("Press Space to Serve",150,180);

//make the player paddle move with the mouse's y position

//playerPaddle.y = World.mouseY;

if (keyDown("up")) {

playerPaddle.y = playerPaddle.y-5;

}
if (keyDown("down")) {

playerPaddle.y = playerPaddle.y +5;

//AI for the computer paddle

//make it move with the ball's y position

//computerPaddle.y = ball.y;

if (keyDown("w")) {

computerPaddle.y = computerPaddle.y-5;

if (keyDown("s")) {

computerPaddle.y = computerPaddle.y +5;

//draw line at the centre

for (var i = 0; i < 400; i=i+20) {

line(200,i,200,i+10);

//create edge boundaries

//make the ball bounce with the top and the bottom edges

createEdgeSprites();

ball.bounceOff(topEdge);

ball.bounceOff(bottomEdge);

ball.bounceOff(playerPaddle);

ball.bounceOff(computerPaddle);

playerPaddle.bounceOff(edges);
computerPaddle.bounceOff(edges);

//serve the ball when space is pressed

if (keyDown("space") && gameState === "serve") {

serve();

gameState = "play";

//reset the ball to the centre if it crosses the screen

if(ball.x > 400 || ball.x <0) {

if (ball.x > 400) {

computerscore = computerscore +1;

if (ball.x < 0) {

playerscore = playerscore +1;

reset();

gameState = "serve";

if (playerscore === 5 || computerscore === 5) {

gameState = "Gameover"

text ("Game Over",170,160)

text ("Press R To Restart",150,180)

if (keyDown("R")&& gameState === "Gameover") {


gameState = "serve";

playerscore = 0;

computerscore = 0;

drawSprites();

function serve() {

ball.velocityX = 3;

ball.velocityY = 4;

function reset() {

ball.x = 200;

ball.y = 200;

ball.velocityX = 0;

ball.velocityY = 0;

2.
var PLAY = 1;
var END = 0;
var gameState = PLAY;

var trex, trex_running, trex_collided;


var ground, invisibleGround, groundImage;

var cloudsGroup, cloudImage;


var obstaclesGroup, obstacle1, obstacle2, obstacle3, obstacle4, obstacle5, obstacle6;

var score;
var gameOverImg,restartImg
var jumpSound , checkPointSound, dieSound

function preload(){
trex_running = loadAnimation("trex1.png","trex3.png","trex4.png");
trex_collided = loadAnimation("trex_collided.png");

groundImage = loadImage("ground2.png");

cloudImage = loadImage("cloud.png");

obstacle1 = loadImage("obstacle1.png");
obstacle2 = loadImage("obstacle2.png");
obstacle3 = loadImage("obstacle3.png");
obstacle4 = loadImage("obstacle4.png");
obstacle5 = loadImage("obstacle5.png");
obstacle6 = loadImage("obstacle6.png");

restartImg = loadImage("restart.png")
gameOverImg = loadImage("gameOver.png")

jumpSound = loadSound("jump.mp3")
dieSound = loadSound("die.mp3")
checkPointSound = loadSound("checkPoint.mp3")
}

function setup() {
createCanvas(600, 200);

var message = "This is a message";


console.log(message)

trex = createSprite(50,160,20,50);
trex.addAnimation("running", trex_running);
trex.addAnimation("collided", trex_collided);

trex.scale = 0.5;

ground = createSprite(200,180,400,20);
ground.addImage("ground",groundImage);
ground.x = ground.width /2;

gameOver = createSprite(300,100);
gameOver.addImage(gameOverImg);
restart = createSprite(300,140);
restart.addImage(restartImg);

gameOver.scale = 0.5;
restart.scale = 0.5;

invisibleGround = createSprite(200,190,400,10);
invisibleGround.visible = false;

//create Obstacle and Cloud Groups


obstaclesGroup = createGroup();
cloudsGroup = createGroup();

trex.setCollider("rectangle",0,0,trex.width,trex.height);
trex.debug = true

score = 0;

function draw() {

background(180);
//displaying score
text("Score: "+ score, 500,50);

if(gameState === PLAY){

gameOver.visible = false;
restart.visible = false;

ground.velocityX = -(4 + 3* score/100)


//scoring
score = score + Math.round(getFrameRate()/60);

if(score>0 && score%100 === 0){


checkPointSound.play()
}

if (ground.x < 0){


ground.x = ground.width/2;
}

//jump when the space key is pressed


if(keyDown("space")&& trex.y >= 161.5) {
trex.velocityY = -12;
jumpSound.play();
}

//add gravity
trex.velocityY = trex.velocityY + 0.8

//spawn the clouds


spawnClouds();

//spawn obstacles on the ground


spawnObstacles();

if(obstaclesGroup.isTouching(trex)){
//trex.velocityY = -12;
jumpSound.play();
gameState = END;
dieSound.play()

}
}
else if (gameState === END) {
gameOver.visible = true;
restart.visible = true;

//change the trex animation


trex.changeAnimation("collided", trex_collided);

ground.velocityX = 0;
trex.velocityY = 0

//set lifetime of the game objects so that they are never destroyed
obstaclesGroup.setLifetimeEach(-1);
cloudsGroup.setLifetimeEach(-1);

obstaclesGroup.setVelocityXEach(0);
cloudsGroup.setVelocityXEach(0);

if(mousePressedOver(restart)) {
reset();
}
}

//stop trex from falling down


trex.collide(invisibleGround);

drawSprites();
}

function reset(){
gameState=PLAY
obstaclesGroup.destroyEach();
cloudsGroup.destroyEach();
score=0
trex.changeAnimation("running",trex_running);
}

function spawnObstacles(){
if (frameCount % 60 === 0){
var obstacle = createSprite(600,165,10,40);
obstacle.velocityX = -(6 + score/100);

//generate random obstacles


var rand = Math.round(random(1,6));
switch(rand) {
case 1: obstacle.addImage(obstacle1);
break;
case 2: obstacle.addImage(obstacle2);
break;
case 3: obstacle.addImage(obstacle3);
break;
case 4: obstacle.addImage(obstacle4);
break;
case 5: obstacle.addImage(obstacle5);
break;
case 6: obstacle.addImage(obstacle6);
break;
default: break;
}

//assign scale and lifetime to the obstacle


obstacle.scale = 0.5;
obstacle.lifetime = 300;

//add each obstacle to the group


obstaclesGroup.add(obstacle);
}
}

function spawnClouds() {
//write code here to spawn the clouds
if (frameCount % 60 === 0) {
var cloud = createSprite(600,120,40,10);
cloud.y = Math.round(random(80,120));
cloud.addImage(cloudImage);
cloud.scale = 0.5;
cloud.velocityX = -3;

//assign lifetime to the variable


cloud.lifetime = 200;

//adjust the depth


cloud.depth = trex.depth;
trex.depth = trex.depth + 1;

//add each cloud to the group


cloudsGroup.add(cloud);
}
}

3.
4. var fixedRect, movingRect;
5. var box1, box2;
6.
7. function setup() {
8.   createCanvas(1200,800);
9.   fixedRect = createSprite(400, 100, 50, 80);
10.  fixedRect.shapeColor = "green";
11.  //fixedRect.debug = true;
12.  movingRect = createSprite(400, 800,80,30);
13.  movingRect.shapeColor = "purple";
14.  //movingRect.debug = true;
15.
16.  movingRect.velocityY = -5;
17.  fixedRect.velocityY = +5;
18.
19.  box1 = createSprite(50,300,20,50)
20.  box2 = createSprite(1050,300,20,50)
21.
22.  box1.shapeColor = "orange"
23.  box2.shapeColor = "yellow"
24.
25.  box1.velocityX = 3
26.  box2.velocityX = -3
27.}
28.
29.function draw() {
30.  background(0,0,0);  
31.
32. bounce(movingRect,fixedRect)
33. bounce(box1, box2)
34.  drawSprites();
35.}
36.
37.function bounce (object1,object2)
38.{
39.    if (object1.x - object2.x < object2.width/2 + object1.width/2
40.        && object2.x - object1.x < object2.width/2 + object1.width/2) {
41.      object1.velocityX = object1.velocityX * (-1);
42.      object2.velocityX = object2.velocityX * (-1);
43.    }
44.    if (object1.y - object2.y < object2.height/2 + object1.height/2
45.      && object2.y - object1.y < object2.height/2 + object1.height/2){
46.        object1.velocityY = object1.velocityY * (-1);
47.        object2.velocityY = object2.velocityY * (-1);
48.    }
49.}

You might also like