Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javagame;
- import javax.management.timer.Timer;
- import java.util.Random;
- import org.newdawn.slick.*;
- import org.newdawn.slick.geom.Polygon;
- import org.newdawn.slick.state.*;
- public class Play extends BasicGameState{
- public float p1x1=200;
- public float p1y1=160;
- public int facing=0; //0=north 90=east 180=south 270=west
- public int hasEaten=0;
- public int body[][]={{10,100},{20,300}};
- public boolean end=false;
- public String locX,locY;
- public String ft,rot;
- public boolean foodOnField=false;
- public Random random = new Random(System.nanoTime());
- public int foodX ;
- public int foodY ;
- public int score=0;
- public int bombX;
- public int bombY;
- public Polygon foodPoly;
- public Polygon snakePoly;
- public Polygon bombPoly;
- Image pea;
- Image keha;
- Image food,food2,food3,food4;
- Image bomb;
- public int rn= 0;
- // 640 360
- public Play(int state){
- }
- public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
- pea = new Image("res/uss_pea.png");
- keha = new Image("res/uss_keha.png");
- food = new Image("res/food.png");
- food2 = new Image("res/food2.png");
- food3 = new Image("res/food3.png");
- food4 = new Image("res/food4.png");
- bomb = new Image("res/bomb.png");
- bombX=20*random.nextInt(31);
- bombY=20*random.nextInt(17);
- foodX=20*random.nextInt(31);
- foodY=20*random.nextInt(17);
- pea.setCenterOfRotation(15, 15);
- keha.setCenterOfRotation(15, 15);
- foodPoly= new Polygon();
- foodPoly.addPoint(foodX,foodY);
- foodPoly.addPoint(foodX+20,foodY);
- foodPoly.addPoint(foodX+20,foodY+20);
- foodPoly.addPoint(foodX,foodY+20);
- snakePoly= new Polygon();
- snakePoly.addPoint(p1x1,p1y1);
- snakePoly.addPoint(p1x1+20,p1y1);
- snakePoly.addPoint(p1x1+20,p1y1+20);
- snakePoly.addPoint(p1x1,p1y1+20);
- bombPoly= new Polygon();
- bombPoly.addPoint(bombX,bombY);
- bombPoly.addPoint(bombX+20,bombY);
- bombPoly.addPoint(bombX+20,bombY+20);
- bombPoly.addPoint(bombX,bombY+20);
- }
- public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
- pea.draw(p1x1, p1y1);
- pea.setRotation(facing);
- if(!foodOnField){
- rn = random.nextInt(3);
- }
- switch(rn){
- case 0:
- food2.draw(foodX,foodY);
- break;
- case 1:
- food3.draw(foodX,foodY);
- break;
- case 2:
- food4.draw(foodX,foodY);
- break;
- default:
- food.draw();
- break;
- }
- g.drawString(locX, 500, 10);
- g.drawString(locY, 500, 30);
- g.drawString(ft, 500, 20);
- g.drawString(rot, 500, 40);
- g.drawString(Integer.toString(score), 10, 10);
- bomb.draw(bombX,bombY);
- }
- public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
- locY="Y"+p1y1;
- locX="X"+p1x1;
- rot=facing+" deg";
- ft=""+snakePoly.intersects(foodPoly);
- updatePoly();
- Food(gc,delta);
- moveAuto(delta,gc,end);
- keyInAuto(gc,true);
- hitWall();
- collisionBad();
- bomb();
- }
- public int getID(){
- return 1;
- }
- private void moveAuto(int delta,GameContainer gc,boolean b){
- int s=20;
- Input input = gc.getInput();
- if(!b){
- if(input.isKeyDown(Input.KEY_SPACE)){
- s=40;
- }
- if(input.isKeyDown(Input.KEY_LCONTROL)){
- s=5;
- }
- if(facing==0){
- p1y1-=s*delta/300f;
- //wait(250);
- }else if(facing==90){
- p1x1+=s*delta/300f;
- //wait(250);
- }else if(facing==180){
- p1y1+=s*delta/300f;
- //wait(250);
- }else if(facing==270){
- p1x1-=s*delta/300f;
- }else if(facing<0||facing==360){
- if(facing==360){facing=0;}
- if(facing==-90){facing=270;}
- if(facing==-180){facing=180;}
- if(facing==-270){facing=90;}
- }else{
- System.out.println("Error moving snake");
- }
- }
- }
- private void keyInAuto(GameContainer gc,boolean b){
- Input input = gc.getInput();
- if(b){
- if(input.isKeyPressed(Input.KEY_LEFT)){
- facing=facing-90;
- }else if(input.isKeyPressed(Input.KEY_RIGHT)){
- facing=facing+90;
- }
- }
- }
- private void hitWall(){
- if(p1y1<=-20){p1y1=340;}
- if(p1y1>=380){p1y1=0;}
- if(p1x1<=-20){p1x1=620;}
- if(p1x1>=660){p1x1=0;}
- }
- private void Food(GameContainer gc,int delta){
- if(collisionDetected()){
- foodX=20*random.nextInt(31);
- foodY=20*random.nextInt(17);
- if(foodPoly.intersects(bombPoly))
- {foodX=20*random.nextInt(31);
- foodY=20*random.nextInt(17);}
- else{
- score=score+20;
- foodOnField=false;
- }
- }else{
- foodOnField=true;
- }
- }
- private void bomb(){
- if(collisionBad()){
- end=true;
- bombX=20*random.nextInt(31);
- bombY=20*random.nextInt(17);
- if(foodPoly.intersects(bombPoly))
- {bombX=20*random.nextInt(31);
- bombY=20*random.nextInt(17);
- }
- }
- }
- private boolean collisionDetected(){
- if(snakePoly.intersects(foodPoly)){
- return true;
- }else{
- return false;
- }
- }
- private boolean collisionBad(){
- if(snakePoly.intersects(bombPoly)){
- return true;
- }else{
- return false;
- }
- }
- private void updatePoly(){
- foodPoly.setCenterX(foodX+10);
- foodPoly.setCenterY(foodY+10);
- bombPoly.setCenterX(bombX+10);
- bombPoly.setCenterY(bombY+10);
- snakePoly.setCenterX(p1x1+10);
- snakePoly.setCenterY(p1y1+10);
- snakePoly.addPoint(p1x1,p1y1);
- snakePoly.addPoint(p1x1+20,p1y1);
- snakePoly.addPoint(p1x1+20,p1y1+20);
- snakePoly.addPoint(p1x1,p1y1+20);
- foodPoly.addPoint(foodX,foodY);
- foodPoly.addPoint(foodX+20,foodY);
- foodPoly.addPoint(foodX+20,foodY+20);
- foodPoly.addPoint(foodX,foodY+20);
- bombPoly.addPoint(bombX,bombY);
- bombPoly.addPoint(bombX+20,bombY);
- bombPoly.addPoint(bombX+20,bombY+20);
- bombPoly.addPoint(bombX,bombY+20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement