V2 des Gameloop und Logic

sollte gehen
11.12.24 14:13
This commit is contained in:
Paul-1108 2024-12-11 14:13:33 +01:00
parent 5f211026e4
commit 32bad236ed
3 changed files with 75 additions and 55 deletions

View File

@ -1,21 +1,54 @@
import processing.core.PApplet;
import java.awt.*; import java.awt.*;
import java.util.LinkedList;
import java.util.Queue;
public class GameLogic { public class GameLogic extends PApplet {
private final int width; public static void main(String[] args) {
private final int height; PApplet.main("GameLogic");
private int[][] pitch;
public GameLogic(int width, int height){
this.width = width;
this.height = height;
pitch = new int[width][height];
} }
public boolean checkPlayer(Point point, int id){ /* public boolean checkForPlayerCollision(Point point){
int c = get(point.x, point.y);
if (c==0){
return true; //Kollision erkannt
} else {
return false; 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;
}
}
} }

View File

@ -1,5 +1,4 @@
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;
@ -17,27 +16,34 @@ public class GameLoop {
Player player2; Player player2;
public GameLoop(){ public GameLoop(){
player1 = new Player(0, "192.168.33.33", 9000); player1 = new Player(0, "ip", 9000);
player2 = new Player(1, "192.168.33.44", 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))){
break;
}
if(checkP2(positions.get(1))){
break;
}
addPositions(positions); 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;
}
if(gl.doCheck(player1.getTrail(), positions.get(1))){ //Check Position von P2 mit Spur von P1
System.out.println("Spieler 2 hat verloren");
break;
}
draw(); draw();
} }
} }
@ -46,41 +52,16 @@ public class GameLoop {
//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(); Point p1 = tracker.getP1();
org.opencv.core.Point p2 = tracker.getP2(); Point p2 = tracker.getP2();
java.awt.Point p1Java = new java.awt.Point((int) p1.x, (int) p1.y); player1.setKoords(p1.x, p1.y);
java.awt.Point p2Java = new java.awt.Point((int) p2.x, (int) p2.y); player2.setKoords(p2.x, p2.y);
player1.setKoords(p1Java.x, p1Java.y);
player2.setKoords(p2Java.x, p2Java.y);
List<Point> output = new ArrayList<>(); List<Point> output = new ArrayList<>();
output.set(0, p1Java); output.set(0, p1);
output.set(1, p2Java); output.set(1, p2);
return output; return output;
} }

View File

@ -20,14 +20,20 @@ public class Player {
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) {