使用JavaFX完成劊子手游戲
1.1 題目說明

1.2 分析程序
本題讓我畫一個小人,首先使用new Arc()命令繪制半個橢圓當基座,然后使用new Line()繪制線條,line1和line2繪制桿,line3繪制繩子,然后用new Circle()命令畫圓,circle當做小人的頭,用line4、line5畫小人的手、line6畫身子和line7、line8畫腳,準備做好后,創建一個Pane面板,然后使用getChildren()方法中的add()方法將所有小組件放到面板中,
1.3系統測驗
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-1C1lg05f-1640435147141)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml\wpsE6AC.tmp.jpg)]](https://img.uj5u.com/2021/12/27/292330271504132.png)
1.4代碼展示
package version2;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
/**
* @Auther: paradise
* @Date: 2021/6/24 - 06 - 24 - 16:33
*/
public class Executioner extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
Arc arc = new Arc(80,280,50,25,0,180);
arc.setFill(Color.WHITE);
arc.setStroke(Color.BLACK);
Line line1 = new Line(80,255,80,30);
Line line2 = new Line(80,30,260,30);
Line line3 = new Line(260,30,260,50);
Circle circle = new Circle(260,80,30);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
Line line4 = new Line(260-30*Math.cos(Math.toRadians(30)),80+30*Math.sin(Math.toRadians(30)),260-100*Math.sin(Math.toRadians(60)),160);
Line line5 = new Line(260+30*Math.cos(Math.toRadians(30)),80+30*Math.sin(Math.toRadians(30)),260+100*Math.sin(Math.toRadians(60)),160);
Line line6 = new Line(260,110,260,210);
Line line7 = new Line(260,210,260-100*Math.sin(Math.toRadians(60)),275);
Line line8 = new Line(260,210,260+100*Math.sin(Math.toRadians(60)),275);
pane.getChildren().addAll(arc,line1,line2, line3,circle,line4,line5,line6,line7,line8);
Scene scene = new Scene(pane,400,300);
primaryStage.setTitle("HangMan Game");
primaryStage.setScene(scene);
primaryStage.show();
}
}
2.1 題目說明
? 結合以上兩個功能,實作影片方式的儈子手游戲,當用戶猜錯 7 次,絞刑架上的人擺動,當一個單詞完成后,用戶使用回車鍵繼續猜下一個單詞,
初始狀態如下:

