processing 游戏编程实例

游戏描述
1.通过键盘控制界面的切换,部分游戏需要辅助鼠标。
2。创设欢迎界面,规则界面,模拟菜单
3。创设辅助界面
4。三个游戏分别为

1.模拟弹簧,使用鼠标进行操作,并加入旋转效果
2.无先前实例无法描述
3.模仿一般类射击游戏

5。缺陷:画质,游戏前需要一些辅助说明,经过他人试玩,部分游戏上手较慢。

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

import processing.dxf.*;
static int premode;
static float headX=0.2*1024,headY=0.7*620;
static int score=0;
static float boxX=0,boxY=0;
static int flag=0;
static int finalscore;
float volume=10;
int ass=0;


PVector vol=new PVector (0,0);

float time=0.001;
static int manX=500,manY=590;
PVector[] vball={new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0)};
PVector[] position ={new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0)};
PVector vman=new PVector(0,0);
PVector g = new PVector(0,9.8);
float vo=0;
float time_2=0.01;
int life=0;
float ti=0;

float volu=0;
static int order=0;
PFont f;
int xleft=8;
int xbull=10;
int mode=0;
static int dur=0;
int xflag=1;
float xt=0;
PVector []xpos={new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0),new PVector(0,0)};
PVector xv=new PVector(0,0);
//Minim minim;


