Collision checking for outer bounds works apparentlly solely on Windows, commented out for now #1

Merged
delvh merged 5 commits from f/collision_checking into develop 2020-04-03 10:43:25 +02:00
6 changed files with 10 additions and 218 deletions
Showing only changes of commit 3723c333e1 - Show all commits

View File

@ -1,10 +1,6 @@
package dev.lh;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.*;
import dev.lh.ui.GameWindow;
@ -227,8 +223,7 @@ public class FoodFactory {
case blue:
snakeAdditionalLength = 1;
break;
}// switch
}
return snakeAdditionalLength;
}
}

View File

@ -38,5 +38,4 @@ public class Main {
* @since Snake 1.0
*/
public static GameWindow getGame() { return game; }
}

View File

@ -29,5 +29,4 @@ public interface Updateable {
* @since Snake 1.0
*/
void render(Graphics g);
}

View File

@ -52,114 +52,26 @@ public class Endscreen extends JDialog {
} catch (Exception e) {
e.printStackTrace();
}
// JScrollPane scrollPane = new JScrollPane();
// scrollPane.setBounds(10, 412, 349, 238);
// contentPanel.add(scrollPane);
// String[][] combis = new String[highscorePoints.length][2];
// for (int i = 0; i < highscorePoints.length; i++) {
// combis[i][0] = highscorePlayers[i];
// combis[i][1] = String.valueOf(highscorePoints[i]);
// }
// table = new JTable(combis, tableTitle);
// table.setRowSelectionAllowed(false);
// table.setFillsViewportHeight(true);
// table.setFont(new Font("Times New Roman", Font.PLAIN, 15));
// scrollPane.setViewportView(table);
// JLabel lblNewLabel = new JLabel("Highscores");
// lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC,
// 18));
// lblNewLabel.setBounds(65, 292, 98, 41);
// contentPanel.add(lblNewLabel);
JButton btnNewButton = new JButton("Play again");
btnNewButton.setMnemonic(KeyEvent.VK_ENTER);
btnNewButton.addActionListener(e -> { Main.startGame(); dispose(); });
contentPanel.setLayout(new BorderLayout(0, 0));
btnNewButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
// btnNewButton.setIcon(new
// ImageIcon(ClassLoader.getSystemResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
btnNewButton.setIconTextGap(5);
// Endscreen.class.getResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png")));
btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 15));
contentPanel.add(btnNewButton, BorderLayout.SOUTH);
// JButton btnClose = new JButton("Close game");
//
// btnClose.addActionListener(new ActionListener() {// Beginn Listener new game
// public void actionPerformed(ActionEvent e) {
// System.exit(0);
// }
// });
//
// btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 15));
// btnClose.setBounds(400, 500, 200, 100);
// contentPanel.add(btnClose);
//
// tfName = new JTextField();
// tfName.setFont(new Font("Times New Roman", Font.PLAIN, 15));
// tfName.setBounds(102, 344, 257, 40);
// contentPanel.add(tfName);
// tfName.setColumns(10);
//
// JTextArea txtrBitteErstNamen = new JTextArea();
// txtrBitteErstNamen.setVisible(false);
// txtrBitteErstNamen.setBackground(UIManager.getColor("ScrollBar.foreground"));
// txtrBitteErstNamen.setText("Bitte erst Namen \r\neingeben und\r\ndann
// Speichern!!!!");
// txtrBitteErstNamen.setBounds(468, 412, 155, 92);
// contentPanel.add(txtrBitteErstNamen);
// txtrBitteErstNamen.setVisible(false);
//
// JButton btnSaveHighscore = new JButton("Save Highscore");
// btnSaveHighscore.setMnemonic(KeyEvent.VK_ENTER);
// btnSaveHighscore.setIconTextGap(5);
// btnSaveHighscore.setIcon(
// new
// ImageIcon(Endscreen.class.getResource("/javax/swing/plaf/metal/icons/ocean/floppy.gif")));
// btnSaveHighscore.setFont(new Font("Times New Roman", Font.PLAIN, 15));
//
// btnSaveHighscore.addActionListener(new ActionListener() {// Beginn Listener
// Save Highscore
// public void actionPerformed(ActionEvent e) {
// relocate(score1);
// writeFiles();
// table.updateUI();
// }
//
// });
//
// btnSaveHighscore.setBounds(468, 344, 155, 50);
// contentPanel.add(btnSaveHighscore);
JLabel lblDeinPunktestand = new JLabel("Dein Punktestand: " + String.valueOf(score));
lblDeinPunktestand.setFont(new Font("Times New Roman", Font.PLAIN, 25));
contentPanel.add(lblDeinPunktestand, BorderLayout.NORTH);
// JLabel lblYourName = new JLabel("Your Name:");
// lblYourName.setFont(new Font("Times New Roman", Font.PLAIN, 15));
// lblYourName.setBounds(10, 355, 82, 29);
// contentPanel.add(lblYourName);
Image resultImage = Toolkit.getDefaultToolkit()
.getImage(this.getClass()
.getResource((score < goodOrBadResult) ? "/Snake/src/main/resources/Try_Again.jpg" : "/Snake/src/main/resources/1211548-200.png"));
resultImage.flush();
// JCheckBox chckbxNewCheckBox = new JCheckBox("");
// JLabel lblDasIstEin = new JLabel("Das ist ein hervorragender Wert!");
// lblDasIstEin.setFont(new Font("Times New Roman", Font.PLAIN, 15));
// if (score >= goodOrBadResult) {
// chckbxNewCheckBox.setIcon(new
// ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/1211548-200.png")));
// chckbxNewCheckBox.setBounds(300, 200, 200, 200);
// lblDasIstEin.setBounds(10, 100, 212, 50);
// } else {
// chckbxNewCheckBox.setIcon(new
// ImageIcon(ClassLoader.getSystemResource("/dev/lh/snake/Try_Again.jpg")));
// chckbxNewCheckBox.setBounds(300, 200, 250, 210);
// lblDasIstEin.setText("Das kannst du aber noch verbessern!");
// lblDasIstEin.setBounds(10, 100, 240, 50);
// }
// contentPanel.add(chckbxNewCheckBox, BorderLayout.CENTER);
// contentPanel.add(lblDasIstEin, BorderLayout.EAST);
setVisible(true);
}
@ -174,81 +86,4 @@ public class Endscreen extends JDialog {
* @since Snake 1.0
*/
public void setScore(int score) { this.score = score; }
/*
* public static void readInHighscoresPoints() { try { // FileReader reads text
* files in the default encoding. FileReader fileReader = new
* FileReader(fileNamePoints);
* // Always wrap FileReader in BufferedReader. BufferedReader bufferedReader =
* new BufferedReader(fileReader);
* currentIndex = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i
* < currentIndex; i++) { highscorePoints[i]=
* Integer.parseInt(bufferedReader.readLine()); } // Always close files.
* bufferedReader.close(); fileReader.close(); } catch (FileNotFoundException
* ex) { System.out.println("Error 404:File '" + fileNamePoints +
* "' not found");
* } catch (IOException ex) { System.out.println("Error reading file '" +
* fileNamePoints + "'"); ex.printStackTrace(); } } private void
* readInHighscoresPlayers(){ try { // FileReader reads text files in the
* default encoding. FileReader fileReader = new FileReader(fileNamePlayers);
* // Always wrap FileReader in BufferedReader. BufferedReader bufferedReader =
* new BufferedReader(fileReader);
* for (int i = 0; i < currentIndex; i++) { highscorePlayers[i]=
* bufferedReader.readLine(); } // Always close files. bufferedReader.close();
* fileReader.close(); } catch (FileNotFoundException ex) {
* System.out.println("Error 404:File '" + fileNamePlayers + "' not found");
* } catch (IOException ex) { System.out.println("Error reading file '" +
* fileNamePlayers + "'"); ex.printStackTrace(); } } /* private void
* writeFiles() { File dateiPoints = new File("." + File.separator +
* fileNamePoints); FileWriter fwpoints = null; BufferedWriter bwpoints = null;
* try { fwpoints = new FileWriter(dateiPoints); bwpoints = new
* BufferedWriter(fwpoints); bwpoints.write(highscorePoints.length); for (int
* i=0;i<highscorePoints.length;i++) { bwpoints.write(highscorePoints[i]); } }
* catch (Exception e1) { e1.printStackTrace(); } finally { try {
* bwpoints.close(); fwpoints.close(); alreadySaved = true; } catch (IOException
* e2) { e2.printStackTrace(); } }
* File dateiPlayers = new File("." + File.separator + fileNamePlayers);
* FileWriter fwplayers = null; BufferedWriter bwplayers = null; try { fwplayers
* = new FileWriter(dateiPlayers); bwplayers = new BufferedWriter(fwplayers);
* for (int i=0;i<highscorePlayers.length;i++) {
* bwplayers.write(highscorePlayers[i]); } } catch (Exception e1) {
* e1.printStackTrace(); } finally { try { bwplayers.close(); fwplayers.close();
* alreadySaved = true; } catch (IOException e2) { e2.printStackTrace(); } }
* }
* /** Launch the application.
*/
/*
* public void relocate(int newScore) {
* String newPlayer = new String(tfName.getText()); if (newPlayer.equals("")) {
* txtrBitteErstNamen.setVisible(true); return; } else { sortFiles(newScore,
* newPlayer); } }
* private void sortFiles(int newScore, String newPlayer) { if
* (highscorePoints.length==highscorePlayers.length&&
* highscorePoints.length<=30) { for(int i=0;i<highscorePoints.length;i++) {
* if(highscorePoints[i]<newScore) { int tmp=highscorePoints[i];
* highscorePoints[i]=newScore; for(int k=i+1;k<highscorePoints.length;k++) {
* int tmp2=highscorePoints[k]; highscorePoints[k]=tmp; tmp=tmp2; } String
* temp=highscorePlayers[i]; highscorePlayers[i]=newPlayer; for(int
* k=i+1;k<highscorePlayers.length;k++) { String temp2=highscorePlayers[k];
* highscorePlayers[k]=temp; temp=temp2; } return; } } } else
* if(highscorePoints.length==highscorePlayers.length&&highscorePoints.length>=
* 30){ for (int i=30;i<highscorePoints.length;i++) { highscorePoints[i]=null;
* highscorePlayers[i]=null; } for(int i=0;i<highscorePoints.length;i++) {
* if(highscorePoints[i]<newScore) { int tmp=highscorePoints[i];
* highscorePoints[i]=newScore; for(int k=i+1;k<highscorePoints.length;k++) {
* int tmp2=highscorePoints[k]; highscorePoints[k]=tmp; tmp=tmp2; } String
* temp=highscorePlayers[i]; highscorePlayers[i]=newPlayer; for(int
* k=i+1;k<highscorePlayers.length;k++) { String temp2=highscorePlayers[k];
* highscorePlayers[k]=temp; temp=temp2; } return; } } } }
*/
/*
* private void relocate(int toCompare, boolean is30) { if(is30) { for(int i=0;
* i<30;i++) { if(temp[i]<toCompare) { int tmp=temp[i]; temp[i]=toCompare;
* for(int k=i+1;k<temp.length;k++) { int tmp2=temp[k]; temp[k]=tmp; tmp=tmp2; }
* arrange(temp); return; } else { temp[30]=toCompare; arrange(temp); } } } else
* { for(int i=0; i<temp.length;i++) { if(temp[i]<toCompare) { int tmp=temp[i];
* temp[i]=toCompare; for(int k=i+1;k<temp.length;k++) { int tmp2=temp[k];
* temp[k]=tmp; tmp=tmp2; } arrange(temp); return; } else { temp[30]=toCompare;
* arrange(temp); }
* } } }
*/
}
}

