0% found this document useful (0 votes)
16 views2 pages

Tetris

This Java program implements a simple Tetris game using JavaFX. It creates a window with a grid of colored rectangles that represent the Tetris tiles. The game updates every 0.2 seconds to redraw the tiles with random colors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Tetris

This Java program implements a simple Tetris game using JavaFX. It creates a window with a grid of colored rectangles that represent the Tetris tiles. The game updates every 0.2 seconds to redraw the tiles with random colors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Codigo por eclipse y javafx

package com.example.tetris;

import .animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Tetris extends Application {

private static final int TILE_SIZE = 25;


private static final int WIDTH = 10 * TILE_SIZE;
private static final int HEIGHT = 20 * TILE_SIZE;
private Pane pane;

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage primaryStage) {
pane = new Pane();
Scene scene = new Scene(pane, WIDTH, HEIGHT);
primaryStage.setTitle("Tetris");
primaryStage.setScene(scene);
primaryStage.show();

AnimationTimer timer = new AnimationTimer() {


long lastUpdate = 0;

@Override
public void handle(long now) {
if (now - lastUpdate >= 200_000_000) { // 0.2 second
draw();
lastUpdate = now;
}
}
};
timer.start();
}

private void draw() {


pane.getChildren().clear();

for (int y = 0; y < 20; y++) {


for (int x = 0; x < 10; x++) {
Rectangle tile = new Rectangle(TILE_SIZE, TILE_SIZE);
tile.setX(x * TILE_SIZE);
tile.setY(y * TILE_SIZE);
tile.setFill(Color.color(Math.random(), Math.random(),
Math.random()));
tile.setStroke(Color.BLACK);
tile.setArcWidth(10);
tile.setArcHeight(10);
tile.setOpacity(0.8);
tile.setEffect(new javafx.scene.effect.DropShadow(10,
Color.BLACK));
pane.getChildren().add(tile);
}
}
}
}

You might also like