Processing 之 制作游戏flappy bird

来源:互联网 发布:js 打乱数组方法 编辑:程序博客网 时间:2024/05/29 03:33

Processing是使用java语言的数据可视化软件。

虽然用于制作游戏不太合适,但是大家可以提高代码编写能力、增强思维逻辑。

这里说明,游戏不是作者设计的,而是模仿红极一时的flappy bird,很简单但很耐玩的一款游戏。

代码用于大家学习,由于国内Processing还不是很普及,较少人使用,但是这款软件还是很不错的。

游戏截图(由于作者没有艺术细胞,制作的特别难看,请别介意)

初始界面:


游戏开始:


游戏失败:

游戏胜利:



代码分为3个类:

Bird:鸟类

Obstacle:障碍物类

Game:程序入口


下面附上代码:

Bird

————————————————————

public class Bird {  private float x = 100;  private float y = 50;  private float vx = 0;  private float vy = 0;  private float a = 300;  private String state = "live";  private PImage image = loadImage("picture\\bird.jpg");  public Bird() {  }  public Bird(float x, float y, float vx, float vy, float a) {    super();    this.x = x;    this.y = y;    this.vx = vx;    this.vy = vy;    this.a = a;  }  public float getX() {    return x;  }  public void setX(float x) {    this.x = x;  }  public float getY() {    return y;  }  public void setY(float y) {    this.y = y;  }  public float getVx() {    return vx;  }  public void setVx(float vx) {    this.vx = vx;  }  public float getVy() {    return vy;  }  public void setVy(float vy) {    this.vy = vy;  }  public float getA() {    return a;  }  public void setA(float a) {    this.a = a;  }  public String getState() {    return state;  }  public void setState(String state) {    this.state = state;  }  public void update(){    if(state.equals("live")){      vy += (a/frameRate);      y += (vy/frameRate);      if(y < 0){        y = 0;        vy = 0;        a = 300;      }      if(y >= height){        state = "dead";      }    }  }  public void fly(){    if(state.equals("live")){      vy = -120;      a = -100;      count = 0;    }  }  int count = 0;  public void show(){    if(count==10){      a = 300;    }    fill(0);    image(image,x-10,y-10,20,20);    //ellipse(x,y,20,20);    fill(255);    count++;  }}
————————————————————


Obstacle

————————————————————

public class Obstacle {  private float x1;  private float y1;  private float width1;  private float height1;  private float x2;  private float y2;  private float width2;  private float height2;  private float speed;  private int K;  public Obstacle() {  }  public Obstacle(float x1, float y1, float width1, float height1, float x2,      float y2, float width2, float height2, float speed) {    super();    this.x1 = x1;    this.y1 = y1;    this.width1 = width1;    this.height1 = height1;    this.x2 = x2;    this.y2 = y2;    this.width2 = width2;    this.height2 = height2;    this.speed = speed;  }  public float getX1() {    return x1;  }  public void setX1(float x1) {    this.x1 = x1;  }  public float getY1() {    return y1;  }  public void setY1(float y1) {    this.y1 = y1;  }  public float getWidth1() {    return width1;  }  public void setWidth1(float width1) {    this.width1 = width1;  }  public float getHeight1() {    return height1;  }  public void setHeight1(float height1) {    this.height1 = height1;  }  public float getX2() {    return x2;  }  public void setX2(float x2) {    this.x2 = x2;  }  public float getY2() {    return y2;  }  public void setY2(float y2) {    this.y2 = y2;  }  public float getWidth2() {    return width2;  }  public void setWidth2(float width2) {    this.width2 = width2;  }  public float getHeight2() {    return height2;  }  public void setHeight2(float speed) {    this.speed = speed;  }  public float getSpeed() {    return height2;  }  public void setSpeed(float speed) {    this.speed = speed;  }  public void update(){    x1 -= speed;    x2 -= speed;  }  public void show(){    fill(20,210,20);    rect(x1,y1,width1,height1);    rect(x2,y2,width2,height2);    fill(255);   // text(x1*100/frameRate,50,50);  }  public boolean touch(Bird bird){    if(bird.getY() < y1+height1){      if(abs(bird.getX() - (x1+width1/2)) < 10 + width1/2){        return true;      }    }else if(bird.getY() > y2){      if(abs(bird.getX() - (x2+width2/2)) < 10 + width2/2){        return true;      }    }else{      if(bird.getX() > x1 && bird.getX() < x1 + width1){        if(abs(bird.getY() - (y1+height1)) < 10){          return true;         }        if(abs(bird.getY() - (y2)) < 10){          return true;        }      }    }    return false;  }}
————————————————————


Game

————————————————————

String state = "option";Bird bird;Obstacle[] obstacles;int obstaclesSize;float speed = 1;void setup(){  smooth();  frameRate(120);  bird = new Bird();  obstaclesSize = 30;  obstacles = new Obstacle[obstaclesSize];  size(500,300);}void draw(){  background(90,220,220);  if(state.equals("option")){    option();  }else if(state.equals("new")){    bird.show();    for(Obstacle o : obstacles){      o.show();    }  }else if(state.equals("start")){    bird.update();    bird.show();    for(Obstacle o : obstacles){      o.update();      o.show();    }    judge();  }else if(state.equals("over")){    over();  }else if(state.equals("win")){    win();  }}void mousePressed(){  if(state.equals("option")){    if(mouseX > 80 && mouseX < 150 && mouseY > 200 && mouseY < 230){      speed = 1.5;      state = "new";      for(int i=0;i<obstaclesSize;i++){        float x = 300 + i*200;        float h = random(0,100);        obstacles[i] = new Obstacle(x,0,20,h,x,h+80,20,300-h-80,speed);      }    }    if(mouseX > 230 && mouseX < 300 && mouseY > 200 && mouseY < 230){      speed = 1.8;      state = "new";      for(int i=0;i<obstaclesSize;i++){        float x = 300 + i*200;        float h = random(0,100);        obstacles[i] = new Obstacle(x,0,20,h,x,h+80,20,300-h-80,speed);      }    }    if(mouseX > 380 && mouseX < 450 && mouseY > 200 && mouseY < 230){      speed = 2.3;      state = "new";      for(int i=0;i<obstaclesSize;i++){        float x = 300 + i*200;        float h = random(0,100);        obstacles[i] = new Obstacle(x,0,20,h,x,h+80,20,300-h-80,speed);      }    }  }else if(state.equals("new")){    state = "start";  }else if(state.equals("start")){    bird.fly();  }else if(state.equals("over")){    if(mouseX > 230 && mouseX < 300 && mouseY > 200 && mouseY < 230){      bird = new Bird();      state = "option";    }  }else if(state.equals("win")){    if(mouseX > 230 && mouseX < 300 && mouseY > 200 && mouseY < 230){      bird = new Bird();      state = "option";    }  }}void option(){  rect(80,200,70,30);  rect(230,200,70,30);  rect(380,200,70,30);  fill(0);  textSize(50);  text("Game",180,100);  textSize(15);  text("easy",90,220);  text("normal",240,220);  text("difficult",390,220);  fill(255);}void judge(){  for(Obstacle o : obstacles){    if(o.touch(bird)){      bird.setState("dead");    }  }  if(bird.getState().equals("dead")){    state = "over";  }  if(bird.getX() > obstacles[obstaclesSize-1].getX1() + obstacles[obstaclesSize-1].getWidth1() + 100){    state = "win";  }}void over(){  rect(230,200,70,30);  fill(0);  textSize(50);  text("Game Over",100,100);  textSize(15);  text("restart",240,220);  fill(255);}void win(){  rect(230,200,70,30);  fill(0);  textSize(50);  text("Win",180,100);  textSize(15);  text("restart",240,220);  fill(255);}
————————————————————


0 0
原创粉丝点击