Advertisement
Guest User

SnakeGame

a guest
Jun 11th, 2012
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. package javagame;
  2.  
  3. import javax.management.timer.Timer;
  4. import java.util.Random;
  5. import org.newdawn.slick.*;
  6. import org.newdawn.slick.geom.Polygon;
  7. import org.newdawn.slick.state.*;
  8.  
  9.  
  10. public class Play extends BasicGameState{
  11.     public float p1x1=200;
  12.     public float p1y1=160;
  13.     public int facing=0; //0=north 90=east 180=south 270=west
  14.     public int hasEaten=0;
  15.     public int body[][]={{10,100},{20,300}};
  16.     public boolean end=false;
  17.     public String locX,locY;
  18.     public String ft,rot;
  19.     public boolean foodOnField=false;
  20.     public Random random = new Random(System.nanoTime());
  21.     public int foodX ;
  22.     public int foodY ;
  23.     public int score=0;
  24.     public int bombX;
  25.     public int bombY;
  26.     public Polygon foodPoly;
  27.     public Polygon snakePoly;
  28.     public Polygon bombPoly;
  29.     Image pea;
  30.     Image keha;
  31.     Image food,food2,food3,food4;
  32.     Image bomb;
  33.     public int rn= 0;
  34.  
  35.     // 640 360
  36.    
  37.     public Play(int state){
  38.     }
  39.    
  40.     public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
  41.         pea = new Image("res/uss_pea.png");
  42.         keha = new Image("res/uss_keha.png");
  43.         food = new Image("res/food.png");
  44.         food2 = new Image("res/food2.png");
  45.         food3 = new Image("res/food3.png");
  46.         food4 = new Image("res/food4.png");
  47.         bomb = new Image("res/bomb.png");
  48.         bombX=20*random.nextInt(31);
  49.          bombY=20*random.nextInt(17);
  50.          foodX=20*random.nextInt(31);
  51.          foodY=20*random.nextInt(17);
  52.         pea.setCenterOfRotation(15, 15);
  53.         keha.setCenterOfRotation(15, 15);
  54.         foodPoly= new Polygon();
  55.                 foodPoly.addPoint(foodX,foodY);
  56.                 foodPoly.addPoint(foodX+20,foodY);
  57.                 foodPoly.addPoint(foodX+20,foodY+20);
  58.                 foodPoly.addPoint(foodX,foodY+20);
  59.         snakePoly= new Polygon();
  60.                 snakePoly.addPoint(p1x1,p1y1);
  61.                 snakePoly.addPoint(p1x1+20,p1y1);
  62.                 snakePoly.addPoint(p1x1+20,p1y1+20);
  63.                 snakePoly.addPoint(p1x1,p1y1+20);
  64.         bombPoly= new Polygon();
  65.                 bombPoly.addPoint(bombX,bombY);
  66.                 bombPoly.addPoint(bombX+20,bombY);
  67.                 bombPoly.addPoint(bombX+20,bombY+20);
  68.                 bombPoly.addPoint(bombX,bombY+20);
  69.  
  70.  
  71.     }
  72.    
  73.     public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
  74.         pea.draw(p1x1, p1y1);
  75.         pea.setRotation(facing);
  76.        
  77.         if(!foodOnField){
  78.             rn = random.nextInt(3);
  79.             }
  80.         switch(rn){
  81.                 case 0:
  82.                     food2.draw(foodX,foodY);
  83.                 break;
  84.                 case 1:
  85.                     food3.draw(foodX,foodY);
  86.                 break;
  87.                 case 2:
  88.                     food4.draw(foodX,foodY);
  89.                 break;
  90.                 default:
  91.                     food.draw();
  92.                 break;
  93.         }
  94.        
  95.         g.drawString(locX, 500, 10);
  96.         g.drawString(locY, 500, 30);
  97.         g.drawString(ft, 500, 20);
  98.         g.drawString(rot, 500, 40);
  99.         g.drawString(Integer.toString(score), 10, 10);
  100.         bomb.draw(bombX,bombY);
  101.        
  102.        
  103.     }
  104.     public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
  105.     locY="Y"+p1y1;
  106.     locX="X"+p1x1;
  107.     rot=facing+" deg";
  108.     ft=""+snakePoly.intersects(foodPoly);
  109.         updatePoly();
  110.         Food(gc,delta);
  111.         moveAuto(delta,gc,end);
  112.         keyInAuto(gc,true);
  113.         hitWall();
  114.         collisionBad();
  115.         bomb();
  116.  
  117.        
  118.     }
  119.     public int getID(){
  120.         return 1;
  121.     }
  122.     private void moveAuto(int delta,GameContainer gc,boolean b){
  123.         int s=20;
  124.         Input input = gc.getInput();
  125.         if(!b){
  126.             if(input.isKeyDown(Input.KEY_SPACE)){
  127.                 s=40;
  128.             }
  129.             if(input.isKeyDown(Input.KEY_LCONTROL)){
  130.                 s=5;
  131.             }
  132.  
  133.  
  134.         if(facing==0){
  135.         p1y1-=s*delta/300f;
  136.         //wait(250);
  137.         }else if(facing==90){
  138.             p1x1+=s*delta/300f;
  139.             //wait(250);
  140.         }else if(facing==180){
  141.             p1y1+=s*delta/300f;
  142.             //wait(250);
  143.         }else if(facing==270){
  144.             p1x1-=s*delta/300f;
  145.  
  146.         }else if(facing<0||facing==360){
  147.             if(facing==360){facing=0;}
  148.             if(facing==-90){facing=270;}
  149.             if(facing==-180){facing=180;}
  150.             if(facing==-270){facing=90;}
  151.         }else{
  152.             System.out.println("Error moving snake");
  153.         }
  154.         }
  155.     }
  156.     private void keyInAuto(GameContainer gc,boolean b){
  157.         Input input = gc.getInput();
  158.         if(b){
  159.         if(input.isKeyPressed(Input.KEY_LEFT)){
  160.             facing=facing-90;
  161.            
  162.         }else if(input.isKeyPressed(Input.KEY_RIGHT)){
  163.             facing=facing+90;
  164.         }
  165.         }
  166.     }
  167.     private void hitWall(){
  168.         if(p1y1<=-20){p1y1=340;}
  169.         if(p1y1>=380){p1y1=0;}
  170.         if(p1x1<=-20){p1x1=620;}
  171.         if(p1x1>=660){p1x1=0;}
  172.     }
  173.     private void Food(GameContainer gc,int delta){
  174.         if(collisionDetected()){
  175.              foodX=20*random.nextInt(31);
  176.              foodY=20*random.nextInt(17);
  177.              if(foodPoly.intersects(bombPoly))
  178.              {foodX=20*random.nextInt(31);
  179.              foodY=20*random.nextInt(17);}
  180.              else{
  181.                 score=score+20;
  182.             foodOnField=false;
  183.             }
  184.         }else{
  185.             foodOnField=true;
  186.         }
  187.     }
  188.     private void bomb(){
  189.         if(collisionBad()){
  190.             end=true;
  191.              bombX=20*random.nextInt(31);
  192.              bombY=20*random.nextInt(17);
  193.              if(foodPoly.intersects(bombPoly))
  194.              {bombX=20*random.nextInt(31);
  195.              bombY=20*random.nextInt(17);
  196.              }
  197.         }
  198.     }
  199.     private boolean collisionDetected(){
  200.         if(snakePoly.intersects(foodPoly)){
  201.             return true;
  202.  
  203.         }else{
  204.             return false;
  205.         }
  206.     }
  207.     private boolean collisionBad(){
  208.         if(snakePoly.intersects(bombPoly)){
  209.         return true;
  210.         }else{
  211.             return false;
  212.         }
  213.        
  214.     }
  215.     private void updatePoly(){
  216.         foodPoly.setCenterX(foodX+10);
  217.         foodPoly.setCenterY(foodY+10);
  218.         bombPoly.setCenterX(bombX+10);
  219.         bombPoly.setCenterY(bombY+10);
  220.         snakePoly.setCenterX(p1x1+10);
  221.         snakePoly.setCenterY(p1y1+10);
  222.         snakePoly.addPoint(p1x1,p1y1);
  223.         snakePoly.addPoint(p1x1+20,p1y1);
  224.         snakePoly.addPoint(p1x1+20,p1y1+20);
  225.         snakePoly.addPoint(p1x1,p1y1+20);
  226.         foodPoly.addPoint(foodX,foodY);
  227.         foodPoly.addPoint(foodX+20,foodY);
  228.         foodPoly.addPoint(foodX+20,foodY+20);
  229.         foodPoly.addPoint(foodX,foodY+20);
  230.         bombPoly.addPoint(bombX,bombY);
  231.         bombPoly.addPoint(bombX+20,bombY);
  232.         bombPoly.addPoint(bombX+20,bombY+20);
  233.         bombPoly.addPoint(bombX,bombY+20);
  234.     }
  235.    
  236.    
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement