Skip to content
Snippets Groups Projects
Commit 27826734 authored by Sebastian Lehto's avatar Sebastian Lehto
Browse files

ensimmäinen heikosti toimiva versio

parents
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX"/>
<classpathentry kind="output" path="bin"/>
</classpath>
.project 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Tetris</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=15
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=15
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=15
File added
File added
File added
package tetris;
public class Launcher {
public static void main(String[] args) {
// TODO Auto-generated method stub
Main.main(args);
}
}
package tetris;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.stage.WindowEvent;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
public class Main extends Application {
//Muuttujat
public static final int ASKEL = 25;
public static final int KOKO = 25;
public static int LEVEYS = KOKO * 12;
public static int KORKEUS = KOKO * 24;
public static int[][] RUUDUKKO = new int [LEVEYS/KOKO][KORKEUS/KOKO];
private static Pane ikkuna = new Pane();
private static Muoto muoto;
private static Scene scene = new Scene(ikkuna, LEVEYS + 150, KORKEUS);
public static int score = 0;
private static boolean peliOhi = false;
private static Muoto seuraavaMuoto = Ohjain.teeMuoto();
private static int riveja = 0;
private static int top = 0;
public static void main(String[] args) {
launch(Main.class);
}
@Override
public void start(Stage stage) throws Exception {
for (int[] sarake : RUUDUKKO) {
Arrays.fill(sarake, 0);
}
ikkuna.setStyle("-fx-background-color: FFBBBB;");
Line viiva = new Line(LEVEYS, 0, LEVEYS, KORKEUS);
Text scoreTeksti = new Text("Score: ");
scoreTeksti.setStyle("-fx-font-size: 20px;");
scoreTeksti.setY(50);
scoreTeksti.setX(LEVEYS + 5);
Text taso = new Text("Taso: ");
taso.setStyle("-fx-font-size: 20px;");
taso.setY(100);
taso.setX(LEVEYS + 5);
ikkuna.getChildren().addAll(scoreTeksti, viiva, taso);
Muoto a = seuraavaMuoto;
ikkuna.getChildren().addAll(a.a, a.b, a.c, a.d);
moveOnKeyPress(a);
muoto = a;
seuraavaMuoto = Ohjain.teeMuoto();
stage.setScene(scene);
stage.setTitle("TETRIS");
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent> () {
@Override
public void handle(WindowEvent e) {
Platform.exit();
System.exit(0);
}
});
Timer liiku = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (muoto.a.getY() == 0 || muoto.b.getY() == 0 || muoto.c.getY() == 0 || muoto.d.getY() == 0) {
top++;
} else {
top = 0;
}
if (top == 2) {
Text over = new Text("GAME OVER");
over.setFill(Color.RED);
over.setStyle("-fx-font-size: 50px;");
over.setY(250);
over.setX(10);
ikkuna.getChildren().add(over);
peliOhi = true;
}
if (top == 15) {
System.exit(0);
}
if (!peliOhi) {
if (muoto.suurinY() >= KORKEUS - KOKO) {
Muoto a = seuraavaMuoto;
ikkuna.getChildren().addAll(a.a, a.b, a.c, a.d);
moveOnKeyPress(a);
muoto = a;
seuraavaMuoto = Ohjain.teeMuoto();
}
Ohjain.liikutaAlas(muoto);
scoreTeksti.setText("Score: " + Integer.toString(score));
taso.setText("Taso: " + Integer.toString(riveja));
}
}
});
}
};
liiku.schedule(task, 0, 300);
}
private void moveOnKeyPress(Muoto muoto) {
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
switch (event.getCode()) {
case UP:
Ohjain.kaanna(muoto);
if (muoto.pieninX() < 0) {
Ohjain.liikuOikealle(muoto);
} else if (muoto.suurinX() > LEVEYS - KOKO) {
Ohjain.liikuVasemmalle(muoto);
}
break;
case DOWN:
Ohjain.liikutaAlas(muoto);
break;
case RIGHT:
Ohjain.liikuOikealle(muoto);
break;
case LEFT:
Ohjain.liikuVasemmalle(muoto);
break;
}
}
});
}
private void removeRows(Pane pane) {
ArrayList<Node> neliot = new ArrayList<>();
int rivit = 0;
for (int i = 0; i < KORKEUS / KOKO; i++) {
int maara = 0;
for (int j = 0; j < LEVEYS / KOKO; j++) {
if (RUUDUKKO[j][i] != 0) {
maara++;
}
}
if (maara == LEVEYS / KOKO) {
rivit++;
riveja++;
}
}
}
}
package tetris;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Muoto {
public static final int ASKEL = Main.ASKEL;
public static final int KOKO = Main.KOKO;
public static int LEVEYS = Main.LEVEYS;
public static int KORKEUS = Main.KORKEUS;
//Muoto koostuu neljästä neliöstä
Rectangle a, b, c, d;
//Eri muodoilla on nimi ja oma väri
Color color;
private String nimi;
//Neliö C toimii palikan keskipisteenä, muut saavat sijaintinsa sen suhteen
public int[][] suhteet;
//Leveys jolle palikat asetetaan alussa
public int AL = LEVEYS / 2;
public Muoto(Rectangle a, Rectangle b, Rectangle c, Rectangle d, String nimi) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.nimi = nimi;
this.alusta();
}
public void alusta() {
switch (nimi) {
case "j":
this.color = Color.DARKSALMON;
int [][] sj = {{-1, -1},{-1, 0},{1, 0}};
suhteet = sj;
break;
case "l":
this.color = Color.DARKSEAGREEN;
int [][] sl = {{1, -1},{1, 0},{-1, 0}};
suhteet = sl;
break;
case "o":
this.color = Color.INDIANRED;
int [][] so = {{0, -1},{1, -1},{1, 0}};
suhteet = so;
break;
case "s":
this.color = Color.LIGHTSTEELBLUE;
int [][] ss = {{1, -1},{0, -1},{-1, 0}};
suhteet = ss;
break;
case "t":
this.color = Color.NAVAJOWHITE;
int [][] st = {{-1, 0},{0, 1},{1, 0}};
suhteet = st;
break;
case "z":
this.color = Color.PALEVIOLETRED;
int [][] sz = {{-1, -1},{0, -1},{1, 0}};
suhteet = sz;
break;
case "i":
this.color = Color.SLATEGREY;
int [][] si = {{-2, 0},{-1, 0},{1, 0}};
suhteet = si;
break;
}
c.setX(AL);
c.setY(KOKO);
paivitaPaikat();
this.a.setFill(color);
this.b.setFill(color);
this.c.setFill(color);
this.d.setFill(color);
}
//Neliöiden paikat paivitetään asennon mukaan
public void paivitaPaikat() {
a.setX(c.getX() + suhteet[0][0] * KOKO);
a.setY(c.getY() + suhteet[0][1] * KOKO);
b.setX(c.getX() + suhteet[1][0] * KOKO);
b.setY(c.getY() + suhteet[1][1] * KOKO);
d.setX(c.getX() + suhteet[2][0] * KOKO);
d.setY(c.getY() + suhteet[2][1] * KOKO);
}
public void muutaAsentoa() {
for (int i = 0; i < 3; i++) {
int x = suhteet[i][0];
int miinusy = -1 * suhteet[i][1];
suhteet[i][0] = miinusy;
suhteet[i][1] = x;
}
paivitaPaikat();
while (true) {
boolean stop = true;
if (this.suurinY() > KORKEUS - KOKO) {
c.setY(c.getY() - KOKO);
this.paivitaPaikat();
stop = false;
}
if (this.pieninX() < 0) {
c.setX(c.getX() + KOKO);
this.paivitaPaikat();
stop = false;
} else if (this.suurinX() > LEVEYS - KOKO) {
c.setX(c.getX() - KOKO);
this.paivitaPaikat();
stop = false;
} else if (stop){
break;
}
}
}
public int suurinY() {
int y = (int) a.getY();
if (b.getY() > y) {
y = (int) b.getY();
}
if (c.getY() > y) {
y = (int) c.getY();
}
if (d.getY() > y) {
y = (int) d.getY();
}
return y;
}
public int suurinX() {
int x = (int) a.getX();
if (b.getX() > x) {
x = (int) b.getX();
}
if (c.getX() > x) {
x = (int) c.getX();
}
if (d.getX() > x) {
x = (int) d.getX();
}
return x;
}
public int pieninX() {
int x = (int) a.getX();
if (b.getX() < x) {
x = (int) b.getX();
}
if (c.getX() < x) {
x = (int) c.getX();
}
if (d.getX() < x) {
x = (int) d.getX();
}
return x;
}
public String getNimi() {
return this.nimi;
}
}
package tetris;
import javafx.scene.shape.*;
import java.util.Random;
public class Ohjain {
public static final int ASKEL = Main.ASKEL;
public static final int KOKO = Main.KOKO;
public static int LEVEYS = Main.LEVEYS;
public static int KORKEUS = Main.KORKEUS;
public static int [][] RUUDUKKO = Main.RUUDUKKO;
public static void liikuOikealle(Muoto muoto) {
if (muoto.suurinX() < LEVEYS - KOKO) {
if (tilaa(muoto, 1, 0)) {
muoto.a.setX(muoto.a.getX() + ASKEL);
muoto.b.setX(muoto.b.getX() + ASKEL);
muoto.c.setX(muoto.c.getX() + ASKEL);
muoto.d.setX(muoto.d.getX() + ASKEL);
}
}
}
public static void liikuVasemmalle(Muoto muoto) {
if (muoto.pieninX() > 0) {
int liikutaA = RUUDUKKO[((int) muoto.a.getX() / KOKO) - 1][(int) muoto.a.getY()/KOKO];
int liikutaB = RUUDUKKO[((int) muoto.b.getX() / KOKO) - 1][(int) muoto.b.getY()/KOKO];
int liikutaC = RUUDUKKO[((int) muoto.c.getX() / KOKO) - 1][(int) muoto.c.getY()/KOKO];
int liikutaD = RUUDUKKO[((int) muoto.d.getX() / KOKO) - 1][(int) muoto.d.getY()/KOKO];
if (tilaa(muoto, -1, 0)) {
muoto.a.setX(muoto.a.getX() - ASKEL);
muoto.b.setX(muoto.b.getX() - ASKEL);
muoto.c.setX(muoto.c.getX() - ASKEL);
muoto.d.setX(muoto.d.getX() - ASKEL);
}
}
}
public static void kaanna(Muoto muoto) {
switch (muoto.getNimi()) {
case "o":
break;
default:
muoto.muutaAsentoa();
break;
}
}
public static void liikutaAlas(Muoto muoto) {
if (tilaa(muoto, 0, 1)) { //muoto.suurinY() < KORKEUS - KOKO
muoto.a.setY(muoto.a.getY() + KOKO);
muoto.b.setY(muoto.b.getY() + KOKO);
muoto.c.setY(muoto.c.getY() + KOKO);
muoto.d.setY(muoto.d.getY() + KOKO);
}
}
public static boolean tilaa(Muoto muoto, int xDiff, int yDiff) {
int ax = (int) muoto.a.getX() / KOKO;
int ay = (int) muoto.a.getY() / KOKO;
int bx = (int) muoto.b.getX() / KOKO;
int by = (int) muoto.b.getY() / KOKO;
int cx = (int) muoto.c.getX() / KOKO;
int cy = (int) muoto.c.getY() / KOKO;
int dx = (int) muoto.d.getX() / KOKO;
int dy = (int) muoto.d.getY() / KOKO;
if (ay < (KORKEUS / KOKO) - 1 && RUUDUKKO[ax + xDiff][ay + yDiff] == 0 &&
by < (KORKEUS / KOKO) - 1 && RUUDUKKO[bx + xDiff][by + yDiff] == 0 &&
cy < (KORKEUS / KOKO) - 1 && RUUDUKKO[cx + xDiff][cy + yDiff] == 0 &&
dy < (KORKEUS / KOKO) - 1 && RUUDUKKO[dx + xDiff][dy + yDiff] == 0) {
return true;
}
return false;
}
public static Muoto teeMuoto() {
String nimi;
Rectangle a = new Rectangle(KOKO - 1, KOKO - 1);
Rectangle b = new Rectangle(KOKO - 1, KOKO - 1);
Rectangle c = new Rectangle(KOKO - 1, KOKO - 1);
Rectangle d = new Rectangle(KOKO - 1, KOKO - 1);
Random rn = new Random();
int random = rn.nextInt(101);
if (random < 15) {
nimi = "j";
} else if (random < 30) {
nimi = "l";
} else if (random < 45) {
nimi = "o";
} else if (random < 60) {
nimi = "s";
} else if (random < 75) {
nimi = "t";
} else if (random < 90) {
nimi = "z";
} else {
nimi = "i";
}
return new Muoto(a, b, c, d, nimi);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment