From 32bad236eddefaaa976d959bc3d30a9b09025718 Mon Sep 17 00:00:00 2001 From: Paul-1108 Date: Wed, 11 Dec 2024 14:13:33 +0100 Subject: [PATCH] V2 des Gameloop und Logic sollte gehen 11.12.24 14:13 --- src/main/java/GameLogic.java | 57 +++++++++++++++++++++++++------- src/main/java/GameLoop.java | 63 +++++++++++++----------------------- src/main/java/Player.java | 10 ++++-- 3 files changed, 75 insertions(+), 55 deletions(-) diff --git a/src/main/java/GameLogic.java b/src/main/java/GameLogic.java index 1ff7af9..a35eb5f 100644 --- a/src/main/java/GameLogic.java +++ b/src/main/java/GameLogic.java @@ -1,21 +1,54 @@ +import processing.core.PApplet; + import java.awt.*; +import java.util.LinkedList; +import java.util.Queue; -public class GameLogic { +public class GameLogic extends PApplet { - private final int width; - private final int height; - - private int[][] pitch; - - public GameLogic(int width, int height){ - this.width = width; - this.height = height; - pitch = new int[width][height]; + public static void main(String[] args) { + PApplet.main("GameLogic"); } - public boolean checkPlayer(Point point, int id){ - return false; +/* public boolean checkForPlayerCollision(Point point){ + int c = get(point.x, point.y); + if (c==0){ + return true; //Kollision erkannt + } else { + return false; + } } + public void drawCheckTrail(Queue 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 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; + } + } } diff --git a/src/main/java/GameLoop.java b/src/main/java/GameLoop.java index 07d30cb..4e7ad0c 100644 --- a/src/main/java/GameLoop.java +++ b/src/main/java/GameLoop.java @@ -1,5 +1,4 @@ import processing.core.PApplet; - import java.awt.Point; import java.util.ArrayList; import java.util.List; @@ -17,27 +16,34 @@ public class GameLoop { Player player2; 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(); graphics = new GraphicsProgram(); String[] args = {"GraphicsProgram"}; PApplet.runSketch(args, graphics); - gl = new GameLogic(1080,720); + gl = new GameLogic(); run(); } private void run(){ while(true){ communicate(); + List positions = track(); - if(checkP1(positions.get(0))){ - break; - } - if(checkP2(positions.get(1))){ - break; - } + 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(); } } @@ -46,41 +52,16 @@ public class GameLoop { //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 track(){ - org.opencv.core.Point p1 = tracker.getP1(); - org.opencv.core.Point p2 = tracker.getP2(); + Point p1 = tracker.getP1(); + 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); - - 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 output = new ArrayList<>(); - output.set(0, p1Java); - output.set(1, p2Java); + output.set(0, p1); + output.set(1, p2); return output; } diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 9b822d7..609cf0e 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -20,14 +20,20 @@ public class Player { 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 public void setKoords(int x, int y){ addToTrail(x,y); this.x = x; this.y = y; - - } private void addToTrail(int x, int y) {