📄 gamecanvas.java
字号:
}
}
}
}
private void updatePos() {
rotateGun(); //转动大炮的瞄准角度
updateShot(); //更新炮弹
updatePlane(); //更新飞机的位置
updateState();
}
private void updateState(){
for (int i = 0; i < plane.length; i++) {
for(int j = 0;j<shot.length;j++){
//isShotPlane
if (Math.abs(plane_XY[i][0] - shot_x[j]) < (plane[i].getWidth() / 2 -3 ) &&
(plane_XY[i][1] + plane[i].getHeight()) >= (shot_y[j] + 5) && (shot_y[j]+shot[j].getHeight() ) > plane_XY[i][1] &&
!isShotPlane[i] && isFly[i]) {
isShotPlane[i] = true;
isFly[i] = false;
shotReset(j);
}
if(isSendBomb[i]){
//isBombShot
if (Math.abs(plane_XY[i][2] - shot_x[j]) < (bomb.getWidth() / 2 + shot[j].getWidth() / 2) &&
(plane_XY[i][3] + bomb.getHeight()) >= shot_y[j] && (shot_y[j]+shot[j].getHeight()) > plane_XY[i][3] &&
!isBombShot[i] && isSendBomb[i]) {
shotReset(j);
isBombShot[i] = true;
} else
//isBombGun
if (Math.abs(WIDTH / 2 - plane_XY[i][2]) < (bomb.getWidth() / 2 + gun[0].getWidth() / 2) &&
HEIGHT * 4 / 5 + 5 <= (plane_XY[i][3] + bomb.getHeight()) && !isBombGun[i] && isSendBomb[i]) {
isBombGun[i] = true;
} else
//isBombGround
if (plane_XY[i][3] + bomb.getHeight() >= HEIGHT && plane_XY[i][2] > 0 &&
plane_XY[i][2] < WIDTH && !isBombGround[i] && isSendBomb[i]) {
isBombGround[i] = true;
}
}
}
}
}
private void rotateGun() {
if(rotate){
if (gun_left) {
if (degree >= 90) {
degree = 90;
}
else {
degree += 30;
}
}
if (gun_right) {
if (degree <= -90) {
degree = -90;
}
else {
degree -= 30;
}
}
switch (degree) {
case 0:
cgun = gun[0];
cdegree = 0;
break;
case 30:
cgun = gun[1];
cdegree = 0;
break;
case 60:
cgun = gun[2];
cdegree = 0;
break;
case 90:
cgun = gun[0];
cdegree = 90;
break;
case -30:
cgun = gun[2];
cdegree = 270;
break;
case -60:
cgun = gun[1];
cdegree = 270;
break;
case -90:
cgun = gun[0];
cdegree = 270;
break;
default:
break;
}
rotate = false;
}else{
rotatedelaycount++;
if(rotatedelaycount>=10){
rotate = true;
}
}
}
private void updateShot() {
for(int j=0;j<shot.length;j++){
if (fire[j]) { //如果发射了炮弹 则不断更新炮弹的位置
switch (tdegree[j]) {
case 0:
shot_y[j] -= 6;
break;
case 30:
shot_x[j] -= 3;
shot_y[j] -= 6;
break;
case 60:
shot_x[j] -= 6;
shot_y[j] -= 3;
break;
case 90:
shot_x[j] -= 6;
shot_y[j] = HEIGHT * 4 / 5;
break;
case -30:
shot_x[j] += 3;
shot_y[j] -= 6;
break;
case -60:
shot_x[j] += 6;
shot_y[j] -= 3;
break;
case -90:
shot_x[j] += 6;
shot_y[j] = HEIGHT * 4 / 5;
break;
default:
break;
}
if (shot_y[j] < 0 || shot_x[j] > WIDTH || shot_x[j] < 0) { //只有炮弹出了屏幕才可以
shotReset(j);
}
}
else { //如果没有发射 则不断将炮弹发射的角度用当前角度更新
tdegree[j] = degree;
}
}
}
private void checkPlaneOut(int i){
if(i%2==0) {
if (plane_XY[i][0] < 0) out[i] = true; else out[i] = false;
} else {
if (plane_XY[i][0] > WIDTH) out[i] = true; else out[i] = false;
}
}
private void updatePlane() {
fly = Math.abs(rand.nextInt() % difficulty); //根据难度变量的改变 修改飞机出发的频率
for (int i = 0; i < plane.length; i++) {
checkPlaneOut(i);
if (out[i] || !isFly[i]) {
if(!isSendBomb[i] && i==fly){
tfly = fly;
planeReset(i) ;
isFly[i] = true ;
}
}
if (isFly[i] ) { //如果第I只飞机起飞了
plane_XY[i][0] += plane_XY[i][5];
plane_XY[i][1] += 1; //Y坐标则以1的单位递增 形成飞机向下飞的效果
if(i%2==1){
if(plane_XY[i][0]<=plane_XY[i][2]&&plane_XY[i][0] + plane_XY[i][5]>plane_XY[i][2] && !isSendBomb[i]){
plane_XY[i][3] = plane_XY[i][1];
isSendBomb[i] = true;
}
}else{
if(plane_XY[i][0]>=plane_XY[i][2]&&plane_XY[i][0] + plane_XY[i][5]<plane_XY[i][2] && !isSendBomb[i]){
plane_XY[i][3] = plane_XY[i][1];
isSendBomb[i] = true;
}
}
}
if (isSendBomb[i]) { //如果不允许投弹 则有一个炸弹处在屏幕中下落状态
plane_XY[i][3] += bomb_move_step_h; //炸弹以bomb_move_step_h为单位,向下运动
}
} //end for
}
private int getBombX() {
bombX = Math.abs(rand.nextInt() % (WIDTH*2)); //产生一个-5~WIDTH+5之间的随机数,来确定投弹的X坐标,
return bombX; //只有X在屏幕范围才投弹 从而形成不一定每架飞机过都投弹的效果
}
private void planeReset(int i) {
planeSpeed = Math.abs(rand.nextInt() % 3)+2 ; //产生速度
plane_XY[i][1] = Math.abs(rand.nextInt() % HEIGHT/6); //飞机起飞的Y坐标
if (i % 2 == 0) { //确定起飞的X坐标
plane_XY[i][0] = WIDTH;
plane_XY[i][5] = -1*planeSpeed; //确定速度
} else if(i%2==1){
plane_XY[i][0] = 0;
plane_XY[i][5] = planeSpeed;
}
plane_XY[i][2] = getBombX(); //投弹的X坐标
plane_XY[i][3] = 0; //投弹的Y坐标
hitPlane[i] = 0;
hitBomb[i] = 0;
hitGun[i] = 0;
hitGround[i] = 0;
isShotPlane[i] = false;
isSendBomb[i] = false;
isBombShot[i] = false;
isBombGun[i] = false;
isBombGround[i] = false;
isFly[i] = false; //飞机可以飞
out[i] = false;
}
private void shotReset(int j) { //炮弹复位
shot_x[j] = WIDTH / 2;
shot_y[j] = HEIGHT * 4 / 5 + 7;
fire[j] = false; //允许发射下一颗炮弹
tdegree[j] = degree; //将当前的角度赋给临时的炮弹发射角度
}
private void checkStage() {
if (life == 0) {
gameover = true;
doGameOver();
} else if (left_plane <= 0) {
if (stage < maxstage) {
initPlot();
}else {
win = true;
gameover = true;
doGameOver();
}
}
if(isNewStage){
stageinterval++;
if(stageinterval>=20){
isNewStage=false;
}
}
}
public void killthread(){
showpause=true;
stopthread=true;
killthread=true;
}
public void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case LEFT:
gun_left = true;
break;
case RIGHT:
gun_right = true;
break;
case FIRE:
case UP:
if(shotNum>=shot.length || shotNum==0) {
fireinterval = 4;
shotNum = 0;
}
if (!fire[shotNum]) {
if(fireinterval<4) fireinterval++;
if(fireinterval>=4){
fire[shotNum] = true;
fireinterval = 0;
shotNum ++ ;
}
}
break;
}
}
public void keyReleased(int keyCode) {
switch (getGameAction(keyCode)) {
case LEFT:
gun_left = false;
break;
case RIGHT:
gun_right = false;
break;
case FIRE:
case UP:
fireinterval = 4;
break;
}
}
protected void showNotify(){
//init();
}
protected void hideNotify(){
stopthread();
}
public void stopthread(){
stopthread=true;
}
public void doGameOver(){
if(gameover){
if(highscore.isHighScore(score)){
BigGun.setScore(score);
BigGun.changeCanvas(Resources.CVS_INPUT, true);
}else{
showover=true;
}
}
}
private void updateLeftPlane(int i){
left_plane--; //玩家的任务数减1
if (i < 2) { //这里是根据被击中的不同种类飞机,对玩家的成绩进行更新
score += 100;
}
else if (i < 4 && i > 2) {
score += 150;
}
else {
score += 200;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -