Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 157b699f rédigé par Pierrick Lermite's avatar Pierrick Lermite
Parcourir les fichiers

collision added

parent bb7f1010
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
package App.Display;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import App.Ground;
import App.Block.Block;
public class GroundDisplay {
private Graphics g;
private Ground ground;
public GroundDisplay(Ground gr, Graphics gg){
this.g = gg;
this.ground = gr;
}
public void draw(){
Graphics2D[][] l_g = new Graphics2D[ground.getnbX()][ground.getnbY()];
Block[][] grid = ground.getGrid();
int blocksize = 60;
for(int j = 0; j < ground.getnbY(); j++){
for (int i = 0; i < ground.getnbX(); i++){
l_g[i][j] = (Graphics2D)g;
if (grid[i][j].getLife() == -1){
l_g[i][j].setColor(Color.white);
l_g[i][j].fillRect(i * blocksize, j * blocksize, blocksize, blocksize);
}
}
}
}
}
package App.Display;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class PlayerDisplay {
private Graphics2D g2;
private int x;
private int y;
private Graphics g;
public PlayerDisplay(int xx, int yy, Graphics gg){
this.x = xx;
this.y = yy;
this.g = gg;
}
public void draw(){
g2 = (Graphics2D)g;
g2.setColor(Color.red);
g2.fillRect(x , y, 34, 34);
}
}
package App; package App;
import App.Block.*;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel; import javax.swing.JPanel;
import App.Display.GroundDisplay;
import App.Display.PlayerDisplay;
public class GamePanel extends JPanel implements Runnable{ public class GamePanel extends JPanel implements Runnable{
// SCREEN SETTINGS // SCREEN SETTINGS
...@@ -26,10 +27,8 @@ public class GamePanel extends JPanel implements Runnable{ ...@@ -26,10 +27,8 @@ public class GamePanel extends JPanel implements Runnable{
Thread gameThread; Thread gameThread;
// Set Player's default position // Set Player's default position
int playerX = 100; Player player1 = new Player(75, 75);
int playerY = 100; Ground game = new Ground(15, 13);
int playerSpeed = 2;
Ground game = new Ground(13, 15);
public GamePanel(){ public GamePanel(){
...@@ -83,49 +82,34 @@ public class GamePanel extends JPanel implements Runnable{ ...@@ -83,49 +82,34 @@ public class GamePanel extends JPanel implements Runnable{
} }
public void update(){ public void update(){
if(keyH.upPressed == true){
playerY -= playerSpeed;
if(keyH.upPressed == true && !game.collisionTestBLock(player1,0 ,-player1.getSpeed())){
player1.addY(-player1.getSpeed());
} }
if(keyH.downPressed == true){ if(keyH.downPressed == true && !game.collisionTestBLock(player1,0 ,player1.getSpeed())){
playerY += playerSpeed; player1.addY(player1.getSpeed());
} }
if(keyH.rightPressed == true){ if(keyH.rightPressed == true && !game.collisionTestBLock(player1,player1.getSpeed() ,0 )){
playerX += playerSpeed; player1.addX(player1.getSpeed());
} }
if(keyH.leftPressed == true){ if(keyH.leftPressed == true && !game.collisionTestBLock(player1,-player1.getSpeed() ,0 )){
playerX -= playerSpeed; player1.addX(-player1.getSpeed());
} }
} }
public void paintComponent(Graphics g){ public void paintComponent(Graphics g){
super.paintComponent(g); super.paintComponent(g);
//Joueur 1 //Joueur 1
Graphics2D g2 = (Graphics2D)g; PlayerDisplay p1 = new PlayerDisplay(player1.getX(), player1.getY(), g);
p1.draw();
g2.setColor(Color.red);
g2.fillRect(playerX, playerY, 35, 35);
// Terrain // Terrain
int row = game.getrow(); GroundDisplay groundD = new GroundDisplay(game, g);
int col = game.getcol(); groundD.draw();
Graphics2D[][] l_g = new Graphics2D[row][col];
Block[][] grid = game.getGrid();
int blocksize = 59;
for(int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
l_g[i][j] = (Graphics2D)g;
if (grid[i][j].getLife() == -1){
l_g[i][j].setColor(Color.white);
l_g[i][j].fillRect(j * blocksize, i * blocksize, blocksize, blocksize);
}
}
}
} }
} }
...@@ -2,21 +2,21 @@ package App; ...@@ -2,21 +2,21 @@ package App;
import App.Block.*; import App.Block.*;
public class Ground { public class Ground {
private Block[][] grid; private Block[][] grid;
private int row, col; private int nbx, nby;
public Ground(int r, int c){ public Ground(int x, int y){
this.row = r; this.nbx = x;
this.col = c; this.nby = y;
this.grid = new Block[row][col]; this.grid = new Block[x][y];
} }
public void newGround(){ public void newGround(){
for (int i = 0; i < row; i++){ for (int j = 0; j < nby; j++){
for(int j = 0; j < col; j++){ for(int i = 0; i < nbx; i++){
if( i == 0 || i == row - 1 || j == 0 || j == col - 1){ if( i == 0 || i == nbx - 1 || j == 0 || j == nby - 1){
//Frontière exterieur //Frontière exterieur
grid[i][j] = new ImbreakableBlock(); grid[i][j] = new ImbreakableBlock();
} }
...@@ -24,22 +24,61 @@ public class Ground { ...@@ -24,22 +24,61 @@ public class Ground {
if( i % 2 == 0 && j % 2 == 0){ if( i % 2 == 0 && j % 2 == 0){
//Terrain intérieur //Terrain intérieur
grid[i][j] = new ImbreakableBlock(); grid[i][j] = new ImbreakableBlock();
} }
else{ else{
//Vide //Vide
grid[i][j] = new VoidBlock(); grid[i][j] = new VoidBlock();
} }
} }
}
}
}
// A FINIR
public boolean collisionTestBLock(Player p, int dx, int dy){
int boxsizeplayer = 34;
int pX = p.getX();
int pY = p.getY();
int boxsizebloc = 60;
int blockX = 0;
int blockY = 0;
for (int j = 0; j < nby; j++){
for(int i = 0; i < nbx; i++){
if (grid[i][j].getLife() != 0){
blockX = i * boxsizebloc;
blockY = j * boxsizebloc;
if((pX + dx < blockX + boxsizebloc && pX + dx > blockX &&
pY + dy < blockY + boxsizebloc && pY + dy > blockY) || //POINT 1
(pX + boxsizeplayer + dx < blockX + boxsizebloc && pX + boxsizeplayer + dx > blockX &&
pY + dy < blockY + boxsizebloc && pY + dy > blockY )|| //POINT 2
(pX + boxsizeplayer + dx < blockX + boxsizebloc && pX + boxsizeplayer + dx > blockX &&
pY + boxsizeplayer + dy < blockY + boxsizebloc && pY + boxsizeplayer + dy > blockY )|| //POINT 3
(pX + dx < blockX + boxsizebloc && pX + dx > blockX &&
pY + boxsizeplayer + dy < blockY + boxsizebloc && pY + boxsizeplayer + dy > blockY) //POINT 4
){
return true;
}
}
} }
} }
return false;
} }
public int getrow(){
return row; public int getnbX(){
return nbx;
} }
public int getcol(){ public int getnbY(){
return col; return nby;
} }
public Block[][] getGrid(){ public Block[][] getGrid(){
......
package App;
public class Player {
private int x;
private int y;
private int speed;
public Player(int xx, int yy){
this.x = xx;
this.y = yy;
this.speed = 2;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getSpeed(){
return speed;
}
public void addX(int dx){
this.x += dx;
}
public void addY(int dy){
this.y += dy;
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter