Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.codef0x.snake;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.ArrayList;
- import java.util.Timer;
- import java.util.TimerTask;
- public class Board extends JPanel implements KeyListener {
- Snake player;
- Food food;
- ArrayList<SnakePart> snakeCoordinates;
- public Board() {
- this.snakeCoordinates = new ArrayList<>();
- snakeCoordinates.add(new SnakePart(150, 150));
- snakeCoordinates.add(new SnakePart(140, 150));
- snakeCoordinates.add(new SnakePart(130, 150));
- snakeCoordinates.add(new SnakePart(120, 150));
- this.player = new Snake(snakeCoordinates);
- this.food = new Food();
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame("Snake");
- frame.setDefaultCloseOperation(3);
- Board board = new Board();
- frame.add(board);
- frame.setSize(500, 500);
- frame.addKeyListener(new Board());
- frame.setVisible(true);
- board.run(board);
- }
- @Override
- public void paintComponent(Graphics g) {
- clear(g);
- this.player.coordinates.forEach(snakePart -> {
- g.setColor(Color.BLUE);
- g.fillRect(snakePart.x, snakePart.y, 10, 10);
- });
- g.setColor(Color.RED);
- g.fillRect(this.food.foodX, this.food.foodY, 10, 10);
- if (ateFood(this.food, g)) {
- this.food.spawn(getHeight(), getWidth());
- //this.player.grow();
- }
- }
- public void clear(Graphics g) {
- g.clearRect(0, 0, getHeight(), getWidth());
- }
- @Override
- public void update(Graphics g) {
- paintComponent(g);
- }
- public boolean ateFood(Food food, Graphics g) {
- if (this.player.coordinates.get(0).x == food.foodX && this.player.coordinates.get(0).y == food.foodY) {
- g.clearRect(food.foodX, food.foodY, 10, 10);
- return true;
- }
- return false;
- }
- public boolean hitSomething() {
- boolean hitLeftWall = this.player.coordinates.get(0).x < 10;
- boolean hitUpperWall = this.player.coordinates.get(0).y < 0;
- boolean hitRightWall = this.player.coordinates.get(0).x > getWidth() - 10;
- boolean hitLowerWall = this.player.coordinates.get(0).y > getHeight() - 10;
- for (int i = 1; i < 3; i++) {
- if (this.player.coordinates.get(i).x == this.player.coordinates.get(0).x && this.player.coordinates.get(i).y == this.player.coordinates.get(0).y) {
- return true;
- }
- }
- if (hitLeftWall || hitUpperWall || hitRightWall || hitLowerWall) {
- return true;
- }
- return false;
- }
- public void gameOver(Graphics g) {
- clear(g);
- g.setColor(Color.BLACK);
- g.setFont(new Font("TimesRoman", Font.PLAIN, 32));
- g.drawString("Game over!", getWidth() / 2 - 70, getHeight() / 2);
- }
- @Override
- public void keyPressed(KeyEvent event) {
- int keyCode = event.getKeyCode();
- System.out.println("Before altering:");
- System.out.println(player.dx + " " + player.dy);
- if (keyCode == 37) {
- this.player.dx = -10;
- this.player.dy = 0;
- } else if (keyCode == 38) {
- this.player.dx = 0;
- this.player.dy = -10;
- } else if (keyCode == 39) {
- this.player.dx = 10;
- this.player.dy = 0;
- } else if (keyCode == 40) {
- this.player.dx = 0;
- this.player.dy = 10;
- }
- System.out.println("After altering:");
- System.out.println(player.dx + " " + player.dy);
- }
- @Override
- public void keyTyped(KeyEvent event) {
- return;
- }
- @Override
- public void keyReleased(KeyEvent event) {
- return;
- }
- public void run(Board board) {
- Timer game = new Timer();
- game.schedule(new TimerTask() {
- boolean initiallySpawned = false;
- @Override
- public void run() {
- Graphics g = board.getGraphics();
- if (hitSomething()) {
- game.cancel();
- gameOver(g);
- return;
- }
- if (!initiallySpawned) {
- board.food.spawn(getHeight(), getWidth());
- initiallySpawned = true;
- }
- player.move();
- if (ateFood(food, g)) {
- food.spawn(getHeight(), getWidth());
- }
- update(g);
- }
- }, 0, 500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement