// Add the player at the bottom of the court
var bottomOfCourt = 900;
var playerPosition = 650;
var playerSize = 200;
var player = stamp('hamster2', playerPosition, bottomOfCourt, playerSize);
// Add the ball at the top of the court
var topOfCourt = 510;
var ballPosition = random(200, 500);
var ballSize = 100;
var ball = stamp('buttonpink', ballPosition, topOfCourt, ballSize);
// Add the score at the top of the screen
var score = 0;
var sb = text(score, 100, 50, 'white');
function touching() {
// 'x' is the x-coordinate of your mouse pointer
var xTapped = x;
// Work out how long the player is going to take
// to move to the mouse pointer
var distance = Math.abs(player.x - x);
var speed = 2;
var time = (distance / speed);
// Move the player to the mouse pointer
player.move(xTapped, player.y, time);
}
// Repeat this over and over again...
var scoreLocked = false;
function loop() {
// If the player is touching the ball:
if (player.hits(ball)) {
// Bounce the ball back to the top of the court
ball.move(
random(200, 450),
topOfCourt,
random(800, 2000)
)
// Increase the user's score
if (!scoreLocked) {
score = score + 1
sb.change(score)
}
Tennis Smash
fill('tenniscourt2')
// Add the player at the bottom of the court
var bottomOfCourt = 900;
var playerPosition = 650;
var playerSize = 200;
var player = stamp('hamster2', playerPosition, bottomOfCourt, playerSize);
// Add the ball at the top of the court
var topOfCourt = 510;
var ballPosition = random(200, 500);
var ballSize = 100;
var ball = stamp('buttonpink', ballPosition, topOfCourt, ballSize);
// Add the score at the top of the screen
var score = 0;
var sb = text(score, 100, 50, 'white');
function touching() {
// 'x' is the x-coordinate of your mouse pointer
var xTapped = x;
// Work out how long the player is going to take
// to move to the mouse pointer
var distance = Math.abs(player.x - x);
var speed = 2;
var time = (distance / speed);
// Move the player to the mouse pointer
player.move(xTapped, player.y, time);
}
// Repeat this over and over again...
var scoreLocked = false;
function loop() {
// If the player is touching the ball:
if (player.hits(ball)) {
// Bounce the ball back to the top of the court
ball.move(
random(200, 450),
topOfCourt,
random(800, 2000)
)
// Increase the user's score
if (!scoreLocked) {
score = score + 1
sb.change(score)
}
scoreLocked = true;
setTimeout(function() {
scoreLocked = false;
}, 100);
}
// If the ball is at the top of the court...
if (ball.y == topOfCourt) {
// Bounce it back
ball.move(random(80, 700), 980, 1200);
}
// If the ball gets past the player...
if (ball.y > (bottomOfCourt + 10)) {
// Explode the ball
ball.explode();
// Stop the main loop
loop = null;
// Change the background
fill('zam', 0);
}
}