commit 5059220f31116ea4491aeab1079e84a223d84425 Author: Lenz Wiechers <> Date: Mon Feb 17 00:04:11 2020 +0100 Uploading the project diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..905d2ce --- /dev/null +++ b/README.txt @@ -0,0 +1,3 @@ +Programmiert von Lenz und Tilman. + +Funktioniert nur im Netz der KKOS. \ No newline at end of file diff --git a/assets/heart.png b/assets/heart.png new file mode 100644 index 0000000..cca1313 Binary files /dev/null and b/assets/heart.png differ diff --git a/assets/player.png b/assets/player.png new file mode 100644 index 0000000..251c0fa Binary files /dev/null and b/assets/player.png differ diff --git a/bin/Ball.class b/bin/Ball.class new file mode 100644 index 0000000..b11a698 Binary files /dev/null and b/bin/Ball.class differ diff --git a/bin/GUI$1.class b/bin/GUI$1.class new file mode 100644 index 0000000..f948e35 Binary files /dev/null and b/bin/GUI$1.class differ diff --git a/bin/GUI.class b/bin/GUI.class new file mode 100644 index 0000000..f8d3ff4 Binary files /dev/null and b/bin/GUI.class differ diff --git a/bin/Game.class b/bin/Game.class new file mode 100644 index 0000000..3f21c04 Binary files /dev/null and b/bin/Game.class differ diff --git a/bin/Lives.class b/bin/Lives.class new file mode 100644 index 0000000..af1d4f0 Binary files /dev/null and b/bin/Lives.class differ diff --git a/bin/Main.class b/bin/Main.class new file mode 100644 index 0000000..d3120b3 Binary files /dev/null and b/bin/Main.class differ diff --git a/bin/MySQLConnector.class b/bin/MySQLConnector.class new file mode 100644 index 0000000..fc2885d Binary files /dev/null and b/bin/MySQLConnector.class differ diff --git a/bin/Picture.class b/bin/Picture.class new file mode 100644 index 0000000..33b4fa0 Binary files /dev/null and b/bin/Picture.class differ diff --git a/bin/Player.class b/bin/Player.class new file mode 100644 index 0000000..9b4f54e Binary files /dev/null and b/bin/Player.class differ diff --git a/bin/Wall.class b/bin/Wall.class new file mode 100644 index 0000000..f6bcd36 Binary files /dev/null and b/bin/Wall.class differ diff --git a/src/Ball.java b/src/Ball.java new file mode 100644 index 0000000..f35659f --- /dev/null +++ b/src/Ball.java @@ -0,0 +1,56 @@ +public class Ball extends Picture { + + private static final long serialVersionUID = 1L; + + public float speedX = 0.00000011f; + public float speedY = 0.0000001f; + public float posiX = 300; + public float posiY = 350; + + public Ball() { + + super("player"); + + } + + public int getPosi(char coordinate, long dt) { + if (coordinate == 'x') { + posiX -= speedX * dt; + return (int) posiX; + } else if (coordinate == 'y') { + posiY += speedY * dt; + return (int) posiY; + } else + return 0; + } + + public void setPosi(char coordinate, int posi) { + if (coordinate == 'x') { + posiX = posi; + } else if (coordinate == 'y') { + posiY = posi; + } + } + + public void changeDir(char coordinate) { + if (coordinate == 'x') { + speedX = -speedX; + } else if (coordinate == 'y') { + speedY = -speedY; + } + } + + public void sonic() { + if (speedX > 0) { + speedX += 0.00000000004f; + } else { + speedX -= 0.00000000004f; + } + if(speedY > 0) { + speedY += 0.00000000004f; + } else { + speedY -= 0.00000000004f; + } + } + +} diff --git a/src/GUI.java b/src/GUI.java new file mode 100644 index 0000000..8af8d5e --- /dev/null +++ b/src/GUI.java @@ -0,0 +1,157 @@ + +// Imports: +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.SQLException; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import java.sql.ResultSet; + +// Diese Klasse wird benötigt, um nach dem Beenden des Spiels seinen Namen mit dem Score in die Datenbank einzutragen. + +public class GUI extends JFrame { // extends JFrame --> Jede 'GUI' ist auch ein 'JFrame' + + private static final long serialVersionUID = -2037442253698128981L; + + int spielNr; // Nummer des aktuellen Spiels (später entnommen aus der Datenbank) + int score; // Anahl erreichter Ballwechsel im zuletzt beendeten Spiel + + // Deklarieren der GUI - Elemente: + JPanel panel; // Auf dem Panel werden alle GUI - Elemente gespeichert + + JTextField eingabe; // Eingabefeld für Namen des Spielers + JButton enter; // Ein Button, der wenn er gedrückt wird, die Daten in die Datenbank einfügen + // soll + JLabel info; // Ein JLabel auf dem ein kurzer Info - Text abgebildet wird + + // Die folgenden JLabel werden für die Bestenliste benötigt, welche unter den + // anderen GUI - Elementen abgebildet werden soll. + JLabel scores[]; // Hier werden die Top 10 Scores angezeigt + JLabel names[]; // Hier werden die Namen der Personen mit den Top 10 Scores angezeigt + JLabel gameNr[]; // Hier werden die gameNr des jeweiligen Top 10 Lauf angezeigt + + ResultSet rs; // Hier wird das Ergebnis der Abfrage der Datenbank gespeichert + + MySQLConnector datenbank; // Die Datenbank, auf der alles gespeichert ist + + public GUI(int score) throws SQLException { // Erstellen des Konstruktors mit der Eingabe score + + // Initialisieren der Datenbank + // datenbank = new MySQLConnector("LenzundTilman", "Xc37zTVG8", "10.16.225.2", + // "pong"); + + this.score = score; // score wird initialisiert mit dem Wert des erreichten Scores (Kontakte des + // Spielers mit dem Ball) + + // datenbank.connect(); // Herstellen der Verbindung zur Datenbank: + // spielNr = datenbank.getRowCount("highscore") + 1; // Errechnung der aktuellen + // Spielnummer + + // Initialisieren der GUI - Elemente: + panel = new JPanel(); + eingabe = new JTextField(); + enter = new JButton("Enter"); + info = new JLabel("Congrats! You achieved a Score of " + score + "! Please enter a name!"); + + spielNr = 1000; + // Initialisieren des Frames: + this.setBounds(300, 300, 370, 400); // Setzen der Position auf dem Monito und der Größe + this.setVisible(true); // Sichtbarkeit des Frames + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Progamm wird geschlossen wenn das Fenster + // geschlossen wird + this.setTitle("Game " + spielNr); // Der Titel des Frames wird gesetzt. + this.setContentPane(panel); + this.getContentPane(); + this.setResizable(false); // Festergrösse ist unveränderlich + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Progamm wird geschlossen wenn das Fenster geschlossen + // wird + this.getContentPane().setLayout(null); // kein Layout + + scores = new JLabel[11]; + names = new JLabel[11]; + gameNr = new JLabel[11]; + + // Tabellennamen für die Top 10 Tabelle werden gestzt: + scores[0] = new JLabel("Score"); + names[0] = new JLabel("Name"); + gameNr[0] = new JLabel("Game No."); + + panel.add(scores[0]); + panel.add(names[0]); + panel.add(gameNr[0]); + + scores[0].setBounds(300, 120, 150, 20); + names[0].setBounds(150, 120, 150, 20); + gameNr[0].setBounds(0, 120, 150, 20); + + // Anfrage an die Datenbank wird gestellt: + // rs = datenbank.executeQuery("SELECT * FROM highscore ORDER BY Score DESC"); + + // Erstellen der Top 10 Liste: + for (int i = 1; i < scores.length; i++) { + try { + // rs.next(); // Auslesen der nächsten Zeile + + // Setzen der Labels mit den jeweiligen Inhalten: + // scores[i] = new JLabel(Integer.toString(rs.getInt("Score"))); + // names[i] = new JLabel(rs.getString("Name")); + // gameNr[i] = new JLabel(Integer.toString(rs.getInt("SpielNr"))); + + scores[i] = new JLabel(Integer.toString(i)); + names[i] = new JLabel(Integer.toString(i)); + gameNr[i] = new JLabel(Integer.toString(i)); + + scores[i].setBounds(300, 120 + i * 20, 150, 20); // + names[i].setBounds(150, 120 + i * 20, 150, 20); // Poition(und Grösse) des Panels der Top Scores + gameNr[i].setBounds(0, 120 + i * 20, 150, 20); // (mit Zeilenangabe im Fall von Fehlern) + + // Hinzufügen der jeweiligen Labels: + panel.add(scores[i]); + panel.add(names[i]); + panel.add(gameNr[i]); + + System.out.println(i); + + } catch (Exception e) { + System.out.println("Fehler: " + e); // Falls etwas schief läuft + } + } + + // Hinzufügen von 'info', 'eingabe', und 'enter' zum panel + panel.add(info); + panel.add(eingabe); + panel.add(enter); + + + + // Setzen der Positionen und Größen von 'eingabe', 'enter', und 'info' + eingabe.setBounds(50, 50, 250, 20); + enter.setBounds(50, 80, 100, 30); + info.setBounds(5, 5, 400, 20); + + // Hinzufügen des Action Listeners zu 'eingabe' + enter.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + try { + button_ActionPerformed(evt); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + + } + + public void button_ActionPerformed(ActionEvent evt) throws SQLException { // Was passieren soll, wenn der Button + // gedrückt wird + //datenbank.updateDatabase("INSERT INTO `highscore` (`Score`, `Name`, `SpielNr`) VALUES ('" + score + "', '" + // + eingabe.getText() + "','" + spielNr + "')"); // Einfügen der Daten in die Datenbank + this.dispose(); // Der Frame wird geschlossen + //datenbank.disconnect(); // Die Verbindung zur Datenbank wird getrennt + } + +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..8a2e1c1 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,131 @@ +import java.awt.Color; +import java.awt.Font; +import java.sql.SQLException; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; + +public class Game { + int timer1 = 0; + int timer2 = 0; + long lastT; + int lives = 3; + + public void delay(int time) { + + try { + Thread.sleep(time); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public Game() throws SQLException { + + JPanel panel = new JPanel(); + + JFrame frame = new JFrame(); + + Player player = new Player(38, 40); + + Wall Walls[] = new Wall[3]; + + Lives Lives[] = new Lives[3]; + + Ball ball = new Ball(); + int score = 0; + + JLabel scoreLabel = new JLabel(); + + Font f = new Font("TimesRoman", Font.BOLD, 25); + + for (int i = 0; i < 3; i++) { + Walls[i] = new Wall(i); + Lives[i] = new Lives(); + panel.add(Walls[i]); + panel.add(Lives[i]); + } + + panel.add(player); + frame.addKeyListener(player); + scoreLabel.setText(Integer.toString(score)); + scoreLabel.setForeground(new Color(255, 255, 255)); + scoreLabel.setFont(f); + panel.add(scoreLabel); + + panel.add(ball); + + frame.setContentPane(panel); + frame.setSize(600, 700); + frame.setVisible(true); + frame.setTitle("Pong by Lenz and Tilman"); + frame.setLocation(300, 10); + frame.setResizable(false); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(null); + + panel.setBackground(Color.BLACK); + + scoreLabel.setBounds(540, 300, 100, 100); + + ball.setBounds(ball.getPosi('x', 0), ball.getPosi('y', 0), 10, 10); + + panel.add(ball); + Lives[0].setBounds(25, 630, 18, 15); + Lives[1].setBounds(43, 630, 18, 15); + Lives[2].setBounds(62, 630, 18, 15); + + for (int i = 0; i < 3; i++) { + Walls[i].setBounds(Walls[i].getPosi('x'), Walls[i].getPosi('y'), Walls[i].getSize('x'), + Walls[i].getSize('y')); + } + long dt; + player.setBounds(player.getPosi('x', 0), player.getPosi('y', 0), 10, 100); + //delay(3000); + lastT = System.nanoTime(); + while (lives < 0) { + dt = System.nanoTime() - lastT; + lastT = System.nanoTime(); + + player.setBounds(player.getPosi('x', dt), player.getPosi('y', dt), 10, 100); + player.sonic(); + + ball.setBounds(ball.getPosi('x', dt), ball.getPosi('y', dt), 10, 10); + + if (player.getPosi('x', 0) <= ball.getPosi('x', 0) + 10 && player.getPosi('y', 0) <= ball.getPosi('y', 0) + && player.getPosi('y', 0) >= ball.getPosi('y', 0) - 100) { + ball.changeDir('x'); + ball.setPosi('x', 560); + score++; + scoreLabel.setText(Integer.toString(score)); + } else if (ball.getPosi('x', 0) < 20) { + ball.changeDir('x'); + ball.setPosi('x', 20); + } else if (ball.getPosi('y', 0) > 640 || ball.getPosi('y', 0) < 20) { + ball.changeDir('y'); + } else if (ball.getPosi('x', 0) > 570) { + Lives[lives - 1].setBounds(0, 0, 0, 0); + lives--; + if (lives == 0) { + frame.setVisible(false); + } else { + ball.setPosi('x', 300); + ball.setPosi('y', 350); + ball.setBounds(ball.getPosi('x', 0), ball.getPosi('y', 0), 10, 10); + delay(1000); + lastT = System.nanoTime(); + } + } + + ball.sonic(); + + delay(15); + + } + frame.dispose(); + GUI Gui = new GUI(score); + + } + +} \ No newline at end of file diff --git a/src/Lives.java b/src/Lives.java new file mode 100644 index 0000000..7ff395d --- /dev/null +++ b/src/Lives.java @@ -0,0 +1,7 @@ + +public class Lives extends Picture{ + public Lives() { + super("heart"); + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..5a39a9e --- /dev/null +++ b/src/Main.java @@ -0,0 +1,15 @@ +import java.sql.SQLException; + +public class Main { + + @SuppressWarnings("unused") + + public static void main(String[] args) throws SQLException { + + // System.out.println("test"); + System.setProperty("sun.java2d.opengl", "true"); + Game Game = new Game(); + + } + +} \ No newline at end of file diff --git a/src/MySQLConnector.java b/src/MySQLConnector.java new file mode 100644 index 0000000..29a9888 --- /dev/null +++ b/src/MySQLConnector.java @@ -0,0 +1,48 @@ +import java.sql.Connection; +import java.sql.Statement; +import java.sql.SQLException; +import java.sql.ResultSet; +import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; + +public class MySQLConnector { + + private MysqlDataSource ds; + private Connection conn; + private Statement s; + + public MySQLConnector(String user, String pw, String server, String db) { + ds = new MysqlDataSource(); + ds.setUser(user); + ds.setPassword(pw); + ds.setServerName(server); + ds.setDatabaseName(db); + } + + public void connect() throws SQLException { + conn = ds.getConnection(); + } + + public void disconnect() throws SQLException { + s.close(); + conn.close(); + } + + public ResultSet executeQuery(String query) throws SQLException { + s = conn.createStatement(); + ResultSet rs = s.executeQuery(query); + return rs; + } + + public void updateDatabase(String whatever) throws SQLException{ + s = conn.createStatement(); + s.executeUpdate(whatever); + } + + public int getRowCount(String table) throws SQLException{ + s = conn.createStatement(); + ResultSet rs = s.executeQuery("Select * From " + table); + rs.last(); + return rs.getRow(); + } + +} \ No newline at end of file diff --git a/src/Picture.java b/src/Picture.java new file mode 100644 index 0000000..fcd38b9 --- /dev/null +++ b/src/Picture.java @@ -0,0 +1,24 @@ +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.JOptionPane; + +public class Picture extends JLabel { + + private static final long serialVersionUID = 5088057670084213472L; + + public Picture(String name) { + super(); + try { + BufferedImage test = ImageIO.read(new File("assets/" + name + ".png")); + this.setIcon(new ImageIcon(test)); + } catch (IOException ex) { + JOptionPane.showMessageDialog(null, "Please check your file paths", "Error", JOptionPane.ERROR_MESSAGE); + } + } + +} diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..9713dee --- /dev/null +++ b/src/Player.java @@ -0,0 +1,108 @@ +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class Player extends Picture implements KeyListener { + + private static final long serialVersionUID = -4062796732924767091L; + + private int upCode; + private int downCode; + private float posiX = 570; + private float posiY = 300; + private int sizeX = 10; + private int sizeY = 100; + private boolean up = false; + private boolean down = false; + private float speed = 0.0000002f; + + public Player(int newUpCode, int newDownCode) { + super("player"); + + upCode = newUpCode; + downCode = newDownCode; + + + + } + + public void keyPressed(KeyEvent e) { + //System.out.println(e.getKeyCode()); + if (e.getKeyCode() == upCode) { + + up = true; + down = false; + + } else if (e.getKeyCode() == downCode) { + + up = false; + down = true; + + } + + } + + public void keyReleased(KeyEvent e) { + + if (e.getKeyCode() == upCode && up) { + up = false; + down = false; + } else if (e.getKeyCode() == downCode && down) { + down = false; + up = false; + } + + } + + public void keyTyped(KeyEvent e) { + + } + + public int getPosi(char coordinate, long dt) { + + if (up && posiY > 10) { + posiY -= speed * dt; + } else if (down && posiY < 555) { + posiY += speed * dt; + } + if (posiY < 10) { + posiY = 10; + } + if (posiY > 659) { + posiY = 659; + } + if (posiX < 10) { + posiX = 10; + } + if (posiX > 665) { + posiX = 665; + } + if (coordinate == 'x') { + return (int) posiX; + } else if (coordinate == 'y') { + return (int) posiY; + } else + return 0; + } + + public void setPosi(char coordinate, int posi) { + if (coordinate == 'x') { + posiX = posi; + } else if (coordinate == 'y') { + posiY = posi; + } + } + + public int getSize(char coordinate) { + if (coordinate == 'x') { + return sizeX; + } else if (coordinate == 'y') { + return sizeY; + } else + return 0; + } + + public void sonic() { + speed += 0.000000000004f; + } + +} diff --git a/src/Wall.java b/src/Wall.java new file mode 100644 index 0000000..098a200 --- /dev/null +++ b/src/Wall.java @@ -0,0 +1,47 @@ + +public class Wall extends Picture { + private int pos = 0; + private int posX; + private int posY; + private int sizeX; + private int sizeY; + + public Wall(int pos) { + super("player"); + this.pos = pos; + if (pos == 0) { + sizeX = 570; + sizeY = 10; + posX = 10; + posY = 10; + } else if (pos == 1) { + sizeX = 10; + sizeY = 650; + posX = 10; + posY = 10; + } else if (pos == 2) { + sizeX = 570; + sizeY = 10; + posX = 10; + posY = 650; + } + } + + public int getPosi(char coordinate) { + if (coordinate == 'x') { + return posX; + } else if (coordinate == 'y') { + return posY; + } else + return 0; + } + + public int getSize(char coordinate) { + if (coordinate == 'x') { + return sizeX; + } else if (coordinate == 'y') { + return sizeY; + } else + return 0; + } +}