//**
// * Programa PinPong.
//*/
import processing.serial.*;
//String portname = "COM4"; // aquí colocamos el puerto por el que recibimos el dato
//Serial port; // Creamos un objeto llamado port de la clase Serial
//Traido desde Pro_Osciloscopio
Serial port; // Create object from Serial class
//int val; // Data received from the serial port
//int[] values;
// Variables para definir la pelota
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 5; // Radio
float dy = 0; // Dirección
// variables para definir la pala
int paddle_width = 5;
int paddle_height = 20;
int paddle_pos; // nueva posición
int paddle_ppos; // última posición
int dist_wall = 15;
void setup() {
size(255, 255);
rectMode(CENTER_RADIUS);
ellipseMode(CENTER_RADIUS);
noStroke();
smooth();
ball_y = height/2;
ball_x = 1;
// Abre el puerto al que esta conectada la tarjeta con una velocidad de (19200 bps)
port = new Serial(this,Serial.list()[0], 9600);
}
void draw() {
background(51);
ball_x += ball_dir * 1.0;
ball_y += dy;
if(ball_x > width+ball_size) {
ball_x = -width/2 - ball_size;
ball_y = random(0, height);
dy = 0;
}
while (port.available() >= 3) {
paddle_ppos = paddle_pos;
paddle_pos =port.read();
}
PFont font;
// The font must be located in the sketch's
// "data" directory to load successfully
font = loadFont("Ziggurat.vlw");
textFont(font, 32);
text(paddle_pos, 80, 150);
smooth();
// Desplaza la pala verticalmente en la pantalla
float paddle_y = constrain(paddle_pos, paddle_height, height-paddle_height);
// Testea si la pelota toca la pala
float py = width-dist_wall-paddle_width-ball_size;
if(ball_x == py
&& ball_y > paddle_y - paddle_height - ball_size
&& ball_y < paddle_y + paddle_height + ball_size) {
ball_dir *= -1;
if(paddle_pos != paddle_ppos) {
dy = (paddle_pos - paddle_ppos)/2.0;
if(dy > 5) { dy = 5; }
if(dy < -5) { dy = -5; }
}
}
// Si la pelota toca la pala o la pared, cambia de dirección
if(ball_x < ball_size && ball_dir == -1) {
ball_dir *= -1;
}
// Si la pelota toca la parte superior o inferior del borde, cambia dirección
if(ball_y > height-ball_size) {
dy = dy * -1;
}
if(ball_y < ball_size) {
dy = dy * -1;
}
// Dibuja la pelota
fill(255);
ellipse(ball_x, ball_y, ball_size, ball_size);
// Dibuja la paleta
fill(153);
rect(width-dist_wall, paddle_y, paddle_width, paddle_height);
}
hlpd/curso_arduino/fuentes/caf0030504 (última edición 2011-07-20 15:19:56 efectuada por _desactivada_csoto)