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.Image;
- 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 java.awt.Component;
- import javax.swing.*;
- import javafx.embed.swing.*;
- public class Board extends JPanel implements ActionListener {
- private final int B_WIDTH = 500;
- private final int B_HEIGHT = 500;
- private final int DOT_SIZE = 10;
- private final int ALL_DOTS = 25000;
- private final int RAND_POS = 48;
- private final int DELAY = 100;
- private final int x[] = new int[ALL_DOTS];
- private final int y[] = new int[ALL_DOTS];
- private int dots;
- private int dw = 1;
- private int bseCount;
- private int[] bse_x = new int[15];
- private int bs_x;
- private int bs_y;
- private int bsDir = 1;
- private int cnt = 0;
- private int cnt_h = 0;
- private int cnt_w = 0;
- private int apple_x;
- private int apple_y;
- private int heart_x;
- private int heart_y;
- private final int obstacle_x [] = new int[10];
- private final int obstacle_y[] = new int[10];
- private int r1 = (int)(Math.random() * RAND_POS);
- private int r2 = (int)(Math.random() * RAND_POS);
- private final int worm_x[] = new int [10];
- private final int worm_y[] = new int [10];
- private boolean leftDirection = false;
- private boolean rightDirection = true;
- private boolean upDirection = false;
- private boolean downDirection = false;
- public boolean inGame = true;
- private Timer timer;
- private Image ball;
- private Image apple;
- private Image head;
- private Image obs;
- private Image snake;
- private Image heart;
- public Board() {
- addKeyListener(new TAdapter());
- setBackground(Color.gray);
- setFocusable(true);
- setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
- loadImages();
- initGame();
- }
- private void loadImages() {
- ImageIcon iid = new ImageIcon("dot.png");
- ball = iid.getImage();
- ImageIcon iia = new ImageIcon("apple.png");
- apple = iia.getImage();
- ImageIcon iih = new ImageIcon("head.png");
- head = iih.getImage();
- ImageIcon iiobs = new ImageIcon("obs.phg");
- obs = iiobs.getImage();
- ImageIcon iiw = new ImageIcon("worm.png");
- snake = iiw.getImage();
- ImageIcon iiheart = new ImageIcon("heart.png");
- heart = iiheart.getImage();
- }
- private void initGame() {
- leftDirection = false;
- rightDirection = true;
- upDirection = false;
- downDirection = false;
- dots = 3;
- for (int z = 0; z < dots; z++) {
- x[z] = 250 - z * 10;
- y[z] = 250;
- }
- locateApple();
- localeHeart();
- locateObstacle();
- locateBlockSnake();;
- timer = new Timer(DELAY, this);
- timer.start();
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- doDrawing(g);
- }
- private void doDrawing(Graphics g) {
- if (inGame) {
- g.drawImage(apple, apple_x, apple_y, this);
- if(cnt%10 == 0) {
- g.drawImage(heart, heart_x, heart_y, this);
- }
- for(int i = 0; i < 10; ++i) {
- g.drawImage(this.snake, this.bse_x[i], this.bs_y, this);
- }
- for( int i = 0; i < 10 ; i++ ) {
- //g.drawImage(worm, worm_x[i] , worm_y[i] , this);
- g.drawImage(ball, obstacle_x[i], obstacle_y[i] , this);
- }
- for (int z = 0; z < dots; z++) {
- if (z == 0) {
- g.drawImage(head, x[z], y[z], this);
- } else {
- g.drawImage(ball, x[z], y[z], this);
- }
- }
- Toolkit.getDefaultToolkit().sync();
- } else {
- gameOver(g);
- }
- }
- private void gameOver(Graphics g) {
- String msgcnt = "eaten an apple - "+ (cnt - cnt_h);
- String msgheart = "eaten hearts - " + cnt_h;
- String msg = "Game Over";
- Font small = new Font("Helvetica", Font.BOLD, 14);
- FontMetrics metr = getFontMetrics(small);
- g.setColor(Color.white);
- g.setFont(small);
- g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
- g.drawString(msgcnt, 200,300 );
- g.drawString(msgheart, 200, 320);
- cnt = 0;
- cnt_h = 0;
- }
- private void checkApple() {
- if ((x[0] == apple_x) && (y[0] == apple_y)) {
- dots++;
- cnt++;
- locateApple();
- locateObstacle();
- }
- }
- private void checkHeart() {
- if ((x[0] == heart_x) && (y[0] == heart_y)) {
- dots+=3;
- cnt_h++;
- cnt++;
- if(cnt% 10== 0)localeHeart();
- locateObstacle();
- }
- }
- private void move() {
- for (int z = dots; z > 0; z--) {
- x[z] = x[(z - 1)];
- y[z] = y[(z - 1)];
- }
- if (leftDirection) {
- x[0] -= DOT_SIZE;
- }
- if (rightDirection) {
- x[0] += DOT_SIZE;
- }
- if (upDirection) {
- y[0] -= DOT_SIZE;
- }
- if (downDirection) {
- y[0] += DOT_SIZE;
- }
- this.bs_x += 10 * this.bsDir;
- for(int i = 0; i < 15; ++i) {
- if (bs_x >= 0 && bs_x <= 500) {
- this.bse_x[i] = this.bs_x + i * 10;
- }
- if (this.bs_x + 15 * 10 == 600) {
- this.bsDir = -1;
- }
- if (this.bs_x == 0) {
- this.bsDir = 1;
- }
- }
- }
- private void checkObstacle() {
- for(int i = 0 ; i < 10 ; i++) {
- if(x[0] == obstacle_x[i] && y[0] == obstacle_y[i]) {
- inGame = false;
- timer.stop();
- }
- }
- }
- private void checkCollision() {
- for (int z = dots; z > 0; z--) {
- if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
- inGame = false;
- }
- }
- if (y[0] >= B_HEIGHT) {
- inGame = false;
- }
- if (y[0] < 0) {
- inGame = false;
- }
- if (x[0] >= B_WIDTH) {
- inGame = false;
- }
- if (x[0] < 0) {
- inGame = false;
- }
- if(!inGame) {
- timer.stop();
- }
- }
- private void locateApple() {
- int r = (int) (Math.random() * RAND_POS);
- apple_x = ((r * DOT_SIZE));
- r = (int) (Math.random() * RAND_POS);
- apple_y = ((r * DOT_SIZE));
- }
- private void locateObstacle() {
- for(int i = 0 ; i < 10 ; i++) {
- int r = (int)(Math.random() * RAND_POS);
- obstacle_x[i] = r * DOT_SIZE;
- r = (int)(Math.random() * RAND_POS);
- obstacle_y[i] = r* DOT_SIZE;
- }
- }
- private void checkBlockWorm() {
- for(int i = 0; i < 15; ++i) {
- for(int j = 0; j <100; ++j) {
- if (this.x[j] == this.bse_x[i] && this.y[j] == this.bs_y) {
- if (j > 1) {
- this.dots = j;
- } else {
- this.inGame = false;
- this.timer.stop();
- }
- }
- }
- }
- }
- private void locateBlockSnake() {
- bs_y = (int)(Math.random() * 600.0D / 10.0D) - 2;
- bs_y *= 10;
- bs_x = 0;
- for(int i = 0; i < 15; ++i) {
- this.bse_x[i] = this.bs_x + i * 10;
- }
- }
- private void localeHeart() {
- if(cnt % 10 == 0) {
- int r = (int) (Math.random() * RAND_POS);
- heart_x = ((r * DOT_SIZE));
- r = (int) (Math.random() * RAND_POS);
- heart_y = ((r * DOT_SIZE));
- }
- }
- public void actionPerformed(ActionEvent e) {
- if (inGame) {
- checkApple();
- checkCollision();
- checkHeart();
- checkObstacle();
- checkBlockWorm();
- move();
- }
- repaint();
- }
- private class TAdapter extends KeyAdapter {
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && (!rightDirection)) {
- leftDirection = true;
- upDirection = false;
- downDirection = false;
- }
- if ((key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) && (!leftDirection)) {
- rightDirection = true;
- upDirection = false;
- downDirection = false;
- }
- if ((key == KeyEvent.VK_UP ||key == KeyEvent.VK_W) && (!downDirection)) {
- upDirection = true;
- rightDirection = false;
- leftDirection = false;
- }
- if ((key == KeyEvent.VK_DOWN|| key == KeyEvent.VK_S) && (!upDirection)) {
- downDirection = true;
- rightDirection = false;
- leftDirection = false;
- }
- if((key == KeyEvent.VK_ENTER)&&(!inGame)) {
- inGame = true;
- initGame();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement