Question

In: Computer Science

Please Use Javascript and P5 Pongish Create a one player pong game. This should have: 1....

Please Use Javascript and P5

Pongish

Create a one player pong game. This should have:

1. A paddle/rectangle controlled by the player on the right of the screen (moves up/down)

2. A ball/circle that starts moving to the left of the screen.

3. This ball bounces off the TOP, BOTTOM, and LEFT of the screen.
4. This ball bounces off the paddle (use hitTestPoint)
5. If the ball goes beyond the right of the screen, place the ball back at the center of the screen and set its velocity to the left again.
7. (optional) Display a "score" number on the screen that ticks up by 1 every time the player catches the ball with the paddle. Resets to zero when the ball resets.

Ideally, this project would use objects for both paddle and ball.

Solutions

Expert Solution

var Ball_X = Math.floor(Math.random() * 300) + 50;
var Ball_Y = 50;
var diameter = 50;
var Ball_XChange = 5;
var Ball_YChange = 5;

var xPaddle;
var yPaddle;
var paddleWidth = 100;
var paddleHeight = 25;

var started = false;
var score = 0;

function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
background(0);
   Ball_X += Ball_XChange;
   Ball_Y += Ball_YChange;
   if (Ball_X < diameter/2 ||
Ball_X > windowWidth - 0.5*diameter) {
       Ball_XChange *= -1;
}
   if (Ball_Y < diameter/2 ||
Ball_Y > windowHeight - diameter) {
Ball_YChange *= -1;
   }
  
if ((Ball_X > xPaddle &&
Ball_X < xPaddle + paddleWidth) &&
(Ball_Y + (diameter/2) >= yPaddle)) {
Ball_XChange *= -1;
Ball_YChange *= -1;
score++;
}
   fill(255, 0, 255);
   noStroke();
   ellipse(Ball_X, Ball_Y, diameter, diameter);
  
if (!started) {
xPaddle = windowWidth / 2;
yPaddle = windowHeight - 100;
started = true;
}
  
fill(0, 255, 255);
noStroke();
rect(xPaddle, yPaddle, paddleWidth, paddleHeight);
  
fill(0, 255, 255);
textSize(24);
   text("Score: " + score, 10, 25);
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
xPaddle -= 50;
} else if (keyCode === RIGHT_ARROW) {
xPaddle += 50;
}
}


Related Solutions

Please Use JavaScript and P5 1) Greeter Create an page with an input, and a button....
Please Use JavaScript and P5 1) Greeter Create an page with an input, and a button. When the button is clicked, output the phrase "Hello {Name}" to the developer console, with {Name} being the value the user put into the input field. Use a function that takes the name as an argument, and returns the full phrase as its output.
Connect Four is a game that alternates player 1 and player 2. You should keep track...
Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. For this program we will just be building the structure not the full program. Your assignment: Create a two player Connect Four C language program that follows the requirements below. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a letter for which...
In a game of “Chuck a luck” a player bets on one of the numbers 1...
In a game of “Chuck a luck” a player bets on one of the numbers 1 to 6. Three dice are then rolled and if the number bet by the player appears i times (where i equals to 1, 2 or 3) the player then wins i units. On the other hand if the number bet by the player does not appear on any of the dice the player loses 1 unit. If x is the players’ winnings in the...
PLEASE USE PYTHON THANK YOU In the game of Lucky Sevens, the player rolls a pair...
PLEASE USE PYTHON THANK YOU In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are many ways to win: (1, 6), (2, 5), and soon. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people's eyes...
Use JavaScript/HTML to create a Tic Tac Toe game displaying X's O's. Score the result. Each...
Use JavaScript/HTML to create a Tic Tac Toe game displaying X's O's. Score the result. Each player takes a turn putting their mark until done or tied.
Please take the code below and add some javascript to it. Please use javascript inline. Please...
Please take the code below and add some javascript to it. Please use javascript inline. Please be sure to specify within the webpage with something like 'Click here' too show what was done and how to activate it. Please post in a format that can be directly copied. Thank you in advance HINT: add some fun widgets to the page index-css.html: <!DOCTYPE html> <html lang="en"> <head> <!-- title for web page --> <title>Index-CSS Page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1">...
Coin taking game This game is played between 2 players, player 1 and player 2. There...
Coin taking game This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both...
Consider the following two-player game, in which Player 1 is the IMF, and Player 2 is...
Consider the following two-player game, in which Player 1 is the IMF, and Player 2 is a debtor country. Reform Waste Aid 3, 2 -2, 3 No Aid -2, 1 0, 0 a) Compute all (pure and mixed) Nash equilibria. b) Do you think that the above game is the case of a resource curse? Interpret the game with a story of a resource curse.
Use the following payoff matrix for a one-shot game to answer the accompanying questions. Player 2...
Use the following payoff matrix for a one-shot game to answer the accompanying questions. Player 2 Strategy X Y Player 1 A 5, 5 0, -200 B -200, 0 20, 20 a. Determine the Nash equilibrium outcomes that arise if the players make decisions independently, simultaneously, and without any communication. Instructions: You may select more than one answer. Click the box with a check mark for the correct answers and click twice to empty the box for the wrong answers....
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a method (inputGrade) that can put a name and a grade to the grade object. - Create another method (showAlltheGrades) to show all the grade in that object. - Create the third method (MaxGrade) that can display the maximum grade and the student name. - Using “prompt” and inputGrade method input 10 student names and their grades. - Display all the grades and names by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT