我正在为Android设备制作游戏。我正在使用手机的传感器。我正在使用处理这个应用程序
但是我的阵列存在问题。 我想检查我的椭圆是否属于我的阵列。但似乎我错了。我已经检查了< =谁通常是第一个问题,但事实并非如此。
我会把代码发给你,抱歉我的英文不好,我真的希望你能帮助我!真正的问题在于功能" DEF BALLE" ,如果你只有一个问题,我可能知道什么...有代码。
//VARIABLE USED
int colonnes = 10;
int lignes= 10;
int[][] plateau; // tableau 2D
int posX_tabl;
int posY_tabl;
int x_balle;
int y_balle;
float largeurColonne = width/colonnes;
float hateurRangee = height/lignes;
float accelerometerX, accelerometerY, accelerometerZ;
float xx=width/2;
float yy=height-(height/10);
float vxx=0;
float vyy=0;
float axx=0;
float ayy=0;
/*-----------------------------------------SETUP--------------------------------------------------*/
void setup() {
smooth();
sensor = new KetaiSensor(this);
sensor.start();
orientation(LANDSCAPE);
plateau = new int[colonnes][lignes];
creer_plateau();
}
/*-----------------------------------------DRAW--------------------------------------------------*/
void draw() {
//Création du menu principal
if (menu == true) {
creer_plateau();
menu();
}
//Création du jeu
else if (jouer==true) {
nettoyer();
dessine_plateau();
calcul_gravite();
ellipse( xx+width/11, - yy+height-25, 40, 40);
calculer_position();
definir_balle();
//Création des menus recommencer et option
} else if (reco==true) {
reco();
} else if (option == true) {
background(0);
option();
}
mouseReleased = false;
}
void mousePressed() {
mouseReleased = false;
}
void mouseReleased() {
mouseReleased = true;
}
/*---------------------------INIT ARRAY-----------------*/
//Create random value for random case in the game
void creer_plateau() {
int aleacouleur;
for (int i=0; i < colonnes; i++) {
for (int j=0; j < lignes; j++) {
aleacouleur = (int)(random(0, 3));
if (aleacouleur == 0) {
plateau[i][j]=0;
} else {
plateau[i][j]=1;
}
}
plateau[0][9] = 2;
plateau[9][0] = 3;
}
}
/*-----------------------------DRAW ARRAY------------*/
//Draw the array according to the random value
void dessine_plateau() {
for (int i=0; i < colonnes; i++) {
for (int j=0; j < lignes; j++) {
if (plateau[i][j] == 0) {
fill(0);
}
if (plateau[i][j] == 1) {
fill(255);
}
if (plateau[i][j]==2) {
fill(255, 255, 0);
}
if (plateau[i][j]==3) {
fill(0, 255, 0);
}
float x = i*largeurColonne;
float y = j*hateurRangee;
rect( x+1, y+1, largeurColonne-2, hateurRangee-2 );
}
}
}
/*---------------------------CHECK POSITION ELLIPSE-----------------------*/
// THIS FUNCTION IS PROBABLY USELESS
//We Watch where is the ellipse
void calculer_position() {
for ( int j=0; j< 10; j++) {
for (int i=0; i<10; i++) {
if ( xx+width/11> i*40 && xx+width/11< i*40+40 && - yy+height-25 > j*40 && - yy+height-25 < j*40+40) {
posX_tabl = i;
posY_tabl = j;
}
}
//if ( xx+width/2> i*40 && xx+width/2 < i*40+40 && - yy+height/2 > j*40 && - yy+height/2 < j*40+40)
}
}
/*----------------CALCUL GRAVITE----------------------------*/
void calcul_gravite() {
vxx=vxx+axx;
vyy=vyy+ayy;
xx=xx+vxx*0.1;
yy=yy+vyy*0.1;
fill(0, 250, 0);
text(vxx, 100, 100);
if (xx+width/11>= width || xx+width/11 <= 0) {
vxx=-0.8*vxx;
}
if (-yy+height-25 >= height || -yy+height-25 <= 0) {
vyy=-0.8*vyy;
}
}
/*----------------DEF BALLE--------------------------------*/
//THERE IS THE PROBLEM WITH THE ARRAY IF U CAN HELP ME AND IF U HAVE QUESTION ABOUT THE CODE U CAN ASK ME
void definir_balle() {
if (plateau[int((xx+width/11)/(hateurRangee+11))][int((-yy+height-25)/largeurColonne)]==0) {
text("loose", 50, 50);
} else if (plateau[int((xx+width/11)/(hateurRangee+11))][int((-yy+height-25)/largeurColonne)]==3) {
text("WIN", 50, 50);
}
//this part was only a try
/*if (plateau[posX_tabl][posY_tabl]==0)
{
print("game over \n");
jouer=false;
reco=true;
menu=false;
} else if (plateau[posX_tabl][posY_tabl]==1) {
//print("courage \n");
} else if (plateau[posX_tabl][posY_tabl]==3) {
//print("gg wp \n");
}*/
}
答案 0 :(得分:2)
数组中的第一个值具有索引[0]
,因此第十个值将位于[9]
,而不是[10]
。如果您尝试引用array[array.length]
(在您的情况下为array[10]
),您将遇到异常