Browse Source

V2 des Gameloop und Logic

sollte gehen
11.12.24 14:13
master
Paul-1108 1 week ago
parent
commit
32bad236ed
3 changed files with 71 additions and 51 deletions
  1. 44
    11
      src/main/java/GameLogic.java
  2. 19
    38
      src/main/java/GameLoop.java
  3. 8
    2
      src/main/java/Player.java

+ 44
- 11
src/main/java/GameLogic.java View File

import java.awt.*;
import processing.core.PApplet;


public class GameLogic {
import java.awt.*;
import java.util.LinkedList;
import java.util.Queue;


private final int width;
private final int height;
public class GameLogic extends PApplet {


private int[][] pitch;
public static void main(String[] args) {
PApplet.main("GameLogic");
}


public GameLogic(int width, int height){
this.width = width;
this.height = height;
pitch = new int[width][height];
/* public boolean checkForPlayerCollision(Point point){
int c = get(point.x, point.y);
if (c==0){
return true; //Kollision erkannt
} else {
return false;
}
} }


public boolean checkPlayer(Point point, int id){
return false;
public void drawCheckTrail(Queue<int[]> trail){
int particleSize = trail.size()+5;
background(255);

for (int[] koordinates : new LinkedList<>(trail)) {
int x = koordinates[0];
int y = koordinates[1];
fill(0,0,0);
rect(x, y, particleSize, particleSize);
}
} }
*/


public boolean doCheck(Queue<int[]> trail, Point point){
int particleSize = trail.size()+5;
background(255);

for (int[] koordinates : new LinkedList<>(trail)) {
int x = koordinates[0];
int y = koordinates[1];
fill(0,0,0);
rect(x, y, particleSize, particleSize);
}

int c = get(point.x, point.y);
if (c==0){
return true; //Kollision erkannt
} else {
return false;
}
}


} }

+ 19
- 38
src/main/java/GameLoop.java View File

import processing.core.PApplet; import processing.core.PApplet;

import java.awt.Point; import java.awt.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
Player player2; Player player2;


public GameLoop(){ public GameLoop(){
player1 = new Player(0, "192.168.33.33", 9000);
player2 = new Player(1, "192.168.33.44", 9000);
player1 = new Player(0, "ip", 9000);
player2 = new Player(1, "ip", 9000);
tracker = new Tracker(); tracker = new Tracker();
graphics = new GraphicsProgram(); graphics = new GraphicsProgram();
String[] args = {"GraphicsProgram"}; String[] args = {"GraphicsProgram"};
PApplet.runSketch(args, graphics); PApplet.runSketch(args, graphics);
gl = new GameLogic(1080,720);
gl = new GameLogic();
run(); run();
} }


private void run(){ private void run(){
while(true){ while(true){
communicate(); communicate();

List<Point> positions = track(); List<Point> positions = track();
if(checkP1(positions.get(0))){

addPositions(positions);

if(gl.doCheck(player2.getTrail(), positions.get(0))){ //Check Position von P1 mit Spur von P2
System.out.println("Spieler 1 hat verloren");
break; break;
} }
if(checkP2(positions.get(1))){

if(gl.doCheck(player1.getTrail(), positions.get(1))){ //Check Position von P2 mit Spur von P1
System.out.println("Spieler 2 hat verloren");
break; break;
} }
addPositions(positions);
draw(); draw();
} }
} }
//offen für Auto Kommunikation //offen für Auto Kommunikation
} }


private boolean checkP1(Point point){
if(gl.checkPlayer(point, 0)){
return true;
}
else {
return false;
}
}

private boolean checkP2(Point point){
if(gl.checkPlayer(point, 0)){
return true;
}
else {
return false;
}
}

private void addPositions(List positions){

}

private List<Point> track(){ private List<Point> track(){
org.opencv.core.Point p1 = tracker.getP1();
org.opencv.core.Point p2 = tracker.getP2();

java.awt.Point p1Java = new java.awt.Point((int) p1.x, (int) p1.y);
java.awt.Point p2Java = new java.awt.Point((int) p2.x, (int) p2.y);
Point p1 = tracker.getP1();
Point p2 = tracker.getP2();


player1.setKoords(p1Java.x, p1Java.y);
player2.setKoords(p2Java.x, p2Java.y);
player1.setKoords(p1.x, p1.y);
player2.setKoords(p2.x, p2.y);


List<Point> output = new ArrayList<>(); List<Point> output = new ArrayList<>();
output.set(0, p1Java);
output.set(1, p2Java);
output.set(0, p1);
output.set(1, p2);
return output; return output;
} }



+ 8
- 2
src/main/java/Player.java View File

arduinoCommunicator = new ArduinoCommunication(finalIpAddress, finalPortNr); arduinoCommunicator = new ArduinoCommunication(finalIpAddress, finalPortNr);
} }


public int getX(){
return x;
}

public int getY(){
return y;
}



//fügt 2 Koordinaten zum Trail hinzu nd verhindert, dass dieser zulang wird //fügt 2 Koordinaten zum Trail hinzu nd verhindert, dass dieser zulang wird
public void setKoords(int x, int y){ public void setKoords(int x, int y){
addToTrail(x,y); addToTrail(x,y);
this.x = x; this.x = x;
this.y = y; this.y = y;


} }


private void addToTrail(int x, int y) { private void addToTrail(int x, int y) {

Loading…
Cancel
Save