Saturday, October 14, 2006

Pong code not working the way I want it to

For some reason the code "half" works...

When I start my Processing code, my gLed is ON... should be off (set both Leds to LOW in the void section)

Also - when I start playing the game, I wanted the LEDS to blink when the ball hit the paddle... but that slows the entire game down tremendously...

My guess is that the lighting of the pins should occur somehow in sync with the serial event part of the code...

Here is the code... put in BOLD the relevant parts...

ARDUINO CODE:

int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
int yLed = 3;
int gLed = 4;

void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);

pinMode(yLed, OUTPUT);
pinMode(gLed, OUTPUT);

digitalWrite(yLed,LOW);
digitalWrite(gLed,LOW);
}

void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(1)/4;
// read switch, multiply by 255
// so that you're sending 0 or 255:
thirdSensor = 255 * digitalRead(2);
// send sensor values:
Serial.print(firstSensor, BYTE);
Serial.print(secondSensor, BYTE);
Serial.print(thirdSensor, BYTE);

//digitalWrite(yLed, HIGH);
digitalWrite(gLed, HIGH);

if (Serial.read() == 66)
{
digitalWrite(yLed, HIGH);
delay(1000);
digitalWrite(yLed, LOW);
}
else if (Serial.read() == 67)
{
digitalWrite(gLed, HIGH);
delay(1000);
digitalWrite(gLed, LOW);
}
}
}

********************************

PROCESSING CODE:

import processing.serial.*;

Serial port; // The serial port
int[] serialInArray = new int[3]; //Array for serial input signals
int serialCount = 0; // A count of how many bytes we receive
float rightControl, leftControl, resetButton; // Controls
boolean firstContact = false; // Whether we've heard from the microcontroller

PFont font;

//Game Initial Settings
int screenW = 800; //Screen Widht
int screenH = 800; //Screen Height
float ballSpeed = 6; //Initial Ball Speed
float ballSize = 50; //Initail Ball Size

//Game initial stats
int scoreL = 0; //Left player score
int scoreR = 0; //Right player score

int previousScore = 0; //Previous moment's score to determine pause or start state
int level = 1; //Game Level
int previousLevel = 0; //Previous moment's leve to determine scoring

int playgame = -1; //Flag to see if game is paused
int started = 0;

Pong pong; //Pong class variable - each level is a new class

void setup() {
size(screenW,screenH);
frameRate(30);
smooth();
font = loadFont("CourierNew36.vlw");

pong = new Pong(1);

// Print a list of the serial ports, for debugging purposes:
//println(Serial.list());

// Open whatever port is the one you're using.
port = new Serial(this, Serial.list()[0], 9600);
port.write(65); // Send a capital A to start the microcontroller sending
}

void draw() {

background(0);

// If no serial data has beeen received, send again until we get some.
if (firstContact == false) {
delay(300);
port.write(65);
}

//Call class to draw the table/game
pong.drawGameTable();

//If score is over 5 go to next level - reset counters on the way
if (scoreR + scoreL > 5)
{
previousLevel = level;
level++;
pong = new Pong(level);
scoreR = 0;
scoreL = 0;
}
}

//------------------------------
void serialEvent(Serial port) {

float[] buttonFlag = new float[1000];
int i = 0;
int flag = 1; //flag to see if any of the buttonFlags are 0

// if this is the first byte received,
// take note of that fact:
if (firstContact == false) {
firstContact = true;
}
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = port.read();
serialCount++;

// If we have 3 bytes:
if (serialCount > 2 ) {
leftControl = serialInArray[0];
rightControl = serialInArray[1];
resetButton = serialInArray[2];

//println(resetButton);

/*
//If button state changes to zero somewhere along the array, reset game
if (i == 1000) //If i hits 1000, reset back to zero
{
i = 0;
}
i++; //Increment i

buttonFlag[i] = resetButton;

for (int c = 0; c < 1000; c++)
{
if (buttonFlag[c] == 0)
{
flag = 0;
}
}

if (flag == 0)
{
playgame = playgame * -1;
flag = 1;
}
*/

// Send a capital A to request new sensor readings:
port.write(65);

// Reset serialCount:
serialCount = 0;
}
}

//------------------------------
void mouseReleased()
{
playgame = playgame * -1;
}

*************************
class Pong
{
//Ball parameters
float ballX = screenW/2;
float ballY = screenH/2;

//Location of players
float leftX = 20, leftY = screenH/2;
float rightX = screenW - 20, rightY = screenH/2;

//Size of stick
float stickW = 10, stickH = 60;

//Direction
int ballXdirection = 1;
int ballYdirection = 1;


Pong(int _level)
{
level = _level;
ballSize = ballSize - level * 1.10;
ballSpeed = ballSpeed + level * 1.001;
playgame = -1;
}
//-----------------------------------------------------------------------------
void drawGameTable()
{
//External Box and middle line
background(0);
rectMode(CORNER);
strokeWeight(2);
stroke(255);
fill(0);

rect(5, 5, screenW-10, screenH-10);
line(screenW/2, 5, screenW/2, screenH-5);

//Game values
rect(screenW/2-65,10,125,40);
textFont(font, 30);
fill(255);
text(scoreR, screenW/2-40, 32);
text(scoreL, screenW/2+25, 32);
text("x",screenW/2-8,32);
textFont(font, 15);
text("Level ", screenW/2-30, 47);
text(level, screenW/2+20, 47);

// If Mouse is pressed play the game - pressed again - reset game
if (playgame < 0)
{
rectMode(CORNERS);
fill(200,200,200,50);
rect(0,0,screenW,screenH);

if ((started == 0) || (level > previousLevel))
{
fill(255);
text("PRESS MOUSE TO START",screenW/2-100,screenH/2);
previousLevel = level;
ballX = screenW/2;
ballY = screenH/2;
}
else if ((started == 1) || (scoreR + scoreL == previousScore))
{
fill(255);
text("PRESS MOUSE TO CONTINUE",screenW/2-100,screenH/2);
}
}
else if (playgame > 0)
{
started = 1;
pong.moveLeftStick();
pong.moveRightStick();
pong.drawBall();
}
}
//-----------------------------------------------------------------------------
void drawBall()
{
// Update the position of the ball
ballX = ballX + ( ballSpeed * ballXdirection );
ballY = ballY + ( ballSpeed * ballYdirection );

//Ball hitting one of the paddles?
if (dist(ballX,ballY,rightX,rightY) < (ballSize/2+stickW-2))
{
ballXdirection *= -1;
port.write(66);
}

//Ball on Comp side and within Comp paddle?
if (dist(ballX,ballY,leftX,leftY) < (ballSize/2+stickW-2))
{
ballXdirection *= -1;
port.write(67);
}

//Dont let ball go of screen vertically
if (ballY > ((screenH - 8) - ballSize/2) || (ballY < ballSize/2 + 6))
{
ballYdirection *= -1;
}

//SCORE POINTS
if (ballX > rightX + ballSize)
{
playgame = -1;
previousScore = scoreR + scoreL;
scoreR++;
ballX = screenW/2;
ballY = screenH/2;
leftY = screenH/2;
rightY = screenH/2;
}
if (ballX < leftX - ballSize)
{
playgame = -1;
scoreL++;
ballX = screenW/2;
ballY = screenH/2;
leftY = screenH/2;
rightY = screenH/2;
}

//Draw Ball
fill(random(255),random(255),random(255));
ellipseMode(CENTER);
ellipse(ballX, ballY, ballSize, ballSize);
}
//-----------------------------------------------------------------------------
void moveRightStick()
{
rectMode(CENTER);

if (rightY+stickH/2-20 > screenH) {
rightY = screenH-stickH/2-20;
}
else if (rightY-stickH/255-20 < 0) {
rightY = stickH/2 + 21;
}
else {
rightY = (rightControl/255)*screenH;
//rightY = ballY;
}

//Draw Right Paddle
fill(0,0,255);
rect(rightX,rightY,stickW,stickH);

}
//-----------------------------------------------------------------------------
void moveLeftStick()
{
rectMode(CENTER);

if (leftY+stickH/2-10 > screenH) {
leftY = screenH-stickH/2;
}
else if (leftY-stickH/2+5 <= 10) {
leftY = stickH/2 + 12;
}
else {
leftY = (leftControl/255)*screenH;
//leftY = ballY;
}
//Draw left paddle
fill(255,0,0);
rect(leftX,leftY,stickW,stickH);
}
}

No comments: