mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 15:34:07 +00:00
changement de beaucoup de class et ajout de Mouvement
This commit is contained in:
2
Makefile
2
Makefile
@@ -14,7 +14,7 @@ LIB_DIR = lib
|
|||||||
JAR = $(LIB_DIR)/*
|
JAR = $(LIB_DIR)/*
|
||||||
|
|
||||||
# main
|
# main
|
||||||
all: $(MAIN_FILE) run
|
all: $(MAIN_FILE) run clean
|
||||||
|
|
||||||
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class
|
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class
|
||||||
|
|
||||||
|
|||||||
@@ -1,69 +1,80 @@
|
|||||||
package Environnements;
|
package Environnements;
|
||||||
|
|
||||||
|
import java.lang.Object;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class Map{
|
import Item.Items;
|
||||||
int longueur;
|
|
||||||
int largeur;
|
|
||||||
int nbMurs;
|
|
||||||
int frequencesFruits;
|
|
||||||
Object [][] grille;
|
|
||||||
|
|
||||||
public Map(int longueur,int largeur, int nbMurs,int frequencesFruits){
|
public class Map {
|
||||||
this.largeur=largeur;
|
private Object[][] grid;
|
||||||
this.longueur=longueur;
|
private Random random;
|
||||||
this.nbMurs=nbMurs;
|
|
||||||
this.frequencesFruits=frequencesFruits;
|
public int longueur;
|
||||||
|
public int largeur;
|
||||||
|
|
||||||
|
public Map(long longueur, long largeur) {
|
||||||
|
this.longueur = (int)longueur;
|
||||||
|
this.largeur = (int)largeur;
|
||||||
|
|
||||||
grille=new Object[longueur][largeur];
|
this.random = new Random();
|
||||||
|
|
||||||
for (int i=0;i<nbMurs;i++){
|
this.grid = new Object[this.largeur][this.longueur];
|
||||||
this.placeMur();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j=0;j<this.largeur*this.longueur;j+=this.frequencesFruits){
|
|
||||||
this.placeFruit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLongueur(){ //Accéder à la variable Longueur
|
public String getCoordinate(int x, int y) {
|
||||||
System.out.println("Longueur : "+ this.longueur);
|
if (x >= 0 && x < grid[0].length && y >= 0 && y < grid.length) {
|
||||||
return this.longueur;
|
Object coordinate = grid[y][x];
|
||||||
}
|
if (coordinate instanceof Items) {
|
||||||
|
return ((Items) coordinate).getName();
|
||||||
public int getLargeur(){ //Accéder à la variable Largeur
|
}
|
||||||
System.out.println("Largeur : "+ this.largeur);
|
}
|
||||||
return this.largeur;
|
return "Empty";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printGrille(){ //Affichage simple de la grille de jeu
|
public String getStringGrid() {
|
||||||
for (int i=0;i<longueur;i++){
|
StringBuffer stringGrid = new StringBuffer();
|
||||||
for (int j=0;j<largeur;j++){
|
|
||||||
System.out.print(this.grille[i][j]);
|
for(Object[] i : this.grid) {
|
||||||
|
for(Object value : i) {
|
||||||
|
stringGrid.append(value);
|
||||||
|
}
|
||||||
|
stringGrid.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringGrid.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItems(Items[] items, int number) {
|
||||||
|
for(int i = 0; i<(items.length-1); i++) {
|
||||||
|
int value = this.random.nextInt(number);
|
||||||
|
number -= value;
|
||||||
|
randomize(items[i], value);
|
||||||
|
}
|
||||||
|
randomize(items[items.length-1], number);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[][] getGrid() {
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ajoutBordure() {
|
||||||
|
for(int i = 0; i < this.grid.length; i++) {
|
||||||
|
for(int k = 0; k < this.grid[0].length; k++) {
|
||||||
|
if (i == 0 || i == this.grid.length - 1 || k == 0 || k == this.grid[0].length - 1) {
|
||||||
|
this.grid[i][k] = Items.MUR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
System.out.println();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Réfléchir pour mettre plusieurs murs à des endroits différents
|
private void randomize(Items item, int number) {
|
||||||
public void placeMur(){ //Positionner un mur défini aléatoirement sur la Map
|
for(int i = 0; i<number; i++) {
|
||||||
Random r=new Random();
|
int x = this.random.nextInt(1, this.grid[0].length);
|
||||||
int x=r.nextInt(this.longueur-1); // Le "-1" permet d'éviter les murs faisant déjà partie de
|
int y = this.random.nextInt(1, this.grid.length);
|
||||||
int y=r.nextInt(this.largeur-1); // la bordure de la Map
|
|
||||||
int L=r.nextInt(this.longueur-x);
|
|
||||||
int l=r.nextInt(this.largeur-y);
|
|
||||||
|
|
||||||
Murs mur =new Murs(x, y,L, l);
|
if(!(getGrid()[y][x] instanceof Items)) this.grid[y][x] = item;
|
||||||
mur.insereMur(this);
|
else i--;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void placeFruit(){
|
|
||||||
Random r=new Random();
|
|
||||||
Fruits fruit=new Fruits();
|
|
||||||
|
|
||||||
int x=r.nextInt(this.longueur);
|
|
||||||
int y=r.nextInt(this.largeur);
|
|
||||||
|
|
||||||
this.grille[x][y]=fruit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class Murs{
|
|||||||
if (b){
|
if (b){
|
||||||
for (int i=this.debut_horizontal;i<this.debut_horizontal+this.longueur;i++){
|
for (int i=this.debut_horizontal;i<this.debut_horizontal+this.longueur;i++){
|
||||||
for (int j=this.debut_vertical;j<this.debut_vertical+this.largeur;j++){
|
for (int j=this.debut_vertical;j<this.debut_vertical+this.largeur;j++){
|
||||||
m.grille[i][j]=Items.MUR;
|
m.getGrid()[i][j]=Items.MUR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
import java.util.Scanner;
|
import java.lang.Object;
|
||||||
|
|
||||||
|
import Environnements.Map;
|
||||||
import Item.Items;
|
import Item.Items;
|
||||||
import personnages.*;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
// Items[] items = {Items.BANANE, Items.FRAISE, Items.ORANGE};
|
||||||
|
|
||||||
|
Map map = new Map(10, 10);
|
||||||
|
|
||||||
|
map.ajoutBordure();
|
||||||
|
map.addItems(new Items[]{Items.BANANE, Items.MUR, Items.FRAISE}, 3);
|
||||||
|
|
||||||
|
System.out.println(map.getCoordinate(1, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/personnages/Mouvement.java
Normal file
22
src/personnages/Mouvement.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package personnages;
|
||||||
|
|
||||||
|
enum Mouvement {
|
||||||
|
DROITE(1, 0),
|
||||||
|
HAUT(0, -1),
|
||||||
|
BAS(0, 1),
|
||||||
|
GAUCHE(-1, 0);
|
||||||
|
|
||||||
|
private final int deltaX;
|
||||||
|
private final int deltaY;
|
||||||
|
|
||||||
|
Mouvement(int deltaX, int deltaY) {
|
||||||
|
this.deltaX = deltaX;
|
||||||
|
this.deltaY = deltaY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void editCoordinate(int[] coordinate) {
|
||||||
|
coordinate[0] += this.deltaX;
|
||||||
|
coordinate[1] += this.deltaY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -9,15 +9,14 @@ public class Personnage {
|
|||||||
private int size;
|
private int size;
|
||||||
protected int[] coordinate;
|
protected int[] coordinate;
|
||||||
|
|
||||||
public Items item;
|
protected ArrayList<int[]> CoordinateSnake;
|
||||||
|
|
||||||
private ArrayList<Effects> effectsList;
|
private ArrayList<Effects> effectsList;
|
||||||
|
|
||||||
protected Personnage(int size, int[] coordinate) {
|
protected Personnage(int size, int[] coordinate) {
|
||||||
this.coordinate = coordinate;
|
this.coordinate = coordinate;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] getCoordinate() {
|
public int[] getCoordinate() {
|
||||||
return coordinate;
|
return coordinate;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,26 +5,13 @@ public class Player extends Personnage {
|
|||||||
super(size, coordinate);
|
super(size, coordinate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveCoordinate(int keys) {
|
public void changeCoordinate(int keys) {
|
||||||
switch (keys) {
|
switch (keys) {
|
||||||
case 77: // w
|
case 77: Mouvement.HAUT.editCoordinate(coordinate); // w
|
||||||
this.coordinate[1]++;
|
case 73: Mouvement.BAS.editCoordinate(coordinate); // s
|
||||||
break;
|
case 61: Mouvement.GAUCHE.editCoordinate(coordinate); // a
|
||||||
|
case 64: Mouvement.DROITE.editCoordinate(coordinate); // d
|
||||||
case 73: // s
|
default: break;
|
||||||
this.coordinate[1]--;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 61: // a
|
|
||||||
this.coordinate[0]--;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 64: // d
|
|
||||||
this.coordinate[0]++;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: // autre
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user