Skip to content

Commit

Permalink
Adding a tile background instead of plain colors. Updating some code …
Browse files Browse the repository at this point in the history
…to Java 1.7. Renaming spanish method names to English. Better pausing/resuming after an attempt of closing the game window.
  • Loading branch information
santiagolizardo committed Feb 28, 2015
1 parent 1823d82 commit 44d3ee6
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 107 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.santiagolizardo.tetriskit</groupId>
<artifactId>TetrisKit</artifactId>
<version>1.0.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>TetrisKit</name>
Expand All @@ -19,8 +19,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
<version>3.1</version>
</plugin>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/santiagolizardo/tetriskit/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public class Constants {

public static final String GAME_TITLE = "TetrisKit";
public static final String GAME_VERSION = "v1.1";
public static final String GAME_VERSION = "v1.2";
public static final String GAME_URL = "https://github.com/Ponup/tetriskit";

public final static short BLOCK_SIZE = 15;

Expand Down
43 changes: 24 additions & 19 deletions src/main/java/com/santiagolizardo/tetriskit/MainGUI.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
package com.santiagolizardo.tetriskit;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.santiagolizardo.tetriskit.logic.Engine;
import com.santiagolizardo.tetriskit.panels.CreditsPanel;
import com.santiagolizardo.tetriskit.panels.GameSetupPanel;
import com.santiagolizardo.tetriskit.panels.MainPanel;
import com.santiagolizardo.tetriskit.panels.ScoresPanel;
import com.santiagolizardo.tetriskit.resources.SoundLoader;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Application main entry point.
*/
@SuppressWarnings("serial")
public class MainGUI extends JFrame implements ActionListener {

private static final Logger logger = Logger.getLogger(MainGUI.class.getName());

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainGUI tetris = new MainGUI();
tetris.setVisible(true);
Expand All @@ -50,24 +56,16 @@ public MainGUI() {
ScoresPanel scoresPanel = new ScoresPanel();
scoresPanel.setActionListener(this);

CreditsPanel creditsPanel = new CreditsPanel();
creditsPanel.setActionListener(this);

setupPanel = new GameSetupPanel();
setupPanel.getButtonStart().addActionListener(this);
setupPanel.getButtonBack().addActionListener(this);

cardLayout = new CardLayout();

container = (JPanel) getContentPane();
container.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
container.setOpaque(true);
container.setBackground(new Color(0xFF2A00));

container.setLayout(cardLayout);
container.add(mainPanel, "main");
container.add(scoresPanel, "scores");
container.add(creditsPanel, "credits");
container.add(setupPanel, "setup");

pack();
Expand All @@ -77,13 +75,14 @@ public MainGUI() {
setLocationRelativeTo(null);
}

@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == setupPanel.getButtonStart()) {
short boardWidth = setupPanel.getAltoTablero();
short boardHeight = setupPanel.getAnchoTablero();
short boardWidth = setupPanel.getBoardHeight();
short boardHeight = setupPanel.getBoardWidth();
Engine engine = new Engine(boardHeight, boardWidth);
engine.setVisible(true);
engine.start();
engine.turnOn();
} else {
String action = event.getActionCommand();
if ("SHOW_MAIN".equals(event.getActionCommand())) {
Expand All @@ -96,11 +95,7 @@ public void actionPerformed(ActionEvent event) {
setTitle("View scores - Tetris kit");
cardLayout.show(container, "scores");
} else if ("btnCredits".equals(action)) {
setTitle("Credits - Tetris kit");
cardLayout.show(container, "credits");
} else if ("btnGamePieces".equals(action)) {
setTitle("Pieces - Tetris kit");
cardLayout.show(container, "pieces");
openGameUrl();
} else if ("btnGameOptions".equals(action)) {
setTitle("Game options - Tetris kit");
cardLayout.show(container, "preferences");
Expand All @@ -110,4 +105,14 @@ public void actionPerformed(ActionEvent event) {
}
}
}

private void openGameUrl() {
try {
URL url = new URL(Constants.GAME_URL);
Desktop desktop = Desktop.getDesktop();
desktop.browse(url.toURI());
} catch (URISyntaxException | IOException e) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public void update() {
repaint();
}

public void paint(Graphics g) {
// Double buffering
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image, 0, 0, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public void setPiece(short[][] piece) {
repaint();
}

public void paint(Graphics g) {
super.paint(g);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image, 0, 0, this);
}
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/com/santiagolizardo/tetriskit/logic/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
import com.santiagolizardo.tetriskit.panels.StatusPanel;
import com.santiagolizardo.tetriskit.resources.Sounds;
import com.santiagolizardo.tetriskit.resources.SoundLoader;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Most of the game logic is managed by this class.
*/
@SuppressWarnings("serial")
public class Engine extends JDialog implements Runnable, KeyListener {

private final static Logger logger = Logger.getLogger(Engine.class.getName());

private Thread thread;

public BoardCanvas board;
Expand All @@ -44,20 +49,26 @@ public Engine(short boardWidth, short boardHeight) {
* Constants.BLOCK_SIZE + 40);

setResizable(false);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

gameState = new GameState(boardWidth, boardHeight);

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if( gameState.getState().equals(GameStates.StateFinished ) ) {
dispose();
return;
}
gameState.setState(GameStates.StatePaused);
int option = JOptionPane.showConfirmDialog(Engine.this,
"Are you sure you want to stop the game now?");
int option = JOptionPane.showOptionDialog(Engine.this,
"Are you sure you want to stop the game now?", "Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (option == JOptionPane.YES_OPTION) {
gameState.setState(GameStates.StateStopped);
super.windowClosing(e);
dispose();
} else {
gameState.setState(GameStates.StatePlaying);
turnOn();
}
}
});
Expand Down Expand Up @@ -102,17 +113,16 @@ private void organizeLayout() {
container.add(statusPanel);
}

public void start() {
if (thread == null) {
thread = new Thread(this);
thread.setPriority(Thread.MAX_PRIORITY);
thread.setName(getClass().getName());
thread.start();
public void turnOn() {
gameState.setState(GameStates.StatePlaying);

gameState.setState(GameStates.StatePlaying);
}
thread = new Thread(this);
thread.setPriority(Thread.MAX_PRIORITY);
thread.setName(getClass().getName());
thread.start();
}

@Override
public void run() {
try {
while (gameState.getState().equals(GameStates.StatePlaying)) {
Expand All @@ -139,8 +149,8 @@ public void run() {
JOptionPane.showMessageDialog(this, "Game over.", "Tetris",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
} catch (InterruptedException | HeadlessException e) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}

Expand All @@ -152,6 +162,7 @@ private void updateStats() {
statusPanel.refresh(gameState);
}

@Override
public void keyPressed(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_SPACE:
Expand All @@ -172,9 +183,11 @@ public void keyPressed(KeyEvent event) {
}
}

@Override
public void keyReleased(KeyEvent event) {
}

@Override
public void keyTyped(KeyEvent event) {
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/santiagolizardo/tetriskit/panels/BasePanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.santiagolizardo.tetriskit.panels;

import com.santiagolizardo.tetriskit.MainGUI;
import com.santiagolizardo.tetriskit.resources.ImageLoader;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
Expand Down Expand Up @@ -28,4 +35,26 @@ public BasePanel() {
setOpaque(true);
setBackground(bgColor);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int x = 0, y = 0;

int TILE_WIDTH = 256;
int TILE_HEIGHT = 256;

try {
Image image = new ImageLoader().loadImage("tile.png");
for (x = 0; x < getWidth(); x += TILE_WIDTH) {
for (y = 0; y < getHeight(); y += TILE_HEIGHT) {
g.drawImage(image, x, y, this);
}
}

} catch (IOException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

This file was deleted.

Loading

0 comments on commit 44d3ee6

Please sign in to comment.