Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball circle;
- Paddle left;
- Paddle right;
- //Sterowanie
- boolean upL, downL;
- boolean upR, downR;
- //Wyniki
- int scoreL = 0;
- int scoreR = 0;
- int maxScore = 5;
- void setup(){
- size(1100, 600);
- //Piłka
- circle = new Ball(width/2, width/2, 40, 40, 4, 4);
- //Paletki
- textSize(30);
- textAlign(CENTER, CENTER);
- rectMode(CENTER);
- left = new Paddle(40, height/2, 20, 125, 6, true);
- right = new Paddle(width-40, height/2, 20, 125, 6, false);
- }
- void draw(){
- background(0);
- circle.drawBall();
- circle.moveBall();
- circle.bounceOff();
- left.drawPaddle();
- right.drawPaddle();
- left.movePaddle();
- right.movePaddle();
- left.paddleLimit();
- right.paddleLimit();
- left.detectBall(circle);
- right.detectBall(circle);
- scores();
- gameOver();
- }
- void scores() {
- fill(255);
- text(scoreL + " : ", 520, 50);
- text(scoreR, width-520, 50);
- }
- void gameOver(){
- if(scoreL == maxScore) endGameScreen("Zwycięzca: gracz 1");
- else if(scoreR == maxScore) endGameScreen("Zwycięzca: gracz 2");
- }
- void endGameScreen(String winMsg){
- circle.setSX(0);
- circle.setSY(0);
- fill(255);
- text("Zagraj ponownie", width/2, height/3 + 40);
- text(winMsg, width/2, height/3);
- if(mousePressed){
- scoreR = 0;
- scoreL = 0;
- circle.setSX(4);
- circle.setSY(4);
- }
- }
- class Paddle{
- //PosX, PosY, width, height, speed
- private int x, y, w, h, s;
- private boolean controlType;
- Paddle(int x, int y, int w, int h, int s, boolean controlType){
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- this.s = s;
- this.controlType = controlType;
- }
- void movePaddle(){
- if(controlType){
- if (upL) this.y -= this.s;
- if (downL) this.y += this.s;
- }
- else{
- if (upR) this.y -= this.s;
- if (downR) this.y += this.s;
- }
- }
- void paddleLimit() {
- if(controlType){
- if (this.y - this.h/2 < 0) this.y += this.s;
- if (this.y + this.h/2 > height) this.y -= this.s;
- }
- else{
- if (this.y - this.h/2 < 0) this.y += this.s;
- if (this.y + this.h/2 > height) this.y -= this.s;
- }
- }
- void drawPaddle(){
- fill(255);
- rect(this.x, this.y, this.w, this.h);
- }
- void detectBall(Ball circle){
- int x = circle.getX();
- int y = circle.getY();
- int w = circle.getW();
- int h = circle.getH();
- if(controlType){
- if(x - w/2 < this.x + this.w/2 && y - h/2 < this.y + this.h/2 && y + h/2 > this.y - this.h/2){
- if(circle.getSX() < 0) circle.setSX(-circle.getSX());
- }
- }
- else{
- if(x + w/2 > this.x - this.w/2 && y - h/2 < this.y + this.h/2 && y + h/2 > this.y - this.h/2){
- if(circle.getSX() > 0) circle.setSX(-circle.getSX());
- }
- }
- }
- }
- class Ball{
- //PosX, PosY, width, height, SpeedX, SpeedY
- private int x, y, w, h, sX, sY;
- Ball(int x, int y, int w, int h, int sX, int sY){
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- this.sX = sX;
- this.sY = sY;
- }
- int getX(){ return this.x; }
- int getY(){ return this.y; }
- int getW(){ return this.w; }
- int getH(){ return this.h; }
- int getSX() { return this.sX; }
- void setSX(int sX) { this.sX = sX; }
- void setSY(int sY) { this.sY = sY; }
- void drawBall(){
- fill(255);
- ellipse(this.x, this.y, this.w, this.h);
- }
- void moveBall(){
- this.x += this.sX*2;
- this.y += this.sY*2;
- }
- void bounceOff() {
- if ( this.x > width - this.w/2) {
- setup();
- this.sX *= -1;
- scoreL++;
- } else if ( this.x < 0 + this.w/2) {
- setup();
- scoreR++;
- }
- if ( this.y > height - this.h/2) {
- this.sY *= -1;
- } else if ( this.y < 0 + this.h/2) {
- this.sY *= -1;
- }
- }
- }
- void keyPressed() {
- if (key == 'w' || key == 'W') upL = true;
- if (key == 's' || key == 'S') downL = true;
- if (keyCode == UP) upR = true;
- if (keyCode == DOWN) downR = true;
- }
- void keyReleased() {
- if (key == 'w' || key == 'W') upL = false;
- if (key == 's' || key == 'S') downL = false;
- if (keyCode == UP) upR = false;
- if (keyCode == DOWN) downR = false;
- }
Add Comment
Please, Sign In to add comment