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.getInput();
if (t % 8 == 0) {
pac1.move(grille1);
grille1.update(pac1);
pac1._direction = pac1._input;
pac1._directionOld = pac1._direction;
}
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;
float _xDisplay;
float _z;
String _input;
Pacman(int x, int y) {
_input = "gauche";
_direction= "gauche";
_directionOld= "gauche";
_x = x;
_y = y;
_xNew = x;
_yNew = y;
_z = 0;
}
void move(Grille grille) {
grille.grille[_y][_x] = "v";
if (_direction == "droite") { //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 == "gauche") {
_x -= 1;
}
else if (_direction == "bas") {
_y += 1;
}
else if (_direction == "haut") {
_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 getInput() {
if (keyPressed == true) {
if (key == CODED) {
if (keyCode == LEFT) {
_input = "gauche";
}
if (keyCode == RIGHT) {
_input = "droite";
}
if (keyCode == UP) {
_input = "haut";
}
if (keyCode == DOWN) {
_input = "bas";
}
}
}
}
void display() {
if (_directionOld == "gauche") {
fill(200, 100, 50);
rect(30 * (_x) - _z, 30 * _y, 30, 30);
}
else if (_directionOld == "droite") {
fill(200, 100, 50);
rect(30 * (_x) + _z, 30 * _y, 30, 30);
}
else if (_directionOld == "haut") {
fill(200, 100, 50);
rect(30 * _x, 30 * (_y) - _z, 30, 30);
}
else if (_directionOld == "bas") {
fill(200, 100, 50);
rect(30 * _x, 30 * (_y) + _z, 30, 30);
}
_z += 3.75;
_z = _z % 30;
}
boolean isTurning(){
if ((_direction == "gauche" || _direction == "droite") && (_directionOld == "haut" || _directionOld == "bas")) {
return true;
}
else if ((_direction == "haut" || _direction == "bas") && (_directionOld == "gauche" || _directionOld == "droite")) {
return true;
}
else {
return false;
}
}
}