diff --git a/GameData/Images/blued.png b/GameData/Images/blued.png new file mode 100644 index 0000000..d936c6a Binary files /dev/null and b/GameData/Images/blued.png differ diff --git a/GameData/Images/bluel.png b/GameData/Images/bluel.png new file mode 100644 index 0000000..28bfe58 Binary files /dev/null and b/GameData/Images/bluel.png differ diff --git a/GameData/Images/cookie.png b/GameData/Images/cookie.png new file mode 100644 index 0000000..78560b9 Binary files /dev/null and b/GameData/Images/cookie.png differ diff --git a/GameData/Images/oranged.png b/GameData/Images/oranged.png new file mode 100644 index 0000000..f6cdaf9 Binary files /dev/null and b/GameData/Images/oranged.png differ diff --git a/GameData/Images/orangel.png b/GameData/Images/orangel.png new file mode 100644 index 0000000..8b155a4 Binary files /dev/null and b/GameData/Images/orangel.png differ diff --git a/GameData/Images/pacd.png b/GameData/Images/pacd.png new file mode 100644 index 0000000..bbbfaf0 Binary files /dev/null and b/GameData/Images/pacd.png differ diff --git a/GameData/Images/pacl.png b/GameData/Images/pacl.png new file mode 100644 index 0000000..a5fa9bc Binary files /dev/null and b/GameData/Images/pacl.png differ diff --git a/GameData/Images/pinkd.png b/GameData/Images/pinkd.png new file mode 100644 index 0000000..2e3ef88 Binary files /dev/null and b/GameData/Images/pinkd.png differ diff --git a/GameData/Images/pinkl.png b/GameData/Images/pinkl.png new file mode 100644 index 0000000..4dfd2a5 Binary files /dev/null and b/GameData/Images/pinkl.png differ diff --git a/GameData/Images/redd.png b/GameData/Images/redd.png new file mode 100644 index 0000000..1696bae Binary files /dev/null and b/GameData/Images/redd.png differ diff --git a/GameData/Images/redl.png b/GameData/Images/redl.png new file mode 100644 index 0000000..151b6f7 Binary files /dev/null and b/GameData/Images/redl.png differ diff --git a/src/Button.java b/src/Button.java new file mode 100644 index 0000000..9f1eb45 --- /dev/null +++ b/src/Button.java @@ -0,0 +1,35 @@ +import edu.princeton.cs.introcs.StdDraw; + +public class Button extends Component{ + public Button (int x0,int y0, int w, int h, String t){ + super( x0, y0, w, h, t); + + + } + + //changement visuel qui montre que la souris est sur le bouton + public void hooverDraw(){ + StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE); + hoovered = true; + StdDraw.rectangle(x, y, width-6, height-4); + StdDraw.text(x, y, text); + StdDraw.show(); + } + +//TODO CALL LE HOOVER 1 FOIS? + //check si la souris est dessus + public boolean hoover(){ + if(StdDraw.mouseX()>x-width && StdDraw.mouseX()y-height && StdDraw.mouseY() 0){ + str.deleteCharAt(str.length()-1); + + } + }else if(lettre<123 && lettre>96 && str.length()<20){ + //limite la taille du nom à 20 char + //On va chercher la lettre coresspondante + //CODE A: 97 Z:122 + str.append(alphabet[lettre-97]); + + + } + text = str.toString(); + updatetxt(); + } + + } + + + +} diff --git a/src/Ghost.java b/src/Ghost.java index 89ec94e..bc7103e 100644 --- a/src/Ghost.java +++ b/src/Ghost.java @@ -1,7 +1,50 @@ public class Ghost extends Perso{ -public Ghost(int x0, int y0, int dir0, String nom) { - super(x0, y0, dir0, nom); +public Ghost(int x0, int y0, int dir0, String nom,String pic) { + super(x0, y0, dir0, nom,pic); } +//0: bas 1: haut 2: gauche 3: droite +// fait "rebondir " un fantôme touche un qui mur +public void bounchehitwall(Map map){ + int[] newdir = new int[3]; + int oldir =this.dir; + switch(oldir){ + case 0: + //Si le fantôme touche un mur en se déplacçant dans une direction + //On définie les nouveles directions possibles + + newdir = new int[]{2,3,1}; + break; + case 1: + newdir = new int[]{2,3,0}; + break; + case 2: + newdir = new int[]{1,0,3}; + break; + case 3: + newdir = new int[]{1,0,2}; + break; + + } + //Le fantôme choisis une nouvelle direction au hasard + //ordre: en arrière au dernier recours + int k = 0; + while( k<3 && this.checkhitwall(map, newdir[k])){ + k = k+1; + } + this.dir = newdir[k]; + //évite de prendre comme prochaine direction un retour en arrière + while(Math.abs(this.buffer-this.dir)==1){ + this.buffer = newdir[Main.random.nextInt(3)]; + } +/* + if(!this.checkhitwall(map, this.dir)){ + this.buffer = newdir[Main.random.nextInt(2)]; + }*/ + + //On met cette direction en prochaine direction pour éviter les aller-retour sur place + + + } } diff --git a/src/Joueur.java b/src/Joueur.java new file mode 100644 index 0000000..3d672bd --- /dev/null +++ b/src/Joueur.java @@ -0,0 +1,62 @@ + +public class Joueur extends Perso{ + int score; + int vie; + boolean bonus; + public Joueur(int x0, int y0, int dir0, String nom, String pic) { + super(x0, y0, dir0, nom,pic); + score = 0; + vie = 3; + + } + + public void hitwall(){ + this.dir = -1; + } + + //On regarde si le joueur est sur la même case qu'un cookie + public void checkhitcookie(Map map){ + //On convertit la coordonnée du personnage en coordonnée du tableau de la carte + int[]coordcase = map.toCase(this.x, this.y); + int nx = coordcase [0]; + int ny = coordcase[1]; + + + if (map.coordcookie[nx][ny]!=null){ + + //identifie le cookie touché + Cookie supp = map.coordcookie[nx][ny]; + + + + //retire le cookie de la map + map.coordcookie[nx][ny]=null; + + //décompte du nombre de cookie + map.nbcookie = map.nbcookie-1; + + //augmente le score + this.score = this.score+2; + + //rafraichit l'affichage du score + Main.refreshScore(); + + //efface le cookie de l'affichage + supp.effacer(); + + //efface le cookie dans la liste chaînée + map.deleteCookie(supp); + + //si il ne reste plus de cookie, on termine la partie + if(map.nbcookie<1){ + Main.fin(); + } + + + + } + } + + + +} diff --git a/src/Main.java b/src/Main.java index 41faa67..5c832c3 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,31 +1,321 @@ +import java.awt.Font; import java.awt.event.KeyEvent; +import java.util.List; import java.util.Random; import edu.princeton.cs.introcs.StdDraw; public class Main { static Random random = new Random(System.currentTimeMillis()); - - + //TODO Score + //TODO multi + //TODO centrage + //TODO uml + //TODO livrable + //TODO animation ? //Display - public final static int WIN_WIDTH= 640; - public final static int WIN_HEIGHT= 640; - public final static int FPS= 1000; - static double STEP= 1.5; - static Perso player; + static int WIN_WIDTH= 640; + static int WIN_HEIGHT= 640; + static int FPS= 1000; + static double STEP= 2; + static Joueur player; static Ghost red; static Ghost blue; static Ghost pink; static Ghost orange; static Map map; - static int tailleMur = 20; static int taille = 10; + static Button btnjouer; + static Button btncredit; + static Button btnscore; + static Button btnChnom; + static Button btnsaveScore; + static Button btnMenujouer; + static Button btnretour; + static Button btnrename; + static EditText editnom; + static ScoreSheet fscore; + static int duree; + //chemin du dossieur feuille de score + static String cheminFeuilleDeScore = "GameData/Score/scoreSheet.csv"; + //chemin du dossier image + static String dossierImage = "GameData/Images/"; + + //font pour les titres et titre principal + static Font fontTitrePrincipal = new Font("Georgia", Font.BOLD, 60); + static Font fontTitre = new Font("Georgia", Font.BOLD, 30); - //TODO Init fonction + + public static void menuPrincipal(){ + boolean menu = true; + StdDraw.clear(StdDraw.BLACK); + StdDraw.setPenColor(StdDraw.YELLOW); + //titre + StdDraw.setFont(fontTitrePrincipal); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/(1.3), "Pacman"); + StdDraw.setFont(); + + //Bouton play + + btnjouer.dessiner(); + //bouton scores + btnscore.dessiner(); + + //bouton changer de nom + btnChnom.dessiner(); + + //bouton crédit + btncredit.dessiner(); + + //bouton 2 joueurs ? + while(menu){ + if(btnjouer.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + + } + } + + if(btnscore.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + menuScore(); + } + } + + if(btnChnom.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + menuNom(); + } + } + + if(btncredit.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + credit(); + } + } + + + + } + + } + public static void AfficherScore(List scorelist){ + //Position de départ d'affichage + int y = WIN_WIDTH-200; + int x = WIN_HEIGHT/2; + int k = 0; + + StdDraw.setPenColor(StdDraw.WHITE); + if (scorelist!=null){ + //parcoure la liste des scores ligne par ligne + for (String txt: scorelist){ + StdDraw.text(x, y+k, txt); + //crée l'écart entre les lignes + k = k+30; + + } + } + //Ajout d'un cadre + StdDraw.setPenColor(StdDraw.BLUE); + StdDraw.rectangle(WIN_WIDTH/2, WIN_HEIGHT/2-40, 100, 200); + StdDraw.show(); + + } + + + public static void menuScore(){ + boolean menu = true; + + StdDraw.clear(StdDraw.BLACK); + StdDraw.setPenColor(StdDraw.YELLOW); + //change la police + StdDraw.setFont(fontTitre); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-70, "TOP 10 scores:"); + StdDraw.setFont(); + + AfficherScore(fscore.scoresligne); + btnretour.dessiner(); + btnMenujouer.dessiner(); + + while(menu){ + if(btnMenujouer.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + + } + } + if(btnretour.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + menuPrincipal(); + } + } + + } + + } + public static void menuNom(){ + boolean menu = true; + + StdDraw.clear(StdDraw.BLACK); + StdDraw.setPenColor(StdDraw.BLUE); + //change la police + StdDraw.setFont(fontTitre); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-70, "Nouveau Nom:"); + StdDraw.setFont(); + StdDraw.setPenColor(StdDraw.YELLOW); + editnom.dessiner(); + + btnrename.dessiner(); + btnretour.dessiner(); + while(menu){ + if(btnretour.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + menuPrincipal(); + } + }else + if(btnrename.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + menuPrincipal(); + //change le nom du perso + player.name = editnom.text; + } + } + //lance le mode edition + editnom.writing(); + + + } + + } + public static void credit(){ + boolean menu = true; + StdDraw.clear(StdDraw.BLACK); + StdDraw.setPenColor(StdDraw.BLUE); + StdDraw.setFont(fontTitre); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-70, "Crédits"); + StdDraw.setFont(); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/1.2, "Images: https://pixabay.com/"); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/2.2, "KRITTER Nicolas G6C ISEP"); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/3.2, "VANACKER Eliott G7B ISEP"); + btnretour.dessiner(); + while(menu){ + if(btnretour.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + menuPrincipal(); + } + } + + } + } + public static void fin(){ + StdDraw.clear(StdDraw.BLACK); + //calcul le temps qu'à durée la partie + duree = (int) ((System.currentTimeMillis()/1000) - duree); + StdDraw.setPenColor(StdDraw.YELLOW); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-20, player.name); + StdDraw.setPenColor(StdDraw.WHITE); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-50, "Temps:"+duree+"s => Bonus:"+(int)(300/duree)); + + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-100,player.vie+" vie(s) restantes"); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-140,player.score+" "+(int)(300/duree)+" x "+player.vie); + player.score = player.score+(int)(300/duree)*player.vie; + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT-175,"Score Final: "+player.score); + btnsaveScore.dessiner(); + btnMenujouer.dessiner(); + btnretour.dessiner(); + AfficherScore(fscore.scoresligne); + boolean menu = true; + while(menu){ + + if(btnMenujouer.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + + + init(); + } + } + + if(btnsaveScore.hoover()){ + if(StdDraw.mousePressed()){ + menu = false; + fscore.writeScore(player.name,player.score); + StdDraw.text(WIN_WIDTH-50, 0, "Fait"); + StdDraw.show(); + StdDraw.pause(1000); + menuScore(); + } + } + if(btnretour.hoover()){ + if(StdDraw.mousePressed()){ + player.vie = 3; + player.score = 0; + menu = false; + menuPrincipal(); + } + } + + } + + + } + public static void panelJeux(){ + + StdDraw.clear(StdDraw.BLACK); + //Afficher tous les murs + afficherMur(map.listemur); + //Afficher tous les coockies + afficherCookie(map.listcookie); + //affiche les éléments du tableau de bord + StdDraw.setPenColor(StdDraw.YELLOW); + StdDraw.text(220, -16,player.name+":"); + StdDraw.setPenColor(StdDraw.WHITE); + StdDraw.text(320, -4,"Vie(s): "); + StdDraw.text(320 , -30,"Score: "+player.score); + StdDraw.text(500, -30,"High Score: "+ fscore.getHighScore()); + + for(int k = 0; k2){ + duree = (int)(System.currentTimeMillis()/1000); + } + } public static void init(){ + // reinit a la mort + if (player.vie<1){ + player.vie = 3; + player.score = 0; + map = new Map(); + } + //Affiche le nombre de vie restante + StdDraw.clear(StdDraw.BLACK); + StdDraw.setPenColor(StdDraw.WHITE); + StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/2,player.name+": "+player.vie+" vie(s)"); + + //affiche et pause pendant 3 secondes + StdDraw.show(3000); + //nécessaire pour ne pas passer à l'affichage suivant + //avant la fin de la pause + StdDraw.pause(0); + + //réaffiche le panel pour le jeu + panelJeux(); + + //On replace les personnages à leur endroit de spawn player.x = map.xStart; player.y = map.yStart; + player.buffer = -1; + player.dir = -1; red.x = map.xRSpawn; red.y = map.yRSpawn; blue.x = map.xBSpawn; @@ -34,113 +324,232 @@ public static void init(){ pink.y = map.yPSpawn; orange.x = map.xOSpawn; orange.y = map.yOSpawn; - map.generateCoockie(map); + + + + } - // Nommer le perso - public void creerPerso(Perso perso,String nom){ - perso.name = nom; + + public static void refreshScore(){ + StdDraw.setPenColor(StdDraw.BLACK); + + StdDraw.filledRectangle(320 , -30, 50, 15); + StdDraw.setPenColor(StdDraw.WHITE); + StdDraw.text(320 , -30,"Score: "+player.score); + + + } - //TODO Display Score - // Afficher les perso - public static void afficherPerso(Perso perso){ - switch (perso.name){ - case "blue": - StdDraw.setPenColor(StdDraw.BLUE); - break; - case "red": - StdDraw.setPenColor(StdDraw.RED); - break; - case "pink": - StdDraw.setPenColor(StdDraw.PINK); - break; - case "orange": - StdDraw.setPenColor(StdDraw.ORANGE); - break; - default: - StdDraw.setPenColor(StdDraw.YELLOW); - break; - } - StdDraw.filledCircle(perso.x,perso.y,taille); + + // afficher murs + public static void afficherMur(Mur listemur){ + + Mur mur = listemur; + StdDraw.setPenColor(StdDraw.BLUE); + StdDraw.rectangle(WIN_WIDTH/2, WIN_HEIGHT/2, WIN_WIDTH/2-15, WIN_HEIGHT/2-15); + //parcour la liste chaînée des murs et on les dessines + while(mur!=null){ + + //StdDraw.setPenColor(StdDraw.BLUE); + StdDraw.picture(mur.x, mur.y, Main.dossierImage+"mur.jpg",2*mur.large,2*mur.longe); + mur = mur.suivant; + } + + + } - //TODO afficher murs - public static void afficherMur(Mur mur){ - StdDraw.setPenColor(StdDraw.GRAY); - StdDraw.filledRectangle(mur.x,mur.y,mur.large,mur.longe); + public static void afficherCookie(Cookie listecookie){ + Cookie cookie = listecookie; + //parcour la liste chaînée des cookies et dessine les cookies + while(cookie!=null){ + + StdDraw.setPenColor(StdDraw.WHITE); + StdDraw.picture(cookie.x, cookie.y, Main.dossierImage+"cookie.png",cookie.taille,cookie.taille); + + + + cookie = cookie.suivant; + } + + } - - //TODO afficher coockies - public static void main(String[] args) { - //controle + + + + + public static void main(String[] args) { + boolean play = true; int pause = 0; + fscore = new ScoreSheet(cheminFeuilleDeScore); + fscore.init(); + fscore.readFile(); + + //Jeux - Map map = new Map(); - player = new Perso(map.xStart,map.yStart,-1,"Joueur"); - red = new Ghost(map.xRSpawn, map.yRSpawn, random.nextInt(3),"red"); - blue = new Ghost(map.xBSpawn, map.yBSpawn, random.nextInt(3),"blue"); - pink = new Ghost(map.xPSpawn, map.yPSpawn, random.nextInt(4),"pink"); - orange = new Ghost(map.xOSpawn, map.yOSpawn, random.nextInt(3),"orange"); - Ghost[] listGhost= {red,blue,pink,orange}; + map = new Map(); + player = new Joueur(map.xStart,map.yStart,-1,"Joueur","pac"); + red = new Ghost(map.xRSpawn, map.yRSpawn, 1,"red","red"); + blue = new Ghost(map.xBSpawn, map.yBSpawn, 3,"blue","blue"); + pink = new Ghost(map.xPSpawn, map.yPSpawn, 0,"pink","pink"); + orange = new Ghost(map.xOSpawn, map.yOSpawn, 2,"orange","orange"); + //Boutons + btnjouer = new Button(WIN_WIDTH/2,WIN_HEIGHT/2,80,20,"Play"); + btnscore = new Button(btnjouer.x,btnjouer.y-2*btnjouer.height-30,80,20,"Score"); + btnChnom = new Button(btnscore.x,btnscore.y-2*btnscore.height-30,80,20,"Changer de nom"); + btncredit = new Button(btnChnom.x,btnChnom.y-2*btnChnom.height-30,80,20,"Credits"); + btnsaveScore = new Button(WIN_WIDTH/2,60,80,20,"Sauver le Score"); + btnMenujouer = new Button(WIN_WIDTH/2,20,80,20,"Jouer"); + btnretour = new Button(90,-20,80,20,"retour"); + btnrename = new Button(WIN_WIDTH/2,20,80,20,"Accepter"); + editnom = new EditText(WIN_WIDTH/2,WIN_HEIGHT/2); + + Ghost[] listGhost= {red,blue,pink,orange}; + - //TODO Ecran Menu - //TODO Bouton Play - //TODO Text field nom - //TODO generer labyrinthe - //TODO generer coockie; - + //Génère la grille de la fenètre StdDraw.setXscale(0, WIN_WIDTH); - StdDraw.setYscale(0, WIN_HEIGHT); + StdDraw.setYscale(-50, WIN_HEIGHT); + + menuPrincipal(); + + /* + * + * + * + * + */ + StdDraw.clear(StdDraw.BLACK); + + + + //affiche le panel du jeux + panelJeux(); + + + + while(true){ + //bouton retour au menu + if(btnretour.hoover()){ + if(StdDraw.mousePressed()){ + menuPrincipal(); + } + } - + //gestion du jeux if(play){ - pause = 0; - StdDraw.clear(StdDraw.BLACK); - StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/8,"(x,y): "+map.coord[(int)(player.x)][(int)(player.y)]); - + //affiche les cookies + afficherCookie(map.listcookie); + //éviter la répétition lorsque que la touche P est pressée + if (pause>1){ + pause -= 1; + }else{ + pause=0; + } + + //TODO bonus + // Changement de direction avec les flèches if (StdDraw.isKeyPressed(KeyEvent.VK_DOWN)) { - player.dir = 0; + //la prochaine direction sera vers le haut + player.buffer = 0; + } if (StdDraw.isKeyPressed(KeyEvent.VK_UP)) { - player.dir = 1; + + player.buffer = 1; } if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT)) { - player.dir = 2; + + player.buffer = 2; } if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT)) { - player.dir = 3; + + player.buffer = 3; } - for (Mur mur: map.listmur){ - afficherMur(mur); + + + if(!player.checkhitwall(map, player.buffer)){ + + //si la prochaine direction est libre, on la prend + + + player.dir = player.buffer; + + + + } + + player.move(); + + + player.checkhitcookie(map); + if(player.checkhitwall(map)){ + player.hitwall(); + } + player.afficher(); + for (Ghost ghost: listGhost){ - //TODO check collision wall + //Le fantome choisis sa prochaine direction si il suit l'ancienne 1 fois sur 200 + if(random.nextInt(100)<15 && ghost.buffer == ghost.dir){ + ghost.buffer = random.nextInt(4); + while(Math.abs( ghost.buffer - ghost.dir)==1){ + ghost.buffer = random.nextInt(4); + } + } + if(!ghost.checkhitwall(map, ghost.buffer)){ + //si sa prochaine direction est libre, le fantome la prend + + ghost.dir = ghost.buffer; + + + } + ghost.move(); + + if(ghost.checkhitwall(map)){ + //si il touche un mur, on le fait rebondir + ghost.bounchehitwall(map); + } map.checkhitghost(player, ghost); - map.checkhitwall(ghost,map); - Perso.move(ghost); - afficherPerso(ghost); + + + + ghost.afficher();; } - map.checkhitwall(player,map); - Perso.move(player); - afficherPerso(player); + + + //Affichage du jeux - + + + StdDraw.show(10000/FPS); + } else{ - StdDraw.pause(1); - pause = 0; + + //Pour éviter la répétition de l'inversion play pause on met un timer avant + //de pouvoir réactiver la touche P + if (pause>1 ){ + pause -= 1; + }else{ + pause=0; + } + + StdDraw.pause(50); + } if (StdDraw.isKeyPressed(KeyEvent.VK_P) && pause==0) { - pause = 1; + pause = 10; play = !play; } diff --git a/src/Map.java b/src/Map.java index 2136a26..f4d469d 100644 --- a/src/Map.java +++ b/src/Map.java @@ -1,6 +1,7 @@ - +//true mur false vide +// case: 1 cookie, 2 mur, 0 vide public class Map { - int nbfromage; + int xRSpawn; int yRSpawn; int xOSpawn; @@ -13,121 +14,181 @@ public class Map { int yStart; int largeur; int longeur; - Mur[] listmur; - int[] coordMurx; - int[] coordMury; - int [][] coord; + Mur listemur; + Cookie listcookie; + boolean [][] coord; + int lon; + int lar; + Cookie[][] coordcookie; + int nbcookie; public Map(){ - xRSpawn = Main.WIN_WIDTH/2; - yRSpawn = Main.WIN_HEIGHT/4; - xOSpawn = Main.WIN_WIDTH/2; - yOSpawn = Main.WIN_HEIGHT/3; - xBSpawn = Main.WIN_WIDTH/3; - yBSpawn = Main.WIN_HEIGHT/2; - xPSpawn = Main.WIN_WIDTH/4; - yPSpawn = Main.WIN_HEIGHT/3; - xStart = Main.WIN_WIDTH/4; - yStart = Main.WIN_HEIGHT/5; + + //échelle de l'écran d'affichage largeur = Main.WIN_WIDTH; longeur = Main.WIN_HEIGHT; - listmur= new Mur[7]; - coord = new int[largeur+1][longeur+1]; - for (int k = 0; k0){ + Main.init(); + }else{ + Main.fin(); + } } } - + + + + + + + + + + + + + + + + + + } diff --git a/src/Mur.java b/src/Mur.java index 6d348ae..8e56bab 100644 --- a/src/Mur.java +++ b/src/Mur.java @@ -4,14 +4,54 @@ public class Mur { int y; int large; int longe; + Mur suivant; public Mur(int x0,int y0,int la,int lo){ x= x0; y = y0; - large = la; - longe = lo; - - + large = la-1; + longe = lo-1; + suivant = null; } + public static Mur addMur(Mur liste ,Mur next) { + + Mur courant = liste; + if(courant==null){ + liste = next; + + + } + else if (courant.suivant==null){ + + courant.suivant = next; + } + else{ + addMur(courant.suivant,next); + } + + + return liste; + + + } -} + /*public static Mur addMur(Mur mur,Mur next) { + Mur courant = mur; + if (mur==null){ + mur = next; + return mur; + } + if (courant.suivant==null){ + courant.suivant = next; + } + else{ + addMur(courant.suivant,next); + } + + + return mur; + + + }*/ + +} \ No newline at end of file diff --git a/src/Perso.java b/src/Perso.java index f7b0ca4..783bcda 100644 --- a/src/Perso.java +++ b/src/Perso.java @@ -1,38 +1,188 @@ +import edu.princeton.cs.introcs.StdDraw; public class Perso { double x; double y; + //direction int dir; - int score; - int vie; + int buffer; String name; + String image; + double taille; + //sert a la direction de l'image + boolean imgdir; - public Perso (int x0, int y0,int dir0, String nom){ + + + public Perso (int x0, int y0,int dir0, String nom, String pic){ x = x0; y = y0; name = nom; dir = dir0; - vie = 3; + buffer = dir0; + taille = Main.taille; + image = pic; } - - public static void move (Perso perso){ - switch(perso.dir){ + // 0: bas 1: haut 2: gauche 3: droite + public void move (){ + //Efface la précédente occurence + + effacer(); + + switch(this.dir){ case 0: - perso.y = perso.y-Main.STEP; + + this.y = this.y-Main.STEP; + break; case 1: - perso.y = perso.y+Main.STEP; + + this.y = this.y+Main.STEP; + break; case 2: - perso.x = perso.x-Main.STEP; + //sert pour l'affichage de l'image, définit si la dernière direction est droite ou gauche(false) + imgdir = false; + this.x = this.x-Main.STEP; + break; case 3: - perso.x = perso.x+Main.STEP; + imgdir = true; + this.x = this.x+Main.STEP; break; - default: - break; + } + } -} + public boolean checkhitwall(Map map){ + //On passe la coordonnée du personnage en numéro de case du tableau de la carte + int[]coordcase = map.toCase(this.x, this.y); + int x = coordcase [0]; + int y = coordcase[1]; + int ntaille = (int)(Math.round((((this.taille)/Main.WIN_WIDTH)*map.lon))); + + + //On regarde si le perso touche un mur dans la direction dans laquelle il se déplace + switch(this.dir){ + case 0: + //On prend en compte la taille/épaisseur (+ - taille) + if (map.coord[x][y-ntaille] || map.coord[x-ntaille][y-ntaille] || map.coord[x+ntaille][y-ntaille]){ + //on fait rebondir le perso si il touche un mur + this.y = this.y+2*Main.STEP; + + return true; + } + break; + case 1: + if (map.coord[x][y+ntaille] || map.coord[x-ntaille][y+ntaille] || map.coord[x+ntaille][y+ntaille]){ + this.y = this.y-2*Main.STEP; + return true; + } + break; + case 2: + if (map.coord[x-ntaille][y] || map.coord[x-ntaille][y+ntaille] || map.coord[x-ntaille][y-ntaille]){ + this.x = this.x+2*Main.STEP; + return true; + } + break; + case 3: + if (map.coord[x+ntaille][y] || map.coord[x+ntaille][y+ntaille] || map.coord[x+ntaille][y-ntaille]){ + this.x = this.x-2*Main.STEP; + return true; + } + break; + } + return false; + //TODO expliquer méthode des cases + } + + + public boolean checkhitwall(Map map, int direc){ + + //on transcrit la position du perso en case sur le tableau de la carte + //Pour savoir dans quel case de la carte il se trouve + int[]coordcase = map.toCase(this.x, this.y); + int x = coordcase [0]; + int y = coordcase[1]; + + int ntaille = (int)(Math.round((((this.taille)/Main.WIN_WIDTH)*map.lon))); + //int step = (int)(Math.round(Main.STEP)); + int step = (int)(Math.round((((Main.STEP*this.taille)/Main.WIN_WIDTH)*map.lon))); + + // 0: bas 1: haut 2: gauche 3: droite + + //On vérifie si la prochaine direction (celle dans le buffer ) est libre + switch(direc){ + //y + case 0: + //On prend en compte la taille/épaisseur (+ - taille) du perso ainsi que ça prochaine position (+ - step) + if (map.coord[x-(ntaille)][y-(ntaille)-step] || map.coord[x+(ntaille)][y-(ntaille)-step] || map.coord[x][y-(ntaille)-step]){ + // la prochaine direction n'est pas libre + + + return true; + + } + break; + case 1: + if (map.coord[x-(ntaille)][y+(ntaille)+step] || map.coord[x+(ntaille)][y+(ntaille)+step] || map.coord[x][y+(ntaille)+step]){ + + return true; + + } + break; + //x + case 2: + if (map.coord[x-(ntaille)-step][y+(ntaille)] || map.coord[x-(ntaille)-step][y-(ntaille)] || map.coord[x-(ntaille)-step][y]){ + + return true; + + } + break; + case 3: + if (map.coord[x+(ntaille)+step][y+(ntaille)] || map.coord[x+(ntaille)+step][y-(ntaille)] || map.coord[x+(ntaille)+step][y]){ + + return true; + + } + break; + } + return false; + } + + //afficher le personnage par une image + public void afficher(){ + if (this.imgdir){ + //dessine l'image du fantome avec la bonne direction + StdDraw.picture(this.x, this.y, Main.dossierImage+this.image+"d"+".png",this.taille*2.5,this.taille*2.5); + }else{ + StdDraw.picture(this.x, this.y, Main.dossierImage+this.image+"l"+".png",this.taille*2.5,this.taille*2.5); + } + + } + //repeint la position du perso + public void effacer(){ + StdDraw.setPenColor(StdDraw.BLACK); + //+1 due au décalage + switch(dir){ + case 0: + StdDraw.filledSquare(x,y+1,1.1*taille+2); + break; + case 1: + StdDraw.filledSquare(x,y-1,1.1*taille+2); + break; + case 2: + StdDraw.filledSquare(x+1,y,1.1*taille+2); + + break; + case 3: + StdDraw.filledSquare(x-1,y,1.1*taille+2); + + break; + + } + } + +} \ No newline at end of file diff --git a/src/ScoreSheet.java b/src/ScoreSheet.java new file mode 100644 index 0000000..8429a1c --- /dev/null +++ b/src/ScoreSheet.java @@ -0,0 +1,146 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + +public class ScoreSheet { + + //créé l'objet document + File scoreSheet; +//liste qui recevera tous les scores + List scoresligne; + //séparateurs pour différencer les lignes et les colonnes + static String separateur = " : "; + static String newline="\n"; + + StringBuilder str = new StringBuilder(); + + public ScoreSheet(String nomfichier){ + scoreSheet = new File(nomfichier); + } + + public void init(){ + //détecte si le fichier est déjà créé + if (!scoreSheet.isFile()){ + //Crée le fichier vide le cas échéant + try {//essaye de le faire si possible + //créé le dossier + scoreSheet.getParentFile().mkdirs(); + //créé le fichier + scoreSheet.createNewFile(); + + } catch (IOException e) { + + e.printStackTrace(); + } + + + } + } + //Lire le fichier csv et en sortir le tableau + public void readFile() { + //liste tempon + List temp = new ArrayList(); + + //Essaye d'ouvrir le fichier des scores et de le lire + try { + //Ouvre le fichier pour le lire + FileReader reader = new FileReader(scoreSheet); + //Permet de lire un fichier ligne par ligne avec readLine + BufferedReader buffreader = new BufferedReader(reader); + + //On lit le fichier ligne par ligne pour séparer les colonnes + for (String line = buffreader.readLine(); line != null; line = buffreader.readLine()) { + //on rajoute chaque ligne dans le tableau + temp.add(line); + } + //close les readers + reader.close(); + buffreader.close(); + + //on garede le score en mode ligne par ligne + scoresligne = temp; + + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + + e.printStackTrace(); + } + + + } + + public void writeScore(String nom, int score){ + int k = 0; + // réinitialise les stringbuilder + str.setLength(0); + //construit la nouvelle lignes (utilisation de string builder améliore les performances + str.append(nom); + str.append(separateur); + str.append(score); + + //String res = nom+" : "+score; + //Si il y a déjà des scores + if(scoresligne!=null && scoresligne.size()>0){ + //tant que le score est inféreur aux scores enrgistrés, on avance + while(Integer.parseInt(scoresligne.get(k).split(separateur)[1]) >score && k On récupère {nom,score} après le split, et on transforme en Int + k = k+1; + } + //On garde que les 10 meilleurs scores + if(k<10){ + //Ajout du score dans la liste à la bonne place + scoresligne.add(k, str.toString()); + + if(scoresligne.size()>10){ + scoresligne.set(11,null); + } + } + }else{//si la liste des score est vide, on l'initialise + scoresligne = new ArrayList(); + scoresligne.add(k, str.toString()); + } + try { + //créé l'objet qui va écrire dans le fichier + FileWriter fileWriter = new FileWriter(scoreSheet); + for (String line:scoresligne){ + //Ajoute chaque lignes du fichier et le délimiteur pour une nouvelle ligne + fileWriter.append(line); + fileWriter.append(newline); + } + //lance l'écriture et ferme le writer + fileWriter.flush(); + fileWriter.close(); + + } catch (IOException e) { + + e.printStackTrace(); + } + + + } + + + + //retournle score en haut du tableau + public String getHighScore(){ + + if (scoresligne!=null && scoresligne.size()>0){ + return scoresligne.get(0) ; + }else{ + return " "; + } + + } + + + +}