やすこ
第3章の進捗としては、P103まで終了。
とはいえArduinoがないので、書き込んでおらず、センサもないのでうまくいってるか疑問。とりあえずソースだけ完成させる作戦です。
しの様のArduinoへの書き込みと連動して、Flow Controlを行ったProce55ingソースも載せときます。
※これはNow you can play Monski Pongのソースとは違います!!まだArduinoへ新しいソースを書き込んでいない人は、さたけの日記へのコメントを参照してください。
import processing.serial.*;
//myPort(任意名)というオブジェクトを用意
Serial myPort;
int linefeed = 10;
float leftPaddle, rightPaddle;
int resetButton, serveButton;
int leftPaddleX, rightPaddleX;
int paddleHeight = 50;
int paddleWidth = 10;
float leftMinimum = 200;//ここは自分のセンサーの値にする
float rightMinimum = 250;
float leftMaximum = 450;
float rightMaximum = 600;
int ballSize = 10;
int xDirection = 1;
int yDirection = 1;
int xPos, yPos;
boolean ballInMotion = false;
int leftScore = 0;
int rightScore = 0;
PFont myFont;
int fontSize = 36;
boolean madeContact = false;
void setup(){
size(640, 480);
xPos = width/2;
yPos = height/2;
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(linefeed);
leftPaddle = height/2;
rightPaddle = height/2;
resetButton = 0;
serveButton = 0;
leftPaddleX = 50;
rightPaddleX = width - 50;
noStroke();
PFont myFont = createFont(PFont.list()[2], fontSize);
textFont(myFont);
}
void draw(){
if (madeContact == false){
myPort.write('\r');
}
background(0);
rect(leftPaddleX, leftPaddle, paddleWidth, paddleHeight);
rect(rightPaddleX, rightPaddle, paddleWidth, paddleHeight);
if (ballInMotion == true){
animateBall();
}
if (serveButton == 1){
ballInMotion = true;
}
if (resetButton == 1){
leftScore = 0;
rightScore =0;
ballInMotion = true;
}
text(leftScore, fontSize, fontSize);
text(rightScore, width-fontSize, fontSize);
}
void serialEvent(Serial myPort){
madeContact = true;
String myString = myPort.readStringUntil(linefeed);
if (myString != null){
myString = trim(myString);
int sensors[] = int (split(myString, ','));
if(sensors.length ==4){
float leftRange = leftMaximum - leftMinimum;
float rightRange = rightMaximum - rightMinimum;
leftPaddle = height * (sensors[0] - leftMinimum)/leftRange;
rightPaddle = height * (sensors[1] - rightMinimum)/rightRange;
resetButton = sensors[2];
serveButton = sensors[3];
print("left:"+leftPaddle + "\tright:"+rightPaddle);
println("\treset:"+resetButton+"\tserve:"+serveButton);
myPort.write('\r');
}
}
}
void animateBall(){
if (xDirection < 0){
if ((xPos <= leftPaddleX)){
if((leftPaddle - (paddleHeight/2) <= yPos)&&
(yPos <= leftPaddle + (paddleHeight/2))){
xDirection =-xDirection;
}
}
}
else {
if ((xPos >= (rightPaddleX + ballSize/2))){
if((rightPaddle - (paddleHeight/2) <=yPos)&&
(yPos <= rightPaddle + (paddleHeight/2))){
xDirection =-xDirection;
}
}
}
if (xPos <0){
rightScore++;
resetBall();
}
if (xPos > width){
leftScore++;
resetBall();
}
if ((yPos - ballSize/2 <= 0) || (yPos +ballSize/2 >= height)){
yDirection = -yDirection;
}
xPos = xPos + xDirection;
yPos = yPos + yDirection;
rect(xPos,yPos, ballSize, ballSize);
}
void resetBall(){
xPos = width/2;
yPos = height/2;
}
0 件のコメント:
コメントを投稿