Uploading the project
commit
5059220f31
|
@ -0,0 +1,3 @@
|
|||
Programmiert von Lenz und Tilman.
|
||||
|
||||
Funktioniert nur im Netz der KKOS.
|
Binary file not shown.
After Width: | Height: | Size: 238 B |
Binary file not shown.
After Width: | Height: | Size: 6.1 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
public class Lives extends Picture{
|
||||
public Lives() {
|
||||
super("heart");
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue