You are on page 1of 15

Ballon program

----------------------------

var startGame = false;

function enter()

background('Scene1');

cursor(ARROW);

function loop()

clear();

drawIntroScreen();

function mouseClicked()

if (startGame)

showScene( "Game" );

function drawIntroScreen()
{

var ballX = width / 2;

var ballY = height / 2;

var ballSize = height / 2;

stroke(1);

fill("yellow");

ellipse( ballX, ballY, ballSize );

noStroke();

fill("black");

textSize(24);

textAlign(CENTER);

text("Catch\nthe\nfalling balls", width / 2, height / 2 - 30);

if ( Math.floor(frameCount / 30) % 2 === 0 )

textSize(12);

text("Click the ball to start the game...", width / 2, height - 20);

if ( dist( mouseX, mouseY, ballX, ballY ) < ballSize / 2 )

noFill();
stroke(1);

ellipse( ballX, ballY, ballSize + 10 );

startGame = true;

Game

var maxBallsDropped = 10;

var balls;

var ballsDropped;

var ballsCaught;

function initGame()

balls = [];

ballsDropped = 0;

ballsCaught = 0;

for(var i = 0; i < 10; i++)

addBall(balls);

function enter()
{

background('Scene2');

noCursor();

textSize(12);

textAlign(LEFT);

initGame();

function loop()

clear();

displayBalls(balls);

updateBalls(balls);

displayNeedle();

displayStats();

function displayNeedle()

stroke(1);
noFill();

triangle(mouseX, mouseY, mouseX - 10, mouseY + 10, mouseX - 8, mouseY + 15);

function displayGlobalBalls()

displayBalls(balls);

function catchBall(ball)

if ( ballsDropped < maxBallsDropped )

ballsCaught++;

initBall(ball);

function displayBalls(arBalls)

for(var i = 0; i < arBalls.length; i++)

displayBall( arBalls[i] );

}
function displayBall(ball)

fill(ball.color);

stroke(1);

ellipse(ball.x, ball.y, ball.size);

if ( dist( mouseX, mouseY, ball.x, ball.y ) < ball.size / 2 )

noFill();

ellipse(ball.x, ball.y, ball.size + 5);

catchBall(ball);

function displayStats()

fill("black");

noStroke();

text( "Caught: " + ballsCaught, 10, height - 40);

text( "Dropped: " + ballsDropped, 10, height - 20);

}
function updateBalls(arBalls)

for(var i = 0; i < arBalls.length; i++)

updateBall( arBalls[i] );

function updateBall(ball)

ball.y += ball.size / 20 + ballsCaught / 100;

// test if hits the ground

if ( ball.y > height )

ballsDropped++;

if ( ballsDropped >= maxBallsDropped )

showScene( "GameOver", ballsCaught );

// reinit the ball

initBall(ball);

}
}

function addBall(arBalls)

var ball = { x : 0, y : 0, color : "", size: 10 };

initBall(ball);

arBalls.push(ball);

function initBall(ball)

ball.x = random(10, width - 10);

ball.y = 10;

ball.color = random(["white", "yellow", "green", "blue", "red"]);

ball.size = random(10, 30);

Game over

function enter()

background('Scene');

cursor(ARROW);
}

function loop()

clear();

fill("black");

noStroke();

textSize( map( sin(frameCount * 0.1), 0, 1, 24, 32) );

textAlign(CENTER);

text("GAME OVER", width / 2, height / 2);

textSize(12);

text("Score: " + PublicVars.Arguments, width / 2, height / 2 + 20);

text("Click mouse button to restart game...", width / 2, height - 20);

function mouseClicked()

showScene( "Intro" );

}
MOVABLE CAR

var shapes = [];

var selected = -1;

var move = false;

var dx = 0;

var dy = 0;

function enter()

// Draw bear face

addCircle(400, 300, 200, "papayawhip");

// Draw left year

addCircle(250, 100, 50, "moccasin");

addCircle(270, 122, 20, "tan");

// Draw right year

addCircle(550, 100, 50, "moccasin");

addCircle(530, 122, 20, "tan");

// Draw left eye

addCircle(300, 220, 30);

addCircle(315, 230, 10, "darkgray");


// Draw right eye

addCircle(500, 220, 30);

addCircle(485, 230, 10, "darkgray");

// Draw nose

addCircle(400, 400, 90, "wheat");

addCircle(400, 350, 20, "sandybrown");

// Draw mouth

addRect(390, 420, 20, 50, "pink");

function loop()

clear();

moveSelected();

displayShapes();

displayInstructions()

function moveSelected()

{
move = move && mouseIsPressed && selected != -1;

if (move)

shapes[selected].x = mouseX - dx;

shapes[selected].y = mouseY - dy;

else

move = mouseIsPressed;

selected = findShape(mouseX, mouseY);

if (selected != -1)

dx = mouseX - shapes[selected].x;

dy = mouseY - shapes[selected].y;

function displayShapes()

for(var i = 0; i < shapes.length; i++)

var o = shapes[i];
stroke(i == selected ? "red" : "black");

strokeWeight(1);

fill(o.color);

if (o.type == "rect")

rect(o.x, o.y, o.width, o.height);

else if (o.type == "circle")

circle(o.x, o.y, o.radius);

function findShape(x, y)

for(var i = shapes.length - 1; i >= 0; i--)

var o = shapes[i];

if (o.type == "circle")

if (collisionPointCircle(x, y, o.x, o.y, o.radius))

return i
}

else if (o.type == "rect")

if (collisionPointRect(x, y, o.x, o.y, o.width, o.height))

return i;

return -1;

function addRect(x, y, width, height, color)

shapes.push({

type : "rect",

x : x,

y : y,

width : width,

height : height,

color : color || "white"

});

function addCircle(x, y, radius, color)

{
shapes.push({

type : "circle",

x : x,

y : y,

radius : radius,

color : color || "white"

});

function displayInstructions()

fill(0);

noStroke();

text('Use your mouse to move the shapes...', 10, height - 10);

You might also like