Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball b;
- Paddle p;
- int score = 0;
- void setup() {
- frameRate(144);
- size(640, 380);
- b = new Ball(50, height/2, 50);
- p = new Paddle(width-100, height/2, 30, 120);
- }
- void draw() {
- background(100);
- b.display();
- b.move();
- b.bounce();
- p.display();
- //p.move();
- if(b.overlaps(p)){
- b.turn();
- score += 1;
- }
- String myText = "Score: " + score;
- textSize(32);
- textAlign(CENTER);
- text(myText, width/2, 50);
- }
- class Ball {
- float x, y, d;
- int xdirection = 1;
- int ydirection = 1;
- float xspeed = 4;
- float yspeed = 4;
- Ball(float tempX, float tempY, float tempD) {
- x = tempX;
- y = tempY;
- d = tempD;
- }
- void display() {
- ellipse(x, y, d, d);
- }
- void move() {
- x = x + (xspeed * xdirection);
- //y = y + (yspeed * ydirection);
- }
- void bounce() {
- if (x < 0+d/2 || x > width-d/2) {
- xdirection *= -1;
- }
- if (y < 0+d/2 || y > height-d/2) {
- ydirection *= -1;
- }
- }
- boolean overlaps(Paddle paddle){
- float dist = dist(x, y, paddle.x, paddle.y);
- if(dist < d/2 + paddle.h/2){
- return true;
- } else {
- return false;
- }
- }
- void turn() {
- xdirection *= -1;
- }
- }
- class Paddle {
- float x, y, w, h;
- Paddle(float tempX, float tempY, float tempH, float tempW) {
- x = tempX;
- y = tempY;
- h = tempH;
- w = tempW;
- }
- void display(){
- rectMode(CENTER);
- rect(x, y, h, w);
- }
- void move(){
- y = mouseY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment