Datenbankanbindung/TetrisMenu mit Highscore/Spiel startet wieder
This commit is contained in:
parent
c5b6896038
commit
ca70adbf32
Binary file not shown.
@ -16,6 +16,7 @@ public class Board extends JPanel implements KeyListener
|
|||||||
private static int FPS =60;
|
private static int FPS =60;
|
||||||
private static int delay =FPS/1000;
|
private static int delay =FPS/1000;
|
||||||
|
|
||||||
|
//Spielfeld Breite und Länge
|
||||||
public static final int BOARD_WIDTH=11;
|
public static final int BOARD_WIDTH=11;
|
||||||
public static final int BOARD_HEIGHT=20;
|
public static final int BOARD_HEIGHT=20;
|
||||||
public static final int BLOCK_SIZE=30;
|
public static final int BLOCK_SIZE=30;
|
||||||
@ -26,16 +27,17 @@ public class Board extends JPanel implements KeyListener
|
|||||||
private Stein currenStein;
|
private Stein currenStein;
|
||||||
private Random ran;
|
private Random ran;
|
||||||
|
|
||||||
private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.decode("#00ff80"),Color.decode("#ff8000"),Color.decode("#ffb3b3"),
|
// Steinfarben
|
||||||
|
private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.decode("#00ff80"),Color.decode("#ff8000"),Color.decode("#ffb3b3"),
|
||||||
Color.decode("#8000ff"),Color.decode("#ff0040"),};
|
Color.decode("#8000ff"),Color.decode("#ff0040"),};
|
||||||
|
|
||||||
|
|
||||||
|
// Steinform
|
||||||
public Board() {
|
public Board() {
|
||||||
ran = new Random();
|
ran = new Random();
|
||||||
/*steine[0]= new Stein(new int[][]{
|
steine[0]= new Stein(new int[][]{
|
||||||
{1,1,1},
|
{1,1,1},
|
||||||
{0,1,0}
|
{0,1,0},
|
||||||
|
|
||||||
}, this,colors[0]);
|
}, this,colors[0]);
|
||||||
steine[1]= new Stein(new int[][]{
|
steine[1]= new Stein(new int[][]{
|
||||||
@ -63,7 +65,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
|
|||||||
{1,1,1,1}
|
{1,1,1,1}
|
||||||
}, this,colors[6]);
|
}, this,colors[6]);
|
||||||
|
|
||||||
currenStein= steine[0];*/
|
currenStein= steine[0];
|
||||||
|
|
||||||
loop = new Timer(delay, new ActionListener() {
|
loop = new Timer(delay, new ActionListener() {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
@ -71,7 +73,6 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
update();
|
update();
|
||||||
|
|
||||||
repaint();
|
repaint();
|
||||||
//System.out.println(n++);
|
//System.out.println(n++);
|
||||||
}
|
}
|
||||||
@ -127,6 +128,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Bewegen die Steine mit Taste, Taste-Space : nach unten, Taste d : nach rechts, Taste a : nach links, leider klappt nicht.
|
||||||
@Override
|
@Override
|
||||||
public void keyTyped(KeyEvent e) {
|
public void keyTyped(KeyEvent e) {
|
||||||
// switch(e.getKeyChar()) {
|
// switch(e.getKeyChar()) {
|
||||||
@ -142,6 +144,8 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Bewegen die Steine mit Taste, Taste-Space : nach unten, Taste Enter : nach rechts, Taste Escape : nach links
|
||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
||||||
|
@ -1,102 +1,42 @@
|
|||||||
package Tetris;
|
package Tetris;
|
||||||
|
|
||||||
import java.sql.*;
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Connection connection = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
connection = DriverManager.getConnection("jdbc:sqlite:userhighscore.db");
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
statement.setQueryTimeout(30);
|
|
||||||
|
|
||||||
Menue menue = new Menue();
|
// Datenbankverbindung herstellen
|
||||||
menue.getButton().addActionListener(new java.awt.event.ActionListener() {
|
PlayerDBController playerDBController = PlayerDBController.getInstance();
|
||||||
@Override
|
playerDBController.initDBConnection();
|
||||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
List<Player> playerListe = playerDBController.holeSpielerDaten();
|
||||||
String name = JOptionPane.showInputDialog(menue.getParent(),
|
JFrame tetrisMenueJframe = new TetrisMenue("Tetris");
|
||||||
"What is your name?", null);
|
tetrisMenueJframe.setVisible(true);
|
||||||
if(name == null){
|
TetrisMenue tetrisMenue = (TetrisMenue) tetrisMenueJframe;
|
||||||
name = "anon";
|
tetrisMenue.getSpielStartenButton().addActionListener(new ActionListener() {
|
||||||
}
|
@Override
|
||||||
menue.getParent().setVisible(false);
|
public void actionPerformed(ActionEvent e) {
|
||||||
new GameGui(name);
|
String spielerName = tetrisMenue.getSpielerNameTextField().getText();
|
||||||
}
|
new GameGui(spielerName);
|
||||||
});
|
PlayerDBController.getInstance().speichereSpieler(new Player(spielerName,0));
|
||||||
String playername = menue.getName();
|
tetrisMenueJframe.setVisible(false);
|
||||||
ResultSet resultSet = statement.executeQuery("select * from userscore where name = "+ playername);
|
|
||||||
if(!resultSet.next())
|
|
||||||
{
|
|
||||||
// Playername gibt es noch nicht in der Datenbank also anlegen.
|
|
||||||
// Es fehlt noch der score.
|
|
||||||
statement.executeUpdate("insert into userscore values(1," + playername + ", 123)");
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
catch(SQLException e)
|
// Menue menue = new Menue();
|
||||||
{
|
// final Player[] aktuellerSpieler = new Player[1];
|
||||||
// if the error message is "out of memory",
|
// menue.getButton().addActionListener(new java.awt.event.ActionListener() {
|
||||||
// it probably means no database file is found
|
// @Override
|
||||||
System.err.println(e.getMessage());
|
// public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||||
}
|
// aktuellerSpieler[0] = new Player(menue.getTextField().getText(), 0);
|
||||||
finally
|
// menue.getParent().setVisible(false);
|
||||||
{
|
// //new GameGui(name);
|
||||||
try
|
// playerDBController.speichereSpieler(aktuellerSpieler[0]);
|
||||||
{
|
// }
|
||||||
if(connection != null)
|
// });
|
||||||
connection.close();
|
|
||||||
}
|
//String playername = menue.getName();
|
||||||
catch(SQLException e)
|
//new GameGui(playername);
|
||||||
{
|
|
||||||
// connection close failed.
|
|
||||||
System.err.println(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private static void CreateSqlLiteDatabaseConnection(){
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// Connection connection = null;
|
|
||||||
// // create a database connection
|
|
||||||
// connection = DriverManager.getConnection("jdbc:sqlite:userhighscore.db");
|
|
||||||
// Statement statement = connection.createStatement();
|
|
||||||
// statement.setQueryTimeout(30); // set timeout to 30 sec.
|
|
||||||
|
|
||||||
// statement.executeUpdate("drop table if exists userscore");
|
|
||||||
// statement.executeUpdate("create table userscore (id integer, name string, score integer)");
|
|
||||||
// statement.executeUpdate("insert into userscore values(1, 'leo', 123)");
|
|
||||||
// statement.executeUpdate("insert into userscore values(2, 'yui', 456)");
|
|
||||||
// ResultSet rs = statement.executeQuery("select * from userscore");
|
|
||||||
// while(rs.next())
|
|
||||||
// {
|
|
||||||
// // read the result set
|
|
||||||
//
|
|
||||||
// System.out.println("name = " + rs.getString("name"));
|
|
||||||
// System.out.println("score = " + rs.getInt("score"));
|
|
||||||
// System.out.println("id = " + rs.getInt("id"));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// catch(SQLException e)
|
|
||||||
// {
|
|
||||||
// // if the error message is "out of memory",
|
|
||||||
// // it probably means no database file is found
|
|
||||||
// System.err.println(e.getMessage());
|
|
||||||
// }
|
|
||||||
// finally
|
|
||||||
// {
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// if(connection != null)
|
|
||||||
// connection.close();
|
|
||||||
// }
|
|
||||||
// catch(SQLException e)
|
|
||||||
// {
|
|
||||||
// // connection close failed.
|
|
||||||
// System.err.println(e.getMessage());
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,7 @@
|
|||||||
package Tetris;
|
package Tetris;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
public class GameGui {
|
public class GameGui {
|
||||||
public static final int width= 550, height =650;
|
public static final int width= 550, height =650;
|
||||||
@ -14,18 +15,19 @@ public class GameGui {
|
|||||||
panel.setBorder(BorderFactory.createMatteBorder(500, 10, 10, 50,Color.black));
|
panel.setBorder(BorderFactory.createMatteBorder(500, 10, 10, 50,Color.black));
|
||||||
|
|
||||||
//Button
|
//Button
|
||||||
//JButton button1 = new JButton("Start");
|
JButton buttonStop = new JButton("Pause");
|
||||||
//button1.setPreferredSize( new Dimension(150,50));
|
buttonStop.setPreferredSize( new Dimension(100,30));
|
||||||
JButton buttonPause = new JButton("Pause");
|
JButton buttonVerlassen = new JButton("verlassen");
|
||||||
buttonPause.setPreferredSize( new Dimension(100,30));
|
buttonVerlassen.setPreferredSize( new Dimension(100,30));
|
||||||
JButton buttonScore = new JButton("Score");
|
|
||||||
buttonScore.setPreferredSize( new Dimension(100,30));
|
|
||||||
|
|
||||||
//Button untereinander
|
//Button untereinander
|
||||||
panel.setLayout(new GridLayout(0,1));
|
panel.setLayout(new GridLayout(0,1));
|
||||||
panel.setBackground(Color.YELLOW);
|
panel.setBackground(Color.YELLOW);
|
||||||
panel.add(buttonPause);
|
panel.add(buttonStop);
|
||||||
panel.add(buttonScore);
|
panel.add(buttonVerlassen);
|
||||||
|
|
||||||
|
|
||||||
frame.add(panel,BorderLayout.EAST);
|
frame.add(panel,BorderLayout.EAST);
|
||||||
frame.setSize(width,height);
|
frame.setSize(width,height);
|
||||||
@ -36,6 +38,14 @@ public class GameGui {
|
|||||||
frame.addKeyListener(board);
|
frame.addKeyListener(board);
|
||||||
frame.add(board,BorderLayout.CENTER);
|
frame.add(board,BorderLayout.CENTER);
|
||||||
frame.requestFocusInWindow();
|
frame.requestFocusInWindow();
|
||||||
|
|
||||||
|
// ActionListener al = ev -> {if(ev.getSource() == buttonVerlassen)
|
||||||
|
// {
|
||||||
|
// System.exit(0);
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// buttonStop.addActionListener(al);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
36
src/Tetris/Player.java
Normal file
36
src/Tetris/Player.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package Tetris;
|
||||||
|
|
||||||
|
public class Player implements Comparable<Player> {
|
||||||
|
private String name;
|
||||||
|
private int score;
|
||||||
|
|
||||||
|
public Player(String name, int score) {
|
||||||
|
this.name = name;
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getScore() {
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(int score) {
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Player andere) {
|
||||||
|
return andere.score-score;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return name +": "+ score;
|
||||||
|
}
|
||||||
|
}
|
171
src/Tetris/PlayerDBController.java
Normal file
171
src/Tetris/PlayerDBController.java
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
package Tetris;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
// Beispiel aus https://javabeginners.de/Datenbanken/SQLite-Datenbank.php , sieh unten
|
||||||
|
class PlayerDBController {
|
||||||
|
|
||||||
|
private static final PlayerDBController dbcontroller = new PlayerDBController();
|
||||||
|
private static Connection connection;
|
||||||
|
private static final String DB_PATH = "userhighscore.db";
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.err.println("Fehler beim Laden des JDBC-Treibers");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PlayerDBController(){
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PlayerDBController getInstance(){
|
||||||
|
return dbcontroller;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Datenbankverbindung herstellen
|
||||||
|
|
||||||
|
public void initDBConnection() {
|
||||||
|
try {
|
||||||
|
if (connection != null)
|
||||||
|
return;
|
||||||
|
System.out.println("Creating Connection to Database...");
|
||||||
|
connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
|
||||||
|
if (!connection.isClosed())
|
||||||
|
System.out.println("...Connection established");
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
if (!connection.isClosed() && connection != null) {
|
||||||
|
connection.close();
|
||||||
|
if (connection.isClosed())
|
||||||
|
System.out.println("Connection to Database closed");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Es wird eingegebenen Spielername,Score mit 0 in der Datenbank mit insert gespeichert.
|
||||||
|
public void speichereSpieler(Player player) {
|
||||||
|
try {
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
statement.setQueryTimeout(30);
|
||||||
|
// Spielername gibt es noch garnicht
|
||||||
|
ResultSet resultSet = statement.executeQuery("select * from userscore where name='"+player.getName()+"'");
|
||||||
|
if(!resultSet.next()){
|
||||||
|
statement.executeUpdate("insert into userscore values('"+player.getName()+"',"+player.getScore()+")");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// Spielername gibt es schon, update player.score wenn größer als score im Datenbank.
|
||||||
|
String query = "UPDATE userscore SET score='"+ player.getScore() +"' WHERE name='"+player.getName()+"'";
|
||||||
|
statement.executeUpdate(query);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Couldn't handle DB-Query");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// SpielerDaten aus Datenbank holen, dann in einer Arraylist speicher und zurückgeben.
|
||||||
|
public List<Player> holeSpielerDaten() {
|
||||||
|
List<Player> playerList = new ArrayList<Player>();
|
||||||
|
try {
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
statement.setQueryTimeout(30);
|
||||||
|
// statement.executeUpdate("create table userscore (name string, score integer)");
|
||||||
|
// statement.executeUpdate("insert into userscore values('leo', 123)");
|
||||||
|
// statement.executeUpdate("insert into userscore values('yui', 456)");
|
||||||
|
System.out.println("vorRS");
|
||||||
|
|
||||||
|
ResultSet resultSet = statement.executeQuery("SELECT * FROM userscore;");
|
||||||
|
System.out.println("nachRS");
|
||||||
|
while(resultSet.next())
|
||||||
|
{
|
||||||
|
playerList.add(new Player(resultSet.getString("name"), resultSet.getInt("score")));
|
||||||
|
|
||||||
|
// Playername gibt es noch nicht in der Datenbank also anlegen.
|
||||||
|
// Es fehlt noch der score.
|
||||||
|
//statement.executeUpdate("insert into userscore values(1," + playername + ", 123)");
|
||||||
|
}
|
||||||
|
resultSet.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Couldn't handle DB-Query");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return playerList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleDB() {
|
||||||
|
try {
|
||||||
|
Statement stmt = connection.createStatement();
|
||||||
|
stmt.executeUpdate("DROP TABLE IF EXISTS books;");
|
||||||
|
stmt.executeUpdate("CREATE TABLE books (author, title, publication, pages, price);");
|
||||||
|
stmt.execute("INSERT INTO books (author, title, publication, pages, price) VALUES ('Paulchen Paule', 'Paul der Penner', '2001-05-06', '1234', '5.67')");
|
||||||
|
|
||||||
|
PreparedStatement ps = connection
|
||||||
|
.prepareStatement("INSERT INTO books VALUES (?, ?, ?, ?, ?);");
|
||||||
|
|
||||||
|
ps.setString(1, "Willi Winzig");
|
||||||
|
ps.setString(2, "Willi's Wille");
|
||||||
|
ps.setDate(3, Date.valueOf("2011-05-16"));
|
||||||
|
ps.setInt(4, 432);
|
||||||
|
ps.setDouble(5, 32.95);
|
||||||
|
ps.addBatch();
|
||||||
|
|
||||||
|
ps.setString(1, "Anton Antonius");
|
||||||
|
ps.setString(2, "Anton's Alarm");
|
||||||
|
ps.setDate(3, Date.valueOf("2009-10-01"));
|
||||||
|
ps.setInt(4, 123);
|
||||||
|
ps.setDouble(5, 98.76);
|
||||||
|
ps.addBatch();
|
||||||
|
|
||||||
|
connection.setAutoCommit(false);
|
||||||
|
ps.executeBatch();
|
||||||
|
connection.setAutoCommit(true);
|
||||||
|
|
||||||
|
ResultSet rs = stmt.executeQuery("SELECT * FROM books;");
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.println("Autor = " + rs.getString("author"));
|
||||||
|
System.out.println("Titel = " + rs.getString("title"));
|
||||||
|
System.out.println("Erscheinungsdatum = "
|
||||||
|
+ rs.getDate("publication"));
|
||||||
|
System.out.println("Seiten = " + rs.getInt("pages"));
|
||||||
|
System.out.println("Preis = " + rs.getDouble("price"));
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
connection.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Couldn't handle DB-Query");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// PlayerDBController dbc = PlayerDBController.getInstance();
|
||||||
|
// dbc.initDBConnection();
|
||||||
|
// dbc.handleDB();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
@ -6,16 +6,16 @@ import java.io.FileNotFoundException;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Stein {
|
public class Stein {
|
||||||
SteineTyp Block;
|
//SteineTyp Block;
|
||||||
private int x = 4, y = -2;
|
private int x = 4, y = 0;
|
||||||
private int normal = 850;
|
private int normal = 850;
|
||||||
private int fast = 50;
|
private int fast = 50;
|
||||||
private int rotation;
|
// private int rotation;
|
||||||
private long beginTime;
|
private long beginTime;
|
||||||
private int delayTime = normal;
|
private int delayTime = normal;
|
||||||
private int deltax = 0;
|
private int deltax = 0;
|
||||||
private boolean collision = false;
|
private boolean collision = false;
|
||||||
private int size;
|
// private int size;
|
||||||
|
|
||||||
public static final int BOARD_WIDTH = 11;
|
public static final int BOARD_WIDTH = 11;
|
||||||
public static final int BOARD_HEIGHT = 20;
|
public static final int BOARD_HEIGHT = 20;
|
||||||
@ -25,71 +25,12 @@ public class Stein {
|
|||||||
private Board board;
|
private Board board;
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
public Stein() {
|
public Stein(int[][] coords,Board board,Color color) {
|
||||||
/* this.coords = coords;
|
this.coords = coords;
|
||||||
this.board = board;
|
this.board = board;
|
||||||
this.color = color;*/
|
this.color = color;
|
||||||
//Ewen Kerbs still in progress
|
|
||||||
// Hier werden die verschiedene steine auf deren typ "analysiert" und dann ausgewertet wie viel platz die aufm spielbrett benötigen
|
|
||||||
Block = SteineTyp.randomSteine();
|
|
||||||
switch (Block)
|
|
||||||
{
|
|
||||||
case I:
|
|
||||||
size = 4;
|
|
||||||
break;
|
|
||||||
case O:
|
|
||||||
size =2;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
size =3;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ewen Kerbs still in progress
|
|
||||||
public int[][][] BlockBuilder(SteineTyp type) throws FileNotFoundException {
|
|
||||||
int[][][] size;
|
|
||||||
switch(type)
|
|
||||||
{
|
|
||||||
case I:
|
|
||||||
/* 3 dimensionales array um die rotations zu erkennen + */
|
|
||||||
size = new int[4][4][4];
|
|
||||||
break;
|
|
||||||
case O:
|
|
||||||
size = new int[4][2][2];
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
size = new int[4][3][3];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
File file = new File("Steine/"+type+".txt");
|
|
||||||
Scanner leser = new Scanner(file);
|
|
||||||
//i= 4 verschiedene stein varianten (1=0° 2=90° 3=180°4=270°)
|
|
||||||
for (int varianten = 0; varianten < 4 ; varianten++) {
|
|
||||||
for (int j = 0; j < size[0].length; j++) {
|
|
||||||
if(leser.hasNext())
|
|
||||||
{
|
|
||||||
String[] srow = leser.next().split("");
|
|
||||||
int[] row = new int[size[0].length];
|
|
||||||
for (int k = 0; k <row.length; k++)
|
|
||||||
{
|
|
||||||
row[j]= Integer.valueOf(srow[j]);
|
|
||||||
size[varianten][j][k] =row[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rotate()
|
|
||||||
{
|
|
||||||
rotation++;
|
|
||||||
if(rotation==4)
|
|
||||||
{
|
|
||||||
rotation=0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setX(int x) {
|
public void setX(int x) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
@ -103,7 +44,6 @@ public class Stein {
|
|||||||
this.x = 2;
|
this.x = 2;
|
||||||
this.y = 0;
|
this.y = 0;
|
||||||
collision = false;
|
collision = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Kollisons abfrage
|
//Kollisons abfrage
|
||||||
@ -138,7 +78,7 @@ public class Stein {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deltax = 0;
|
deltax = 0;
|
||||||
|
// stapeln
|
||||||
if (System.currentTimeMillis() - beginTime > delayTime) {
|
if (System.currentTimeMillis() - beginTime > delayTime) {
|
||||||
if (!(y + 1+coords.length > BOARD_HEIGHT)) {
|
if (!(y + 1+coords.length > BOARD_HEIGHT)) {
|
||||||
for (int row = 0; row < coords.length; row++) {
|
for (int row = 0; row < coords.length; row++) {
|
||||||
|
85
src/Tetris/TetrisMenue.form
Normal file
85
src/Tetris/TetrisMenue.form
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Tetris.TetrisMenue">
|
||||||
|
<grid id="27dc6" binding="tetrisMenuePanel" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
<margin top="20" left="20" bottom="40" right="40"/>
|
||||||
|
<constraints>
|
||||||
|
<xy x="20" y="20" width="576" height="536"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<foreground color="-1"/>
|
||||||
|
</properties>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<component id="dca30" class="javax.swing.JLabel">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="10" fill="0" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<horizontalAlignment value="0"/>
|
||||||
|
<text value="Highscores:"/>
|
||||||
|
<verticalAlignment value="3"/>
|
||||||
|
<verticalTextPosition value="3"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<scrollpane id="b4fda">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="2" row-span="2" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="256" height="287"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<component id="c58cf" class="javax.swing.JList" binding="list1" custom-create="true" default-binding="true">
|
||||||
|
<constraints/>
|
||||||
|
<properties/>
|
||||||
|
</component>
|
||||||
|
</children>
|
||||||
|
</scrollpane>
|
||||||
|
<component id="96af" class="javax.swing.JTextField" binding="spielerNameTextField">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="200" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<horizontalAlignment value="2"/>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="84141" class="javax.swing.JLabel">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="30" height="20"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Spielername:"/>
|
||||||
|
<verticalAlignment value="0"/>
|
||||||
|
<verticalTextPosition value="0"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="84295" class="javax.swing.JButton" binding="spielStartenButton" default-binding="true">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="9" fill="0" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="80" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Spiel starten"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="3d5e3" class="javax.swing.JLabel" binding="tetrisBildLabel" custom-create="true">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<horizontalAlignment value="2"/>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</form>
|
58
src/Tetris/TetrisMenue.java
Normal file
58
src/Tetris/TetrisMenue.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package Tetris;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class TetrisMenue extends JFrame {
|
||||||
|
private JTextField spielerNameTextField;
|
||||||
|
private JButton spielStartenButton;
|
||||||
|
private JPanel tetrisMenuePanel;
|
||||||
|
private JLabel tetrisBildLabel;
|
||||||
|
private JList list1;
|
||||||
|
|
||||||
|
public TetrisMenue(String title) {
|
||||||
|
super(title);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
this.setPreferredSize(new Dimension(640,480));
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
this.setContentPane(tetrisMenuePanel);
|
||||||
|
this.pack();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createUIComponents() {
|
||||||
|
// Bild zu JPanel hinzufügen (https://stackoverflow.com/questions/299495/how-to-add-an-image-to-a-jpanel)
|
||||||
|
try{
|
||||||
|
BufferedImage myPicture = ImageIO.read(new File("tetris.png"));
|
||||||
|
tetrisBildLabel = new JLabel(new ImageIcon(myPicture.getScaledInstance(300,125,Image.SCALE_SMOOTH)));
|
||||||
|
//add(tetrisBildLabel);
|
||||||
|
}
|
||||||
|
catch (IOException exception)
|
||||||
|
{
|
||||||
|
System.out.println("Fehler bei laden vom Tetris Bild: " + exception.getMessage());
|
||||||
|
}
|
||||||
|
java.util.List<Player> playerList = PlayerDBController.getInstance().holeSpielerDaten();
|
||||||
|
Player[] players = playerList.toArray(new Player[0]);
|
||||||
|
Arrays.sort(players);
|
||||||
|
list1 = new JList(players);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getSpielStartenButton() {
|
||||||
|
return spielStartenButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JTextField getSpielerNameTextField() {
|
||||||
|
return spielerNameTextField;
|
||||||
|
}
|
||||||
|
}
|
BIN
tetris.png
Normal file
BIN
tetris.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 138 KiB |
BIN
userhighscore.db
BIN
userhighscore.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user