View File

@ -1,10 +1,6 @@
package dev.lh.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
@ -89,7 +85,7 @@ public class GameWindow extends JFrame {
});
Timer timer = new Timer(50,
(evt) -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); });
evt -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); });
timer.start();
setVisible(true);
@ -112,4 +108,4 @@ public class GameWindow extends JFrame {
* @since Snake 1.1
*/
public void close() { dispose(); }
}
}

View File

@ -4,10 +4,7 @@ import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import dev.lh.Main;
@ -47,35 +44,6 @@ public class StartScreen extends JFrame {
});
}
// public static void readInHighscores() {
// try {
// combination.clear();
// // FileReader reads text files in the default encoding.
// FileReader fileReader = new FileReader(fileName);
//
// // Always wrap FileReader in BufferedReader.
// BufferedReader bufferedReader = new BufferedReader(fileReader);
//
// currentIndex = Integer.parseInt(bufferedReader.readLine());
// for (int i = 0; i < currentIndex; i++) {
// String[] spielerScore = new String[2];
// spielerScore[0] = bufferedReader.readLine();
// spielerScore[1] = bufferedReader.readLine();
// combination.add(spielerScore);
// }
// // Always close files.
// bufferedReader.close();
// fileReader.close();
// } catch (FileNotFoundException ex) {
// System.out.println("Error 404:File '" + fileName + "' not found");
//
// } catch (IOException ex) {
// System.out.println("Error reading file '" + fileName + "'");
// ex.printStackTrace();
//
// }
// }
/**
* Create the frame.
*/