⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hangmangame.java

📁 WebHangman a game can be play on the website contain the total code of JAVA
💻 JAVA
字号:
package com.aztsoft.games.hangman;

import com.aztsoft.games.dict.Dictionary;
import com.aztsoft.games.util.GameLogger;
import com.aztsoft.games.util.Rect;

import java.awt.*;


/**
 * The state and behaviour of the game
 */
public class HangmanGame {
    public static int kMAX_TRIES = 7;
    private HiddenKeyword hiddenKeyword;//=new HiddenKeyword("START");
    private LetterBox letterBox;
    private ImageBox imageBox;
    private int triesNumberLeft = kMAX_TRIES;
    private boolean gameOver = false;


    final static private int offsetX = 20;
    final static private int offsetY = 50;
    final static private int absWidth = 408;
    final static private int absHeight = 455;
    final static private int width = absWidth - offsetX * 2;
    final static private int height = absHeight - offsetY - offsetX - 10;

    Rect rect = new Rect(offsetX, offsetY, width, height);
    Dimension dimension;
    private int gamesWon = 0;
    private int gamesLost = 0;

    public HangmanGame(Dimension size) {
        dimension = size;
        rect = new Rect(offsetX, offsetY, size.width - offsetX * 2, size.height - offsetY * 2);
        letterBox = new LetterBox(new Rect(size.width / 2 - 120, size.height / 2 + 50, 270, 90), Dictionary.getTargetLanguageUpperCharSet());
    }

    public void decreaseTriesLeft() {
        triesNumberLeft--;
        if (triesNumberLeft <= 0) {
            gameOver = true;
        }
    }

    public void setGameOver(boolean gameOver) {
        this.gameOver = gameOver;
    }

    public void resetTriesLeft() {
        triesNumberLeft = kMAX_TRIES;
    }


    public int getTriesNumberLeft() {
        return triesNumberLeft;
    }

    public boolean isGameOver() {
        return gameOver;
    }

    public LetterBox getLetterBox() {
        return letterBox;
    }

    public ImageBox getImageBox() {
        return imageBox;
    }

    public HiddenKeyword getHiddenKeyword() {
        return hiddenKeyword;
    }

    private boolean lettersOnlyAZ(String st) {
        boolean b = true;
        int maxIndex = 25;
        int offset = 65;
        for (int i = 0; i < st.length(); i++) {
            if ((int) st.charAt(i) < offset || (int) st.charAt(i) > offset + maxIndex) {
                return false;
            }
        }
        return b;
    }

    public void initialise() {
        letterBox = new LetterBox(new Rect((int) dimension.getWidth() / 2 - 120, (int) dimension.getHeight() / 2 + 50, 270, 90), Dictionary.getTargetLanguageUpperCharSet());
        triesNumberLeft = kMAX_TRIES;
        int positionOfSourceWord = Dictionary.getRandomIndex();
        String hiddenWord = getTargetKeyword(positionOfSourceWord);

        while (!lettersOnlyAZ(hiddenWord) || hiddenWord.length() < 5) {
            positionOfSourceWord = Dictionary.getRandomIndex();
            hiddenWord = getTargetKeyword(positionOfSourceWord);
            GameLogger.log("hangman trying to find keyword");
        }

        hiddenKeyword = new HiddenKeyword(hiddenWord);
        GameLogger.log("Just set the hiddenkeyword" + hiddenWord);
        GameLogger.log("hidden keyword:" + hiddenWord);
        GameLogger.log("starting ImageBox" + hiddenWord);
        imageBox = new ImageBox(hiddenWord);
        imageBox.loadImages();


    }

    private long millisRunning = 0;


    public void startGame() {
        initialise();
        gameOver = false;
        Thread timer = new Thread() {
          public void run() {
                int oldValue = 0;
                while (!gameOver) {
                    long millisStarting = System.currentTimeMillis();
                    pause(500);
                    long millisEnding = System.currentTimeMillis();
                    millisRunning = millisRunning + (millisEnding - millisStarting);
                    int newValue = (int) millisRunning / 5000 % imageBox.kMAXIMAGES;
                    if (newValue != oldValue) {
                        oldValue = newValue;
                        imageBox.changeImage();
                    }
                }
            }
        };
        timer.start();
    }


    private String getTargetKeyword(int positionOfSourceWord) {
        return Dictionary.getTargetKeywordToUpper(positionOfSourceWord);
    }

    public void congratulations() {
        //System.out.println("Congratulations " + ", you win a banana!");
        gamesWon++;
    }

    public void gameOver() {
        //System.out.println("Sorry " + ", this time you lose!");
        gamesLost++;
    }

    public int getGamesWon() {
        return gamesWon;
    }

    public int getGamesLost() {
        return gamesLost;
    }

    public void pause(int mil) {
        try {
            Thread.currentThread().sleep(mil);
        } catch (Exception ex) {
        }
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -