int Player_x = 50;
int Player_y = 50;
int counter = 10;
int direction = 0;
int dleft = 10;
int dright = 20;
int dup = 30;
int ddown = 40;
int snakecounter = 1;
int dx = 0;
int dy = 0;
int X = 0;
int Y = 0;
int [] snakeX = new int[100];
int [] snakeY = new int[100];
int field_x = (int)random(0, 10);
int field_y = (int)random(0, 10);
void generate_fruit()
{
field_x = (int)random(0, 10);
field_y = (int)random(0, 10);
}
void set_table()
{
background(0, 0, 0);
for (int i = 0 ; i < 10; i++)
{
for (int j = 0 ; j < 10; j++)
{
if (!(field_x == i && field_y == j)) {
fill(255, 255, 255);
rect(i*50, j*50, 50, 50);
}
else
{
fill(0, 0, 255);
rect(i*50, j*50, 50, 50);
}
if (i*50 == Player_x && j*50 == Player_y && field_x == i && field_y == j) {
snakecounter++;
generate_fruit();
}
}
}
}
void setup()
{
size(700, 700);
set_table();
}
void draw()
{
set_table();
snakeX[snakecounter - 1] = Player_x;
snakeY[snakecounter - 1] = Player_y;
for (int i = 0; i < snakecounter - 1; i++)
{
if (snakecounter > 1){
if (direction == dleft) snakeX[snakecounter - 1] +=50;
if (direction == dright) snakeX[snakecounter - 1] -=50;
if (direction == dup) snakeY[snakecounter - 1] += 50;
if (direction == ddown) snakeY[snakecounter-1] -= 50;
fill(122,122,122);
rect(snakeX[snakecounter - 1], snakeY[snakecounter -1], 50, 50);
}
}
fill(255, 0, 0);
rect(Player_x, Player_y, 50, 50);
}
void keyPressed() {
if (keyCode == UP && Player_y > 0)
{
direction = dup;
Player_y -= 50;
}
if (keyCode == DOWN && Player_y < 450)
{
direction = ddown;
Player_y += 50;
}
if (keyCode == LEFT && Player_x > 0)
{
direction = dleft;
Player_x -= 50;
}
if (keyCode == RIGHT && Player_x <450)
{
direction = dright;
Player_x += 50;
}
}
|