booleandeadly_wall=true; //if this is true, the player will die if he runs against the wall, if this is set to false, the player will get to the other side when they run against the wall
“Snake” by RedstoneTim
https://openprocessing.org/sketch/543759
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Arrow keys or WASD to move the black snake, get larger by eating the orange food and don't run into yourself
CC Attribution ShareAlike
Snake
xxxxxxxxxx
float tile = 32, x = tile*2, y = tile*2, f_x, f_y;
int delay = 40, snake_length = 100, toShow = 0;
char pressed = 'D';
boolean deadly_wall = true; //if this is true, the player will die if he runs against the wall, if this is set to false, the player will get to the other side when they run against the wall
Body[] body;
boolean[] show_body = new boolean[snake_length+1];
float[] last_x = new float[snake_length+1], last_y = new float[snake_length+1];
void setup() {
size(640, 640);
frameRate(12);
applePos();
body = new Body[snake_length];
for (int i = 0; i<snake_length; i++) {
body[i] = new Body(i);
}
}
void draw() {
for (int i = 3; i<snake_length+1; i++) {
//when player runs against themselves
if (x == last_x[i] && y == last_y[i] && show_body[i]) {
playerDied();
}
}
//draw background
for (int i = 0; i<width; i+=tile) {
for (int j = 0; j<height; j+=tile) {
fill(255);
rect(i, j, tile, tile);
}
}
//show bodies of the snake
for (int i = 0; i<snake_length; i++) {
body[i].showBody();
}
//set x pos for the other snakebodies
last_x[0] = last_x[0]!=x ? x : last_x[0];
last_y[0] = last_y[0]!=y ? y : last_y[0];
for (int a = snake_length; a>0; a--) {
last_x[a] = last_x[a] != last_x[a-1] ? last_x[a-1]:last_x[a];
last_y[a] = last_y[a] != last_y[a-1] ? last_y[a-1]:last_y[a];
}
fill(0);
rect(x, y, tile, tile); //player
fill(255, 123, 0);
rect(f_x, f_y, tile, tile); //apple
y-=pressed == 'W' ? tile:0;
y+=pressed == 'S' ? tile:0;
x-=pressed == 'A' ? tile:0;
x+=pressed == 'D' ? tile:0;
//if the wall is deadly, player will de
if (deadly_wall) {
if (x<0 || x>width-tile || y<0 || y>height-tile) {
playerDied();
}
//if the wall isn't deadly, just get to the other side of the map
} else if (!deadly_wall) {
x=x<0 ? width-tile:x;
x=x>width-tile ? 0:x;
y=y<0 ? height-tile:y;
y=y>height-tile ? 0:y;
}
//when player eats apple, show other body and make new apple pos
if (x==f_x && y==f_y) {
applePos();
show_body[toShow] = true;
toShow++;
//println("Player ate apple. Showing body number", toShow-1);
}
}
void playerDied() {
x = tile*2;
y = tile*2;
for(int i = 0; i < show_body.length; i++){
show_body[i] = false;
toShow = 0;
}
applePos();
pressed = 'D';
}
void applePos() {
f_x = int(random(width)/tile)*tile;
f_y = int(random(height)/tile)*tile;
}
void keyPressed() {
pressed = (key == 'w' || keyCode == UP) && pressed != 'S' ? 'W':pressed;
pressed = (key == 'a' || keyCode == LEFT) && pressed != 'D' ? 'A':pressed;
pressed = (key == 's' || keyCode == DOWN) && pressed != 'W' ? 'S':pressed;
pressed = (key == 'd' || keyCode == RIGHT) && pressed != 'A' ? 'D':pressed;
}
class Body {
//for the extra bodies of the snake
int number;
Body(int number) {
this.number = number>=1 ? number:number;
}
//just show the snake's body
void showBody() {
if (show_body[this.number]) {
fill(0);
rect(last_x[number+1], last_y[number+1], tile, tile);
}
}
}
See More Shortcuts