PongGame
parent
ff3375a644
commit
4d108c9120
|
@ -5,17 +5,19 @@ import java.security.SecureRandom;
|
|||
import java.util.Base64;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class Controller {
|
||||
private Datenbanken datenbanken;
|
||||
private Window view;
|
||||
private Modell erstelleKonto;
|
||||
private UserDao userDao;
|
||||
private UserWindow userWindwo;
|
||||
|
||||
public Controller() throws SQLException {
|
||||
this.datenbanken = new Datenbanken();
|
||||
public Controller(Connection connection) throws SQLException {
|
||||
this.view = new Window();
|
||||
this.userDao = new UserDaoImpl(connection); // DAO-Implementierung wird hier verwendet
|
||||
|
||||
this.view.setAction(new ActionListener() {
|
||||
@Override
|
||||
|
@ -31,33 +33,26 @@ public class Controller {
|
|||
|
||||
public void erstelleKonto() throws SQLException, NoSuchAlgorithmException {
|
||||
String email = view.textFieldEmail.getText();
|
||||
if (checkUser(email)) {
|
||||
String name = view.textFieldName.getText();
|
||||
String vorname = view.textFieldVorname.getText();
|
||||
String password = view.textFieldPassword.getText();
|
||||
String teleN = view.textFieldTeleN.getText();
|
||||
String salt = createSalt();
|
||||
String hashedPassword = hashPassword(password, salt);
|
||||
erstelleKonto = new Modell(name, vorname, hashedPassword, email, teleN);
|
||||
|
||||
erstelleKonto = new Modell(name, vorname, hashedPassword, email, teleN); // Salt wird übergeben
|
||||
// Benutzer in der Datenbank speichern
|
||||
userDao.speichereUser(erstelleKonto);
|
||||
|
||||
// Nach dem Speichern kann die automatisch generierte ID verwendet werden
|
||||
System.out.println("Benutzer erstellt mit ID: " + erstelleKonto.getId());
|
||||
|
||||
datenbanken.fügeKontoEin(erstelleKonto);
|
||||
datenbanken.sendeUserDaten(email);
|
||||
view.showMessage();
|
||||
view.dispose();
|
||||
} else {
|
||||
view.showError();
|
||||
}
|
||||
userWindwo = new UserWindow();
|
||||
}
|
||||
|
||||
public void einzahlung(int betrag) {
|
||||
if (betrag > 0)
|
||||
erstelleKonto.setKontoStand(erstelleKonto.getKontoStand() + betrag);
|
||||
}
|
||||
|
||||
private boolean checkUser(String email) throws SQLException {
|
||||
return datenbanken.checkUser(email);
|
||||
}
|
||||
|
||||
// Erzeuge ein Salt
|
||||
private static String createSalt() {
|
||||
|
|
|
@ -1,30 +1,22 @@
|
|||
package BankSystemGUI;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ControllerUserWindow {
|
||||
private UserWindow user;
|
||||
private Datenbanken datenbanken;
|
||||
private String[] speicher;
|
||||
UserDao userDao;
|
||||
UserWindow userWindow;
|
||||
Modell user;
|
||||
|
||||
public ControllerUserWindow(Connection connection) {
|
||||
|
||||
this.userDao = new UserDaoImpl(connection);
|
||||
this.userWindow = new UserWindow();
|
||||
|
||||
public ControllerUserWindow(String email) throws SQLException {
|
||||
this.user = new UserWindow();
|
||||
this.datenbanken = new Datenbanken();
|
||||
sendUserDaten(email);
|
||||
}
|
||||
|
||||
public void sendUserDaten(String email) throws SQLException {
|
||||
datenbanken.sendeUserDaten(email);
|
||||
speicher = datenbanken.getSpeicher();
|
||||
user.showUserDaten(speicher[0] + " " + speicher[1], speicher[2]);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
ControllerUserWindow c1 = new ControllerUserWindow("obay@gmail.com");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package BankSystemGUI;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatenbankVerbindung {
|
||||
|
||||
// Statische Instanzvariable für die Singleton-Instanz
|
||||
private static DatenbankVerbindung verbindung = null;
|
||||
|
||||
// Instanzvariable für die eigentliche Datenbankverbindung
|
||||
private Connection connection;
|
||||
|
||||
// Privater Konstruktor, um die Instanziierung von außen zu verhindern
|
||||
private DatenbankVerbindung() throws SQLException, ClassNotFoundException {
|
||||
// Initialisiere die Verbindung zur Datenbank
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/banksystem", "root", "");
|
||||
}
|
||||
|
||||
// Synchronized, um Thread-Sicherheit zu gewährleisten
|
||||
public static synchronized DatenbankVerbindung getVerbindung() throws SQLException, ClassNotFoundException {
|
||||
if (verbindung == null) {
|
||||
verbindung = new DatenbankVerbindung();
|
||||
}
|
||||
return verbindung;
|
||||
}
|
||||
|
||||
// Methode, um die Datenbankverbindung zurückzugeben
|
||||
public Connection getConnection() {
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
// Methode, um die Verbindung zu schließen
|
||||
public void schliesseVerbindung() throws SQLException {
|
||||
if (this.connection != null && !this.connection.isClosed()) {
|
||||
this.connection.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package BankSystemGUI;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class Datenbanken {
|
||||
private Connection connection;
|
||||
private String[] datenSpeicher;
|
||||
|
||||
public Datenbanken() throws SQLException {
|
||||
this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/banksystem", "root", "");
|
||||
}
|
||||
|
||||
private void fügeKonto(Modell konto) throws SQLException {
|
||||
String query = "INSERT INTO kunden (name, vorname, password, email, teleN, kontostand) VALUES (?, ?, ?, ?, ?, ?)";
|
||||
PreparedStatement stmt = connection.prepareStatement(query);
|
||||
stmt.setString(1, konto.getName());
|
||||
stmt.setString(2, konto.getVorname());
|
||||
stmt.setString(3, konto.getPassword());
|
||||
stmt.setString(4, konto.getEmail());
|
||||
stmt.setString(5, konto.getTeleN());
|
||||
stmt.setInt(6, konto.getKontoStand());
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
|
||||
public void fügeKontoEin(Modell konto) throws SQLException {
|
||||
fügeKonto(konto);
|
||||
}
|
||||
|
||||
public boolean checkUser(String email) throws SQLException {
|
||||
String query = "SELECT email FROM kunden WHERE email = ?";
|
||||
PreparedStatement stmt = connection.prepareStatement(query);
|
||||
stmt.setString(1, email);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
return !rs.next(); // Wenn ein Ergebnis gefunden wurde, existiert der User
|
||||
}
|
||||
|
||||
public void sendeUserDaten(String email) throws SQLException {
|
||||
datenSpeicher = new String[3];
|
||||
String sql = "SELECT name, vorname, kontostand FROM kunden WHERE email = ?";
|
||||
|
||||
PreparedStatement pstmt = connection.prepareStatement(sql);
|
||||
pstmt.setString(1, email);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
datenSpeicher[0] = rs.getString("name");
|
||||
datenSpeicher[1] = rs.getString("vorname");
|
||||
datenSpeicher[2] = rs.getString("kontostand");
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getSpeicher() {
|
||||
return datenSpeicher;
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ public class Modell {
|
|||
private int kontoStand;
|
||||
private int id;
|
||||
|
||||
// Konstruktor ohne ID, für die erstmalige Erstellung des Kontos
|
||||
public Modell(String name, String vorname, String password, String email, String teleN) {
|
||||
this.name = name;
|
||||
this.vorname = vorname;
|
||||
|
@ -18,6 +19,17 @@ public class Modell {
|
|||
this.kontoStand = 0;
|
||||
}
|
||||
|
||||
// Konstruktor mit ID (für spätere Verwendung, z.B. beim Abrufen aus der DB)
|
||||
public Modell(int id, String name, String vorname, String password, String email, String teleN, int kontoStand) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.vorname = vorname;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
this.teleN = teleN;
|
||||
this.kontoStand = kontoStand;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -38,6 +50,11 @@ public class Modell {
|
|||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTeleN() {
|
||||
return teleN;
|
||||
}
|
||||
|
@ -49,6 +66,5 @@ public class Modell {
|
|||
public void setKontoStand(int kontoStand) {
|
||||
this.kontoStand = kontoStand;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package BankSystemGUI;
|
||||
import java.awt.EventQueue;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, SQLException, NoSuchAlgorithmException {
|
||||
|
||||
Controller c1 = new Controller ();
|
||||
|
||||
if (DatenbankVerbindung.getVerbindung() != null)
|
||||
System.out.println("verbindung");
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package BankSystemGUI;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/* Was ist das?
|
||||
* - Die DAO (Data Access Object)-Interface wird für den Zugriff auf die Datenbank
|
||||
* - und das Speichern, Abrufen, Aktualisieren und Löschen von Benutzerdaten verantwortlich
|
||||
*/
|
||||
public interface UserDao {
|
||||
|
||||
// Benutzer speichern
|
||||
void speichereUser(Modell user)throws SQLException;
|
||||
|
||||
// Benutzer nach ID abrufen
|
||||
Modell getUserByName(int id)throws SQLException;
|
||||
|
||||
// Benutzer aktualisieren
|
||||
void updateUser(Modell user)throws SQLException;
|
||||
|
||||
// Benutzer löschen
|
||||
void löscheUser(int id)throws SQLException;
|
||||
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package BankSystemGUI;
|
||||
|
||||
import java.sql.*;
|
||||
public class UserDaoImpl implements UserDao {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
// Konstruktor für die Verbindung zur Datenbank
|
||||
public UserDaoImpl(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void speichereUser(Modell user) throws SQLException {
|
||||
String query = "INSERT INTO kunden (name, vorname, password, email, teleN, kontoStand) VALUES (?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||
stmt.setString(1, user.getName());
|
||||
stmt.setString(2, user.getVorname());
|
||||
stmt.setString(3, user.getPassword());
|
||||
stmt.setString(4, user.getEmail());
|
||||
stmt.setString(5, user.getTeleN());
|
||||
stmt.setInt(6, user.getKontoStand());
|
||||
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
if (affectedRows == 0)
|
||||
throw new SQLException("Speichern des Benutzers fehlgeschlagen, keine Zeilen betroffen.");
|
||||
|
||||
// Abrufen der automatisch generierten ID aus der Datenbank
|
||||
Statement stmtID = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT id FROM kunden");
|
||||
|
||||
if (rs.next())
|
||||
user.setId(rs.getInt(1)); // Setze die ID im Modell
|
||||
else
|
||||
throw new SQLException("Speichern des Benutzers fehlgeschlagen, keine ID zurückgegeben.");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Modell getUserByName(int id) throws SQLException {
|
||||
String query = "SELECT * FROM kunden WHERE id = ?";
|
||||
Modell user = null;
|
||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||
stmt.setInt(1, id);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
user = new Modell(rs.getInt("id"), rs.getString("name"), rs.getString("vorname"),
|
||||
rs.getString("password"), rs.getString("email"), rs.getString("teleN"),
|
||||
rs.getInt("kontoStand"));
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUser(Modell user) throws SQLException {
|
||||
String query = "UPDATE kunden SET name = ?, vorname = ?, password = ?, email = ?, teleN = ?, kontoStand = ? WHERE id = ?";
|
||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||
stmt.setString(1, user.getName());
|
||||
stmt.setString(2, user.getVorname());
|
||||
stmt.setString(3, user.getPassword());
|
||||
stmt.setString(4, user.getEmail());
|
||||
stmt.setString(5, user.getTeleN());
|
||||
stmt.setInt(6, user.getKontoStand());
|
||||
stmt.setInt(7, user.getId());
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void löscheUser(int id) throws SQLException {
|
||||
String query = "DELETE FROM kunden WHERE id = ?";
|
||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||
stmt.setInt(1, id);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -15,6 +15,8 @@ import javax.swing.JPasswordField;
|
|||
import javax.swing.border.LineBorder;
|
||||
import javax.swing.JButton;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.sql.SQLException;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
public class Window extends JFrame {
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
public class Gewinnkarte {
|
||||
private Kategorie[] kategorien;
|
||||
private final int KATEGORIE_ANZAHL = 6;
|
||||
private int summe;
|
||||
|
||||
public Gewinnkarte() {
|
||||
kategorien = new Kategorie[KATEGORIE_ANZAHL];
|
||||
summe = 0;
|
||||
|
||||
for (int i = 1; i <= kategorien.length; i++) {
|
||||
kategorien[i - 1] = new Kategorie(i);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean punkteEintragen(int kategorie, Würfel[] würfel) {
|
||||
if (!kategorien[kategorie].isOffen())
|
||||
return false;
|
||||
|
||||
summe += kategorien[kategorie].punkteEintragen(würfel);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
int i = 1;
|
||||
for (Kategorie k : kategorien) {
|
||||
sb.append(String.format("%2d. %s: ", i++, k.getName()));
|
||||
|
||||
if (k.isOffen())
|
||||
sb.append("offen");
|
||||
else
|
||||
sb.append(k.getPunkte());
|
||||
|
||||
sb.append(System.lineSeparator());
|
||||
}
|
||||
|
||||
sb.append(String.format("Summe: %3d Punkte", summe));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int getKategorieAnzahl() {
|
||||
return KATEGORIE_ANZAHL;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
public class Kategorie {
|
||||
private String name;
|
||||
private final int AUGEN;
|
||||
private int punkte;
|
||||
|
||||
public Kategorie(int augen) {
|
||||
this.AUGEN = augen;
|
||||
this.name = this.AUGEN + "er";
|
||||
this.punkte = -1;
|
||||
}
|
||||
|
||||
public int punkteEintragen(Würfel[] würfel) {
|
||||
int punkte = 0;
|
||||
|
||||
for (Würfel w : würfel) {
|
||||
if (w.getAugen() == AUGEN)
|
||||
punkte += AUGEN;
|
||||
}
|
||||
|
||||
this.punkte = punkte;
|
||||
|
||||
return punkte;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPunkte() {
|
||||
if (punkte == -1)
|
||||
return 0;
|
||||
|
||||
return punkte;
|
||||
}
|
||||
|
||||
public boolean isOffen() {
|
||||
return punkte == -1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
// facade
|
||||
public class KniffelApi {
|
||||
private Spiel spiel;
|
||||
|
||||
public void starteSpiel(ArrayList<String> spielernamen, boolean starWarsModus) {
|
||||
// TODO: StarWars über Vererbung einbauen?
|
||||
spiel = new Spiel(spielernamen);
|
||||
}
|
||||
|
||||
public Gewinnkarte getGewinnkarte() {
|
||||
return spiel.getGewinnkarteAktuellerSpieler();
|
||||
}
|
||||
|
||||
public String getNameAktuellerSpieler() {
|
||||
return spiel.getNameAktuellerSpieler();
|
||||
}
|
||||
|
||||
public int[] wurfDurchführen(String... würfel) {
|
||||
return spiel.wurfDurchführen(würfel);
|
||||
}
|
||||
|
||||
public boolean punkteEintragen(int kategorie) {
|
||||
return spiel.punkteEintragen(kategorie);
|
||||
}
|
||||
|
||||
public boolean nächsterSpieler() {
|
||||
return spiel.nächsterSpieler();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Spiel {
|
||||
private Spieler[] spieler;
|
||||
private int spieleranzahl;
|
||||
private int aktuellerSpieler;
|
||||
private Würfelbecher becher;
|
||||
private int rundenzähler;
|
||||
private int spielerzähler;
|
||||
|
||||
public Spiel(ArrayList<String> spielernamen) {
|
||||
spieleranzahl = spielernamen.size();
|
||||
spieler = new Spieler[spieleranzahl];
|
||||
|
||||
for (int i = 0; i < spielernamen.size(); i++) {
|
||||
spieler[i] = new Spieler(spielernamen.get(i));
|
||||
}
|
||||
|
||||
aktuellerSpieler = (int) (Math.random() * spieleranzahl);
|
||||
rundenzähler = 1;
|
||||
spielerzähler = 1;
|
||||
|
||||
becher = new Würfelbecher();
|
||||
becher.würfle();
|
||||
}
|
||||
|
||||
public Gewinnkarte getGewinnkarteAktuellerSpieler() {
|
||||
return spieler[aktuellerSpieler].getGewinnkarte();
|
||||
}
|
||||
|
||||
public String getNameAktuellerSpieler() {
|
||||
return spieler[aktuellerSpieler].getName();
|
||||
}
|
||||
|
||||
public int[] wurfDurchführen(String... würfel) {
|
||||
if (würfel.length != 0)
|
||||
for (String w : würfel) {
|
||||
becher.würfleBestimmtenWürfel(Integer.parseInt(w) - 1);
|
||||
}
|
||||
|
||||
return becher.getWürfelwerte();
|
||||
}
|
||||
|
||||
public boolean punkteEintragen(int kategorie) {
|
||||
return spieler[aktuellerSpieler].getGewinnkarte().punkteEintragen(kategorie - 1, becher.getWürfel());
|
||||
}
|
||||
|
||||
public boolean nächsterSpieler() {
|
||||
aktuellerSpieler = ++aktuellerSpieler % spieleranzahl;
|
||||
|
||||
if (++spielerzähler > spieleranzahl) {
|
||||
++rundenzähler;
|
||||
spielerzähler = 1;
|
||||
}
|
||||
|
||||
return rundenzähler <= spieler[aktuellerSpieler].getGewinnkarte().getKategorieAnzahl();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SpielStart {
|
||||
private Scanner kb;
|
||||
private KniffelApi api;
|
||||
|
||||
public SpielStart(Scanner kb, KniffelApi api) {
|
||||
this.kb = kb;
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public void verarbeiteEingabe() {
|
||||
ArrayList<String> spielernamen = new ArrayList<>();
|
||||
|
||||
System.out.println("Bitte Spielernamen (max. 6, leere Eingabe für Abbruch) eingeben:");
|
||||
System.out.println();
|
||||
|
||||
for (int i = 1; i <= 6; i++) {
|
||||
System.out.print("Spieler " + i + ": ");
|
||||
String name = kb.nextLine();
|
||||
|
||||
if (name.isEmpty())
|
||||
break;
|
||||
|
||||
spielernamen.add(name);
|
||||
}
|
||||
|
||||
if (spielernamen.size() == 0) {
|
||||
System.out.println("Keine Spieler eingegeben, zurück zum Hauptmenü.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println(spielernamen.size() + " Mitspieler eingegeben.");
|
||||
System.out.println();
|
||||
System.out.print("Möchten Sie den StarWars-Modus spielen? (j/n): ");
|
||||
|
||||
String sw = kb.nextLine();
|
||||
|
||||
api.starteSpiel(spielernamen, sw.toLowerCase().equals("j"));
|
||||
|
||||
new SpielrundeSpielen(kb, api).verarbeiteEingabe();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
public class Spieler {
|
||||
private String name;
|
||||
private Gewinnkarte gewinnkarte;
|
||||
|
||||
public Spieler(String name) {
|
||||
this.name = name;
|
||||
this.gewinnkarte = new Gewinnkarte();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Gewinnkarte getGewinnkarte() {
|
||||
return gewinnkarte;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SpielrundeSpielen {
|
||||
private Scanner kb;
|
||||
private KniffelApi api;
|
||||
|
||||
public SpielrundeSpielen(Scanner kb, KniffelApi api) {
|
||||
this.kb = kb;
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public void verarbeiteEingabe() {
|
||||
do {
|
||||
System.out.print(">>>>>>>>>");
|
||||
gewinnkarteAusgeben();
|
||||
|
||||
String[] zuWürfeln = { "1", "2", "3", "4", "5" };
|
||||
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
int[] würfelwerte = api.wurfDurchführen(zuWürfeln);
|
||||
System.out.print(i + ". Wurf: ");
|
||||
for (int w : würfelwerte) {
|
||||
System.out.print(w + " ");
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println(" 1 2 3 4 5");
|
||||
System.out.println();
|
||||
|
||||
if (i == 3)
|
||||
break;
|
||||
|
||||
System.out.println("Welche Würfel sollen noch einmal geworfen werden? ");
|
||||
System.out.println("Nr. des/der zu werfenden Würfel(s) durch Leerzeichen getrennt eingeben.");
|
||||
System.out.println("Eine leere Eingabe, beendet das Würfeln.");
|
||||
System.out.print("Eingabe: ");
|
||||
|
||||
String eingabe = kb.nextLine();
|
||||
if (eingabe.isEmpty())
|
||||
break;
|
||||
|
||||
zuWürfeln = eingabe.split(" ");
|
||||
}
|
||||
|
||||
gewinnkarteAusgeben();
|
||||
|
||||
System.out.println("In welcher Kategorie sollen die Punkte eingetragen werden?");
|
||||
do {
|
||||
System.out.print("> ");
|
||||
int kategorie = Integer.parseInt(kb.nextLine());
|
||||
|
||||
if (!api.punkteEintragen(kategorie))
|
||||
System.out.println("Kategorie bereits belegt, bitte andere wählen.");
|
||||
else
|
||||
break;
|
||||
} while (false);
|
||||
|
||||
gewinnkarteAusgeben();
|
||||
|
||||
} while (api.nächsterSpieler());
|
||||
|
||||
System.out.println("Spielende!");
|
||||
// TODO: Gewinnerprüfung
|
||||
}
|
||||
|
||||
private void gewinnkarteAusgeben() {
|
||||
System.out.println();
|
||||
System.out.println("##################################################");
|
||||
System.out.println("Gewinnkarte von " + api.getNameAktuellerSpieler());
|
||||
System.out.println(api.getGewinnkarte().toString());
|
||||
System.out.println("##################################################");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TuiMain {
|
||||
private KniffelApi api;
|
||||
private Scanner kb;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Willkommen beim Star-Wars-Kniffel!");
|
||||
|
||||
new TuiMain().hauptmenue();
|
||||
}
|
||||
|
||||
public TuiMain() {
|
||||
api = new KniffelApi();
|
||||
kb = new Scanner(System.in);
|
||||
}
|
||||
|
||||
public void hauptmenue() {
|
||||
|
||||
mainLoop: do {
|
||||
System.out.println();
|
||||
System.out.println("Auswahlmöglichkeiten (Zifferneingabe):");
|
||||
System.out.println("1 -> Spiel starten");
|
||||
System.out.println("9 -> Spiel beenden");
|
||||
System.out.println();
|
||||
System.out.print("Eingabe > ");
|
||||
|
||||
switch (kb.nextLine()) {
|
||||
case "1" -> new SpielStart(kb, api).verarbeiteEingabe();
|
||||
case "9" -> {
|
||||
break mainLoop;
|
||||
}
|
||||
default -> System.out.println("Eingabe nicht erkannt, bitte wiederholen.");
|
||||
}
|
||||
|
||||
} while (true);
|
||||
|
||||
System.out.println("Auf Wiedersehen!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
public class Würfel {
|
||||
private final int SEITEN;
|
||||
private int augen;
|
||||
|
||||
public Würfel() {
|
||||
this(6);
|
||||
}
|
||||
|
||||
public Würfel(int seiten) {
|
||||
this.SEITEN = seiten;
|
||||
}
|
||||
|
||||
public int würfle() {
|
||||
augen = 1 + (int) (Math.random() * SEITEN);
|
||||
|
||||
return augen;
|
||||
}
|
||||
|
||||
public int getAugen() {
|
||||
return augen;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package Übungen.KniffelSpiel;
|
||||
|
||||
public class Würfelbecher {
|
||||
private final int WÜRFELANZAHL = 5;
|
||||
private Würfel[] würfel;
|
||||
|
||||
public Würfelbecher() {
|
||||
würfel = new Würfel[5];
|
||||
|
||||
for (int i = 0; i < WÜRFELANZAHL; i++) {
|
||||
würfel[i] = new Würfel();
|
||||
}
|
||||
}
|
||||
|
||||
public void würfle() {
|
||||
for (int i = 0; i < WÜRFELANZAHL; i++) {
|
||||
würfel[i].würfle();
|
||||
}
|
||||
}
|
||||
|
||||
public void würfleBestimmtenWürfel(int würfelNummer) {
|
||||
würfel[würfelNummer].würfle();
|
||||
}
|
||||
|
||||
public int[] getWürfelwerte() {
|
||||
int[] würfelwerte = new int[WÜRFELANZAHL];
|
||||
|
||||
for (int i = 0; i < WÜRFELANZAHL; i++) {
|
||||
würfelwerte[i] = würfel[i].getAugen();
|
||||
}
|
||||
|
||||
return würfelwerte;
|
||||
}
|
||||
|
||||
public Würfel[] getWürfel() {
|
||||
return würfel;
|
||||
}
|
||||
}
|
|
@ -1,121 +1,49 @@
|
|||
package Übungen.Ponggame;
|
||||
import java.util.Random;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.Random;
|
||||
|
||||
public class Ball extends JPanel implements Runnable {
|
||||
// Ball Eigenschaften
|
||||
final int width = 15;
|
||||
final int height = 15;
|
||||
int yPosition;
|
||||
int xPosition;
|
||||
Rectangle ball;
|
||||
import javax.swing.*;
|
||||
|
||||
Spieler spieler1 = new Spieler(7, 700 / 2, 1);
|
||||
Spieler spieler2 = new Spieler(700 - 40, 700 / 2, 2);
|
||||
|
||||
// Score Labels
|
||||
JLabel scoreSpieler1 = new JLabel ("Score: " + spieler1.getScore());
|
||||
JLabel scoreSpieler2 = new JLabel ("Score: " + spieler2.getScore());
|
||||
public class Ball extends Rectangle {
|
||||
|
||||
|
||||
int delay = 4;
|
||||
double ybewegung;
|
||||
double xbewegung;
|
||||
|
||||
Random rand = new Random();
|
||||
int yBewegung;
|
||||
int xBewegung;
|
||||
int speed = 5;
|
||||
Random random = new Random();
|
||||
|
||||
public Ball() {
|
||||
this.xPosition = 700 / 2;
|
||||
this.yPosition = 700 / 2;
|
||||
ball = new Rectangle(xPosition, yPosition, width, height);
|
||||
this.setOpaque(true);
|
||||
|
||||
// Zufällige Richtung für den Start des Balls
|
||||
xbewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
ybewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
Ball(int x, int y, int width, int height){
|
||||
super(x,y,width,height);
|
||||
xBewegung(speed);
|
||||
yBewegung(2);
|
||||
}
|
||||
public void setBewegung(int bewegung) {
|
||||
yBewegung = bewegung;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.red);
|
||||
g.fillRect(ball.x, ball.y, ball.width, ball.height);
|
||||
public void draw(Graphics g) {
|
||||
g.setColor(Color.green);
|
||||
g.fillOval(x, y, width, height);
|
||||
|
||||
}
|
||||
|
||||
public void xBewegung(int x) {
|
||||
xBewegung = x;
|
||||
}
|
||||
public void yBewegung(int y) {
|
||||
yBewegung = y;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
if (ball.y <= 0 || ball.y >= 650)
|
||||
ybewegung = -ybewegung;
|
||||
|
||||
ball.y += ybewegung;
|
||||
ball.x += xbewegung;
|
||||
setBounds(ball.x, ball.y, ball.width, ball.height);
|
||||
repaint();
|
||||
|
||||
checkKollision();
|
||||
}
|
||||
|
||||
public void setxbewegung(double xbewegung) {
|
||||
this.xbewegung = xbewegung;
|
||||
}
|
||||
|
||||
public void setybewegung(double ybewegung) {
|
||||
this.ybewegung = ybewegung;
|
||||
}
|
||||
|
||||
public void checkKollision() {
|
||||
if (ball.intersects(spieler1.schläger)) {
|
||||
xbewegung = Math.abs(xbewegung) * (1 + rand.nextDouble() * 0.1);
|
||||
ybewegung = ybewegung + (rand.nextDouble() - 0.5) * 0.5;
|
||||
}
|
||||
|
||||
if (ball.intersects(spieler2.schläger)) {
|
||||
xbewegung = -Math.abs(xbewegung) * (1 + rand.nextDouble() * 0.1);
|
||||
ybewegung = ybewegung + (rand.nextDouble() - 0.5) * 0.5;
|
||||
}
|
||||
|
||||
if (ball.x <= 0) { // Spieler 2 bekommt einen Punkt
|
||||
spieler2.setScore(spieler2.getScore() + 1);
|
||||
System.out.println("Spieler 2 Score: " + spieler2.getScore());
|
||||
resetBall();
|
||||
}
|
||||
|
||||
if (ball.x >= 680) { // Spieler 1 bekommt einen Punkt
|
||||
spieler1.setScore(spieler1.getScore() + 1);
|
||||
System.out.println("Spieler 1 Score: " + spieler1.getScore());
|
||||
resetBall();
|
||||
}
|
||||
x -= xBewegung;
|
||||
y += yBewegung;
|
||||
|
||||
}
|
||||
public void resetBall() {
|
||||
ball.x = 700 / 2;
|
||||
ball.y = 700 / 2;
|
||||
|
||||
// Neue zufällige Bewegungsrichtung
|
||||
xbewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
ybewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
|
||||
setBounds(ball.x, ball.y, ball.width, ball.height);
|
||||
repaint();
|
||||
}
|
||||
|
||||
// Methode zum Aktualisieren der Score-Labels
|
||||
public void updateScores() {
|
||||
scoreSpieler1.setText("Score: " + spieler1.getScore());
|
||||
scoreSpieler2.setText("Score: " + spieler2.getScore());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class ComputerGegener extends JPanel implements Runnable {
|
||||
int id;
|
||||
final int width = 8;
|
||||
final int height = 60;
|
||||
int yPosition;
|
||||
int xPosition;
|
||||
int score;
|
||||
int ybewegung = 1;
|
||||
int dealy = 6;
|
||||
Rectangle schläger;
|
||||
|
||||
public ComputerGegener(int x, int y, int id) {
|
||||
this.xPosition = x;
|
||||
this.yPosition = y;
|
||||
this.id = id;
|
||||
this.score = 0;
|
||||
schläger = new Rectangle(x, y, width, height);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
|
||||
}
|
||||
|
||||
public void move() {
|
||||
schläger.y += ybewegung;
|
||||
if (schläger.y <= 0 ||schläger.y >= 600 )
|
||||
ybewegung = -ybewegung;
|
||||
|
||||
setBounds(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
Thread.sleep(dealy);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
package Übungen.Ponggame;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
|
||||
|
||||
|
||||
|
||||
public class GamePanle extends JPanel implements Runnable {
|
||||
|
||||
final static int WIDTH_WINDOW = 700;
|
||||
final static int HEIGH_WINDOW = 700;
|
||||
Dimension fensterSize = new Dimension(WIDTH_WINDOW,HEIGH_WINDOW);
|
||||
|
||||
final static int WIDTH_RECT = 20;
|
||||
final static int HEIGHT_RECT = 100;
|
||||
|
||||
final static int WIDTH_BALL= 20;
|
||||
final static int HEIGHT_BALL = 20;
|
||||
|
||||
Schläger spieler1, spieler2;
|
||||
Ball ball;
|
||||
Graphics graphics;
|
||||
Random random;
|
||||
Thread game;
|
||||
int scoreSpieler1 = 0;
|
||||
int scoreSpieler2 = 0;
|
||||
|
||||
int speed = 4;
|
||||
GamePanle(){
|
||||
zeichneRects();
|
||||
this.setFocusable(true);
|
||||
this.setPreferredSize(fensterSize);
|
||||
this.addKeyListener(new AL());
|
||||
game = new Thread(this);
|
||||
game.start();
|
||||
|
||||
}
|
||||
|
||||
public void zeichneRects() {
|
||||
spieler1 = new Schläger(0,(HEIGH_WINDOW/2) - (HEIGHT_RECT/2),WIDTH_RECT,HEIGHT_RECT,1);
|
||||
spieler2 = new Schläger(WIDTH_WINDOW-WIDTH_RECT,(HEIGH_WINDOW/2) - (HEIGHT_RECT/2),WIDTH_RECT,HEIGHT_RECT,2);
|
||||
ball = new Ball(WIDTH_WINDOW/2,HEIGH_WINDOW/2,WIDTH_BALL,HEIGHT_BALL);
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
|
||||
//Screen farbe
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillRect(0, 0, WIDTH_WINDOW, HEIGH_WINDOW);
|
||||
g.setColor(Color.GRAY);
|
||||
g.fillRect(WIDTH_WINDOW/2, 0, 5, HEIGH_WINDOW);
|
||||
|
||||
spieler1.draw(g);
|
||||
spieler2.draw(g);
|
||||
ball.draw(g);
|
||||
|
||||
g.setFont(new Font("Arial", Font.BOLD, 30)); // Schriftart Arial, fett, Größe 24
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString(scoreSpieler1+"", WIDTH_WINDOW/2 - 50, 35);
|
||||
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString(scoreSpieler2 +"", WIDTH_WINDOW/2 + 50, 35);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void mov() {
|
||||
|
||||
spieler1.move();
|
||||
spieler2.move();
|
||||
ball.move();
|
||||
|
||||
}
|
||||
|
||||
public void checkRänder() {
|
||||
if (spieler1.y <= 0)
|
||||
spieler1.y = 0;
|
||||
|
||||
if (spieler1.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||
spieler1.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||
|
||||
if (spieler2.y <= 0)
|
||||
spieler2.y = 0;
|
||||
|
||||
if (spieler2.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||
spieler2.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||
|
||||
|
||||
if (ball.intersects(spieler1)) {
|
||||
ball.xBewegung = -ball.xBewegung;
|
||||
}
|
||||
if (ball.intersects(spieler2)) {
|
||||
|
||||
ball.xBewegung = -ball.xBewegung;
|
||||
|
||||
}
|
||||
if (ball.x <= 0) {
|
||||
scoreSpieler2++;
|
||||
zeichneRects();
|
||||
}
|
||||
|
||||
if (ball.x >= WIDTH_WINDOW ) {
|
||||
scoreSpieler1++;
|
||||
zeichneRects();
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (ball.y >= HEIGH_WINDOW-HEIGHT_BALL)
|
||||
ball.yBewegung = -ball.yBewegung;
|
||||
|
||||
if (ball.y <= 0)
|
||||
ball.yBewegung = -ball.yBewegung;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
|
||||
mov();
|
||||
checkRänder();
|
||||
repaint();
|
||||
Thread.sleep(10);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class AL extends KeyAdapter {
|
||||
public void keyPressed(KeyEvent e) {
|
||||
spieler1.keyPressed(e);
|
||||
spieler2.keyPressed(e);
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
spieler1.keyReleased(e);
|
||||
spieler2.keyReleased(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
public class GamePlay {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GameWindow game = new GameWindow();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class GameWindow extends JFrame{
|
||||
|
||||
GamePanle game;
|
||||
|
||||
GameWindow(){
|
||||
game = new GamePanle();
|
||||
this.add(game);
|
||||
this.setTitle("Pong Game");
|
||||
this.setResizable(false);
|
||||
this.setBackground(Color.BLACK);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.pack();
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
package Übungen.Ponggame;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public class GameWindwo extends JFrame {
|
||||
// Fenster Eigenschaften
|
||||
static final int WINDOW_WIDTH = 700;
|
||||
static final int WINDOW_HEIGHT = 700;
|
||||
Dimension screenSize = new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
|
||||
// Fenseter Elemente
|
||||
Panel blackScreen = new Panel();
|
||||
Ball ball = new Ball();
|
||||
|
||||
public GameWindwo() {
|
||||
|
||||
this.setTitle("PongGame");
|
||||
this.setSize(screenSize);
|
||||
this.setResizable(false);
|
||||
this.setVisible(true);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setLayout(null);
|
||||
this.getContentPane().setBackground(Color.BLACK);
|
||||
|
||||
ball.setBounds(700 / 2, 700 / 2, 10, 10);
|
||||
|
||||
ball.spieler1.setBounds(7, WINDOW_HEIGHT / 2, 8, 60);
|
||||
ball.scoreSpieler1.setForeground(Color.white);
|
||||
ball.scoreSpieler1.setBounds(10, 10, 100, 10);
|
||||
|
||||
ball.spieler2.setBounds(WINDOW_WIDTH - 40, WINDOW_HEIGHT / 2, 8, 60);
|
||||
ball.spieler2.setForeground(Color.white);
|
||||
ball.scoreSpieler2.setBounds(600, 10, 100, 10);
|
||||
|
||||
this.add(ball);
|
||||
this.add(ball.spieler1);
|
||||
this.add(ball.scoreSpieler1);
|
||||
this.add(ball.spieler2);
|
||||
this.add(ball.scoreSpieler2);
|
||||
this.add(blackScreen);
|
||||
getInputs();
|
||||
}
|
||||
|
||||
public void getInputs() {
|
||||
addKeyListener(new KeyListener() {
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
ball.spieler1.KeyPressed(e);
|
||||
ball.spieler2.KeyPressed(e);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
ball.spieler1.keyReleased(e);
|
||||
ball.spieler2.keyReleased(e);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Panel extends JPanel {
|
||||
private static final int WINDOW_WIDTH = 700;
|
||||
private static final int WINDOW_HEIGHT = 700;
|
||||
|
||||
Panel(){
|
||||
|
||||
this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
this.setBackground(Color.black);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
|
||||
package Übungen.Ponggame;
|
||||
|
||||
|
||||
public class PongGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GameWindwo g1 = new GameWindwo();
|
||||
Thread ball = new Thread(g1.ball);
|
||||
ball.start();
|
||||
Thread spieler1 = new Thread(g1.ball.spieler1);
|
||||
spieler1.start();
|
||||
|
||||
Thread spieler2 = new Thread(g1.ball.spieler2);
|
||||
spieler2.start();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class Schläger extends Rectangle {
|
||||
|
||||
int id;
|
||||
int yBewegung;
|
||||
int speed = 10;
|
||||
|
||||
Schläger(int x, int y, int width, int height, int id){
|
||||
super(x,y,width,height);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
y += yBewegung;
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (id == 1) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_W)
|
||||
setBewegung(-speed);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_S)
|
||||
setBewegung(speed);
|
||||
} else {
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP)
|
||||
setBewegung(-speed);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setBewegung(speed);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
if (id == 1) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_W ||e.getKeyCode() == KeyEvent.VK_S)
|
||||
setBewegung(0);
|
||||
}else
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP ||e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setBewegung(0);
|
||||
}
|
||||
|
||||
public void setBewegung(int bewegung) {
|
||||
yBewegung = bewegung;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
if (id == 1)
|
||||
g.setColor(Color.RED);
|
||||
else
|
||||
g.setColor(Color.BLUE);
|
||||
|
||||
g.fillRect(x,y,width,height);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,130 +0,0 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class Spieler extends JPanel implements Runnable{
|
||||
|
||||
int id;
|
||||
final int width = 8;
|
||||
final int height = 60;
|
||||
int yPosition;
|
||||
int xPosition;
|
||||
int score;
|
||||
int ybewegung;
|
||||
|
||||
int dealy = 2;
|
||||
Rectangle schläger;
|
||||
|
||||
public Spieler(int x, int y, int id) {
|
||||
this.xPosition = x;
|
||||
this.yPosition = y;
|
||||
this.id = id;
|
||||
this.score = 0;
|
||||
schläger = new Rectangle(x, y, width, height);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
|
||||
}
|
||||
|
||||
public void move() {
|
||||
schläger.y += ybewegung;
|
||||
if (schläger.y <= 0)
|
||||
schläger.y = 0;
|
||||
|
||||
if (schläger.y >= 600)
|
||||
schläger.y = 600;
|
||||
|
||||
|
||||
setBounds(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setYbewegung(int ybewegung) {
|
||||
this.ybewegung = ybewegung;
|
||||
}
|
||||
|
||||
public void KeyPressed(KeyEvent e) {
|
||||
switch (id) {
|
||||
case 1:
|
||||
if (e.getKeyCode() == KeyEvent.VK_W)
|
||||
setYbewegung(-1);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_S)
|
||||
setYbewegung(1);
|
||||
|
||||
break;
|
||||
case 2:
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP)
|
||||
setYbewegung(-1);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setYbewegung(1);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
switch (id) {
|
||||
case 1:
|
||||
if (e.getKeyCode() == KeyEvent.VK_W ||e.getKeyCode() == KeyEvent.VK_S)
|
||||
setYbewegung(0);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setYbewegung(0);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
Thread.sleep(dealy);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue