Guest User

openstudy.com/study#/updates/54a40046e4b0e4319f8f00d7

a guest
Jan 1st, 2015
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1. -----------------------------------------------------------------
  2. post 1
  3. -----------------------------------------------------------------
  4. package newpackage.graphics;
  5.  
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.util.ArrayList;
  11. import java.util.Random;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import newpackage.entities.Apple;
  15. import newpackage.entities.BodyPart;
  16. import newpackage.entities.Direction;
  17. import newpackage.entities.Snake;
  18.  
  19. public class Screen extends JPanel implements Runnable {
  20.    
  21.     public static final int WIDTH=800,HEIGHT=600;
  22.     private Thread thread;
  23.     private boolean running=false;
  24.    
  25.     private BodyPart b;
  26.     private ArrayList<BodyPart>snake;
  27.    
  28.     private Apple apple;
  29.     private ArrayList<Apple> apples;
  30.    
  31.     private Random r;
  32.    
  33.     private int xCoor=10;
  34.     private int yCoor=10;
  35.     private int size=5;
  36.     private boolean right =true,left=false,up=false,down=false;
  37.     private int ticks=0;
  38.     private Key key;
  39.     //private Direction key1;
  40.    
  41.    
  42.    
  43.     public Screen() {
  44.         setFocusable(true);
  45.         key = new Key();
  46.         //key1 = new Direction();
  47.         addKeyListener(key);
  48.         //addKeyListener(key1.getKey());
  49.         setPreferredSize(new Dimension(WIDTH,HEIGHT));
  50.        
  51.         r=new Random();
  52.        
  53.         //Snake s=new Snake();
  54.        
  55.         //s.
  56.         start();
  57.         thread = new Thread(this, "game loop");
  58.         thread.start();
  59.     }
  60.  
  61.     public void tick() {
  62.         if(snake.size()==0) {
  63.             b= new BodyPart(xCoor, yCoor, 10);
  64.             snake.add(b);
  65.         }
  66.        
  67.         if(apples.size()==0) {
  68.             //79x79
  69.             int xCoor= r.nextInt(60);
  70.             int yCoor= r.nextInt(60);
  71.            
  72.             apple=new Apple(xCoor, yCoor, 10);
  73.             apples.add(apple);
  74.         }
  75.        
  76.         for(int i=0;i<apples.size();i++) {
  77.             //kada x i y koord zmije dotaknu poziciju jabuke, velicina se povecava a jabuka brise -don't read this
  78.             if(xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()) {
  79.                 size++;
  80.                 apples.remove(i);
  81.                 i--;
  82.            
  83.             }
  84.        
  85.         }
  86.        
  87.         //max size=15-don't read this
  88.         for(int i=0;i<snake.size();i++) {
  89.             if(xCoor==snake.get(i).getxCoor() && yCoor==snake.get(i).getyCoor()) {
  90.                 if(i != snake.size()-1) {
  91.                     stop();
  92.                 }
  93.             }
  94.        
  95.         }
  96.        
  97.         if(xCoor < 0 || xCoor > 79 || yCoor < 0 || yCoor > 60) {
  98.             stop();
  99.         }
  100.        
  101.         ticks++;
  102.        
  103.         //brzina kretanja zmije-don't read this
  104.         //prvobitno je bila postavljena na 250000-don't read this
  105.         if(ticks>690000) {
  106.             //pravci u kojima se krece zmija,x-osa (+ i -) i y-psa (+ i -)-don't read this
  107.             if(right) xCoor++;
  108.             if(left) xCoor--;
  109.             if(up) yCoor--;
  110.             if(down) yCoor++;
  111.                
  112.             ticks=0;
  113.             b=new BodyPart(xCoor, yCoor, 10);
  114.             snake.add(b);
  115.            
  116.             if(snake.size()>size) {
  117.                 snake.remove(0);
  118.             }
  119.        
  120.         }
  121.        
  122.     }
  123.    
  124.     public void paint(Graphics g) {
  125.         g.clearRect(0, 0, WIDTH, HEIGHT);
  126.        
  127.         g.setColor(Color.lightGray);
  128.         //DrawLine(int x1, int y1, int x2, int y2) -don't read this
  129.           //vraca DrawLine za liniju koja pocinje u (x1, y1) i zavrsava se u (x2, y2).-don't read this
  130.         for(int i=0;i< WIDTH /10;i++) {
  131.             g.drawLine(i*10, 0, i*10, HEIGHT);
  132.        
  133.         }
  134.        
  135.         for(int i=0;i< HEIGHT /10;i++) {
  136.             g.drawLine(0,i*10,WIDTH, i*10);
  137.        
  138.         }
  139.        
  140.         for(int i=0;i< snake.size();i++) {
  141.             snake.get(i).draw(g);
  142.         }
  143.        
  144.         for(int i=0;i<apples.size();i++) {
  145.             apples.get(i).draw(g);
  146.        
  147.         }
  148.     }
  149.    
  150.     public void start() {
  151.         xCoor=10;
  152.         yCoor=10;
  153.         size=5;
  154.         right =true;
  155.         left=false;
  156.         up=false;
  157.         down=false;
  158.         ticks=0;
  159.         snake=new ArrayList<BodyPart>();
  160.         apples=new ArrayList<Apple>();
  161.         running = true;
  162.        
  163.     }
  164.    
  165.     public void stop() {
  166.         int i = JOptionPane.showOptionDialog(this, "Do you want to play again?", "GAME OVER", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
  167.         if (i == JOptionPane.YES_OPTION) {
  168.             //Snake s=new Snake();
  169.             //s.
  170.             start();
  171.         } else {
  172.             System.exit(0);
  173.         }
  174.     }
  175.    
  176.     public void run() {
  177.         //dok je pokrenut program, koristi tick metodu i repaint-don't read this
  178.         while (running) {
  179.             //Snake s=new Snake();
  180.             //s.
  181.             tick();
  182.             repaint();
  183.            
  184.            
  185.         }
  186.        
  187.     }
  188.    
  189.     private class Key implements KeyListener {
  190.         //proverava koje je dugme pritisnuto na tastaturi i to dugme koristi za pravac
  191.         public void keyPressed(KeyEvent e) {
  192.             int key=e.getKeyCode();
  193.             if(key==KeyEvent.VK_RIGHT && !left) {
  194.                 up=false;
  195.                 down=false;
  196.                 right=true;
  197.             }
  198.             if(key==KeyEvent.VK_LEFT && !right) {
  199.                 up=false;
  200.                 down=false;
  201.                 left=true;
  202.             }
  203.             if(key==KeyEvent.VK_UP && !down) {
  204.                 left=false;
  205.                 right=false;
  206.                 up=true;
  207.             }
  208.             if(key==KeyEvent.VK_DOWN && !up) {
  209.                 left=false;
  210.                 right=false;
  211.                 down=true;
  212.             }
  213.  
  214.         }
  215.        
  216.         public void keyReleased(KeyEvent e) {
  217.              //To change body of generated methods, choose Tools | Templates.
  218.         }
  219.        
  220.         public void keyTyped(KeyEvent e) {
  221.             //To change body of generated methods, choose Tools | Templates.
  222.         }
  223.     }
  224. }
  225.  
  226.  
  227. ------------------------------------------------------
  228. post 2
  229. ------------------------------------------------------
  230. package newpackage.entities;
  231.  
  232. import java.awt.event.KeyEvent;
  233. import java.awt.event.KeyListener;
  234.  
  235. public class Direction implements KeyListener{
  236.     private boolean right =true,left=false,up=false,down=false;
  237.     public Direction key;
  238.  
  239.     public Direction getKey() {
  240.         return key;
  241.     }
  242.  
  243.     public void setKey(Direction key) {
  244.         this.key = key;
  245.     }
  246.    
  247.     public void keyPressed(KeyEvent e) {
  248.         int key=e.getKeyCode();
  249.         if(key==KeyEvent.VK_RIGHT && !left) {
  250.             up=false;
  251.             down=false;
  252.             right=true;
  253.         }
  254.         if(key==KeyEvent.VK_LEFT && !right) {
  255.             up=false;
  256.             down=false;
  257.             left=true;
  258.         }
  259.         if(key==KeyEvent.VK_UP && !down) {
  260.             left=false;
  261.             right=false;
  262.             up=true;
  263.         }
  264.         if(key==KeyEvent.VK_DOWN && !up) {
  265.             left=false;
  266.             right=false;
  267.             down=true;
  268.         }
  269.  
  270.     }
  271.     public void keyReleased(KeyEvent e) {
  272.              //To change body of generated methods, choose Tools | Templates.
  273.     }
  274.     public void keyTyped(KeyEvent e) {
  275.             //To change body of generated methods, choose Tools | Templates.
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment