如果做android游戏,这些多少也是基础,有用了可以看看
初始化多个子弹(原理是子弹运动的角度不同)
void doFire() { int m = 0; for (int i = 0; i < Balls.length; i++) { if (Balls[i][0] == 0) { m++; Balls[i][0] = 1; Balls[i][1] = shipX + shipImg.getWidth() / 2 - 3; Balls[i][2] = shipY - 20; Balls[i][3] = m; switch (m) { case 1: Balls[i][4] = 9; break; case 2: Balls[i][4] = 8; break; case 3: Balls[i][4] = 10; break; case 4: Balls[i][4] = 7; break; case 5: Balls[i][4] = 11; break; } if (m >= 5) break; } }
小球撞墙,宝物移动
之前初始化x,y和inx, incy的坐标(位置移动,如果越界根据inx,iny的正负确定移动方向,从而位置纠正)
x+= incX; y+= incY; if (x] <= 0|| x + giftImg.getWidth() >= width) { x = incX > 0 ? width- giftImg.getWidth() : 0; incX = -incX; } if (y <= 0 || y + giftImg.getHeight() >= height) { y = incY > 0 ? height- giftImg.getHeight() : 0; incY = -incY; }
宝物飞机碰撞, 飞机处理碰撞的原理等碰撞的原理(利用坐标和长和宽确定两个矩形 是否相交)
boolean checkCollide(int x1, int y1, int w1, int h1, int x2, int y2,int w2, int h2) { if (x1 + w1 >= x2 && x1 <= x2 + w2 && y1 + h1 >= y2 && y1 <= y2 + h2) { return true; } return false; }
组合键的实现
int keyQueue[]=new int [3]; void putKey(int keyCode){ keyQueue[2]=keyQueue[1]; keyQueue[1]=keyQueue[0]; keyQueue[0]=keyCode; } void clearKey(){ keyQueue[2]=keyQueue[1]=keyQueue[0]=0; } protected void keyPressed(int keyCode){ if(System.currentTimeMillis()-keyReleasedTime>400){ clearKey();} putKey(keyCode); } protected void keyReleased(int keyCode){ keyReleasedTime=System.currentTimeMillis(); }
抛物线:
int [][] enemyFleets=new int [14][4];//active ,x,y,type //初始化抛物线运动 void createEnemyFleets(){ int p=0; for(int i=0;i=2){ break; } } } } //画抛物线 int [][] enemyFleets=new int [14][4];//active ,x,y,type for(int i=0;i height){ enemyFleets[i][0]=0; } g.drawImage(enemyFleetImg, enemyFleets[i][1], enemyFleets[i][2], 20); } }