Space Invaders graphics generator
in
Programming Questions
•
10 months ago
Hi everybody
And here is the class
Yesterday I started doing some code to practice a bit the concepts of OOP... I'm trying to make a simple Space Invaders alien generator... Basically I want to create a code that will create a 12x8 grid of square objects, and fill it randomly white or black. Then is the part I got stuck... I want to take that grid and just flip it so to create a symmetric shape.... I basically create the first half of the alien and flip horizontally to complete it.
I am using a 2d Array to create the first grid and push/pop Matrix for the symmetry... But the result is upside down as you can see from the code below... Any ideas?
- Tile [][] grid;
- //number of rows and columns
- int cols;
- int rows;
- void setup() {
- size(600, 600);
- background(255);
- //cols=width/6;
- // rows=height/6;
- cols=12;
- rows=8;
- grid = new Tile[cols][rows]; //the array dimensions are equal to n.of rows/columns
- //loop through cols and rows
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- //start creating objects
- grid[i][j] = new Tile (i*5, j*5, 5);
- }
- }
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- //show me them goodies
- pushMatrix();
- translate(180, 180);
- grid[i][j].display();
- popMatrix();
- }
- }
- }
- void draw() {
- }
And here is the class
- class Tile {
- float x, y;
- float side;
- int col;
- int chance;
- Tile (float tempX, float tempY, float tempSide) {
- x = tempX;
- y = tempY;
- side = tempSide;
- float fate = random(10);
- chance = int(fate * 1.4);
- }
- void display() {
- if (chance <= 5) {
- fill(0);
- println('0');
- }
- else {
- fill(255);
- println('1');
- }
- rect (x, y, side, side);
- pushMatrix();
- translate(60,15);
- rotate(radians(180));
- rect (x, y, side, side);
- popMatrix();
- }
- }
1