Skip to content
Snippets Groups Projects
Commit 92fffc2d authored by bosskkev's avatar bosskkev
Browse files

Modification design et taille fenêtre. Rajout de l'augmentation de la vitesse...

Modification design et taille fenêtre. Rajout de l'augmentation de la vitesse en fonction des collisions.
parent f8b38808
No related branches found
No related tags found
No related merge requests found
Pipeline #23763 failed
......@@ -34,6 +34,7 @@ public class Kernel {
this.graphicEngine = new GraphicEngine(); // Will create a new grid and a new kernel
graphicEngine.init(gameName, width, height); // Will start the kernel and set the keys listeners
graphicEngine.drawRect();
engines.add(this.graphicEngine);//
this.physicEngine = new PhysicEngine();
......@@ -82,14 +83,25 @@ public class Kernel {
public void keyPressed(java.awt.event.KeyEvent evt) {
switch (evt.getKeyCode()) {
case java.awt.event.KeyEvent.VK_UP -> {
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, -1)); }
physicEngine.pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, -3)); }
case java.awt.event.KeyEvent.VK_DOWN -> {
pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 1));
pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 3));
}
case java.awt.event.KeyEvent.VK_Z -> {
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, -1)); }
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, -3)); }
case java.awt.event.KeyEvent.VK_S -> {
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 1));
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 3));
}
}
}
public void keyReleased(java.awt.event.KeyEvent evt) {
switch (evt.getKeyCode()) {
case java.awt.event.KeyEvent.VK_UP, java.awt.event.KeyEvent.VK_DOWN -> {
pongObjectEntityHashMap.get(gameObjects.get(0)).setSpeed(new Coordinates2D(0, 0));
}
case java.awt.event.KeyEvent.VK_Z, java.awt.event.KeyEvent.VK_S -> {
pongObjectEntityHashMap.get(gameObjects.get(1)).setSpeed(new Coordinates2D(0, 0));
}
}
}
......
......@@ -6,7 +6,7 @@ import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class Sound {
public static void PLaymusic(String location){
public static void playMusic(String location){
try {
File musicPath = new File(location);
if (musicPath.exists()) {
......
......@@ -2,6 +2,7 @@ package engine.graphic;
import engine.Engine;
import engine.Kernel;
import pong.PongApp;
import pong.PongObject;
import javax.swing.*;
......@@ -20,12 +21,18 @@ public class GraphicEngine extends JFrame implements Engine {
this.setMinimumSize(new Dimension(width, height));
// this.setContentPane(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.BLACK);
//this.getContentPane().getGraphics().setColor(Color.BLACK);
//this.getContentPane().getGraphics().fillRect(0, 0, width, height);
//this.setMenuBar(null);
this.setLocationRelativeTo(null);
this.pack();
this.setResizable(false);
this.setVisible(true);
this.setLayout(this.getLayout());
this.revalidate();
this.repaint();
}
public void draw(ImageComponent ic) throws IOException {
getContentPane().add(ic);
......@@ -45,5 +52,9 @@ public class GraphicEngine extends JFrame implements Engine {
revalidate();
}
public void drawRect(){
this.getContentPane().add(new RectangleComponent(100, 100, PongApp.rectWidth, PongApp.rectHeight));
}
}
package engine.graphic;
import engine.physic.Coordinates2D;
import pong.PongApp;
import javax.swing.*;
import java.awt.*;
......@@ -18,7 +19,7 @@ public class ImageComponent extends JComponent {
this.image = image;
this.position = position;
//this.setBorder(BorderFactory.createLineBorder(Color.black));
this.setSize(850, 500);
this.setSize(PongApp.width + 20, PongApp.height + 100);
this.setVisible(true);
this.setLayout(null);
//repaint();
......@@ -35,6 +36,7 @@ public class ImageComponent extends JComponent {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, position.getX(), position.getY(), this.width, this.height, this);
//setOpaque(false);
}
......
package engine.graphic;
import pong.PongApp;
import javax.swing.*;
public class RectangleComponent extends JComponent {
private int width;
private int height;
private int x;
private int y;
public RectangleComponent(int x, int y, int width, int height) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.setSize(PongApp.width, PongApp.height);
this.setVisible(true);
this.setLayout(null);
}
@Override
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
g.setColor(java.awt.Color.WHITE);
g.drawRect(this.x, this.y, this.width, this.height);
}
}
......@@ -60,12 +60,12 @@ public class Movable implements Entity {
@Override
public void updatePosition() {
Coordinates2D newPosition = new Coordinates2D(this.position.getX(), this.position.getY());
newPosition.add(this.speed);
newPosition.add(this.speed); // Calculating the new position
if (newPosition.getX() < 0 || newPosition.getX() >= PongApp.width - this.width) {
if (newPosition.getX() <= 100 || newPosition.getX() >= PongApp.rectWidth - this.width + 100) {
this.speed = new Coordinates2D(-speed.getX(), speed.getY());
}
if (newPosition.getY() < 0 || newPosition.getY() >= PongApp.height - this.height -20) { // -20 à cause de la barre verticale de la fenêtre
if (newPosition.getY() <= 100 || newPosition.getY() >= PongApp.rectHeight - this.height + 100) { // -20 à cause de la barre verticale de la fenêtre
this.speed = new Coordinates2D(speed.getX(), -speed.getY());
}
this.position.add(this.speed); // Modifying the position (x = x + vx ; y = y + vy)
......
......@@ -3,10 +3,10 @@ package engine.physic;
import engine.Engine;
import engine.Kernel;
import engine.Sound;
import pong.PongApp;
import pong.PongObject;
import java.util.HashMap;
import java.util.Random;
public class PhysicEngine implements Engine {
......@@ -38,10 +38,16 @@ public class PhysicEngine implements Engine {
if(checkCollision(pongObjectEntityHashMap.get(pongObject), pongObjectEntityHashMap.get(po2))){
i++;
String collisionPath = "src/main/resources/Sound/collision.wav";
Sound.PLaymusic(collisionPath);
Sound.playMusic(collisionPath);
System.out.println("Collision " + i);
pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition);
if(i % 2 == 0){
pongObjectEntityHashMap.get(pongObject).getSpeed().add(new Coordinates2D(1, 1));
}
pongObjectEntityHashMap.get(pongObject).getSpeed().mul(-1);
//TODO: implements pong object collision physics
pongObjectEntityHashMap.get(pongObject).updatePosition();
}
}
......
......@@ -11,24 +11,36 @@ import java.util.ArrayList;
public class PongApp {
public static ArrayList<PongObject> components = new ArrayList<PongObject>();
public static int width = 850;
public static int height = 500;
public static int width = 1000;
public static int height = 800;
public static int widthDiff = 100;
public static int heightDiff = 100;
public static int rectWidth = width - widthDiff*2;
public static int rectHeight = height - heightDiff*2;
public PongApp() throws IOException {
}
public static void main(String[] args) throws IOException {
int racketWidth = 30;
int racketHeight = 120;
int ballWidth = 32;
String son = "src/main/resources/Sound/son.wav";
Sound.PLaymusic(son);
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette1.png", new Coordinates2D(5, 100), 30, 120);
Sound.playMusic(son);
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, 100), racketWidth, racketHeight);
components.add(leftRacket);
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette2.png", new Coordinates2D(815, 100), 30, 120);
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, 100), racketWidth, racketHeight);
components.add(rightRacket);
// TODO: Add rackets
PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(100, 10), 64, 64);
//PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
PongBall pongBall = new PongBall("Ball", "src/main/resources/sprint2_demo/asteroid.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
components.add(pongBall);
Kernel kernel = new Kernel("Pong", width, height, components);
......
src/main/resources/pong/ball.png

1.91 KiB | W: | H:

src/main/resources/pong/ball.png

19.9 KiB | W: | H:

src/main/resources/pong/ball.png
src/main/resources/pong/ball.png
src/main/resources/pong/ball.png
src/main/resources/pong/ball.png
  • 2-up
  • Swipe
  • Onion skin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment