班上,有一个用c++做小游戏打飞机的模拟,类似我们玩的雷电什么得,觉得麻烦,就先用processing做了一个demo,代码如下,发现,好久都没有碰processing了,上次碰,还是两个月寒假的时候做游戏的时候。
简易版demo
有完善版本的时候,再修改吧。
1。简单操作鼠标点击,来完成游戏界面之外操作
2。鼠标点击,发射
3。 鼠标移动,控制飞机移动
4. 键盘上按键s停止
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.*;
AudioPlayer player;
Minim minim;//audio context
int loopcount;
float life = 20;
float energy = 0;
PVector[] enemy = new PVector[5];
PVector[] direction = new PVector[5];
int flag = 0;
int start = 0;
int count;
float time = 0;
float lasting = 0;
int max = 3;
int level = 1;
void setup(){
size(600,600);
background(0);
for(int index = 0 ; index < 5 ; index++ )
{ enemy[index] = new PVector(random(0,600), 20);
direction[index] = new PVector(0, 2);
}
minim = new Minim(this);
player = minim.loadFile("s.mp3", 2048);
player.play();
}
void draw(){
if(start == 0){
fill(#E3E3E3);
textAlign(CENTER);
textSize(44);
fill( #EE6AA7);
text("START",0.5 * width ,0.5 * height);
fill(#E3E3E3);
}
if(start == 1){
if(lasting == 1) {
start = 5;
}
if(count <= 15) {max = 3; level = 1;}
if(count <= 30 && count > 15){ max = 5; level = 2;}
if(count <= 45 && count > 30 ) {max = 7; level = 3;}
if(count > 45 ) {max = 15 + (int)time * 10; level = 4;}
time += 0.01;
if(life <= 0) start = 2;
if(energy < 200 && flag == 0)
energy += 0.6;
for(int index = 0 ; index < 5 ; index++){
if(direction[index].y < max)
direction[index].y *= 1.001;
}
background(0);
textAlign(CENTER);
text("KILLED: "+count , 450, 50);
text("TIME: "+time , 450, 20);
text("LEVEL: "+level , 450, 80);
textSize(20);
rect(10,50,60,20);
fill(#EE2C2C);
if(life >= 0)
rect(10,50,30 * 0.1 * life ,20);
fill(#E3E3E3);
rect(10,20,60,20);
if(energy >= 150){
fill( #EE2C2C );
rect(10,20,30 * 0.01 * energy ,20);
}
if(energy >= 100 &&energy < 150 )
{
fill(#FFD700);
rect(10,20,30 * 0.01 * energy ,20);
}
else{
if(energy < 100){
fill(#D1EEEE);
rect(10,20,30 * 0.01 * energy ,20);
}
}
fill(#9932CC);
fill( #F8F8FF);
fill(#E3E3E3);
ellipse(mouseX,580,30,30);
for(int index = 0 ; index < 5 ; index++){
float dis = sqrt((mouseX - enemy[index].x)*(mouseX - enemy[index].x)+(580 - enemy[index].y)*(580 - enemy[index].y));
if(dis <= 30){
life-= 2;
ellipse(mouseX,580,70,70);
enemy[index].y = 10;
enemy[index].x = random(100,500);
}
}
fill(#9932CC);
fill(#9932CC);{
fill( #F8F8FF);
for(int index = 0 ; index < 5 ; index++)
ellipse(enemy[index].x, enemy[index].y , 10 , 10);
fill( #F8F8FF);
for(int index = 0 ; index < 5 ; index++)
enemy[index].y += direction[index].y;
for(int index = 0 ; index < 5 ; index++)
{
if(enemy[index].y >= 580 )
{
enemy[index].y = 10;
enemy[index].x = random(100,500);
}
}
if(mousePressed)
{ if(energy > 0){
flag = 1;
stroke(#F8F8FF);
line(mouseX,580,mouseX,0);
for(int index = 0 ; index < 5 ; index++)
{
if(enemy[index].x - mouseX <= 10 && enemy[index].x - mouseX >= -10)
{
ellipse(enemy[index].x , enemy[index].y , 30 , 30 );
count++;
enemy[index].y = 10;
enemy[index].x = random(0,600);
}
}
energy -= 2.5;
if(energy <= 0) flag = 0;
}
} }
}
if(start == 2){
background(0);
fill(#E3E3E3);
textAlign(CENTER);
textSize(24);
fill( #EE6AA7);
text("SORRY FAILED",0.5 * width ,0.3 * height);
text("SURVIVED "+time+" seconds",0.5 * width ,0.5 * height);
text("KILLED "+count+" enemy",0.5 * width ,0.7 * height);
fill(#E3E3E3);
}
if(start == 3){
background(0);
textAlign(CENTER);
text("KILLED: "+count , 450, 50);
text("TIME: "+time , 450, 20);
text("LEVEL: "+level , 450, 80);
textSize(20);
rect(10,50,60,20);
fill(#EE2C2C);
if(life >= 0)
rect(10,50,30 * 0.1 * life ,20);
fill(#E3E3E3);
rect(10,20,60,20);
if(energy >= 150){
fill( #EE2C2C );
rect(10,20,30 * 0.01 * energy ,20);
}
if(energy >= 100 &&energy < 150 )
{
fill(#FFD700);
rect(10,20,30 * 0.01 * energy ,20);
}
else{
if(energy < 100){
fill(#D1EEEE);
rect(10,20,30 * 0.01 * energy ,20);
}
}
fill(#9932CC);
fill( #F8F8FF);
fill(#E3E3E3);
ellipse(mouseX,580,30,30);
textAlign(CENTER);
textSize(14);
fill( #EE6AA7);
stroke(#EE6AA7);
text("THIS IS YOU THE AMOUNT OF POWER", 250 , 30);
text("THIS IS YOU THE AMOUNT OF BULLET",250 , 70);
line(120,20,60,30);
line(120,60,60,70);
textSize(22);
text("RULE",300 , 150);
textAlign(LEFT);
text("1. you can use your mouse to control the plane",10 , 190);
text("2. you can press the mouse to fire ",10 , 230);
text("3, if you come across the enemy you are harmed",10 , 270);
text("4. if your power is less than 0 , you falied",10 , 310);
text("5. while playing , you can press s to stop",10 , 350);
textAlign(CENTER);
text("PLAY NOW CLICKHERE",300 , 450);
stroke(#EE6AA7);
fill(#E3E3E3);
}
if(start == 5){
}
}
void mousePressed(){
if(start == 3){
if(mousePressed && mouseX >= 200 && mouseX <= 500 && mouseY >= 400 && mouseY <= 500)
start = 1 ;
}
if(start == 0 ){
if(mousePressed && mouseX >= 200 && mouseX <= 400 && mouseY >= 200 && mouseY <= 400)
start = 3 ;
}
}
void keyPressed(){
if(key == 's'){
lasting = 1 - lasting;
if(lasting == 0)
start = 1;
}
}