Created
February 1, 2015 14:48
-
-
Save samuelhnrq/d380359b5c621ed95b6b to your computer and use it in GitHub Desktop.
TetrisTPiece
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import greenfoot.World; | |
/** | |
* Write a description of class TPiece here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
@SuppressWarnings("UnusedAssignment") | |
public class TPiece implements Piece{ | |
// instance variables - replace the example below with your own | |
private Blue[] pieces = new Blue[4]; | |
private int rotation = 0; | |
/** | |
* Constructor for objects of class TPiece | |
*/ | |
public TPiece(World w){ | |
for (int i = 0; i < pieces.length; i++) { | |
if (i != 3){ | |
pieces[i] = new Blue(this, 1); | |
} else pieces[i] = new Blue(this); | |
} | |
w.addObject(pieces[0], 3, 0); | |
w.addObject(pieces[1], 4, 0); | |
w.addObject(pieces[2], 5, 0); | |
w.addObject(pieces[3], 4, 1); | |
} | |
@Override | |
public void stop() { | |
for (Blue b : pieces){ | |
b.stop(); | |
} | |
} | |
/** | |
* The Method for rotating the piece | |
*/ | |
public void rotate(){ | |
Blue blue; | |
switch (rotation){ | |
case 0: | |
blue = (Blue) pieces[1].getWorld(). //Gigantic, any idea to short it? | |
getObjectsAt(pieces[1].getX() - 1, pieces[1].getY(), null).get(0); | |
blue.setLocation(blue.getX() + 1, blue.getY() - 1); | |
rotation = 90; | |
blue = null; | |
break; | |
case 90: | |
blue = (Blue) pieces[1].getWorld(). //Gigantic, any idea to short it? | |
getObjectsAt(pieces[1].getX(), pieces[1].getY() - 1, Blue.class).get(0); | |
blue.setLocation(blue.getX() - 1, blue.getY() - 1); | |
for(int i = 0; i < 4; i++){ | |
if (pieces[i] == blue){ | |
continue; | |
} | |
pieces[i].addStep(1); | |
} | |
rotation = 180; | |
blue = null; | |
break; | |
case 180: | |
blue = pieces[2]; | |
blue.setLocation(blue.getX() -1, blue.getY() + 1); | |
//GENIUS!!!! | |
for(int i = 1; i < 4; i++){ | |
//pieces[i].removeStep(1); | |
} | |
rotation = 270; | |
blue = null; | |
break; | |
case 270: | |
blue = pieces[0]; | |
blue.setLocation(blue.getX() + 1, blue.getY() + 1); | |
rotation = 0; | |
blue = null; | |
break; | |
} | |
// put your code here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment