karen_constantine

Untitled

Mar 10th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.canvas.Canvas;
  4. import javafx.scene.canvas.GraphicsContext;
  5. import javafx.scene.control.Alert;
  6. import javafx.scene.control.Alert.AlertType;
  7. import javafx.scene.input.KeyCode;
  8. import javafx.scene.layout.VBox;
  9. import javafx.scene.paint.Color;
  10. import javafx.stage.Stage;
  11.  
  12. public class HangmanGame extends Application {
  13.  
  14. private static final String[] WORDS = {"JAVA", "PYTHON", "HTML", "CSS", "JAVASCRIPT"};
  15.  
  16. private String wordToGuess;
  17. private StringBuilder currentGuess;
  18. private int attemptsLeft;
  19. private boolean gameEnded;
  20.  
  21. @Override
  22. public void start(Stage primaryStage) {
  23. Canvas canvas = new Canvas(400, 400);
  24. GraphicsContext gc = canvas.getGraphicsContext2D();
  25.  
  26. resetGame();
  27.  
  28. canvas.setOnKeyPressed(event -> {
  29. if (!gameEnded && event.getCode().isLetterKey()) {
  30. char guessedLetter = event.getText().toUpperCase().charAt(0);
  31. boolean letterFound = false;
  32. for (int i = 0; i < wordToGuess.length(); i++) {
  33. if (wordToGuess.charAt(i) == guessedLetter) {
  34. currentGuess.setCharAt(i, guessedLetter);
  35. letterFound = true;
  36. }
  37. }
  38. if (!letterFound) {
  39. attemptsLeft--;
  40. drawHangman(gc);
  41. }
  42. drawWord(gc);
  43. if (attemptsLeft == 0 || !currentGuess.toString().contains("_")) {
  44. endGame(attemptsLeft == 0);
  45. }
  46. }
  47. });
  48.  
  49. VBox root = new VBox(canvas);
  50. Scene scene = new Scene(root);
  51. primaryStage.setScene(scene);
  52. primaryStage.setTitle("Hangman Game");
  53. primaryStage.show();
  54.  
  55. canvas.requestFocus();
  56. drawHangman(gc);
  57. drawWord(gc);
  58. }
  59.  
  60. private void resetGame() {
  61. wordToGuess = WORDS[(int) (Math.random() * WORDS.length)];
  62. currentGuess = new StringBuilder("_".repeat(wordToGuess.length()));
  63. attemptsLeft = 6;
  64. gameEnded = false;
  65. }
  66.  
  67. private void drawWord(GraphicsContext gc) {
  68. gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
  69. gc.setFill(Color.BLACK);
  70. gc.fillText(currentGuess.toString(), 150, 350);
  71. }
  72.  
  73. private void drawHangman(GraphicsContext gc) {
  74. gc.setStroke(Color.BLACK);
  75. gc.setLineWidth(2);
  76. if (attemptsLeft < 6) {
  77. gc.strokeOval(100, 50, 50, 50); // Head
  78. }
  79. if (attemptsLeft < 5) {
  80. gc.strokeLine(125, 100, 125, 200); // Body
  81. }
  82. if (attemptsLeft < 4) {
  83. gc.strokeLine(125, 120, 75, 100); // Left Arm
  84. }
  85. if (attemptsLeft < 3) {
  86. gc.strokeLine(125, 120, 175, 100); // Right Arm
  87. }
  88. if (attemptsLeft < 2) {
  89. gc.strokeLine(125, 200, 75, 250); // Left Leg
  90. }
  91. if (attemptsLeft < 1) {
  92. gc.strokeLine(125, 200, 175, 250); // Right Leg
  93. }
  94. }
  95.  
  96. private void endGame(boolean lost) {
  97. gameEnded = true;
  98. String message = lost ? "You lost! The word was: " + wordToGuess : "Congratulations! You won!";
  99. Alert alert = new Alert(AlertType.INFORMATION, message);
  100. alert.setTitle("Game Over");
  101. alert.setHeaderText(null);
  102. alert.showAndWait();
  103. resetGame();
  104. }
  105.  
  106. public static void main(String[] args) {
  107. launch(args);
  108. }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment