I'm working on a snake game that will eventually be controlled by an arduino (i have it commented out for now). To begin, I used some of a code I found online and changed a bit of it. I'm having trouble with my speedUp() function. Here is what I have so far:
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int buttonUp = 6;
int buttonRight = 5;
int buttonLeft = 4;
int buttonDown = 3;
color snakeColor = color(100, 255, 100);
color foodColor = color(255, 0, 0);
int windowSize = 300;
float xSpeed = frameRate;
float ySpeed = frameRate;
float speed = frameRate;
int centerX;
int centerY;
int snakeSize = 1;
int snakeX = 0;
int snakeY = 0;
int snakesX[];
int snakesY[];
int foodX = -1;
int foodY = -1;
int moveX = 0;
int moveY = 0;
boolean check = true;
boolean gameOver = false;
PFont Font = createFont("Arial", 20, true);
void setup() {
size(windowSize, windowSize, P3D);
println(Arduino.list());
//arduino = new Arduino(this, Arduino.list()[5], 57600);
// for (int i = 0; i <= 13; i++)
// arduino.pinMode(i, Arduino.INPUT);
background(0);
//speed = 1;
xSpeed = frameRate;
ySpeed = frameRate;
snakesX = new int[100];
snakesY = new int[100];
centerX = width/2;
centerY = height/2;
snakeX = centerX-5;
snakeY = centerY-5;
foodX = -1;
foodY = -1;
snakeSize = 1;
gameOver = false;
check = true;
}
void draw() {
if (speed%10 == 0) {
background(0);
playGame();
}
speed++;
}
void playGame() {
if (gameOver == false) {
drawFood();
drawSnake();
snakeMove();
ateFood();
checkHitSelf();
}
else {
String text = "Game Over";
textAlign (CENTER);
fill(200, 0, 0);
text(text, width/2, height/2, 50);
textFont(Font);
textAlign(CENTER);
String score = "Score: " + snakeSize;
fill(0, 0, 200);
text(score, width/2, height/2 + 50, 10);
}
// move up
/* if (arduino.digitalRead(6) == Arduino.HIGH) {
if (snakesY[1] != snakesY[0]-10) {
moveX = 0;
moveY = -10;
}
}
// move right
if (arduino.digitalRead(5) == Arduino.HIGH) {
if (snakesX[1] != snakesX[0]+10) {
moveX = 10;
moveY = 0;
}
}
// move left
if (arduino.digitalRead(4) == Arduino.HIGH) {
if (snakesX[1] != snakesX[0]-10 ) {
moveX = -10;
moveY = 0;
}
}
// move down
if (arduino.digitalRead(3) == Arduino.HIGH) {
if (snakesX[1] != snakesX[0]+10) {
moveX = 0;
moveY = 10;
}
}
*/
}
void reset() {
snakeX = centerX-5;
snakeY = centerY-5;
gameOver = false;
check = true;
snakeSize = 1;
moveY = 0;
moveX = 0;
}
void checkHitSelf() {
for (int i = 1; i < snakeSize; i++) {
if (snakeX == snakesX[i] && snakeY == snakesY[i]) {
The draw function was something I copied directly from the original code, and to be honest, I can't quite understand what it's doing. I tried changing it and make it my own, but it causes my game not to work properly.
In my speedUp() function I tried just changing the values of moveX and moveY, but that caused the snake movement to be way outta wack. From what I have now, the speed variable isn't really having much effect on the movement of the snake (or correct me if it is!), but any time i try to do anything with it, it either has no effect, or makes it go crazy. Any suggestions on how to either repair the draw() function or how to enable me to cause the snake to speed up?