Help with creating boundary and using shorten.
in
Programming Questions
•
4 months ago
Hi there! I recently submitted a question regarding help with a snake game and I received some amazing help. I was hoping that someone would be able to help walk me through my final two hiccups of the assignment. With the last lot I got some amazing, magical code assistance but for the assignment I've got to keep it as it is given we've only learnt so much and we're not meant to go ahead.
At the moment I'm trying to create some in boundaries inside of the game rather then just having the walls of the game itself. So instead of dying like the snake does when it hits the walls of the game, I was hoping to have the snake stop on impact with these boundaries so it couldn't enter the area. (I've got fences and trees in my background for the game)
I'm using the following lines of code to test this which I then call in the actual drawing of the boundaries (this way I can have boundary1();, boundary(2);, etc.
At the moment I'm trying to create some in boundaries inside of the game rather then just having the walls of the game itself. So instead of dying like the snake does when it hits the walls of the game, I was hoping to have the snake stop on impact with these boundaries so it couldn't enter the area. (I've got fences and trees in my background for the game)
I'm using the following lines of code to test this which I then call in the actual drawing of the boundaries (this way I can have boundary1();, boundary(2);, etc.
- "void boundries()
- {
- noFill();
- noStroke();
- rectMode(CORNER);
- if (inside(x1, y1, x2, y2))
- {
- // println("Inside!");
- }
- else {
- //println("Outside!");
- }
- noFill();
- noStroke();
- rect(x1, y1, x2, y2);
- }
- // a simple boolean function that will return true if the mouse is inside the two points
- boolean inside(int x1, int y1, int x2, int y2)
- {
- // This is broken up for speed.
- // instead of doing four checks and then getting a false we check each one and can get a faster fail
- if (px > x1)
- {
- if ( px < x2)
- {
- if ( py > y1)
- {
- if ( py < y2)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
the second issue I'm having is in regards to resetting my snake after finishing the game. My teacher has told me to use append and shorten in order to achieve this, but I can only seem to shorten the array by 1 through using shorten. My snake starts off at 6 in length, and for every rupee it consumes the snake body grows one. When the game ends (say I have 10 rupees) and the body length is 16, when I reset the game my snake starts at 15 rather then 6.
my line for append is-
- snakePosX = append(snakePosX, snakePosX[snakePosX.length-1]+1);
- snakePosY = append(snakePosY, snakePosY[snakePosY.length-1]+1);
and for shorten it is -
- snakePosX =shorten(snakePosX);
- snakePosY =shorten(snakePosY);
I realise shorten would have to be elaborated somehow, but I seem to be getting a multitude of issues whenever I try to involve the array in any form or even mention .length.
I'd post the full code, but I'm a little paranoid of people in my degree finding it while searching for help like I am now, haha! Any help would be super appreciated though.
1