utano
p.68のNow you can play Monski pong!までのソースコードです。
小さいボールを四角で跳ね返すピンポンゲームみたいになります。
でもなんか反応わるい時もあるので合ってるか自信ないです。
import processing.serial.*;//import the Processing serial library
int linefeed = 10;//Linefeed in ASCII
Serial myPort;//シリアルポートの名前をmyPortにする宣言
float leftPaddle, rightPaddle;
int resetButton, serveButton;
int leftPaddleX, rightPaddleX;
int paddleHeight = 50;
int paddleWidth = 50;
//センサの最大値と最小値
float leftMinimum = 250;
float rightMinimum = 270;
float leftMaximum = 540;
float rightMaximum = 530;
//ボールを足す
int ballSize = 10;
int xDirection = 1;//horinzontal direction ひだり-1 みぎ1
int yDirection = 1;//vertical direction うえ-1 した1
int xPos, yPos;//ボールのx,y座標
boolean ballInMotion = false; //whether the ball should be moving
int leftScore = 0;
int rightScore = 0;
PFont myFont;
int fontSize = 36;
void setup(){
//ウィンドウサイズ
size(640,480);
//ボールをスクリーンの真ん中にもってくる=幅の2分の1の座標指定
xPos = width/2;
yPos = height/2;
//使えるシリアルポートをリストにして挙げる
println(Serial.list());
//Arduinoをつなげているシリアルポートナンバーがあてられているリストの番号を[]に入れてmyPortとする
myPort = new Serial(this, Serial.list()[1], 9600);
//read bytes into a buffer until you get a linefeed (ASCII,10);
myPort.bufferUntil(linefeed);
//initialize the sensor values
leftPaddle = height/2;
rightPaddle = height/2;
resetButton = 0;
serveButton = 0;
//initialize the horizontal paddle positions
leftPaddleX = 50;
rightPaddleX = width - 50;
noStroke();
//create a font with the third font available to the system
PFont myFont = createFont(PFont.list()[2], fontSize);
textFont(myFont);
}
void draw(){
background(0);
//draw the left paddle
rect(leftPaddleX, leftPaddle, paddleWidth, paddleHeight);
//draw the right paddle
rect(rightPaddleX, rightPaddle, paddleWidth, paddleHeight);
//calculate the ball's position and draw it
if(ballInMotion == true){
animateBall();
}
// if the serve button is pressed, start the ball moving
if(serveButton == 1){
ballInMotion = true;
}
//if the reset button is pressed, reset the scores and start the ball moving
if(resetButton == 1){
leftScore = 0;
rightScore = 0;
ballInMotion = true;
}
//print the scores
text(leftScore, fontSize, fontSize);
text(rightScore, width-fontSize, fontSize);
}
//serialEvent メソッドがバッファがsetup()で行った、
//bufferUntil()によって設定された値になると
//自動的にProcessing Sketchによって発動する(意味不明だ)
void serialEvent(Serial myPort){
//read the serial buffer
String myString = myPort.readStringUntil(linefeed);
//if you got any bytes other than the linefeed
if(myString !=null){
myString = trim(myString);
//カンマのところでstringを区切り、それらを整数に変える
int sensors[] = int(split(myString, ','));
//if you received all the sensor strings, use them
if(sensors.length ==4){
//calculate the flex sensors' ranges
float leftRange = leftMaximum - leftMinimum;
float rightRange = rightMaximum - rightMinimum;
//scale the flex sensors' results to the paddles' range
leftPaddle = height * (sensors[0]-leftMinimum)/leftRange;
rightPaddle = height * (sensors[1]-rightMinimum)/rightRange;
//assign the switches' values to the button variables
resetButton = sensors[2];
serveButton = sensors[3];
//print the sensor values
print("left:" + leftPaddle + "\tright:" + rightPaddle);
println("\treset:"+ resetButton + "\tserve:"+ serveButton);
}
}
}
void animateBall(){
//if the ball is moving left
if(xDirection < xdirection =" -xDirection;">= (rightPaddleX + ballSize/2))){
//if the ball is in between the top and bottom of the right paddle
if((rightPaddle - (paddleHeight/2) <= yPos) && (yPos <= rightPaddle + (paddleHeight/2))){ //reverse the horizontal direction: xDirection = -xDirection; } } } // if the ball goes off the screen left if(xPos <> width){
leftScore++;
resetBall();
}
//stop the bal going off the top or the bottom ofthe screen;
if((yPos - ballSize/2 <= 0)||(yPos + ballSize/2 >= height)){
//reverse the y direction of the ball
yDirection = -yDirection;
}
//update the ball position
xPos = xPos + xDirection;
yPos = yPos + yDirection;
//Draw the ball
rect(xPos, yPos, ballSize, ballSize);
}
void resetBall(){
//put the ball back in center
xPos = width/2;
yPos = height/2;
}
2008年6月1日日曜日
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