Daj mi trenie zlatko - projekt z PAZ1a
Created: 2008-06-19 - 16:45
JE NUTNE STIAHNUT NASLEDUJUCE SUBORY A NAKOPIROVAT ICH KU ZDROJAKU (napr. do SRC)
Obrazky ku DTZ
import java.awt.event.MouseEvent;
import edu.neu.ccs.XCircle;
import edu.neu.ccs.gui.ImagePaintable;
import edu.neu.ccs.jpf.JPAZ;
import edu.neu.ccs.pedagogy.Turtle;
/**
*
* @author Pavol Rajz�k
*
* @see Povodna verzia hry:
* www.addictinggames.com/gimmefrictionbaby.html
*
*/
public class DajMiTrenieZlatko extends JPAZ {
/** Charakterizuje, ktora gula je na tahu. */
public int ktoraGula = -1;
/** Korytnacky reprezentujuce gule. */
public Turtle[] gule = new Turtle[1000];
/** Korytnacky reprezentujuce stupne kazdej gule. */
public Turtle[] stupneGuli = new Turtle[1000];
/** Velkosti polomerov jednotlivych guli. */
public int[] velkosti = new int[1000];
/** Velkosti stupnov jednotlivych guli. */
public int[] stupen = new int[1000];
/** Pociatocna velkost hybnosti kazdej gule. */
public double i = 1000;
/**
* Premenna, ktora obsahuje informaciu o tom, ci sa vystrelena gula este
* hybe.
*/
public boolean hybeSa;
/** Aktualny stav skore. */
public int skore;
/** Najvyssie skore. */
public int hiskore;
/** Zistuje, ci je hra ukoncena. */
public boolean hraUkoncena;
/** Zistuje, ci sa ma vytvorit nova hra. */
public boolean novaHra;
public static void main(String[] args) {
new DajMiTrenieZlatko();
}
protected void onStart() {
turtle.turn(90);
turtle.hideTurtle();
turtle.setPosition(turtle.getX()-75,turtle.getY());
turtle.print("Stla�te START na za�atie hry" +
"\n \n \n \n \n \n \n \n \n"+
" Pavol Rajz�k, 2007");
}
protected void onMousePressed(int x, int y, MouseEvent e) {
if (!hybeSa) {
ktoraGula++;
velkosti[ktoraGula] = 10;
gule[ktoraGula] = new Turtle();
gule[ktoraGula].setShape(new XCircle(velkosti[ktoraGula]));
gule[ktoraGula].setPosition(turtle.getPosition());
gule[ktoraGula].setDirection(turtle.getDirection());
gule[ktoraGula].penUp();
hybeSa = true;
stupen[ktoraGula] = 1;
}
if (hraUkoncena)
novaHra = true;
}
public void START() {
Runtime r = Runtime.getRuntime();
double smer = 2;
double uhol;
double gx;
double gy;
boolean daSaZvacsovat = true;
boolean gulaPreslaDeliaciBod = false;
Turtle plocha = new Turtle();
Turtle delo = new Turtle();
Turtle pocitadlo = new Turtle();
Turtle pocitadlo2 = new Turtle();
delo.setPosition(201, 381);
delo.setShape(new ImagePaintable("src\\delo.png"));
turtle = delo;
plocha.turn(90);
plocha.setShape(new ImagePaintable("src\\plocha.png"));
hraUkoncena = false;
pocitadlo.setShape(new String(String.valueOf(skore)));
pocitadlo.setPosition(6, 25);
pocitadlo.turnTowards(6, 20);
pocitadlo2.setShape(new String(String.valueOf(hiskore)));
pocitadlo2.setPosition(6, 70);
pocitadlo2.turnTowards(6, 25);
while (0 == 0) {
/*
* Hlavny program
*/
if (!hraUkoncena) {
novaHra = false;
// kym sa gule hybu
if (hybeSa) {
gule[ktoraGula].step(8 * (i / 1000));
i -= 8;
//ked hybnost klesne pod 1
if (i < 1) {
hybeSa = false;
stupneGuli[ktoraGula] = new Turtle();
stupneGuli[ktoraGula].turn(90);
stupneGuli[ktoraGula].setPosition(gule[ktoraGula].getX(), gule[ktoraGula].getY());
stupneGuli[ktoraGula].setShape(new ImagePaintable("src//"+String.valueOf(stupen[ktoraGula]+".png")));
//zvacsuju sa kym sa da
while (daSaZvacsovat) {
if (gule[ktoraGula].getX() - velkosti[ktoraGula] < 50)
daSaZvacsovat = false;
if (gule[ktoraGula].getX() + velkosti[ktoraGula] > 350)
daSaZvacsovat = false;
if (gule[ktoraGula].getY() + velkosti[ktoraGula] > 360)
daSaZvacsovat = false;
if (gule[ktoraGula].getY() - velkosti[ktoraGula] < 0)
daSaZvacsovat = false;
for (int u = 0; u < ktoraGula; u++) {
if (velkosti[ktoraGula] + velkosti[u] >= gule[ktoraGula]
.distanceTo(gule[u].getPosition()))
daSaZvacsovat = false;
}
gule[ktoraGula].setShape(new XCircle(velkosti[ktoraGula]));
gule[ktoraGula].step(0.00001);//hh :-)
gule[ktoraGula].step(-0.00001);//hh :-)
delay(10);
velkosti[ktoraGula]++;
}
daSaZvacsovat = true;
i = 1000;
}
gy = gule[ktoraGula].getY();
gx = gule[ktoraGula].getX();
uhol = gule[ktoraGula].getDirection();
// Vypocitanie uhlov odrazu od steny:
if (gy < 10 && uhol <= 90) {
gule[ktoraGula].turn(-2 * uhol);
}
if (gy < 10 && uhol > 90 && uhol <= 180) {
gule[ktoraGula].turn(2 * (180 - uhol));
}
if (gx < 60 && uhol > 90 && uhol <= 180) {
gule[ktoraGula].turn(2 * (90 - 180 - uhol));
}
if (gx < 60 && uhol > 180 && uhol <= 270) {
gule[ktoraGula].turn(2 * (90 - (uhol - 180)));
}
if (gx > 340 && uhol <= 90) {
gule[ktoraGula].turn(2 * (90 - uhol));
}
if (gx > 340 && uhol > 270 && uhol <= 360) {
gule[ktoraGula].turn(2 * (90 - 360 - uhol));
}
if (gy < 350)
gulaPreslaDeliaciBod = true;
// koniec hry
if (gy > 350 && gulaPreslaDeliaciBod == true) {
hraUkoncena = true;
//finta f� :)
turtle.step(-0.0001);
turtle.step(0.0001);
pocitadlo.setShape(new String("KONIEC"));
}
if (gy > 402) {
hraUkoncena = true;
pocitadlo.setShape(new String("KONIEC"));
}
// vypocitanie odrazov dvoch guli
for (int narazenaGula = 0; narazenaGula < ktoraGula; narazenaGula++) {
if (gule[ktoraGula].distanceTo(gule[narazenaGula].getPosition()) < velkosti[ktoraGula] + velkosti[narazenaGula] + 2) {
gule[ktoraGula].turnTowards(gule[narazenaGula].getPosition());
gule[narazenaGula].turnTowards(gule[ktoraGula].getPosition());
gule[ktoraGula].setDirection(gule[narazenaGula].getDirection());
if (hybeSa)
stupen[narazenaGula]++;
i -= 10;
stupneGuli[narazenaGula].setShape(new ImagePaintable("src//"+String.valueOf(stupen[narazenaGula]+".png")));
//Ak bola gula narazena 3x
if (stupen[narazenaGula] > 3) {
gule[narazenaGula].setPosition(-200,-200);
velkosti[narazenaGula] = 1;
skore++;
stupneGuli[narazenaGula].setPosition(-20, -20);
pocitadlo.setShape(new String(String.valueOf(skore)));
}
}
}
// pohyb dela
} else {
gulaPreslaDeliaciBod = false;
if (turtle.getDirection() == 180)
smer = -2;
if (turtle.getDirection() == 0)
smer = 2;
turtle.turn(smer);
}
}
if (novaHra) {
//Vypise sa high score
if (hiskore <= skore)
hiskore = skore;
pocitadlo2.setShape(new String(String.valueOf(hiskore)));
for (int l = 0; l <= ktoraGula; l++) {
gule[l].setPosition(-200, -200);
gule[l].kill();
gule[l] = null;
stupen[l] = 1;
velkosti[l] = 10;
if (l != ktoraGula) {
stupneGuli[l].setPosition(-20,-20);
stupneGuli[l].kill();
stupneGuli[l] = null;
}
}
skore = 0;
pocitadlo.setShape(new String(String.valueOf(skore)));
ktoraGula = -1;
i = 1000;
hraUkoncena = false;
hybeSa = false;
r.gc(); //garbage collector - experimentalny feature
}
delay(30);
}
}
}