Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. public class MainWindow extends JFrame {
  4.  
  5.     public MainWindow(){
  6.         setTitle("SNAKE");
  7.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  8.         setSize(360,360);
  9.         setLocation(400,400);
  10.         add(new GameField());
  11.         setVisible(true);
  12.     }
  13.     public static void main(String[] args){
  14.         MainWindow mw = new MainWindow();
  15.     }
  16. }
  17.  
  18.  
  19.  
  20.  
  21.  
  22. import javax.swing.*;
  23. import java.awt.*;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.KeyAdapter;
  27. import java.awt.event.KeyEvent;
  28. import java.util.Random;
  29.  
  30. public class GameField extends JPanel implements ActionListener {
  31.  
  32.     private final int SIZE = 320;
  33.     private final int DOT_SIZE = 16;
  34.     private final int ALL_DOTS = 400;
  35.     private Image dot;
  36.     private Image apple;
  37.     private int appleX;
  38.     private int appleY;
  39.     private int[] x = new int[ALL_DOTS];
  40.     private int[] y = new int[ALL_DOTS];
  41.     private int dots;
  42.     private Timer timer;
  43.     private boolean left = false;
  44.     private boolean right = true;
  45.     private boolean up = false;
  46.     private boolean down = false;
  47.     private boolean inGame = true;
  48.  
  49.     public GameField(){
  50.         setBackground(Color.BLACK);
  51.         loadImages();
  52.         initGame();
  53.         addKeyListener(new FieldKeyListener());
  54.         setFocusable(true);
  55.     }
  56.  
  57.     public void initGame(){
  58.         dots = 3;
  59.         for (int i =0;i<dots;i++){
  60.             x[i] = 48 - i*DOT_SIZE;
  61.             y[i] = 48;
  62.         }
  63.         timer = new Timer(150,this);
  64.         timer.start();
  65.         createApple();
  66.     }
  67.  
  68.     public void createApple(){
  69.         appleX = new Random().nextInt(20)*DOT_SIZE;
  70.         appleY = new Random().nextInt(20)*DOT_SIZE;
  71.     }
  72.  
  73.  
  74.     public void loadImages(){
  75.         ImageIcon iia = new ImageIcon("apple.png");
  76.         apple = iia.getImage();
  77.         ImageIcon iib = new ImageIcon("dot.png");
  78.         dot = iib.getImage();
  79.     }
  80.  
  81.     @Override
  82.     protected void paintComponent(Graphics g) {
  83.         super.paintComponent(g);
  84.         if(inGame){
  85.             g.drawImage(apple,appleX,appleY,this);
  86.             for (int i = 0; i < dots ; i++) {
  87.                 g.drawImage(dot,x[i],y[i],this);
  88.             }
  89.         }else{
  90.             String str = "Game Over";
  91.             //Font f = new Font("Arial",14,Font.BOLD);
  92.             g.setColor(Color.white);
  93.            // g.setFont(f);
  94.             g.drawString(str,125,SIZE/2);
  95.         }
  96.     }
  97.  
  98.     public void move(){
  99.         for (int i = dots; i > 0; i--) {
  100.             x[i] = x[i-1];
  101.             y[i] = y[i-1];
  102.         }
  103.         if(left){
  104.             x[0] -= DOT_SIZE;
  105.         }
  106.         if(right){
  107.             x[0] += DOT_SIZE;
  108.         }
  109.         if(up){
  110.             y[0] -= DOT_SIZE;
  111.         }
  112.         if(down){
  113.             y[0] += DOT_SIZE;
  114.         }
  115.     }
  116.  
  117.     public void checkApple(){
  118.         if(x[0] == appleX && y[0] == appleY){
  119.             dots++;
  120.             createApple();
  121.         }
  122.     }
  123.  
  124.     public void checkCollisions(){
  125.         for (int i = dots; i >0 ; i--) {
  126.             if(i>4 && x[0] == x[i] && y[0] == y[i]){
  127.                 inGame = false;
  128.             }
  129.         }
  130.         if (x[0]>SIZE){
  131.                 inGame = false;
  132.         }
  133.         if (x[0]<0){
  134.             inGame = false;
  135.         }
  136.         if (y[0]>SIZE){
  137.             inGame = false;
  138.         }
  139.         if (y[0]<0){
  140.             inGame = false;
  141.         }
  142.     }
  143.  
  144.     @Override
  145.     public void actionPerformed(ActionEvent e) {
  146.         if(inGame){
  147.             checkApple();
  148.             checkCollisions();
  149.             move();
  150.         }
  151.         repaint();
  152.     }
  153. class FieldKeyListener extends KeyAdapter{
  154.     @Override
  155.     public void keyPressed(KeyEvent e) {
  156.         super.keyPressed(e);
  157.         int key = e.getKeyCode();
  158.         if(key == KeyEvent.VK_LEFT && !right){
  159.             left = true;
  160.             up = false;
  161.             down = false;
  162.         }
  163.         if(key == KeyEvent.VK_RIGHT && !left){
  164.             right = true;
  165.             up = false;
  166.             down = false;
  167.         }
  168.         if(key == KeyEvent.VK_UP && !down){
  169.             right = false;
  170.             up = true;
  171.             left = false;
  172.         }
  173.         if(key == KeyEvent.VK_DOWN && !up){
  174.             right = false;
  175.             left = false;
  176.             down = true;
  177.         }
  178.     }
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement