diff --git a/.idea/Tetris.iml b/.idea/Tetris.iml index c90834f..70c8c92 100644 --- a/.idea/Tetris.iml +++ b/.idea/Tetris.iml @@ -7,5 +7,14 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/Tetris/Board.java b/src/Tetris/Board.java index 1fd315e..ce96f18 100644 --- a/src/Tetris/Board.java +++ b/src/Tetris/Board.java @@ -32,7 +32,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d public Board() { - ran = new Random(); + ran = new Random(); steine[0]= new Stein(new int[][]{ {1,1,1}, {0,1,0} @@ -89,6 +89,10 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d currenStein.reset(); } + public Stein getCurrenStein() { + return currenStein; + } + public Color[][] getBoard(){ return board; } @@ -125,7 +129,17 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d @Override public void keyTyped(KeyEvent e) { - +// switch(e.getKeyChar()) { +// case KeyEvent.VK_SPACE: +// currenStein.speedup(); +// break; +// case KeyEvent.VK_A: +// currenStein.moveLeft(); +// break; +// case KeyEvent.VK_D: +// currenStein.moveRight(); +// break; +// } } @Override @@ -133,7 +147,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d if(e.getKeyChar() == KeyEvent.VK_SPACE){ currenStein.speedup(); }else if(e.getKeyChar() == KeyEvent.VK_ENTER){ - currenStein.moveRigth(); + currenStein.moveRight(); } else if(e.getKeyChar() == KeyEvent.VK_ESCAPE){ currenStein.moveLeft(); diff --git a/src/Tetris/Game.java b/src/Tetris/Game.java index 8cb0957..5bc02b5 100644 --- a/src/Tetris/Game.java +++ b/src/Tetris/Game.java @@ -1,24 +1,102 @@ package Tetris; +import java.sql.*; import javax.swing.*; public class Game { - public static void main(String[] args) { - Menue menue = new Menue(); - menue.getButton().addActionListener(new java.awt.event.ActionListener() { - @Override - public void actionPerformed(java.awt.event.ActionEvent evt) { - String name = JOptionPane.showInputDialog(menue.getParent(), - "What is your name?", null); - if(name == null){ - name = "anon"; + 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(); + menue.getButton().addActionListener(new java.awt.event.ActionListener() { + @Override + public void actionPerformed(java.awt.event.ActionEvent evt) { + String name = JOptionPane.showInputDialog(menue.getParent(), + "What is your name?", null); + if(name == null){ + name = "anon"; + } + menue.getParent().setVisible(false); + new GameGui(name); } - menue.getParent().setVisible(false); - new GameGui(name); + }); + String playername = menue.getName(); + 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)"); } - }); - //String playername = menue.getName(); - //new GameGui(playername); + } + 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()); + } + } + } + + 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()); +// } +// } } } diff --git a/src/Tetris/GameGui.java b/src/Tetris/GameGui.java index cbc3a3b..e1a9ec7 100644 --- a/src/Tetris/GameGui.java +++ b/src/Tetris/GameGui.java @@ -13,17 +13,6 @@ public class GameGui { JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createMatteBorder(500, 10, 10, 50,Color.black)); - //Label - //JLabel label = new JLabel(" Ihr Name:"); - //panel.add(label); - //Textfeld - //JTextField tfName = new JTextField("Bitte Name eingeben"); - //tfName.setForeground(Color.white); - // Hintergrundfarbe wird gesetzt - // tfName.setBackground(Color.GRAY); - // Textfeld wird unserem Panel hinzugefügt - //panel.add(tfName); - //Button //JButton button1 = new JButton("Start"); //button1.setPreferredSize( new Dimension(150,50)); @@ -31,10 +20,10 @@ public class GameGui { buttonPause.setPreferredSize( new Dimension(100,30)); JButton buttonScore = new JButton("Score"); buttonScore.setPreferredSize( new Dimension(100,30)); + //Button untereinander panel.setLayout(new GridLayout(0,1)); panel.setBackground(Color.YELLOW); - //panel.add(button1); panel.add(buttonPause); panel.add(buttonScore); @@ -48,8 +37,5 @@ public class GameGui { frame.add(board,BorderLayout.CENTER); frame.requestFocusInWindow(); } - public static void main(String [] args) - { - } } diff --git a/src/Tetris/Menue.java b/src/Tetris/Menue.java index dd9d2fa..699847e 100644 --- a/src/Tetris/Menue.java +++ b/src/Tetris/Menue.java @@ -43,16 +43,15 @@ public class Menue { parent = new JFrame("Tetris Menü"); button = new JButton(); - button.setText("Namen Eingeben"); button.setPreferredSize(new Dimension(400,500)); - button.setBackground(Color.CYAN); + button.setBackground(Color.cyan); parent.add(button); parent.pack(); parent.setVisible(true); } - public static void main(final String[] args) { - new Menue(); - } + //public static void main(final String[] args) { + // new Menue(); + //} } \ No newline at end of file diff --git a/src/Tetris/Stein.java b/src/Tetris/Stein.java index eb34bd9..6e4fbfd 100644 --- a/src/Tetris/Stein.java +++ b/src/Tetris/Stein.java @@ -114,7 +114,7 @@ public void speedup(){ public void speedDown(){ delayTime=normal; } -public void moveRigth(){ +public void moveRight(){ deltax = 1; } public void moveLeft(){ diff --git a/userhighscore.db b/userhighscore.db new file mode 100644 index 0000000..189dbb2 Binary files /dev/null and b/userhighscore.db differ