Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package com.milan.game.graphics;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JPanel;
  12.  
  13. import com.milan.game.entities.Apple;
  14. import com.milan.game.entities.BodyPart;
  15.  
  16. public class Screen extends JPanel implements Runnable {
  17.    
  18.     private static final long serialVersionUID = 1L;
  19.    
  20.     public static final int WIDTH = 800, HEIGHT = 800;
  21.     private Thread thread;
  22.     private boolean running = false;
  23.    
  24.     private BodyPart b;
  25.     private ArrayList<BodyPart> snake;
  26.    
  27.     private Apple apple;
  28.     private ArrayList<Apple> apples;
  29.    
  30.     private Random r;
  31.    
  32.     private int xCoor = 10, yCoor = 10;
  33.     private int size = 5;
  34.    
  35.     private boolean right = false, left = false, up = false, down = false;
  36.    
  37.     private int ticks = 0;
  38.    
  39.     private Key key;
  40.    
  41.     public Screen() {
  42.         setFocusable(true);
  43.         key = new Key();
  44.         addKeyListener(key);
  45.         setPreferredSize(new Dimension(WIDTH, HEIGHT));
  46.        
  47.         r = new Random();
  48.        
  49.         snake = new ArrayList<BodyPart>();
  50.         apples = new ArrayList<Apple>();
  51.        
  52.         start();
  53.     }
  54.    
  55.     public void tick(){
  56.         if(snake.size() == 0){
  57.             b = new BodyPart(xCoor, yCoor, 10);
  58.             snake.add(b);
  59.         }
  60.        
  61.         if(apples.size() == 0){
  62.             int xCoor = r.nextInt(79);
  63.             int yCoor = r.nextInt(79);
  64.            
  65.             apple = new Apple(xCoor, yCoor, 10);
  66.             apples.add(apple);
  67.         }
  68.        
  69.        
  70.         for(int i = 0; i < apples.size(); i++){
  71.             if(xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()){
  72.                 size++;
  73.                 apples.remove(i);
  74.                 i--;
  75.             }
  76.         }
  77.        
  78.         for(int i = 0; i < snake.size(); i++){
  79.             if(xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()){
  80.                 if(i != snake.size() - 1){
  81.                     stop();
  82.                 }
  83.             }
  84.         }
  85.        
  86.         ticks++;
  87.        
  88.         if(ticks > 500000){
  89.             if(right) xCoor++;
  90.             if(left) xCoor--;
  91.             if(up) yCoor--;
  92.             if(down) yCoor++;
  93.            
  94.             ticks = 0;
  95.            
  96.             b = new BodyPart(xCoor, yCoor, 10);
  97.             snake.add(b);
  98.            
  99.             if(snake.size() > size){
  100.                 snake.remove(0);
  101.             }
  102.         }
  103.        
  104.     }
  105.    
  106.     public void paint(Graphics g) {
  107.         g.clearRect(0, 0, WIDTH, HEIGHT);
  108.        
  109.         g.setColor(new Color(10, 50, 0));
  110.         g.fillRect(0, 0, WIDTH, HEIGHT);
  111.         g.setColor(Color.BLACK);
  112.         for(int i = 0; i < WIDTH / 10; i++){
  113.             g.drawLine(i * 10, 0, i * 10, HEIGHT);
  114.         }
  115.         for(int i = 0; i < HEIGHT / 10; i++) {
  116.             g.drawLine(0, i * 10, WIDTH, i * 10);
  117.         }
  118.        
  119.         for(int i = 0; i < snake.size(); i++){
  120.             snake.get(i).draw(g);
  121.         }
  122.        
  123.         for(int i = 0; i < apples.size(); i++){
  124.             apples.get(i).draw(g);
  125.         }
  126.     }
  127.    
  128.     public void start(){
  129.         running = true;
  130.         thread = new Thread(this, "Game Loop");
  131.         thread.start();
  132.     }
  133.    
  134.     public void stop(){
  135.             running = false;
  136.             try {
  137.                 thread.join();
  138.             } catch (InterruptedException e) {
  139.                 e.printStackTrace();
  140.             }
  141.         }
  142.    
  143.     public void run(){
  144.         while(running){
  145.             tick();
  146.             repaint();
  147.         }
  148.     }
  149.    
  150.     private class Key implements KeyListener {
  151.  
  152.        
  153.         public void keyPressed(KeyEvent e) {
  154.             int key = e.getKeyCode();
  155.            
  156.             if(key == KeyEvent.VK_RIGHT && !left){
  157.                 up = false;
  158.                 down = false;
  159.                 right = true;
  160.             }
  161.            
  162.             if(key == KeyEvent.VK_LEFT && !right){
  163.                 up = false;
  164.                 down = false;
  165.                 left = true;
  166.             }
  167.            
  168.             if(key == KeyEvent.VK_UP && !down){
  169.                 left = false;
  170.                 right = false;
  171.                 up = true;
  172.             }
  173.            
  174.             if(key == KeyEvent.VK_DOWN && !up){
  175.                 left = false;
  176.                 right = false;
  177.                 down = true;
  178.             }
  179.         }
  180.  
  181.         @Override
  182.         public void keyReleased(KeyEvent e) {
  183.             // TODO Auto-generated method stub
  184.            
  185.         }
  186.  
  187.         @Override
  188.         public void keyTyped(KeyEvent e) {
  189.             // TODO Auto-generated method stub
  190.            
  191.         }
  192.        
  193.     }
  194.    
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement