Created
December 15, 2016 13:40
-
-
Save shioken/aa8796c168e1b345c1b9c6241850b664 to your computer and use it in GitHub Desktop.
Snake Game by Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
size(640, 480); | |
frameRate(8); | |
background(0); | |
init(); | |
} | |
class Body { | |
public int x; | |
public int y; | |
public Body(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public boolean touchWall() { | |
return x == 0 || y == 0 || x == WIDTH - 2 || y == HEIGHT - 2; | |
} | |
public boolean checkCollision(Body body) { | |
return x == body.x && y == body.y; | |
} | |
} | |
static final int MAX_LENGTH = 256; | |
static final int WIDTH = 40; | |
static final int HEIGHT = 30; | |
int map[] = new int[WIDTH * HEIGHT]; | |
Body bodies[] = new Body[MAX_LENGTH]; | |
int head_index; | |
int tail_index; | |
int x1, y1; | |
int status; | |
void init() { | |
head_index = 0; | |
tail_index = 1; | |
x1 = 1; | |
y1 = 0; | |
bodies[0] = new Body(WIDTH / 2, HEIGHT / 2); | |
for (int i = 0; i < WIDTH * HEIGHT; i++) { | |
map[i] = 0; | |
} | |
for (int i = 0; i < WIDTH / 2; i++) { | |
int x = (int)random(WIDTH - 2) + 1; | |
int y = (int)random(HEIGHT - 2) + 1; | |
map[x + y * WIDTH] = 1; | |
} | |
for (int i = 0; i < WIDTH / 4; i++) { | |
int x = (int)random(WIDTH - 2) + 1; | |
int y = (int)random(HEIGHT - 2) + 1; | |
map[x + y * WIDTH] = 2; | |
} | |
status = 0; | |
tail_length = 10; | |
} | |
void draw() { | |
if (status == 0) { | |
next(); | |
} | |
int unit = width / WIDTH; | |
background(0); | |
stroke(255); | |
noFill(); | |
rect(unit / 2, unit / 2, width - unit, height - unit); | |
stroke(0); | |
for (int i = 0; i < WIDTH * HEIGHT; i++) { | |
if (map[i] > 0) { | |
if (map[i] == 1) { | |
fill(255, 0, 0); | |
} | |
else if (map[i] == 2) { | |
fill(255, 0, 255); | |
} | |
int x = i % WIDTH; | |
int y = i / WIDTH; | |
rect(x * unit, y * unit, unit, unit); | |
} | |
} | |
//noStroke(); | |
int index = head_index; | |
while (index != tail_index) { | |
fill(255); | |
Body body = bodies[index]; | |
rect(body.x * unit, body.y * unit, unit, unit); | |
index = (index + 1) % MAX_LENGTH; | |
} | |
//saveFrame("snake_###.png"); | |
} | |
int tail_length; | |
void next() { | |
Body body = bodies[head_index]; | |
int p = map[body.x + body.y * WIDTH]; | |
if (p == 1) { | |
tail_length++; | |
map[body.x + body.y * WIDTH] = 0; | |
} | |
else if (p == 2) { | |
status = 1; | |
return; | |
} | |
if (body.touchWall()) { | |
status = 1; | |
return; | |
} | |
int index_tail = (head_index + 1) % MAX_LENGTH; | |
while (index_tail != tail_index) { | |
Body tail_body = bodies[index_tail]; | |
if (body.checkCollision(tail_body)) { | |
status = 1; | |
return; | |
} | |
index_tail = (index_tail + 1) % MAX_LENGTH; | |
} | |
head_index = (head_index - 1 + MAX_LENGTH) % MAX_LENGTH; | |
if (tail_length > 0) { | |
tail_length--; | |
} | |
else { | |
tail_index = (tail_index - 1 + MAX_LENGTH) % MAX_LENGTH; | |
} | |
bodies[head_index] = new Body(body.x + x1, body.y + y1); | |
} | |
void keyPressed() { | |
if (key == CODED) { | |
if (keyCode == UP) { | |
x1 = 0; | |
y1 = -1; | |
} | |
else if (keyCode == DOWN) { | |
x1 = 0; | |
y1 = 1; | |
} | |
else if (keyCode == LEFT) { | |
x1 = -1; | |
y1 = 0; | |
} | |
else if (keyCode == RIGHT) { | |
x1 = 1; | |
y1 = 0; | |
} | |
} | |
else { | |
if (keyCode == ENTER) { | |
if (status == 1) { | |
init(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment