Hey. Ich bin neu im Programmieren und muss nun als Belegaufgabe für das 2. Semester im Modul Programmiertechniken ein einfaches Jump`n run bzw. Platformer game programmieren. Dafür arbeite ich in Eclipse. Ich habe einen Spaß-Boss erstellt, welcher bei Kollision eine Frage stellt (welche sehr leicht ist aber das ist ja egal). Wenn man die Frage richtig beantwortet, soll sich das aufgegangene Frage-Fenster schließen, wenn man falsch antwortet das ganze Spiel, jedoch weiß ich absolut nicht wie ich das umsetzen soll. Hoffe mir kann da jemand helfen. Falls ihr allgemein Lücken oder Fehler im Code seht ist feedback ebenfalls erwünscht.
Allgemeine Spieldatei:
Die Datei für die Level-Daten:
Code vom Dialog-Fenster (siehe oben hier ist das eigentliche Problem):
Der Code des Menüs (aktuell noch nicht mit Spiel verknüpft, aber damit ihr alles habt):
[/B]
Allgemeine Spieldatei:
Java:
[/B]
package fredontherungame;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Fred extends Application {
private HashMap<KeyCode, Boolean> keys = new HashMap<KeyCode, Boolean>();
private ArrayList<Node> platforms = new ArrayList<Node>();
private ArrayList<Node> miniboss = new ArrayList<Node>();
private ArrayList<Node> items = new ArrayList<Node>();
private Pane appRoot = new Pane();
private Pane gameRoot = new Pane();
private Pane uiRoot = new Pane();
private Node player;
private Point2D playerVelocity = new Point2D(0, 0);
private boolean canJump = true;
private int levelWidth;
private boolean dialogEvent = false, running = true;
private void initContent() {
Rectangle bg = new Rectangle(1280, 720);
levelWidth = LevelData.LEVEL1[0].length() * 60;
for (int i = 0; i < LevelData.LEVEL1.length; i++) {
String line = LevelData.LEVEL1[i];
for (int j = 0; j < line.length(); j++) {
switch (line.charAt(j)) {
case '0':
break;
case '1':
Node platform = createEntity(j*60, i*60, 60, 60, Color.BROWN);
platforms.add(platform);
break;
case '2':
Node boss = createEntity(j*60, i*60, 60, 60, Color.GREEN);
miniboss.add(boss);
break;
case '3':
Node coin = createEntity(j*60, i*60, 30, 30, Color.LIGHTBLUE);
items.add(coin);
break;
}
}
}
player = createEntity(0, 600, 40, 40, Color.ALICEBLUE);
player.translateXProperty().addListener((obs, old, newValue) -> {
int offset = newValue.intValue();
if(offset > 640 && offset < levelWidth - 640) {
gameRoot.setLayoutX(-(offset - 640));
}
});
appRoot.getChildren().addAll(bg, gameRoot, uiRoot);
}
private void update() {
if (isPressed(KeyCode.W) && player.getTranslateY() >= 5) {
jumpPlayer();
}
if (isPressed(KeyCode.A) && player.getTranslateX() >= 5) {
movePlayerX(-5);
}
if (isPressed(KeyCode.D) && player.getTranslateX() + 40 <= levelWidth - 5) {
movePlayerX(5);
}
if (playerVelocity.getY() < 10) {
playerVelocity = playerVelocity.add(0, 1);
}
movePlayerY((int)playerVelocity.getY());
for (Node boss : miniboss) {
if (player.getBoundsInParent().intersects(boss.getBoundsInParent())) {
boss.getProperties().put("alive", false);
dialogEvent= true;
running = false;
}
}
for (Iterator<Node> it = miniboss.iterator(); it.hasNext(); ) {
Node boss = it.next();
if (!(Boolean)boss.getProperties().get("alive")) {
it.remove();
gameRoot.getChildren().remove(boss);
}
}
for (Node coin : items) {
if (player.getBoundsInParent().intersects(coin.getBoundsInParent())) {
coin.getProperties().put("alive", false);
}
}
for (Iterator<Node> it = items.iterator(); it.hasNext(); ) {
Node coin = it.next();
if (!(Boolean)coin.getProperties().get("alive")) {
it.remove();
gameRoot.getChildren().remove(coin);
}
}
}
private void movePlayerX(int value) {
boolean movingRight = value > 0;
for (int i = 0; i < Math.abs(value); i++) {
for (Node platform : platforms) {
if (player.getBoundsInParent().intersects(platform.getBoundsInParent())) {
if (movingRight) {
if (player.getTranslateX() + 40 == platform.getTranslateX()) {
return;
}
}
else {
if (player.getTranslateX() == platform.getTranslateX() + 60) {
return;
}
}
}
}
player.setTranslateX(player.getTranslateX() + (movingRight ? 1 : -1));
}
}
private void movePlayerY(int value) {
boolean movingDown = value > 0;
for (int i = 0; i < Math.abs(value); i++) {
for (Node platform : platforms) {
if (player.getBoundsInParent().intersects(platform.getBoundsInParent())) {
if (movingDown) {
if (player.getTranslateY() + 40 == platform.getTranslateY()) {
player.setTranslateY(player.getTranslateY() - 1);
canJump = true;
return;
}
}
else {
if (player.getTranslateY() == platform.getTranslateY() + 60) {
return;
}
}
}
}
player.setTranslateY(player.getTranslateY() + (movingDown ? 1 : -1));
}
}
private void jumpPlayer( ) {
if (canJump) {
playerVelocity = playerVelocity.add(0, -30);
canJump = false;
}
}
private Node createEntity(int x,int y, int w, int h, Color color) {
Rectangle entity = new Rectangle(w, h);
entity.setTranslateX(x);
entity.setTranslateY(y);
entity.setFill(color);
entity.getProperties().put("alive", true);
gameRoot.getChildren().add(entity);
return entity;
}
private boolean isPressed(KeyCode key) {
return keys.getOrDefault(key, false);
}
@Override
public void start(Stage primaryStage) throws Exception {
initContent();
Scene scene = new Scene(appRoot);
scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
primaryStage.setTitle("Fred on the run");
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
if (running) {
update();
}
if (dialogEvent) {
dialogEvent = false;
keys.keySet().forEach(key -> keys.put(key, false));
GameDialog dialog = new GameDialog();
dialog.setOnCloseRequest(event -> {
if (dialog.isCorrect()) {
//hier noch einfügen was passiert nach falsch richtig! (falsch game zu, richtig nur fenster zu)
System.out.println("richtig");
}
else {
System.out.println("falsch");
}
running = true;
});
dialog.open();
}
}
};
timer.start();
}
public static void main(String[] args) {
launch(args);
}
}
[B]
Die Datei für die Level-Daten:
Java:
[/B]
package fredontherungame;
public class LevelData {
public static final String[] LEVEL1 = new String[] {
//einfach beliebig lang machen!
"000000000000111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001110000000",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000",
"111111000000011100000000000000000000000000000001100000000000000000000000000000100000000000001100000000000000",
"000000001110000000000000000000000000000001111000000000000000000000000000000000100001111000000000000000000000",
"000000000000000000000000000000000000111000000000000111000000000000000000001100100000000000001110000000000000",
"000001110000000000011100011000011100000001100011000111000100002000001110000001100000000111000000001110000000",
"111111110011110001111100111110000000000011110000000111000000111100011111100011111111100000000000000000001111"
};
}
[B]
Code vom Dialog-Fenster (siehe oben hier ist das eigentliche Problem):
Java:
[/B]
package fredontherungame;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class GameDialog extends Stage {
private Text textQuestion = new Text();
private TextField fieldAnswer = new TextField();
private Text textActualAnswer = new Text();
private boolean correct = false;
public GameDialog() {
Button btnAbschicken = new Button("Abschicken");
btnAbschicken.setOnAction(event -> {
fieldAnswer.setEditable(false);
textActualAnswer.setVisible(true);
correct = textActualAnswer.getText().equals(fieldAnswer.getText().trim());
});
VBox vbox = new VBox(10, textQuestion, fieldAnswer, textActualAnswer, btnAbschicken);
vbox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vbox);
setScene(scene);
initModality(Modality.APPLICATION_MODAL);
}
public void open() {
textQuestion.setText("Muhahahaha, an mir kommst du nicht vorbei! Was ist (20 x 4 + 40 : 3 - 870) x 0 + 1?");
fieldAnswer.setText("");
fieldAnswer.setEditable(true);
textActualAnswer.setText("1");
textActualAnswer.setVisible(false);
correct = false;
show();
}
public boolean isCorrect() {
return correct;
}
}
[B]
Java:
[/B]
package fredontherungame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//import fredontherungame.Menu.TitleScreenHandler;
public class Menu extends Fred{
JFrame window;
Container con;
JPanel titleNamePanel, startButtonPanel, multiButtonPanel, quitButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 69);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 35);
Font quitFont = new Font ("Times New Roman", Font.PLAIN, 28);
JButton startButton;
JButton multiButton;
JButton quitButton;
// TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
new Menu();
}
public Menu() {
window = new JFrame();
window.setSize(1280, 720);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.DARK_GRAY);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(340, 100, 600, 150);
titleNamePanel.setBackground(Color.DARK_GRAY);
titleNameLabel = new JLabel("FRED ON THE RUN");
titleNameLabel.setForeground(Color.LIGHT_GRAY);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(540, 250, 200, 100);
startButtonPanel.setBackground(Color.DARK_GRAY);
startButton = new JButton("1-SPIELER");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
//startButton.addActionListener(tsHandler);
multiButtonPanel = new JPanel();
multiButtonPanel.setBounds(540, 400, 200, 100);
multiButtonPanel.setBackground(Color.DARK_GRAY);
multiButton = new JButton("2-SPIELER");
multiButton.setBackground(Color.black);
multiButton.setForeground(Color.white);
multiButton.setFont(normalFont);
quitButtonPanel = new JPanel();
quitButtonPanel.setBounds(540, 550, 200, 100);
quitButtonPanel.setBackground(Color.DARK_GRAY);
quitButton = new JButton("VERLASSEN");
quitButton.setBackground(Color.black);
quitButton.setForeground(Color.white);
quitButton.setFont(quitFont);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
multiButtonPanel.add(multiButton);
quitButtonPanel.add(quitButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
con.add(multiButtonPanel);
con.add(quitButtonPanel);
}
//titleNamePanel.setVisible(false);
//startButtonPanel.setVisible(false);
//alle weiteren panel ebenfalls
//public class TitleScreenHandler implements ActionListener{
//public void actionPerformed(ActionEvent event) {
}
[B][B]