Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- public class HangmanGame extends Application {
- private static final String[] WORDS = {"JAVA", "PYTHON", "HTML", "CSS", "JAVASCRIPT"};
- private String wordToGuess;
- private StringBuilder currentGuess;
- private int attemptsLeft;
- private boolean gameEnded;
- @Override
- public void start(Stage primaryStage) {
- Canvas canvas = new Canvas(400, 400);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- resetGame();
- canvas.setOnKeyPressed(event -> {
- if (!gameEnded && event.getCode().isLetterKey()) {
- char guessedLetter = event.getText().toUpperCase().charAt(0);
- boolean letterFound = false;
- for (int i = 0; i < wordToGuess.length(); i++) {
- if (wordToGuess.charAt(i) == guessedLetter) {
- currentGuess.setCharAt(i, guessedLetter);
- letterFound = true;
- }
- }
- if (!letterFound) {
- attemptsLeft--;
- drawHangman(gc);
- }
- drawWord(gc);
- if (attemptsLeft == 0 || !currentGuess.toString().contains("_")) {
- endGame(attemptsLeft == 0);
- }
- }
- });
- VBox root = new VBox(canvas);
- Scene scene = new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Hangman Game");
- primaryStage.show();
- canvas.requestFocus();
- drawHangman(gc);
- drawWord(gc);
- }
- private void resetGame() {
- wordToGuess = WORDS[(int) (Math.random() * WORDS.length)];
- currentGuess = new StringBuilder("_".repeat(wordToGuess.length()));
- attemptsLeft = 6;
- gameEnded = false;
- }
- private void drawWord(GraphicsContext gc) {
- gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
- gc.setFill(Color.BLACK);
- gc.fillText(currentGuess.toString(), 150, 350);
- }
- private void drawHangman(GraphicsContext gc) {
- gc.setStroke(Color.BLACK);
- gc.setLineWidth(2);
- if (attemptsLeft < 6) {
- gc.strokeOval(100, 50, 50, 50); // Head
- }
- if (attemptsLeft < 5) {
- gc.strokeLine(125, 100, 125, 200); // Body
- }
- if (attemptsLeft < 4) {
- gc.strokeLine(125, 120, 75, 100); // Left Arm
- }
- if (attemptsLeft < 3) {
- gc.strokeLine(125, 120, 175, 100); // Right Arm
- }
- if (attemptsLeft < 2) {
- gc.strokeLine(125, 200, 75, 250); // Left Leg
- }
- if (attemptsLeft < 1) {
- gc.strokeLine(125, 200, 175, 250); // Right Leg
- }
- }
- private void endGame(boolean lost) {
- gameEnded = true;
- String message = lost ? "You lost! The word was: " + wordToGuess : "Congratulations! You won!";
- Alert alert = new Alert(AlertType.INFORMATION, message);
- alert.setTitle("Game Over");
- alert.setHeaderText(null);
- alert.showAndWait();
- resetGame();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment