I need help on my programming game. I only have ten minutes or i will fail. This is my code.

edited December 2016 in Questions about Code

I only have a problem with my dot. `Snake mySnake;// My snake. Dot newDot;// Dot that the Snake gets.

int hitCount = 0; // Counter for how many times the dot has been eaten int Snakespeed=7;// Sets speed for Snake.

void setup(){ size(500,500); //Creates new Snake and new Dot. mySnake= new Snake(); newDot= new Dot(); }

void draw(){ background(0,255,0); text (hitCount, width - 50, height - 20);// place scoe keeper in bottom right corner fill(0); newDot.display();// Draws dot randomly on screen mySnake.display();// Snake starts in the middle of the screen mySnake.move();// Allows me to move the Snake.

// Checks if the Snake gets the dot or not. if (rectangle_collision (mySnake.xpos, mySnake.ypos, mySnake.sizeW, mySnake.sizeH, newDot.xpos, newDot.ypos, newDot.sizeW, newDot.sizeH)) {

hitCount= hitCount+1;// Increases hit counter by 1. if (hitCount == 10) { hitCount = 0; newDot.kill();

} else{ newDot= new Dot(); //Redraws Dot somewhere random. } {

if (mySnake.xpos + mySnake.sizeW/2 > width) { mySnake.xpos = mySnake.xpos - Snakespeed; } else if (mySnake.xpos - mySnake.sizeW/2 < 0) { mySnake.xpos = mySnake.xpos + Snakespeed; } else if (mySnake.ypos + mySnake.sizeH/2 > height) { mySnake.ypos = mySnake.ypos - Snakespeed; } else if (mySnake.ypos - mySnake.sizeH/2 < 0) { mySnake.ypos = mySnake.ypos + Snakespeed; } }

// set up of boolean to determine collision boolean rectangle_collision(float SnakeX, float SnakeY, float SnakeW, float SnakeH, float DotX, float DotY, float DotW,float DotH) // if it's true it returns { return (DotX + DotW/2 SnakeX - SnakeW/2 && // and if the left side of the Dot is farther right than the left side of the Snake DotY +DotW/2 < SnakeY + SnakeW/2 && // and if the bottom of the Dot is higher than the bottom of the Snake DotY - DotW/2 > SnakeY - SnakeW/2); // and if the top of the Dot is lower than the top of the Snake

} // Makes Snake move. void keyPressed() { // Only works if i is true. int k = keyCode; if (k == UP) mySnake.up = true; // sets up movement to true if up is pressed else if (k == DOWN) mySnake.down = true; // sets down movement to true if down is pressed else if (k == LEFT) mySnake.left = true; // sets left movement to true if left is pressed else if (k == RIGHT) mySnake.right = true; // sets right movement to true if right is pressed } void keyReleased() { int k = keyCode; //Code for if it is false if (k == UP) mySnake.up = false; else if (k == DOWN) mySnake.down = false; else if (k == LEFT) mySnake.left = false; else if (k == RIGHT) mySnake.right = false; }

class Snake { color c; // sets color of Snake/ C also stores colors so you dont have to add the RG everytime float xpos; // xpos of Snake float ypos; // ypos of Snake int sizeW = 10; // width of Snake int sizeH = 90; // height of Snake boolean up, down, left, right; // booleans for Snake movement

// Snake Class

Snake() { c= color(0,128,0); // Color is green. xpos = width/2; // Snake is centered at the start of game. ypos = width/2; }

void display() {
rectMode(CENTER); // position determined by the center point of Snake
stroke(0); // black outline to Snake
fill(c); // Fill is the light gray
rect(xpos, ypos, sizeW, sizeH); // draw the Snake in the center with dimensions 50x50

} // Uses the boolean I used on top //since the snake moves in any direction it can move diagonally void move() { if (up) ypos -= Snakespeed; if (down) ypos += Snakespeed; if (left) xpos -= Snakespeed; if (right) xpos += Snakespeed; }

class Dot { color c; // color of dot. float xpos; // xpos of dot float ypos; // ypos of dot int sizeW = 10; // width of dot int sizeH = 10; // height of dot

Dot() { c = color(255,0,0); // color is red xpos = int(random(width - sizeW/2)); // xposition of dot ypos = int(random(height - sizeH/2)); // yposition of dot } void display() { rectMode(CENTER);// Changes location of where it is drawn stroke(0);// Oulines the dot fill(c); rect(xpos, ypos, sizeW, sizeH);// Draws it } //Allows the grape to go somewhere else when snake ges it. void teleport() { xpos = int(random(width)); ypos = int(random(height)); } void kill(){ xpos= 1000; ypos= 1000; }} }`

Tagged:

Answers

This discussion has been closed.