Added onclick creation of random blocks. And some Bugfixes.
This commit is contained in:
12
.gitignore
vendored
12
.gitignore
vendored
@@ -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/
|
||||
|
||||
@@ -3,14 +3,19 @@ package ch.bfh.sevennotseven;
|
||||
import java.awt.Canvas;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseAdapter;
|
||||
|
||||
public class FieldCanvas extends Canvas {
|
||||
public class FieldCanvas extends Canvas{
|
||||
|
||||
private int size;
|
||||
private int[][] field;
|
||||
|
||||
static final Color[] colors = {Color.red,Color.green, Color.blue, Color.yellow,Color.magenta};
|
||||
|
||||
FieldCanvas(){
|
||||
|
||||
}
|
||||
|
||||
public void setSize(int s) {
|
||||
this.size = s;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public class Game {
|
||||
private int level;
|
||||
private int score;
|
||||
private int size;
|
||||
private int freeBlocks;
|
||||
private Random rand;
|
||||
|
||||
public Game(){
|
||||
@@ -23,9 +24,14 @@ public class Game {
|
||||
}
|
||||
|
||||
public Game (int size) {
|
||||
/* Initialize variables */
|
||||
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(){
|
||||
@@ -39,7 +45,7 @@ public class Game {
|
||||
}
|
||||
|
||||
public int getLevel(){
|
||||
return 0;
|
||||
return level;
|
||||
}
|
||||
|
||||
public List<Integer> getNextBlocks(){
|
||||
@@ -91,9 +97,6 @@ public class Game {
|
||||
level = 1;
|
||||
score = 0;
|
||||
|
||||
// Initialize random object
|
||||
rand = new Random();
|
||||
|
||||
// Populate game field
|
||||
this.populateField();
|
||||
}
|
||||
@@ -103,23 +106,23 @@ public class Game {
|
||||
* Add n new blocks to random positions on the field,
|
||||
* according to the level number.
|
||||
*/
|
||||
private void populateField(){
|
||||
public void populateField(){
|
||||
|
||||
// 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 y = rand.nextInt(size); // get random y position
|
||||
|
||||
// if the position is free
|
||||
if(field[x][y] == 0){
|
||||
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.
|
||||
for(int i = 0; i < (level * 3); i++){
|
||||
nextBlocks.add(rand.nextInt(numberOfColors));
|
||||
nextBlocks.add(1 + rand.nextInt(numberOfColors));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package ch.bfh.sevennotseven;
|
||||
|
||||
import java.awt.Frame;
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
@@ -37,14 +39,20 @@ public class Window extends Frame {
|
||||
field.setSize(7);
|
||||
game = new Game();
|
||||
|
||||
field.addMouseListener(new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
super.mousePressed(e);
|
||||
game.populateField();
|
||||
field.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
this.add(field);
|
||||
this.setSize(400,400);
|
||||
this.setVisible(true);
|
||||
|
||||
field.setField(game.getField());
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user