2012年11月13日 星期二

期中作品

這是以乒乓球為概念做成的遊戲

而且可以兩個人一起玩

增加朋友間的互動~

圖1


如圖1,左右兩方的藍色條槓為玩家控制,黃色的為球

左方玩家用w、s鍵控制上下移動,右方則↑↓鍵控制



圖2
 
 
 
最上方的紅色部分為雙方的得分數,
且遊戲中上下邊的黑色線條為牆壁
碰到可以反彈
 
 
 


int ballSize = 15;


float ballX;

float ballY;

float ballSpeedX;

float ballSpeedY;

int padding = 25;

int paddleH = 50;

float paddleSpeedY = 3;

float paddleLePosY;

float paddleLeSpeedY = 0;

float paddleRiPosY;

float paddleRiSpeedY = 0;

int scoreLe = 0;

int scoreRi = 0;

void setup() {

size(480, 360);

background(0);

ballX = width/2;

ballY = height/2;

ballSpeedX = 3;

paddleLePosY = height/2-paddleH/2;

paddleRiPosY = height/2-paddleH/2;

}

void draw() {

if (ballX &rt;= width || ballX <= 0 ) {

if ( ballX &rt;= width ) {

scoreLe += 1;

    } 

 else {

scoreRi += 1;

    }

    ballX =  width/2;

    ballY = height/2;

    ballSpeedY = 0;

    paddleLePosY = height/2 - paddleH/2;

    paddleRiPosY = height/2 - paddleH/2;

  }

  noStroke();

  fill(235,168,50);

  rectMode(CORNER);

  rect(0, 0, width, height);

  stroke(100);

  line(padding, padding, width-padding, padding);

  line(padding, height-padding, width-padding, height-padding);

  line(width/1, padding, width/1, height-padding);

  fill(255,0,0);

  textAlign(LEFT);

  text(scoreLe,padding, padding-10); 

  textAlign(RIGHT);

  text(scoreRi, width-padding, padding-10);

  paddleLePosY += paddleLeSpeedY;

  if (paddleLePosY < padding) {

    paddleLePosY = padding;

  } 

  else if (paddleLePosY+paddleH &rt; height-padding) {

    paddleLePosY = height-padding-paddleH;

  }

  paddleRiPosY += paddleRiSpeedY;

  if (paddleRiPosY < padding) {

    paddleRiPosY = padding;

  } 

  else if (paddleRiPosY+paddleH &rt; height-padding) {

    paddleRiPosY = height-padding-paddleH;

  }

  noStroke();

  fill(0,128,255);

  rectMode(CORNER);

  rect(padding, paddleLePosY, 10, paddleH);

  rect(width-padding, paddleRiPosY, 10, paddleH);

  ballX += ballSpeedX;

  ballY += ballSpeedY;

  if ( ballY-ballSize/2 < padding || ballY+ballSize/2 &rt; height-padding ) {

    ballSpeedY *= -1;

  }

  if (ballX-ballSize/2 <= padding && ballX+ballSize/2 &rt; padding) {

    if (ballY &rt;= paddleLePosY && ballY <= paddleLePosY+paddleH) {

      ballSpeedX *= -1;

      float hit = ballY - (paddleLePosY+paddleH/2);

      ballSpeedY += (hit / (paddleH/2)) *2; // range -1 to 1 (*2)

    }

  }

  if (ballX+ballSize/2 &rt;= width-padding && ballX-ballSize/2 < width-padding) {

    if (ballY &rt;= paddleRiPosY && ballY <= paddleRiPosY+paddleH) {

      ballSpeedX *= -1; 

      float hit = ballY - (paddleRiPosY+paddleH/2);

      ballSpeedY += (hit / (paddleH/2)) *2; // range -1 to 1 (*2)

    }

  }

  noStroke();

  fill(255,255,0);

  rectMode(CENTER);

  rect(ballX, ballY, ballSize, ballSize);

}

void keyPressed() {

  if (key == 'w' || key == 'W') {

    paddleLeSpeedY = paddleSpeedY * -1;

  } 

  else if (key == 's' || key == 'S') {

    paddleLeSpeedY = paddleSpeedY;

  } 

  else if (keyCode == UP) {

    paddleRiSpeedY = paddleSpeedY * -1;

  } 

  else if (keyCode == DOWN) {

    paddleRiSpeedY = paddleSpeedY;

  }

}

void keyReleased() {

  if (key == 'w' || key == 'W' || key == 's' || key == 'S') {

    paddleLeSpeedY = 0;

  } 

  else if (keyCode == UP || keyCode == DOWN) {

    paddleRiSpeedY = 0;

  }

}

沒有留言:

張貼留言