Added basic welcome screen and some level stuff.
This commit is contained in:
@@ -25,7 +25,6 @@ public class FieldCanvas extends JPanel{
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
private int size;
|
|
||||||
private Game game;
|
private Game game;
|
||||||
private Point src;
|
private Point src;
|
||||||
private Point dst;
|
private Point dst;
|
||||||
@@ -78,13 +77,12 @@ public class FieldCanvas extends JPanel{
|
|||||||
addMouseListener(ad);
|
addMouseListener(ad);
|
||||||
addMouseMotionListener(ad);
|
addMouseMotionListener(ad);
|
||||||
this.game=g;
|
this.game=g;
|
||||||
this.size = game.getSize();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Point getClickPoint(Point globalPos) {
|
private Point getClickPoint(Point globalPos) {
|
||||||
int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight);
|
int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight);
|
||||||
int space = total/size;
|
int space = total/game.getSize();
|
||||||
|
|
||||||
globalPos.translate(-borderLeft, -borderTop);
|
globalPos.translate(-borderLeft, -borderTop);
|
||||||
if(globalPos.x<0 || globalPos.x >total || globalPos.y < 0 || globalPos.y > total) return null;
|
if(globalPos.x<0 || globalPos.x >total || globalPos.y < 0 || globalPos.y > total) return null;
|
||||||
@@ -102,22 +100,22 @@ public class FieldCanvas extends JPanel{
|
|||||||
|
|
||||||
g.translate(borderLeft, borderTop);
|
g.translate(borderLeft, borderTop);
|
||||||
int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight);
|
int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight);
|
||||||
int space = total/size;
|
int space = total/game.getSize();
|
||||||
|
|
||||||
g.setClip(0, 0, total-4,total-4);
|
g.setClip(0, 0, total-4,total-4);
|
||||||
g.fillRect(0, 0, total-4,total-4);
|
g.fillRect(0, 0, total-4,total-4);
|
||||||
|
|
||||||
g.setColor(Color.white);
|
g.setColor(Color.white);
|
||||||
|
|
||||||
for(int i=0; i<=size; i++) {
|
for(int i=0; i<=game.getSize(); i++) {
|
||||||
g.drawLine(0,i*space,total-2,i*space);
|
g.drawLine(0,i*space,total-2,i*space);
|
||||||
g.drawLine(i*space,0,i*space,total-2);
|
g.drawLine(i*space,0,i*space,total-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(game==null) return;
|
if(game==null) return;
|
||||||
|
|
||||||
for(int x=0; x<size; x++) {
|
for(int x=0; x<game.getSize(); x++) {
|
||||||
for(int y=0; y<size; y++) {
|
for(int y=0; y<game.getSize(); y++) {
|
||||||
int colorCode = game.getField()[x][y];
|
int colorCode = game.getField()[x][y];
|
||||||
if(colorCode!=0) {
|
if(colorCode!=0) {
|
||||||
g.setColor(colors[colorCode-1]);
|
g.setColor(colors[colorCode-1]);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ public class Game {
|
|||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
static final int numberOfColors = 5;
|
static final int numberOfColors = 5;
|
||||||
|
static final int linesPerLevel = 40;
|
||||||
|
|
||||||
// Private members
|
// Private members
|
||||||
private int[][] field;
|
private int[][] field;
|
||||||
@@ -24,6 +25,7 @@ public class Game {
|
|||||||
private int freeBlocks;
|
private int freeBlocks;
|
||||||
private int freeMoves;
|
private int freeMoves;
|
||||||
private int numUndos;
|
private int numUndos;
|
||||||
|
private int linesLeft;
|
||||||
private Random rand;
|
private Random rand;
|
||||||
private ArrayList<UpdateListener> updateListeners;
|
private ArrayList<UpdateListener> updateListeners;
|
||||||
|
|
||||||
@@ -37,16 +39,9 @@ public class Game {
|
|||||||
* @author aaron
|
* @author aaron
|
||||||
* @param size
|
* @param size
|
||||||
*/
|
*/
|
||||||
public Game (int size) {
|
public Game (int size) {
|
||||||
/* Initialize variables */
|
|
||||||
this.size = size;
|
|
||||||
this.level = 1;
|
|
||||||
this.score = 0;
|
|
||||||
this.freeBlocks = size * size;
|
|
||||||
this.updateListeners = new ArrayList<UpdateListener>();
|
|
||||||
|
|
||||||
rand = new Random(); // Initialize random object
|
rand = new Random(); // Initialize random object
|
||||||
this.reset();
|
this.reset(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addUpdateListener(UpdateListener listener) {
|
public void addUpdateListener(UpdateListener listener) {
|
||||||
@@ -75,6 +70,10 @@ public class Game {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getLinesLeft() {
|
||||||
|
return linesLeft;
|
||||||
|
}
|
||||||
|
|
||||||
public int getLevel(){
|
public int getLevel(){
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
@@ -223,7 +222,11 @@ public class Game {
|
|||||||
*
|
*
|
||||||
* @author aaron
|
* @author aaron
|
||||||
*/
|
*/
|
||||||
public void reset(){
|
public void reset(int size){
|
||||||
|
this.size = size;
|
||||||
|
this.freeBlocks = size * size;
|
||||||
|
this.updateListeners = new ArrayList<UpdateListener>();
|
||||||
|
|
||||||
// Initialize new blocks
|
// Initialize new blocks
|
||||||
nextBlocks = new ArrayList<Integer>();
|
nextBlocks = new ArrayList<Integer>();
|
||||||
nextBlocks.add(1);
|
nextBlocks.add(1);
|
||||||
@@ -236,6 +239,7 @@ public class Game {
|
|||||||
score = 0;
|
score = 0;
|
||||||
numUndos = 0;
|
numUndos = 0;
|
||||||
freeMoves = 0;
|
freeMoves = 0;
|
||||||
|
linesLeft=linesPerLevel;
|
||||||
|
|
||||||
// Populate game field
|
// Populate game field
|
||||||
this.populateField();
|
this.populateField();
|
||||||
@@ -328,6 +332,13 @@ public class Game {
|
|||||||
private void nextStep(final Point lastPoint) {
|
private void nextStep(final Point lastPoint) {
|
||||||
if(!checkRemoveBlocks(lastPoint)){
|
if(!checkRemoveBlocks(lastPoint)){
|
||||||
populateField(); //add new blocks
|
populateField(); //add new blocks
|
||||||
|
} else {
|
||||||
|
linesLeft--;
|
||||||
|
if(linesLeft==0) {
|
||||||
|
level++;
|
||||||
|
linesLeft=linesPerLevel;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
emitUpdateEvent();
|
emitUpdateEvent();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,14 @@ package ch.bfh.sevennotseven;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Button;
|
import java.awt.Button;
|
||||||
|
import java.awt.CardLayout;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.Frame;
|
import java.awt.Frame;
|
||||||
import java.awt.HeadlessException;
|
import java.awt.HeadlessException;
|
||||||
import java.awt.Panel;
|
import java.awt.Panel;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
@@ -24,7 +27,7 @@ import javax.swing.JPanel;
|
|||||||
* @author aaron
|
* @author aaron
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Window extends JFrame {
|
public class Window extends JFrame implements ActionListener{
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private Game game;
|
private Game game;
|
||||||
@@ -34,6 +37,10 @@ public class Window extends JFrame {
|
|||||||
private JButton buttonUndo;
|
private JButton buttonUndo;
|
||||||
private JButton buttonFreeMove;
|
private JButton buttonFreeMove;
|
||||||
private JLabel labelScore;
|
private JLabel labelScore;
|
||||||
|
private JLabel labelLinesLeft;
|
||||||
|
private JLabel labelLevel;
|
||||||
|
private JPanel mainPanel;
|
||||||
|
private CardLayout cardLayout;
|
||||||
|
|
||||||
|
|
||||||
public Window(String title) throws HeadlessException {
|
public Window(String title) throws HeadlessException {
|
||||||
@@ -59,12 +66,16 @@ public class Window extends JFrame {
|
|||||||
buttonUndo = new JButton("Undo (0)");
|
buttonUndo = new JButton("Undo (0)");
|
||||||
buttonUndo.setEnabled(false);
|
buttonUndo.setEnabled(false);
|
||||||
labelScore= new JLabel("Score: 0");
|
labelScore= new JLabel("Score: 0");
|
||||||
|
labelLinesLeft = new JLabel("Lines Left: 40");
|
||||||
|
labelLevel = new JLabel("Level: 1");
|
||||||
|
|
||||||
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
|
|
||||||
topPanel.add(buttonFreeMove);
|
topPanel.add(buttonFreeMove);
|
||||||
topPanel.add(buttonUndo);
|
topPanel.add(buttonUndo);
|
||||||
topPanel.add(labelScore);
|
topPanel.add(labelScore);
|
||||||
|
topPanel.add(labelLinesLeft);
|
||||||
|
topPanel.add(labelLevel);
|
||||||
topPanel.add(moves);
|
topPanel.add(moves);
|
||||||
|
|
||||||
game.addUpdateListener(new Game.UpdateListener() {
|
game.addUpdateListener(new Game.UpdateListener() {
|
||||||
@@ -76,19 +87,48 @@ public class Window extends JFrame {
|
|||||||
buttonUndo.setEnabled(game.getAvailUndo()>0);
|
buttonUndo.setEnabled(game.getAvailUndo()>0);
|
||||||
buttonFreeMove.setText("Free Move ("+game.getAvailFreeMoves()+")");
|
buttonFreeMove.setText("Free Move ("+game.getAvailFreeMoves()+")");
|
||||||
buttonUndo.setText("Undo ("+game.getAvailUndo()+")");
|
buttonUndo.setText("Undo ("+game.getAvailUndo()+")");
|
||||||
|
labelLinesLeft.setText("Lines Left: "+game.getLinesLeft());
|
||||||
|
labelLevel.setText("Level: "+game.getLevel());
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
JPanel welcomePanel = new JPanel();
|
||||||
|
int sizes [] = {7,8,9,10};
|
||||||
|
for(int i=0; i<sizes.length; i++) {
|
||||||
|
JButton btn = new JButton(sizes[i]+"x"+sizes[i]);
|
||||||
|
btn.addActionListener(this);
|
||||||
|
btn.setActionCommand(Integer.toString(sizes[i]));
|
||||||
|
welcomePanel.add(btn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
JPanel gamePanel = new JPanel();
|
||||||
|
gamePanel.setLayout(new BorderLayout());
|
||||||
|
gamePanel.add(topPanel, BorderLayout.NORTH);
|
||||||
|
gamePanel.add(field);
|
||||||
|
|
||||||
|
mainPanel = new JPanel();
|
||||||
|
cardLayout = new CardLayout();
|
||||||
|
mainPanel.setLayout(cardLayout);
|
||||||
|
mainPanel.add(welcomePanel);
|
||||||
|
mainPanel.add(gamePanel);
|
||||||
|
|
||||||
|
this.setContentPane(mainPanel);
|
||||||
|
|
||||||
this.add(topPanel, BorderLayout.NORTH);
|
|
||||||
this.add(field);
|
|
||||||
this.setSize(470,460);
|
this.setSize(470,460);
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
int size = Integer.parseInt(e.getActionCommand());
|
||||||
|
cardLayout.last(mainPanel);
|
||||||
|
game.reset(size);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user