Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- @SuppressWarnings("serial")
- public class Board extends JPanel implements ActionListener {
- // größe board
- private final static int BOARDWIDTH = 1000;
- private final static int BOARDHEIGHT = 980;
- //joint / food größe
- private final static int PIXELSIZE = 25;
- private final static int TOTALPIXELS = (BOARDWIDTH * BOARDHEIGHT)
- / (PIXELSIZE * PIXELSIZE);
- // Check if running
- private boolean inGame = true;
- // Timer für ticks
- private Timer timer;
- //schwierigkeitsgrad, höher, gleich leichter
- private static int speed = 100;
- private int lengthofsnake = snake.getJoints;
- // Instances of our snake & food so we can use their methods
- private Snake snake = new Snake();
- private Food food = new Food();
- public Board() {
- addKeyListener(new Keys());
- setBackground(Color.BLACK);
- setFocusable(true);
- setPreferredSize(new Dimension(BOARDWIDTH, BOARDHEIGHT));
- initializeGame();
- //sound
- Sound.BACK.play();
- }
- // Zeichnen
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- draw(g);
- }
- // Draw our Snake & Food (Called on repaint()).
- void draw(Graphics g) {
- // Only draw if the game is running / the snake is alive
- if (inGame == true) {
- g.setColor(Color.white);
- g.drawString("Length: " + lengthofsnake, 500, 500);
- g.setColor(Color.green);
- g.fillRect(food.getFoodX(), food.getFoodY(), PIXELSIZE, PIXELSIZE); // food
- // Draw our snake.
- for (int i = 0; i < snake.getJoints(); i++) {
- // Snake's head
- if (i == 0) {
- g.setColor(Color.RED);
- g.fillRect(snake.getSnakeX(i), snake.getSnakeY(i),
- PIXELSIZE, PIXELSIZE);
- // Body of snake
- } else {
- g.fillRect(snake.getSnakeX(i), snake.getSnakeY(i),
- PIXELSIZE, PIXELSIZE);
- }
- }
- // Sync our graphics together
- Toolkit.getDefaultToolkit().sync();
- } else {
- // If we're not alive, then we end our game
- endGame(g);
- }
- }
- void initializeGame() {
- snake.setJoints(3); // anfang
- for (int i = 0; i < snake.getJoints(); i++) {
- snake.setSnakeX(BOARDWIDTH / 2);
- snake.setSnakeY(BOARDHEIGHT / 2);
- }
- snake.setMovingRight(true);
- food.createFood();
- // set the timer to record our game's speed / make the game move
- timer = new Timer(speed, this);
- timer.start();
- }
- // nähe food
- void checkFoodCollisions() {
- if ((proximity(snake.getSnakeX(0), food.getFoodX(), 20))
- && (proximity(snake.getSnakeY(0), food.getFoodY(), 20))) {
- //sound
- Sound.FC.play();
- System.out.println("intersection");
- snake.setJoints(snake.getJoints() + 1);
- food.createFood();
- }
- }
- //collision mit snake
- void checkCollisions() {
- for (int i = snake.getJoints(); i > 0; i--) {
- if ((i > 5)
- && (snake.getSnakeX(0) == snake.getSnakeX(i) && (snake
- .getSnakeY(0) == snake.getSnakeY(i)))) {
- inGame = false;
- }
- }
- // Intersect board
- if (snake.getSnakeY(0) >= BOARDHEIGHT) {
- inGame = false;
- }
- if (snake.getSnakeY(0) < 0) {
- inGame = false;
- }
- if (snake.getSnakeX(0) >= BOARDWIDTH) {
- inGame = false;
- }
- if (snake.getSnakeX(0) < 0) {
- inGame = false;
- }
- // If the game has ended, then we can stop our timer
- if (!inGame) {
- timer.stop();
- }
- }
- void endGame(Graphics g) {
- //sound
- Sound.BACK.stop();
- Sound.END.play();
- String message = "Game over";
- // Create a new font instance
- Font font = new Font("Times New Roman", Font.BOLD, 80);
- FontMetrics metrics = getFontMetrics(font);
- // Set the color of the text to red, and set the font
- g.setColor(Color.red);
- g.setFont(font);
- // Draw the message to the board
- g.drawString(message, (BOARDWIDTH - metrics.stringWidth(message)) / 2,
- BOARDHEIGHT / 2);
- System.out.println("Game Ended");
- String message1 = "Press Enter for restart";
- Font font1 = new Font("Times New Roman", Font.BOLD, 80);
- FontMetrics metrics1 = getFontMetrics(font1);
- g.setColor(Color.red);
- g.setFont(font1);
- g.drawString(message1, (BOARDWIDTH - metrics1.stringWidth(message1)) / 2,
- BOARDHEIGHT -100);
- }
- // Run constantly as long as we're in game.
- @Override
- public void actionPerformed(ActionEvent e) {
- if (inGame == true) {
- checkFoodCollisions();
- checkCollisions();
- snake.move();
- System.out.println(snake.getSnakeX(0) + " " + snake.getSnakeY(0)
- + " " + food.getFoodX() + ", " + food.getFoodY());
- }
- // Repaint or 'render' our screen
- repaint();
- }
- private class Keys extends KeyAdapter {
- @Override
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if ((key == KeyEvent.VK_LEFT) && (!snake.isMovingRight())) {
- snake.setMovingLeft(true);
- snake.setMovingUp(false);
- snake.setMovingDown(false);
- }
- if ((key == KeyEvent.VK_RIGHT) && (!snake.isMovingLeft())) {
- snake.setMovingRight(true);
- snake.setMovingUp(false);
- snake.setMovingDown(false);
- }
- if ((key == KeyEvent.VK_UP) && (!snake.isMovingDown())) {
- snake.setMovingUp(true);
- snake.setMovingRight(false);
- snake.setMovingLeft(false);
- }
- if ((key == KeyEvent.VK_DOWN) && (!snake.isMovingUp())) {
- snake.setMovingDown(true);
- snake.setMovingRight(false);
- snake.setMovingLeft(false);
- }
- if ((key == KeyEvent.VK_NUMPAD1)){
- tM = 100;
- timer.stop();
- timer.setDelay( tM );
- timer.start();
- }
- if ((key == KeyEvent.VK_NUMPAD2)){
- tM = 1000;
- timer.stop();
- timer.setDelay( tM );
- timer.start();
- }
- if (key == KeyEvent.VK_SPACE) {
- try {
- Thread.sleep(1500);
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- //pause text
- }
- if ((key == KeyEvent.VK_ENTER) && (inGame == false)) {
- inGame = true;
- snake.setMovingDown(false);
- snake.setMovingRight(false);
- snake.setMovingLeft(false);
- snake.setMovingUp(false);
- initializeGame();
- Sound.BACK.play();
- }
- }
- }
- private boolean proximity(int a, int b, int closeness) {
- return Math.abs((long) a - b) <= closeness;
- }
- public static int getAllDots() {
- return TOTALPIXELS;
- }
- public static int getDotSize() {
- return PIXELSIZE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement