Grille grille1 = new Grille(10, 10);
Pacman pac1 = new Pacman(5, 5);
int t = 0;
void setup(){
size(500, 500);
frameRate(10);
grille1.update(pac1);
grille1.display();
}
void draw(){
pac1.getDirection();
if (t % 8 == 0) {
pac1.move(grille1);
grille1.update(pac1);
}
grille1.display();
pac1.display();
t++;
}
class Grille {
int _l;
int _c;
String[][] grille;
Grille(int lignes, int colonnes) {
_l = lignes;
_c = colonnes;
grille = new String[lignes][colonnes];
for (int i = 0; i < lignes; i++) {
for (int j = 0; j < colonnes; j++) {
grille[i][j] = "v";
}
}
}
void display() {
for (int i = 0; i < _l; i++) {
for (int j = 0; j < _c; j++) {
if (grille[i][j] == "v") {
fill(#FFFFFF);
rect(30 * j, 30 * i, 30, 30);
}
else if (grille[i][j] == "pac") {
fill(50, 200, 50);
rect(30 * j, 30 * i, 30, 30);
}
}
}
}
void update(Pacman pac) {
grille[pac._y][pac._x] = "pac";
}
}
class Pacman {
int _x;
int _y;
float _xNew;
float _yNew;
String _direction;
String _directionOld;
int _sens;
int _sensOld;
float _xDisplay;
float _z;
Pacman(int x, int y) {
_direction= "horizontal";
_sens = -1;
_directionOld= "horizontal";
_sensOld = -1;
_x = x;
_y = y;
_xNew = x;
_yNew = y;
_z = 0;
}
void move(Grille grille) {
grille.grille[_y][_x] = "v";
if (_direction == "horizontal" && _sens == 1) { //conditions pour faire se déplacer la tete du serpent, après que tous les autres anneau se soient déplacés
_x += 1;
}
else if (_direction == "horizontal" && _sens == -1) {
_x -= 1;
}
else if (_direction == "vertical" && _sens == 1) {
_y += 1;
}
else if (_direction == "vertical" && _sens == -1) {
_y -= 1;
}
_x = _x % 10; // les 6 lignes qui suivent servent à faire revenir le serpent par le coté opposé quand il sort de la grille
_y = _y % 10; // il y a surement besoin de faire ça de maniere bien plus jolie
if (_x == -1) {
_x = 9;
}
if (_y == -1) {
_y = 9;
}
}
void getDirection() {
_directionOld = _direction;
_sensOld = _sens;
if (keyPressed == true) {
if (key == CODED) {
if (keyCode == LEFT) {
_direction = "horizontal";
_sens = -1;
}
if (keyCode == RIGHT) {
_direction = "horizontal";
_sens = 1;
}
if (keyCode == UP) {
_direction = "vertical";
_sens = -1;
}
if (keyCode == DOWN) {
_direction = "vertical";
_sens = 1;
}
}
}
}
void display() {
_z += 3.75;
if (_direction == "horizontal" && _sens == -1) {
fill(200, 100, 50);
rect(30 * (_x+1) - _z, 30 * _y, 30, 30);
}
else if (_direction == "horizontal" && _sens == 1) {
fill(200, 100, 50);
rect(30 * (_x-1) + _z, 30 * _y, 30, 30);
}
else if (_direction == "vertical" && _sens == -1) {
fill(200, 100, 50);
rect(30 * _x, 30 * (_y+1) - _z, 30, 30);
}
else if (_direction == "vertical" && _sens == 1) {
fill(200, 100, 50);
rect(30 * _x, 30 * (_y-1) + _z, 30, 30);
}
_z = _z % 30;
}
}