Snake Game speedUp() help
in
Programming Questions
•
5 months ago
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]) {
- gameOver = true;
- }
- }
- }
- void ateFood() {
- if (foodX == snakeX && foodY == snakeY) {
- check = true;
- snakeSize++;
- }
- }
- void drawFood() {
- fill(foodColor);
- while (check) {
- int x = (int) random(1, windowSize/10);
- int y = (int) random(1, windowSize/10);
- foodX = 5+x*10;
- foodY = 5+y*10;
- for (int i = 0; i < snakeSize; i++) {
- if (x == snakesX[i] && y == snakesY[i]) {
- check = true;
- i = snakeSize;
- }
- else {
- check = false;
- }
- }
- }
- rect(foodX, foodY, 10, 10);
- }
- void drawSnake() {
- fill(snakeColor);
- for (int i = 0; i < snakeSize; i++) {
- int x = snakesX[i];
- int y = snakesY[i];
- rect(x, y, 10, 10);
- }
- for (int i = snakeSize; i > 0; i--) {
- snakesX[i] = snakesX[i-1];
- snakesY[i] = snakesY[i-1];
- }
- }
- void snakeMove() {
- snakeX += moveX;
- snakeY += moveY;
- if (snakeSize %2 == 0) {
- speedUp();
- }
- if (snakeX > windowSize-2 || snakeX < 0 || snakeY > windowSize-2 || snakeY < 0) {
- gameOver = true;
- }
- snakesX[0] = snakeX;
- snakesY[0] = snakeY;
- }
- void speedUp() {
- moveX++;
- moveY++;
- }
- void keyPressed() {
- if (keyCode == UP) {
- if (snakesY[1] != snakesY[0]-10) {
- moveY = -10;
- moveX = 0;
- }
- }
- if (keyCode == DOWN) {
- if (snakesY[1] != snakesY[0]+10) {
- moveY = 10;
- moveX = 0;
- }
- }
- if (keyCode == LEFT) {
- if (snakesX[1] != snakesX[0]-10 ) {
- moveX = -10;
- moveY = 0;
- }
- }
- if (keyCode == RIGHT) {
- if (snakesX[1] != snakesX[0]+10) {
- moveX = 10;
- moveY = 0;
- }
- }
- if (keyCode == 'R') {
- reset();
- }
- }
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?
Thanks in advance!
1