Friday, May 24, 2019

Animate CC - Pong Game -Part 3 & Part 4 - paddle code

Animate CC - Pong Game 
Part 3 & Part 4
-paddle code-



Pong Part 3 – the Player’s Paddle

In this section, you simply add code to make the player’s paddle move. It moves up and down according to your mouse movement. Code also stops it from exiting the screen.
Add this code to  à
function loop(e:Event):void
{
    Right about here   
          ball.x += ballSpeedX;
the code: press F9 to bring up the actions window

playerPaddle.y = mouseY;

// check if top of paddle is above top of screen
if(playerPaddle.y - playerPaddle.height/2 < 0){
     playerPaddle.y = playerPaddle.height/2;

//check if bottom of paddle is below bottom of screen
} else if(playerPaddle.y + playerPaddle.height/2 > stage.stageHeight){
     playerPaddle.y = stage.stageHeight - playerPaddle.height/2;
}


don't miss this last bracket

CTRL-ENTER to play
WINDOW KEY + G to record
upload - https://ezgif.com/
download as an animated GIF


  
NOTE: left paddle follows the mouse (vertically)

Pong Part 4 – the CPU's Paddle


Part 4 –the CPU’s paddle

This code is meant to simulate a decent paddle player.  The CPU should miss occasionally because it will be a little too slow.
The first bit of code is placed at the very top just under var ballSpeedY:int = -2;
The CODE:

var cpuPaddleSpeed:int = 3;

The next bit of code where we place the rest of the code – in the function loop
Place it just above playerPaddle.y = mouseY;

The CODE

if(cpuPaddle.y < ball.y - 10){
    cpuPaddle.y += cpuPaddleSpeed;
} else if(cpuPaddle.y > ball.y + 10){
    cpuPaddle.y -= cpuPaddleSpeed;
}

Do not forget the very last bracket!


CTRL-ENTER to play
WINDOW KEY + G to record
download as an animated GIF








No comments:

Post a Comment