![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-enQGhtHr-1640435147142)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml\wps8B93.tmp.jpg)][外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-SVUajQNS-1640435147143)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml\wps995F.tmp.jpg)]](https://img.uj5u.com/2021/12/27/292330271504134.png)
2.2 分析程序
創建一個名為missNum和guessNum,都為int型別的私有資料域,missNum為猜錯的次數,guessNum為猜測的次數,同時宣告一個名為missWord、wordMessage、word,型別為String的私有資料域,由于題目要求,當全部輸出錯誤的時候,小人需要晃動,所以我在這里設定了兩個Pane面板,一個名為pane的面板用于整體布局,一個用于名為laterPane的面板,當回答七次之后,在上面繪制小人,然后讓這個面板轉動起來,arc用于繪制基座,line1和lin2用于繪制桿,名為message和word的Label標簽用于顯示資訊,一個名為info用于顯示錯誤資訊,一個名為textField的文本域供用戶輸入,這里使用setLayoutX()方法和setLayoutY()方法進行布局,
設定完界面之后,便開始進行事件處理,使用getText()方法獲取用戶在文本框中輸入的資訊,系統在這個使用已經呼叫了chooseWord()方法隨機生成了一個單詞,用戶沒進行一次猜測的時候,guessNum都會加1,當missedNum小于7的時候,判斷用戶輸入的字母是否在單詞中,如果在的話,使用for回圈和if判斷陳述句看單詞中是否出現多次,使用一個字串str存盤輸入的字符,如果不在的話,str加上*號,每一次輸入完成后,都需要將str賦值給wordMessage,如果猜測的單詞和系統產生的單詞相同,則把畫出相應的部分,當猜測的時候,猜錯一個單詞就會依次畫出繩子、小人的頭、左手、身子、右手、左腳、右腳,這里我使用了一個switch判斷,當猜測錯誤一次的時候,向面板中添加繩子;當猜測錯誤為兩次時,向面板中添加頭;當猜測錯誤為三次時,向面板中添加左手;當猜測錯誤為四次時,向面板中添加身體;當猜測錯誤為五次時,向面板中添加右手;當猜測錯誤為六次時,向面板中添加左腿;當猜測錯誤為七次時,向面板中添加右腿;然后將這些組件添加到laterPane面板里,我在這里使用setRotate方法進行面板的旋轉,newRotate()方法進行角度設定,然后一個double型別的資料,
由于無論猜測成功還是失敗,我們都需要重新開始游戲,所以我設定了一個事件處理,但按下回車鍵并且猜測成功或猜錯次數大于7次的時候,重置面板,初始錯誤次數為0,猜測次數為0,并且面板上有基座和桿,
相關函式:
paintHead():畫小人的頭
PaintLeftArm():畫小人的左手
PaintRope():畫繩子
PaintRightArm():畫小人的右手
PaintBody():畫小人的身子
paintLeftLeg():畫小人的左腿
PaintRightLeg():畫小人的右腿
2.3 系統測驗
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-3ZlXfYvS-1640435147143)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml\wps5FDC.tmp.jpg)]](https://img.uj5u.com/2021/12/27/292330271504135.png)
2.4 代碼展示
package version3;
import javafx.application.Application;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Random;
/**
* @Auther: paradise
* @Date: 2021/6/24 - 06 - 24 - 16:35
*/
public class Game3 extends Application {
private int missedNum=0;
private int guessNum=0;
private String missWord = "";//錯誤資訊
private String wordMessage = "";//單詞資訊
private String word = "";//隨機的單詞
private Line line4 = new Line(260-30*Math.cos(Math.toRadians(30)),80+30*Math.sin(Math.toRadians(30)),260-100*Math.sin(Math.toRadians(60)),160);
private Line line5 = new Line(260, 30, 260, 50);
private Line line6 = new Line(260+30*Math.cos(Math.toRadians(30)),80+30*Math.sin(Math.toRadians(30)),260+100*Math.sin(Math.toRadians(60)),160);
private Line line7 = new Line(260, 110, 260, 210);
private Line line8 = new Line(260, 210, 260 - 100 * Math.sin(Math.toRadians(60)), 270);
private Line line9 = new Line(260, 210, 260 + 100 * Math.sin(Math.toRadians(60)), 275);
private Circle circle = new Circle(260, 80, 30);
@Override
public void start(Stage primaryStage) throws Exception{
final Pane pane = new Pane();
final Arc arc = new Arc(80,280,50,25,0,180);//底座
arc.setFill(Color.WHITE);
arc.setStroke(Color.BLACK);
final Pane laterPane = new Pane();
laterPane.setPrefSize(520,60);
final Line line1 = new Line(80,255,80,30);//底座豎桿
final Line line2 = new Line(80,30,260,30);//底座橫桿
pane.getChildren().addAll(arc,line1,line2);
final Label message = new Label("Guess a word:");
message.setLayoutX(220);
message.setLayoutY(280);
pane.getChildren().add(message);
final Label words = new Label("******");
words.setLayoutX(320);
words.setLayoutY(280);
pane.getChildren().add(words);
final Label info = new Label("");//顯示錯誤資訊
info.setLayoutX(220);
info.setLayoutY(310);
pane.getChildren().add(info);
final TextField textField = new TextField();
textField.setLayoutX(220);
textField.setLayoutY(340);
pane.getChildren().add(textField);
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER && (wordMessage.equals(word) || missedNum >= 7)) {
pane.getChildren().clear();
missedNum = 0;
guessNum = 0;
words.setText("******");
info.setText("");
textField.setText("");
wordMessage = "";
missWord = "";
pane.getChildren().addAll(arc, line1, line2, message, words, info, textField);
}
String chr = textField.getText().trim();
if (!chr.equals("")) {
if (missedNum % 7 == 0 && guessNum == 0) {
String[] words = {"write", "that", "program", "animal", "version", "world"};
word = chooseWord(words);
}
guessNum++;
if (missedNum < 7) {
if (word.indexOf(chr.charAt(0)) != -1) {
String str = "";
words.setText("");
for (int j = 0; j < word.length(); j++) {
if (chr.charAt(0) == word.charAt(j)) {
str += chr.charAt(0);
}
else {
if (wordMessage.length() > j) {
if (wordMessage.charAt(j) != '*'){
str += wordMessage.charAt(j);
}
else str += '*';
}
else str += '*';
}
}
words.setText(str);
wordMessage = str;
if (wordMessage.equals(word)) {
//猜詞成功
info.setText("Guess successfully.To continue the game,press Enter.");
pane.getChildren().clear();
laterPane.getChildren().addAll(line4,line5,circle);
pane.getChildren().addAll(line1,line2,arc,laterPane,message,words,info,textField);
}
} else {
missWord += chr.charAt(chr.length()-1);
switch (missedNum) {
case 0:
pane.getChildren().add(paintRope());
break;
case 1:
pane.getChildren().add(paintHead());
break;
case 2:
pane.getChildren().add(paintLeftArm());
break;
case 3:
pane.getChildren().add(paintBody());
break;
case 4:
pane.getChildren().add(paintRightArm());
break;
case 5:
pane.getChildren().add(paintLeftLeg());
break;
case 6:
pane.getChildren().add(paintRightLeg());
pane.getChildren().clear();
laterPane.getChildren().addAll(line4,line5,line6,line7,line8,line9,circle);
pane.getChildren().addAll(line1,line2,arc,laterPane,message,words,info,textField);
EventHandler<ActionEvent> eventHandler = e -> {
laterPane.setRotate(newRotate());//旋轉角度setRotate(角度)
};
Timeline rotateFan = new Timeline(new KeyFrame(Duration.millis(100), eventHandler));
rotateFan.setCycleCount(Timeline.INDEFINITE);
rotateFan.play();//開始影片
break;
}
missedNum++;
info.setText("Missed Letters:" + missWord);
}
} else {
info.setText("Guess failed!To continue the game,press Enter");
}
}
textField.setText("");
}
});
Scene scene = new Scene(pane, 800, 600, Color.WHITE);
primaryStage.setTitle("Hangman Game");
primaryStage.setScene(scene);
primaryStage.show();
}
//選擇陣列中一個元素的方法
public static String chooseWord(String[] s){
Random random = new Random();
int num = random.nextInt(s.length);
String word = s[num];
return word;
}
private Circle paintHead() {
//頭
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
return circle;
}
private Line paintLeftArm() {
return line4;//左胳膊
}
private Line paintRope() {
return line5 ;//底座吊繩
}
private Line paintRightArm() {
return line6;//右胳膊
}
private Line paintBody() {
return line7;//軀干
}
private Line paintLeftLeg() {
return line8;//左腿
}
private Line paintRightLeg() {
return line9;//右腿
}
public static void main(String[] args) {
launch(args);
}
boolean direction=true;
int rotate=10;
int speed=5;
int counts=0;
public double newRotate()
{
if(rotate/80 == 1||rotate/80 == -1) {
direction = !direction;
}
if (direction)
{
counts++;
return rotate = (rotate + speed) % 360;
}
else
{
return rotate = (rotate - speed) % 360;
}
}
}
speed=5;
int counts=0;
public double newRotate()
{
if(rotate/80 == 1||rotate/80 == -1) {
direction = !direction;
}
if (direction)
{
counts++;
return rotate = (rotate + speed) % 360;
}
else
{
return rotate = (rotate - speed) % 360;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394396.html
標籤:其他
上一篇:sqoop匯入資料到mysql時報錯:ERROR tool.ExportTool: Error during export: Export job failed