AudioPlayer player;
Minim minim;//audio context
int loopcount;
void setup()
{
  size(1024,620);
  minim = new Minim(this);
  player = minim.loadFile("fc.mp3", 2048);
  player.play();
  // minim = new Minim(this);

  // bellSound = minim.loadSnippet("sj.mp3");
   //bellSound.play();

  f=loadFont("C.vlw");
  int index=0;
  for(index=0;index<8;index++)
  {
  xpos[index].x=random(900);
  xpos[index].y=random(550);
  }

  for(index=0;index<10;index++)
 { vball[index].x=random(100);
   vball[index].y=random(100);
   position[index].x=random(1000);
   position[index].y=random(600);
 }

}
void draw(){
  if(mode==0){
      //background(#42f4d1);
      PImage img = loadImage("bg.jpg");
      image(img,0,0);
      textAlign(CENTER);
      fill(#f46242);
      textFont(f);
      text("the game begin",0.5*width,0.5*height);
      text("to continue please press C",0.5*width,0.7*height);


   }
   if (mode==1){
      background(#42f4d4);
      textFont(f);
      textAlign(CENTER);
      text("the rule",0.5*width,0.1*height);
      text("1.press character c to continue",0.5*width,0.2*height);
      text("2.press character b to stop",0.5*width,0.3*height);
      text("3,press character n to begin the new game",0.5*width,0.4*height);
      text("4,press character j to begin the check",0.5*width,0.5*height);
       text("4,press character wasd to control",0.5*width,0.6*height);
      text("enjoy yourself and have fun ",0.5*width,0.7*height);


   }
  if(mode==2 ){
  PImage img = loadImage("283114.jpg");
      image(img,-100,-40);
      textFont(f);
      textAlign(CENTER);
      text("##the ball",0.5*width,0.3*height);
      text("the bat",0.5*width,0.5*height);
      text("the mul",0.5*width,0.7*height);

  }
   if(mode==3 ){
  PImage img = loadImage("283114.jpg");
      image(img,-100,-40);
      textFont(f);
      textAlign(CENTER);
      text("the ball",0.5*width,0.3*height);
      text("##the bat",0.5*width,0.5*height);
      text("the mul",0.5*width,0.7*height);

  }
   if(mode==4 ){
  PImage img = loadImage("283114.jpg");
      image(img,-100,-40);
      textFont(f);
      textAlign(CENTER);
      text("the ball",0.5*width,0.3*height);
      text("the bat",0.5*width,0.5*height);
      text("##the mul",0.5*width,0.7*height);

  }
  if(mode==100){
    background(#4250f4);
   PImage img1 = loadImage("road.jpg");
      image(img1,5,-20);
      textAlign(CENTER);
      textFont(f);
      text("have a rest",0.5*width,0.9*height);
  }
  if(mode==10){
    premode=mode;
    ti+=0.01;
     background(0);
     textSize(49);
     fill(#F0F8FF);
     text("the ball",0.5*width,0.05*height);
     text("score: "+score,0.1*width,0.1*height);
     text("the control",0.1*width,0.2*height);

     ellipse(headX,headY,50,50);
     stroke(#F0F8FF);
     //line(headX,headY,mouseX,mouseY);
     if (ass==0&&mousePressed){
       ass=1;
         stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
       vol.x=(mouseX-headX)*100;
       vol.y=(mouseY-headY)*100;
       noStroke();
       headX+=time*vol.x*0.000000001;
       headY+=time*vol.y*0.000000001;

     }
     if(ass==0){
      stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
     }
     else{
        text("time: "+ti,0.2*width,0.5*height);
       vol.x+=g.x*time*100;
       vol.y+=g.y*time*100;

       if(headX<=10) {vol.x*=-1; headX=20;}
       if(headX>=1010) {vol.x*=-1; headX=1000;}
       if(headY<=10) {vol.y*=-1; headY=20;}
       if(headY>=610) {vol.y*=-1;headY=600;}
       headX+=time*vol.x;
       headY+=time*vol.y;
       if(flag==0){
         flag=1;
         boxX=random(1024);
         boxY=random(620);
         fill(#F0F8FF);
         ellipse(boxX,boxY,20,20);
       }
       else{
         fill(#F0F8FF);
         ellipse(boxX,boxY,20,20);
         float x=headX-boxX,y=headY-boxY;
         if(x<20&&x>-20&&y<20&&y>-20) {
           flag=0;
           float index=0;
           for(index=0;index<100;index++)
           {
             float alpha=2*PI*index / 20.0;
             stroke(#F0F8FF);
             line(boxX,boxY,boxX+20*cos(alpha),boxY+20*sin(alpha));

           }
            boxX=random(1024);
         boxY=random(620);
         score++;
         }


       }



       fill(#F0F8FF);

       text("the speed in x: "+vol.x,0.2*width,0.3*height);
       text("the speed in y: "+vol.y,0.2*width,0.4*height);
       ellipse(headX,headY,50,50);
       noStroke();
       if(headX==0) vol.x*=-1;
       if(mousePressed) {
          stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
        vol.x+=(mouseX-headX)*10;
        vol.y+=(mouseY-headY)*10;
       }




     }



     //if(score>=2) mode=11;

  }
  if(mode==11){
    premode=mode;
    ti+=0.01;
     background(0);
     textSize(49);
     fill(#F0F8FF);
     text("the ball",0.5*width,0.05*height);
     text("score: "+score,0.1*width,0.1*height);
     text("the control",0.1*width,0.2*height);

     ellipse(headX,headY,50,50);
     stroke(#F0F8FF);
     //line(headX,headY,mouseX,mouseY);
     if (ass==0&&mousePressed){
       ass=1;
         stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
       vol.x=(mouseX-headX)*100;
       vol.y=(mouseY-headY)*100;
       noStroke();
       headX+=time*vol.x*0.000000001;
       headY+=time*vol.y*0.000000001;

     }
     if(ass==0){
      stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
     }
     else{
        text("time: "+ti,0.2*width,0.5*height);
       vol.x+=g.x*time*100;
       vol.y+=g.y*time*100;

       if(headX<=10) {vol.x*=-1; headX=20;}
       if(headX>=1010) {vol.x*=-1; headX=1000;}
       if(headY<=10) {vol.y*=-1; headY=20;}
       if(headY>=610) {vol.y*=-1;headY=600;}
       headX+=time*vol.x;
       headY+=time*vol.y;
       if(flag==0){
         flag=1;
         boxX=random(1024);
         boxY=random(620);
         fill(#F0F8FF);
         ellipse(boxX,boxY,20,20);
       }
       else{
         fill(#F0F8FF);
         ellipse(boxX,boxY,20,20);
         float x=headX-boxX,y=headY-boxY;
         if(x<20&&x>-20&&y<20&&y>-20) {
           flag=0;
           float index=0;
           for(index=0;index<100;index++)
           {
             float alpha=2*PI*index / 20.0;
             stroke(#F0F8FF);
             line(boxX,boxY,boxX+20*cos(alpha),boxY+20*sin(alpha));

           }
            boxX=random(1024);
         boxY=random(620);
         score++;
         }


       }



       fill(#F0F8FF);

       text("the speed in x: "+vol.x,0.2*width,0.3*height);
       text("the speed in y: "+vol.y,0.2*width,0.4*height);
       ellipse(headX,headY,50,50);
       noStroke();
       if(headX==0) vol.x*=-1;
       if(mousePressed) {
          stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
        vol.x+=(mouseX-headX)*10;
        vol.y+=(mouseY-headY)*10;
       }




     }



     if(ti>=10) mode=13;

  }
  if(mode==12){
    premode=mode;
    ti+=0.01;
     background(0);
     textSize(49);
     fill(#F0F8FF);
     text("the ball",0.5*width,0.05*height);
     text("score: "+score,0.1*width,0.1*height);
     text("the control",0.1*width,0.2*height);

     ellipse(headX,headY,50,50);
     stroke(#F0F8FF);
     //line(headX,headY,mouseX,mouseY);
     if (ass==0&&mousePressed){
       ass=1;
         stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
       vol.x=(mouseX-headX)*100;
       vol.y=(mouseY-headY)*100;
       noStroke();
       headX+=time*vol.x*0.000000001;
       headY+=time*vol.y*0.000000001;

     }
     if(ass==0){
      stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
     }
     else{
        text("time: "+ti,0.2*width,0.5*height);
       vol.x+=g.x*time*100;
       vol.y+=g.y*time*100;

       if(headX<=10) {vol.x*=-1; headX=20;}
       if(headX>=1010) {vol.x*=-1; headX=1000;}
       if(headY<=10) {vol.y*=-1; headY=20;}
       if(headY>=610) {vol.y*=-1;headY=600;}
       headX+=time*vol.x;
       headY+=time*vol.y;
       if(flag==0){
         flag=1;
         boxX=random(1024);
         boxY=random(620);
         fill(#F0F8FF);
         ellipse(boxX,boxY,20,20);
       }
       else{
         fill(#F0F8FF);
         ellipse(boxX,boxY,20,20);
         float x=headX-boxX,y=headY-boxY;
         if(x<20&&x>-20&&y<20&&y>-20) {
           flag=0;
           float index=0;
           for(index=0;index<100;index++)
           {
             float alpha=2*PI*index / 20.0;
             stroke(#F0F8FF);
             line(boxX,boxY,boxX+20*cos(alpha),boxY+20*sin(alpha));

           }
            boxX=random(1024);
         boxY=random(620);
         score++;
         }


       }



       fill(#F0F8FF);

       text("the speed in x: "+vol.x,0.2*width,0.3*height);
       text("the speed in y: "+vol.y,0.2*width,0.4*height);
       ellipse(headX,headY,50,50);
       noStroke();
       if(headX==0) vol.x*=-1;
       if(mousePressed) {
          stroke(#F0F8FF);
       line(headX,headY,mouseX,mouseY);
        vol.x+=(mouseX-headX)*10;
        vol.y+=(mouseY-headY)*10;
       }






     }



     if(score>=20) mode=13;

  }
  if(mode==14){
    premode=mode;
      ti=0;
      score=0;
      background(0);
      textSize(49);
     fill(#F0F8FF);
     textAlign(CENTER);
     text("again press n",0.5*width,0.4*height);
  }
  if(mode==13) {
    premode=mode;
     background(0);
     textSize(49);
     fill(#F0F8FF);
     textAlign(CENTER);
     finalscore+=score;
     text("congratulation",0.5*width,0.4*height);
     text("with time "+ti+"s",0.5*width,0.5*height);
     text("with score "+score+"s",0.5*width,0.6*height);
     text("to continue press c",0.5*width,0.7*height);
     noStroke();
    // mode=14;
  }

   if(mode==7 ){
     premode=mode;
  PImage img = loadImage("283114.jpg");
      image(img,-100,-40);
      textFont(f);
      textAlign(CENTER);
      fill(#FFF8DC);
      stroke(#FFF8DC);
      text("##free play",0.5*width,0.3*height);
      text("time limit",0.5*width,0.5*height);
      text("ball limte",0.5*width,0.7*height);

  }
  if(mode==8 ){
    premode=mode;
  PImage img = loadImage("283114.jpg");
      image(img,-100,-40);
      //textFont(f);
      textAlign(CENTER);
       fill(#FFF8DC);
        stroke(#FFF8DC);
      text("free play",0.5*width,0.3*height);
      text("##time limit",0.5*width,0.5*height);
      text("ball limte",0.5*width,0.7*height);

  }
  if(mode==9 ){
    premode=mode;
  PImage img = loadImage("283114.jpg");
      image(img,-100,-40);
      //textFont(f);
      textAlign(CENTER);
       fill(#FFF8DC);
        stroke(#FFF8DC);
      text("free play",0.5*width,0.3*height);
      text("time limit",0.5*width,0.5*height);
      text("##ball limte",0.5*width,0.7*height);

  }
   if(mode==20){
     premode=mode;
    ti+=0.01;
  if (life==0){
  vo+=0.01;
    background(0);
    fill(#FFFFF0);
    stroke(#FFFFF0);
    ellipse(manX,manY,30,30);
    textAlign(CENTER);
    textFont(f);
    fill(#FFFFF0);
    text(vo,0.5*width,0.3*height);
    int index=0;
    for(index=0;index<10;index++) {ellipse(position[index].x,position[index].y,20,20);
    position[index].x+=vball[index].x*time_2;
    position[index].y+=vball[index].y*time_2;  
    textFont(f);
    textAlign(CENTER);
    text("time: "+ti,0.1*width,0.1*height);
}

    noStroke();
    for(index=0;index<10;index++){
      if(position[index].x<=10) position[index].x=900;
    else if (position[index].x>=900) position[index].x=10;
    if(position[index].y<=10) position[index].y=600;
    else if(position[index].y>=600) position[index].y=10;
    }
    if(manY<=600){
      vman.y+=0.5*9.8*time_2*10;
      manX+=vman.x*time_2*10;
      manY+=vman.y*time_2*10;
    }
    if(manX<=10) manX=900;
    else if (manX>=900) manX=10;
    if(manY<=10) manY=600;
    else if(manY>=600) manY=10;
    index=0;
    for(index=0;index<10;index++)
    {
      float dist=sqrt((position[index].x-manX)*(position[index].x-manX)+(position[index].y-manY)*(position[index].y-manY));
      if(dist<=20) life=1;
    }
  }
  else {
    mode=23;
  }
if(order<=2) volu+=1;
  //background(0);
  fill(#FFFFF0);
  rect(900,100,0.1*width,0.05*height);
  int index=0;
  if(order==0){
    fill(#00FF00);
    stroke(#00FF00);
    rect(900,100,volu,0.05*height);
  }
  if(order==1){
   fill(#FFFF00); 
   stroke(#FFFF00);
    rect(900,100,volu,0.05*height);
  }
  if(order==2){
    fill(#DEB887);
    stroke(#DEB887);
    rect(900,100,volu,0.05*height);
  }
  if(order==3){
    volu=0.1*width;
     fill(#DEB887);
    stroke(#DEB887);
    rect(900,100,volu,0.05*height);
  }
  if(volu>=0.1*width&&order<=2) {if(order<=3) volu=0;
  order++;
}

}
  if(mode==23){
    premode=mode;
    textFont(f);
    background(0);
    textAlign(CENTER);;
    finalscore+=ti/10;
    text("the time you have expericened: "+ti,0.5*width,0.5*height);
  }


  xt+=0.01;

     //ellipse(50,585,100,100);

    if(mode==27){
      premode=mode;
     background(0);
      stroke(#FFFFF0);
     fill(#FFFFF0);
     line(50,0,50,550);
     line(50,550,1024,550);
     fill(#FFFFF0);
     //ellipse(50,585,100,100);
     stroke(#FFFFF0);
     line(40,570,10,540);
     line(40,570,90,570);
     line(10,540,mouseX,mouseY);
     line(90,570,mouseX,mouseY);
     fill(0);
     ellipse(mouseX,mouseY,20,20);
     line(40,570,40,620);
     textFont(f);
     textAlign(CENTER);
      stroke(#FFFFF0);
      fill(#FFFFF0);
     text("the bullet left: "+xbull,590,590);
          text("time: "+xt,850,590);
       xv.x=(40-mouseX)*2.5;
       xv.y=-(570-mouseY)*2.5;
       int index=0;
       PVector Xpoint=new PVector(mouseX,mouseY);

       float ptime=0.1;
       while(index < 1000){

         stroke(#FFFFF0);
         fill(#FFFFF0);
         point(mouseX+xv.x*ptime*index,mouseY-xv.y*ptime*index+0.5*9.8*ptime*ptime*index*index);
       index++;
       }
       if(mousePressed) mode=28;
        stroke(#FFFFF0);
       text(Xpoint.x,590,410);

        index=0;
       for(index=0;index<8;index++){
         stroke(#FFFFF0);;
         fill(#FFFFF0);
       ellipse(xpos[index].x+124,xpos[index].y,20,20);
       xflag=0;


       }

   }
   if(mode==28){
   premode=mode;
   background(0);
      stroke(#FFFFF0);
     fill(#FFFFF0);
     line(50,0,50,550);
     line(50,550,1024,550);
     fill(#FFFFF0);
     //ellipse(50,585,100,100);
     stroke(#FFFFF0);
     line(40,570,10,540);
     line(40,570,90,570);
     line(10,540,mouseX,mouseY);
     line(90,570,mouseX,mouseY);
     fill(0);
     //ellipse(mouseX,mouseY,20,20);
     line(40,570,40,620);
     textFont(f);
     textAlign(CENTER);
      stroke(#FFFFF0);
      fill(#FFFFF0);
     text("the bullet left: "+xbull,590,590);
     text("time: "+xt,850,590);
     float ptime=0.1;
     int index=dur;
      stroke(#FFFFF0);
         fill(#FFFFF0);
         ellipse(mouseX+xv.x*ptime*index,mouseY-xv.y*ptime*index+0.5*9.8*ptime*ptime*index*index,20,20);
         int jndex=0;
         for(jndex=0;jndex<8;jndex++){
           float xass1=(mouseX+xv.x*ptime*index-xpos[jndex].x-124);
           float xass2=(mouseY-xv.y*ptime*index+0.5*9.8*ptime*ptime*index*index-xpos[jndex].y);

           double dis=sqrt(xass1*xass1+xass2*xass2);
           if(dis>-20&&dis<20) {xpos[jndex].x=100000;xleft--;}
         }
         for(jndex=0;jndex<8;jndex++)
         ellipse(xpos[jndex].x+124,xpos[jndex].y,20,20);
     if(dur<1000) dur++;
     else{
     mode=27;
     xbull--;
     dur=0;

     }
     if(mouseX+xv.x*ptime*index>=1024||mouseY-xv.y*ptime*index+0.5*9.8*ptime*ptime*index*index>=620) {mode=27;
     xbull--;
     dur=0;}
      if(xleft==0) mode=29;
      if(xbull==0) mode=30;
   }
   if(mode==29){
     premode=mode;
     background(0);
     textFont(f);
     textAlign(CENTER);
     finalscore+=10;
     text("good job",0.5*width,0.5*height);
     noStroke();
     int index ;
     for(index=0;index<8;index++)
  {
  xpos[index].x=random(900);
  xpos[index].y=random(550);
  }
   }
   if(mode==30){
     premode=mode;
       background(0);
     textFont(f);
     textAlign(CENTER);
     text("sorry you failed",0.5*width,0.5*height);
     noStroke();
     int index;
     for(index=0;index<8;index++)
  {
  xpos[index].x=random(900);
  xpos[index].y=random(550);
  }
   }


}
void keyPressed(){
   if(key == 'c') {

      if(mode==100) mode=premode;
      else   mode++;

   }
   if(key=='b'){
    mode=100;
   }
   if(key=='n'){
     if(mode==2) mode=10;
     if(mode==3) mode=20;
     if(mode==4) mode=30;
     if(mode==14) mode=2;
     else mode=1;
   }
   if(key=='j'){

     if(mode==9) mode=10;
     if(mode==8) mode=11;
     if(mode==7) mode=12;
      if(mode==2) mode=7;
     if(mode==3) mode=20;
     if(mode==4) mode=27;
     if(vo>=5){
      fill(#FFFFF0);
    stroke(#FFFFF0);
    ellipse(manX,manY,100*order+100,100*order+100);
    int index=0;
    for(index=0;index<10;index++){
      float dis=sqrt((position[index].x-manX)*(position[index].x-manX)+(position[index].y-manY)*(position[index].y-manY));
      if(dis<=100+order*100) {
        vball[index].x *= -1;
        vball[index].y *= -1;

    }
    order=0;
    }
  vo=0;
     }
   }
   if(key=='s'){
      if(mode==3) mode=4;
      if(mode==2) mode=3;
      if(mode==8) mode=9;
      if(mode==7) mode=8;
      if(manY<=600) manY+=10;
      if(manY<=600) vman.y+=10;
   }
   if(key=='w'){
      if(mode==3) mode=2;
     if(mode==4) mode=3;
     if(mode==8) mode=7;
     if(mode==9) mode=8; 
     if(manY>=110)
      manY-=10;
      if(manY<=600) vman.y=-10;
      else vman.y=0;

   }
    if(key=='a'){
      if(manX>=100) manX-=10;
      if(manX<=600) vman.x=-10;
      else vman.x=0;
    }
    if(key=='d'){
      if(manX<=920) manX+=10;
      if(manX<=600) vman.x=10;
      else vman.x=0;
    }

}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值