Last active
May 3, 2020 00:26
-
-
Save georgesb/49055e7db25f67a574f5293377276e90 to your computer and use it in GitHub Desktop.
Paddle Game Starter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="https://cdn.jsdelivr.net/npm/p5@0.10.2/lib/p5.js"></script> | |
<script src="script.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let paddle; | |
let ball; | |
let button; | |
let started; | |
function setup() { | |
createCanvas(600, 400); | |
paddle = new Paddle(50, 160, 20, 80); | |
ball = new Ball(580, 200, 30, 30,-4,random(-2,2)); | |
button = createButton('start'); | |
button.position(19, 19); | |
button.mousePressed(start); | |
} | |
function draw() { | |
background(50, 89, 100); | |
paddle.display(); | |
ball.display(); | |
if(started){ | |
paddle.move(); | |
ball.move(); | |
button.hide(); | |
} | |
} | |
function start(){ | |
started = true; | |
} | |
class Ball { | |
constructor(x, y, w, h, xSpeed,ySpeed) { | |
this.x = x; | |
this.y = y; | |
this.w = w; | |
this.h = h; | |
this.xSpeed = xSpeed; | |
this.ySpeed = ySpeed; | |
} | |
display() { | |
ellipse(this.x, this.y, this.w, this.h); | |
} | |
move() { | |
this.x += this.xSpeed; | |
this.y += this.ySpeed; | |
if (this.hitPaddle()) { | |
this.xSpeed *= -1; | |
} | |
if(this.hitTop() || this.hitBottom()){ | |
this.ySpeed *= -1; | |
} | |
} | |
hitPaddle() { | |
return ((this.x - this.w / 2) < paddle.getRightEdge() && | |
this.y > paddle.getTop() && this.y < paddle.getBottom()); | |
} | |
hitTop(){ | |
return (this.y-15 < 0); | |
} | |
hitBottom(){ | |
return (this.y+15 >400); | |
} | |
} | |
class Paddle { | |
constructor(x, y, w, h) { | |
this.x = x; | |
this.y = y; | |
this.w = w; | |
this.h = h; | |
this.speed = 0; | |
} | |
display() { | |
rect(this.x, this.y, this.w, this.h); | |
} | |
getRightEdge() { | |
return this.x + this.w; | |
} | |
getTop() { | |
return this.y; | |
} | |
getBottom() { | |
return this.y + this.h; | |
} | |
setSpeed(speed) { | |
this.speed = speed; | |
} | |
move() { | |
this.y += this.speed; | |
} | |
} | |
function keyPressed() { | |
switch (keyCode) { | |
case UP_ARROW: | |
case 65: | |
paddle.setSpeed(-4); | |
break; | |
case DOWN_ARROW: | |
case 90: | |
paddle.setSpeed(4); | |
break; | |
} | |
} | |
function keyReleased() { | |
switch (keyCode) { | |
case UP_ARROW: | |
case 65: | |
paddle.setSpeed(0); | |
break; | |
case DOWN_ARROW: | |
case 90: | |
paddle.setSpeed(0); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment