Compare commits
51 Commits
848cdbe5d8
...
master
Author | SHA1 | Date | |
---|---|---|---|
4e2366e374 | |||
![]() |
744c17517b | ||
![]() |
8949a699ef | ||
![]() |
6db3fab008 | ||
3817308e0c | |||
![]() |
89071d4399 | ||
![]() |
1099015cfc | ||
![]() |
ca70adbf32 | ||
![]() |
fde0151bd8 | ||
![]() |
aebe1f9c0c | ||
![]() |
f3ae86d08a | ||
![]() |
c5b6896038 | ||
![]() |
39d721462c | ||
bf200a06ba | |||
33edfa8215 | |||
![]() |
58bbbcf2f6 | ||
![]() |
da39719fa4 | ||
![]() |
bb346648fb | ||
![]() |
58cd1d05f0 | ||
![]() |
ce85efaf76 | ||
89a3585544 | |||
921b25d601 | |||
![]() |
75096139ac | ||
![]() |
b607886184 | ||
![]() |
9194320d79 | ||
![]() |
8d0ab1bf7b | ||
![]() |
11eef601e0 | ||
![]() |
4d0f9a10ba | ||
![]() |
d8d6b9d33c | ||
![]() |
e451450be6 | ||
![]() |
c4a4708ab1 | ||
![]() |
5e7f03746e | ||
![]() |
626f2124ea | ||
![]() |
7af2cf0739 | ||
![]() |
bd9d70383d | ||
![]() |
1839154769 | ||
![]() |
2215dc0ba7 | ||
![]() |
23c795a2e7 | ||
![]() |
f3b15428e7 | ||
![]() |
936f6670e9 | ||
5a340867ae | |||
![]() |
124a4649b0 | ||
ce68473591 | |||
![]() |
2d3e3289f9 | ||
3b7d5d8dad | |||
d40cbe958c | |||
52635342be | |||
6ed9390f77 | |||
74d8c1e6d4 | |||
5241d3f900 | |||
![]() |
c0e68d0b92 |
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@@ -0,0 +1 @@
|
||||
GameGui.java
|
1
.idea/Tetris.iml
generated
1
.idea/Tetris.iml
generated
@@ -7,5 +7,6 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" exported="" name="sqlite-jdbc-3.36.0.3" level="project" />
|
||||
</component>
|
||||
</module>
|
11
.idea/aws.xml
generated
Normal file
11
.idea/aws.xml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="accountSettings">
|
||||
<option name="activeRegion" value="us-east-1" />
|
||||
<option name="recentlyUsedRegions">
|
||||
<list>
|
||||
<option value="us-east-1" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
9
.idea/libraries/sqlite_jdbc_3_36_0_3.xml
generated
Normal file
9
.idea/libraries/sqlite_jdbc_3_36_0_3.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<component name="libraryTable">
|
||||
<library name="sqlite-jdbc-3.36.0.3">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/sqlite-jdbc-3.36.0.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
16
Steine/I.txt
Normal file
16
Steine/I.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
0000
|
||||
1111
|
||||
0000
|
||||
0000
|
||||
0010
|
||||
0010
|
||||
0010
|
||||
0010
|
||||
0000
|
||||
0000
|
||||
1111
|
||||
0000
|
||||
0100
|
||||
0100
|
||||
0100
|
||||
0100
|
12
Steine/J.txt
Normal file
12
Steine/J.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
100
|
||||
111
|
||||
000
|
||||
011
|
||||
010
|
||||
010
|
||||
000
|
||||
111
|
||||
001
|
||||
010
|
||||
010
|
||||
110
|
12
Steine/L.txt
Normal file
12
Steine/L.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
001
|
||||
111
|
||||
000
|
||||
010
|
||||
010
|
||||
011
|
||||
000
|
||||
111
|
||||
100
|
||||
110
|
||||
010
|
||||
010
|
8
Steine/O.txt
Normal file
8
Steine/O.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
11
|
||||
11
|
||||
11
|
||||
11
|
||||
11
|
||||
11
|
||||
11
|
||||
11
|
12
Steine/S.txt
Normal file
12
Steine/S.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
011
|
||||
110
|
||||
000
|
||||
010
|
||||
011
|
||||
001
|
||||
000
|
||||
011
|
||||
110
|
||||
100
|
||||
110
|
||||
010
|
12
Steine/T.txt
Normal file
12
Steine/T.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
010
|
||||
111
|
||||
000
|
||||
011
|
||||
010
|
||||
000
|
||||
111
|
||||
010
|
||||
010
|
||||
110
|
||||
010
|
||||
|
13
Steine/Z.txt
Normal file
13
Steine/Z.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
110
|
||||
011
|
||||
000
|
||||
001
|
||||
011
|
||||
010
|
||||
000
|
||||
110
|
||||
011
|
||||
010
|
||||
110
|
||||
100
|
||||
|
BIN
out/production/Tetris/Tetris/Board$1.class
Normal file
BIN
out/production/Tetris/Tetris/Board$1.class
Normal file
Binary file not shown.
BIN
out/production/Tetris/Tetris/Board.class
Normal file
BIN
out/production/Tetris/Tetris/Board.class
Normal file
Binary file not shown.
BIN
out/production/Tetris/Tetris/GameGui.class
Normal file
BIN
out/production/Tetris/Tetris/GameGui.class
Normal file
Binary file not shown.
@@ -1,2 +0,0 @@
|
||||
public class Tetris {
|
||||
}
|
181
src/Tetris/Board.java
Normal file
181
src/Tetris/Board.java
Normal file
@@ -0,0 +1,181 @@
|
||||
package Tetris;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.Random;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
//Ewen Kerbs,Zhe Wang-Holkenbrink
|
||||
public class Board extends JPanel implements KeyListener {
|
||||
private static int FPS = 60;
|
||||
private static int delay = FPS / 1000;
|
||||
|
||||
//Spielfeld Breite und Länge
|
||||
public static final int BOARD_WIDTH = 11;
|
||||
public static final int BOARD_HEIGHT = 20;
|
||||
public static final int BLOCK_SIZE = 30;
|
||||
|
||||
private Timer loop;
|
||||
private Color[][] board = new Color[BOARD_HEIGHT][BOARD_WIDTH];
|
||||
private Stein[] steine = new Stein[7];
|
||||
private Stein currenStein;
|
||||
private Random ran;
|
||||
private boolean gameOver = false;
|
||||
|
||||
// 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"),};
|
||||
|
||||
|
||||
// Steinform
|
||||
public Board() {
|
||||
ran = new Random();
|
||||
steine[0] = new Stein(new int[][]{
|
||||
{1, 1, 1},
|
||||
{0, 1, 0},
|
||||
|
||||
}, this, colors[0]);
|
||||
steine[1] = new Stein(new int[][]{
|
||||
{1, 1},
|
||||
{1, 1}
|
||||
|
||||
}, this, colors[1]);
|
||||
steine[2] = new Stein(new int[][]{
|
||||
{1, 1, 1},
|
||||
{1, 0, 0}
|
||||
}, this, colors[2]);
|
||||
steine[3] = new Stein(new int[][]{
|
||||
{1, 1, 1},
|
||||
{0, 0, 1}
|
||||
}, this, colors[3]);
|
||||
steine[4] = new Stein(new int[][]{
|
||||
{0, 1, 1},
|
||||
{1, 1, 0}
|
||||
}, this, colors[4]);
|
||||
steine[5] = new Stein(new int[][]{
|
||||
{1, 1, 0},
|
||||
{0, 1, 1}
|
||||
}, this, colors[5]);
|
||||
steine[6] = new Stein(new int[][]{
|
||||
{1, 1, 1, 1}
|
||||
}, this, colors[6]);
|
||||
|
||||
currenStein = steine[0];
|
||||
|
||||
loop = new Timer(delay, new ActionListener() {
|
||||
int n = 0;
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
update();
|
||||
repaint();
|
||||
//System.out.println(n++);
|
||||
}
|
||||
});
|
||||
loop.start();
|
||||
}
|
||||
|
||||
private void update() {
|
||||
if (spielbeenden()) {
|
||||
loop.stop();
|
||||
return;
|
||||
}
|
||||
currenStein.update();
|
||||
}
|
||||
|
||||
private boolean spielbeenden() {
|
||||
if (!gameOver) {
|
||||
for (int i = 0; i < currenStein.getCoords().length; i++) {
|
||||
for (int j = 0; j < currenStein.getCoords()[0].length; j++) {
|
||||
if (currenStein.getCoords()[i][j] != 0) {
|
||||
if (board[currenStein.getY() + i][currenStein.getX() + j] != null) {
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public void setCurrenStein() {
|
||||
currenStein = steine[ran.nextInt(steine.length)];
|
||||
currenStein.reset();
|
||||
}
|
||||
|
||||
public Stein getCurrenStein() {
|
||||
return currenStein;
|
||||
}
|
||||
|
||||
public Color[][] getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
g.setColor(Color.lightGray);
|
||||
|
||||
currenStein.render(g);
|
||||
|
||||
for (int row = 0; row < board.length; row++) {
|
||||
for (int col = 0; col < board[row].length; col++) {
|
||||
if (board[row][col] != null) {
|
||||
g.setColor(board[row][col]);
|
||||
//
|
||||
g.fillRect(col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//linien mit 2-Forschleife
|
||||
|
||||
|
||||
for (int row = 0; row < BOARD_HEIGHT + 1; row++) {
|
||||
g.drawLine(0, BLOCK_SIZE * row, BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * row);
|
||||
}
|
||||
for (int col = 0; col < BOARD_WIDTH + 1; col++) {
|
||||
g.drawLine(col * BLOCK_SIZE, 0, col * BLOCK_SIZE, BLOCK_SIZE * BOARD_HEIGHT);}
|
||||
if (gameOver) {
|
||||
String gameOverString = "GAME OVER";
|
||||
g.setColor(Color.GREEN);
|
||||
g.setFont(new Font("SANS_SERIF", Font.BOLD, 30));
|
||||
g.drawString(gameOverString, 50, GameGui.height / 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
//Tastatur eingabe zur steinbewegung
|
||||
//wang/kerbs
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
|
||||
currenStein.speedup();
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_D) {
|
||||
currenStein.moveRight();
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_A) {
|
||||
currenStein.moveLeft();
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_W) {
|
||||
currenStein.steinRotiert();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
|
||||
currenStein.speedDown();
|
||||
}
|
||||
}
|
||||
}
|
42
src/Tetris/Game.java
Normal file
42
src/Tetris/Game.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package Tetris;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
public class Game {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Datenbankverbindung herstellen
|
||||
PlayerDBController playerDBController = PlayerDBController.getInstance();
|
||||
playerDBController.initDBConnection();
|
||||
List<Player> playerListe = playerDBController.holeSpielerDaten();
|
||||
JFrame tetrisMenueJframe = new TetrisMenue("Tetris");
|
||||
tetrisMenueJframe.setVisible(true);
|
||||
TetrisMenue tetrisMenue = (TetrisMenue) tetrisMenueJframe;
|
||||
tetrisMenue.getSpielStartenButton().addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String spielerName = tetrisMenue.getSpielerNameTextField().getText();
|
||||
new GameGui(spielerName);
|
||||
PlayerDBController.getInstance().speichereSpieler(new Player(spielerName,0));
|
||||
tetrisMenueJframe.setVisible(false);
|
||||
}
|
||||
});
|
||||
// Menue menue = new Menue();
|
||||
// final Player[] aktuellerSpieler = new Player[1];
|
||||
// menue.getButton().addActionListener(new java.awt.event.ActionListener() {
|
||||
// @Override
|
||||
// public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
// aktuellerSpieler[0] = new Player(menue.getTextField().getText(), 0);
|
||||
// menue.getParent().setVisible(false);
|
||||
// //new GameGui(name);
|
||||
// playerDBController.speichereSpieler(aktuellerSpieler[0]);
|
||||
// }
|
||||
// });
|
||||
|
||||
//String playername = menue.getName();
|
||||
//new GameGui(playername);
|
||||
}
|
||||
}
|
@@ -1,19 +1,52 @@
|
||||
package Tetris;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class GameGui {
|
||||
public static final int width= 445, height =629;
|
||||
private JFrame window;
|
||||
public static final int width= 550, height =650;
|
||||
private Board board;
|
||||
private JFrame frame;
|
||||
|
||||
public GameGui(){
|
||||
window = new JFrame("Tetris");
|
||||
window.setSize(width,height);
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.setResizable(false);
|
||||
window.setVisible(true);
|
||||
//Zhe Wang-Holkenbrink
|
||||
public GameGui(String playername){
|
||||
frame = new JFrame("Tetris " + playername);
|
||||
JPanel panel = new JPanel();
|
||||
panel.setBorder(BorderFactory.createMatteBorder(500, 10, 10, 50,Color.black));
|
||||
|
||||
//Button
|
||||
JButton buttonStop = new JButton("Pause");
|
||||
buttonStop.setPreferredSize( new Dimension(100,30));
|
||||
JButton buttonVerlassen = new JButton("verlassen");
|
||||
buttonVerlassen.setPreferredSize( new Dimension(100,30));
|
||||
|
||||
|
||||
|
||||
//Button untereinander
|
||||
panel.setLayout(new GridLayout(0,1));
|
||||
panel.setBackground(Color.YELLOW);
|
||||
panel.add(buttonStop);
|
||||
panel.add(buttonVerlassen);
|
||||
|
||||
|
||||
frame.add(panel,BorderLayout.EAST);
|
||||
frame.setSize(width,height);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setResizable(false);
|
||||
frame.setVisible(true);
|
||||
board = new Board();
|
||||
frame.addKeyListener(board);
|
||||
frame.add(board,BorderLayout.CENTER);
|
||||
frame.requestFocusInWindow();
|
||||
|
||||
// Wenn man Button "Verlassen" drückt,schlißt das Spielfenster.
|
||||
ActionListener al = ev -> {if(ev.getSource() == buttonVerlassen)
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
public static void main(String [] args){
|
||||
new GameGui();
|
||||
};
|
||||
|
||||
buttonVerlassen.addActionListener(al);
|
||||
}
|
||||
|
||||
}
|
||||
|
57
src/Tetris/Menue.java
Normal file
57
src/Tetris/Menue.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package Tetris;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import java.awt.*;
|
||||
|
||||
public class Menue {
|
||||
|
||||
private String name;
|
||||
|
||||
private JFrame parent;
|
||||
|
||||
public JFrame getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(JFrame parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
private JButton button;
|
||||
|
||||
public JButton getButton() {
|
||||
return button;
|
||||
}
|
||||
|
||||
public void setButton(JButton button) {
|
||||
this.button = button;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Menue(){
|
||||
|
||||
parent = new JFrame("Tetris Menü");
|
||||
button = new JButton();
|
||||
button.setText("Namen Eingeben");
|
||||
button.setPreferredSize(new Dimension(400,500));
|
||||
button.setBackground(Color.cyan);
|
||||
parent.add(button);
|
||||
parent.pack();
|
||||
parent.setVisible(true);
|
||||
|
||||
}
|
||||
//public static void main(final String[] args) {
|
||||
// new Menue();
|
||||
//}
|
||||
}
|
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();
|
||||
// }
|
||||
}
|
||||
|
216
src/Tetris/Stein.java
Normal file
216
src/Tetris/Stein.java
Normal file
@@ -0,0 +1,216 @@
|
||||
package Tetris;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Stein {
|
||||
|
||||
|
||||
//SteineTyp Block;
|
||||
private int x = 4, y = 0;
|
||||
private int normal = 850;
|
||||
private int fast = 50;
|
||||
// private int rotation;
|
||||
private long beginTime;
|
||||
private int delayTime = normal;
|
||||
private int deltax = 0;
|
||||
private boolean collision = false;
|
||||
// private int size;
|
||||
|
||||
public static final int BOARD_WIDTH = 11;
|
||||
public static final int BOARD_HEIGHT = 20;
|
||||
public static final int BLOCK_SIZE = 30;
|
||||
|
||||
private int[][] coords;
|
||||
private Board board;
|
||||
private Color color;
|
||||
|
||||
public Stein(int[][] coords,Board board,Color color) {
|
||||
this.coords = coords;
|
||||
this.board = board;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.x = 2;
|
||||
this.y = 0;
|
||||
collision = false;
|
||||
}
|
||||
|
||||
//Kollisons abfrage
|
||||
public void update() {
|
||||
if (collision) {
|
||||
for (int row = 0; row < coords.length; row++) {
|
||||
for (int col = 0; col < coords[0].length; col++) {
|
||||
if (coords[row][col] != 0) {
|
||||
board.getBoard()[y + row][x + col] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
linieLöschen();
|
||||
board.setCurrenStein();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean moveX=true;
|
||||
if (!(x + deltax + coords[0].length > 11) && !(x + deltax < 0)) {
|
||||
for (int row = 0; row < coords.length; row++) {
|
||||
for (int col = 0; col < coords[row].length; col++) {
|
||||
if(coords[row][col] !=0) {
|
||||
if (board.getBoard()[y + row][x + deltax + col] != null) {
|
||||
moveX = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(moveX){
|
||||
x += deltax;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
deltax = 0;
|
||||
// stapeln
|
||||
if (System.currentTimeMillis() - beginTime > delayTime) {
|
||||
if (!(y + 1+coords.length > BOARD_HEIGHT)) {
|
||||
for (int row = 0; row < coords.length; row++) {
|
||||
for (int col = 0; col < coords[row].length; col++) {
|
||||
if (coords[row][col] != 0){
|
||||
|
||||
if (board.getBoard()[y +1 + row][x - deltax + col] != null) {
|
||||
collision = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!collision) {
|
||||
y++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
collision = true;
|
||||
}
|
||||
|
||||
beginTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void linieLöschen(){
|
||||
int bollomLine = board.getBoard().length - 1;
|
||||
for(int topLine = board.getBoard().length - 1; topLine > 0; topLine--){
|
||||
int anZahl = 0;
|
||||
for(int col = 0; col < board.getBoard()[0].length; col++){
|
||||
if(board.getBoard()[topLine][col] != null) {
|
||||
anZahl++;
|
||||
}
|
||||
board.getBoard()[bollomLine][col] = board.getBoard()[topLine][col];
|
||||
}
|
||||
if(anZahl < board.getBoard()[0].length){
|
||||
bollomLine--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void render(Graphics g){
|
||||
for(int row=0;row< coords.length;row++){
|
||||
for(int col = 0;col< coords[0].length;col++){
|
||||
if(coords[row][col] !=0){
|
||||
g.setColor(color);
|
||||
//
|
||||
g.fillRect(col*BLOCK_SIZE+x*BLOCK_SIZE,row*BLOCK_SIZE+y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/*Kerbs Ewen
|
||||
Hier wird der stein rotiert dazu wurden die hilfs methoden : datenarrayflippen, steineflippen implementiert
|
||||
diese helfen das komplexe steinrotiert verständlich zu machen.
|
||||
Beim datenarrayflippen wird einmal das neue array ausgerechnet wie es auszusehen hat.
|
||||
Da aber die bei dieser formatierung das array falsch herum aufgebaut wird benötigt man die hilfsmethode steineflippen
|
||||
Diese dreht dreht das array um sodas der stein richtig gezeigt werden kann
|
||||
|
||||
* */
|
||||
public void steinRotiert()
|
||||
{
|
||||
int[][] rotierterstein = null;
|
||||
rotierterstein = datenarrayflippen(coords);
|
||||
rotierterstein = steineflippen(rotierterstein);
|
||||
//grenzen ueberprufeung damit wird gewährleistet das der stein nicht aus dem array/koordiantensystem/spielbrett raus geht
|
||||
if ((x + rotierterstein[0].length > 10) || (y + rotierterstein.length > 20)) {
|
||||
return;
|
||||
}
|
||||
//ueberpruefung ob bereits ein stein im weg ist sollte dies sein kann der stein sich nicht drehen
|
||||
for (int i = 0; i < rotierterstein.length; i++) {
|
||||
for (int j = 0; j < rotierterstein[i].length; j++) {
|
||||
if(rotierterstein[i][j] !=0)
|
||||
{
|
||||
if (board.getBoard()[y+i][x+j] !=null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
coords = rotierterstein;
|
||||
}
|
||||
|
||||
private int[][] datenarrayflippen(int[][] altesArray)
|
||||
{
|
||||
int[][] updatedArray = new int[altesArray[0].length][altesArray.length];
|
||||
for (int i = 0; i < altesArray.length; i++) {
|
||||
for (int j = 0; j < altesArray[0].length; j++) {
|
||||
updatedArray[j][i] = altesArray[i][j];
|
||||
}
|
||||
}
|
||||
return updatedArray;
|
||||
}
|
||||
|
||||
private int[][] steineflippen(int[][] rotierenedesArray)
|
||||
{
|
||||
int mittelpunkt = rotierenedesArray.length / 2;
|
||||
for (int i = 0; i < mittelpunkt; i++) {
|
||||
int[] rotiertesArray = rotierenedesArray[i];
|
||||
rotierenedesArray[i] = rotierenedesArray[rotierenedesArray.length - i - 1];
|
||||
rotierenedesArray[rotierenedesArray.length - i - 1] = rotiertesArray;
|
||||
}
|
||||
return rotierenedesArray;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void speedup(){
|
||||
delayTime=fast;
|
||||
}
|
||||
public void speedDown(){
|
||||
delayTime=normal;
|
||||
}
|
||||
public void moveRight(){
|
||||
deltax = 1;
|
||||
}
|
||||
public void moveLeft(){
|
||||
deltax = -1;
|
||||
}
|
||||
public int[][] getCoords() {
|
||||
return coords;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
}
|
8
src/Tetris/Test.java
Normal file
8
src/Tetris/Test.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Tetris;
|
||||
|
||||
public class Test {
|
||||
//test klasse falls probleme mit der datenbank vorhanden sind
|
||||
public static void main(String[] args) {
|
||||
GameGui test = new GameGui("test");
|
||||
}
|
||||
}
|
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;
|
||||
}
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="TetrisTest">
|
||||
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="500" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="2d778" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-14728954"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="86903" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="1" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<font name="72 Black" size="36" style="1"/>
|
||||
<text value="TETRIS"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
@@ -1,5 +0,0 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class TetrisTest {
|
||||
private JPanel panel1;
|
||||
}
|
BIN
tetris.png
Normal file
BIN
tetris.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 138 KiB |
BIN
userhighscore.db
Normal file
BIN
userhighscore.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user