Added onclick creation of random blocks. And some Bugfixes.

This commit is contained in:
id101010
2016-06-14 17:09:33 +02:00
parent f7c271b89e
commit 01be52e123
4 changed files with 28 additions and 24 deletions

12
.gitignore vendored
View File

@@ -1,13 +1 @@
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/ /bin/

View File

@@ -3,6 +3,7 @@ package ch.bfh.sevennotseven;
import java.awt.Canvas; import java.awt.Canvas;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.event.MouseAdapter;
public class FieldCanvas extends Canvas{ public class FieldCanvas extends Canvas{
@@ -11,6 +12,10 @@ public class FieldCanvas extends Canvas {
static final Color[] colors = {Color.red,Color.green, Color.blue, Color.yellow,Color.magenta}; static final Color[] colors = {Color.red,Color.green, Color.blue, Color.yellow,Color.magenta};
FieldCanvas(){
}
public void setSize(int s) { public void setSize(int s) {
this.size = s; this.size = s;
} }

View File

@@ -16,6 +16,7 @@ public class Game {
private int level; private int level;
private int score; private int score;
private int size; private int size;
private int freeBlocks;
private Random rand; private Random rand;
public Game(){ public Game(){
@@ -23,9 +24,14 @@ public class Game {
} }
public Game (int size) { public Game (int size) {
/* Initialize variables */
this.size = size; this.size = size;
this.reset(); this.level = 1;
this.score = 0;
this.freeBlocks = size * size;
rand = new Random(); // Initialize random object
this.reset();
} }
public boolean isGameOver(){ public boolean isGameOver(){
@@ -39,7 +45,7 @@ public class Game {
} }
public int getLevel(){ public int getLevel(){
return 0; return level;
} }
public List<Integer> getNextBlocks(){ public List<Integer> getNextBlocks(){
@@ -91,9 +97,6 @@ public class Game {
level = 1; level = 1;
score = 0; score = 0;
// Initialize random object
rand = new Random();
// Populate game field // Populate game field
this.populateField(); this.populateField();
} }
@@ -103,23 +106,23 @@ public class Game {
* Add n new blocks to random positions on the field, * Add n new blocks to random positions on the field,
* according to the level number. * according to the level number.
*/ */
private void populateField(){ public void populateField(){
// while there are blocks left in nextBlocks // while there are blocks left in nextBlocks
while(nextBlocks.size() > 0){ while((nextBlocks.size() > 0) && (freeBlocks > 0)){
int x = rand.nextInt(size); // get random x position int x = rand.nextInt(size); // get random x position
int y = rand.nextInt(size); // get random y position int y = rand.nextInt(size); // get random y position
// if the position is free // if the position is free
if(field[x][y] == 0){ if(field[x][y] == 0){
field[x][y] = nextBlocks.remove(0); // fill with the first element of nextBlocks field[x][y] = nextBlocks.remove(0); // fill with the first element of nextBlocks
freeBlocks--;
} }
} }
// add n new colors to nextBlocks according to the level number. // add n new colors to nextBlocks according to the level number.
for(int i = 0; i < (level * 3); i++){ for(int i = 0; i < (level * 3); i++){
nextBlocks.add(rand.nextInt(numberOfColors)); nextBlocks.add(1 + rand.nextInt(numberOfColors));
} }
} }
} }

View File

@@ -5,6 +5,8 @@ package ch.bfh.sevennotseven;
import java.awt.Frame; import java.awt.Frame;
import java.awt.HeadlessException; import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
@@ -37,14 +39,20 @@ public class Window extends Frame {
field.setSize(7); field.setSize(7);
game = new Game(); game = new Game();
field.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
game.populateField();
field.repaint();
}
});
this.add(field); this.add(field);
this.setSize(400,400); this.setSize(400,400);
this.setVisible(true); this.setVisible(true);
field.setField(game.getField()); field.setField(game.getField());
} }
/** /**