1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public void keyPressed(KeyEvent k)
{
boolean avance = true;
//traitement permettant de bloquer l'avancement du grinch en la venue d'un obstacle
for(int j=0; j<tableauXObstacle.size(); j++)
{
//variable permettant de ralonger la zone de touché de l'obstacle
int lTabX = tableauXObstacle.get(j)+50;
int lTabY = tableauYObstacle.get(j)+50;
//si les coordonnées du grinch sont égaux avec ceux d'un obstacle
if(((grinch.getX()>=tableauXObstacle.get(j)-20)&&(grinch.getX()<=lTabX))
&&((grinch.getY()>=tableauYObstacle.get(j)-20)&&(grinch.getY()<=lTabY)))
{
avance = false;
}
}
if(avance)
{
int Caract = k.getKeyCode();
//on fait bouger l'image vers le haut
if(Caract==KeyEvent.VK_UP)
{
if(y>5)
y = y - 5;
grinch.setLocation(x, y);
}
//on fait bouger l'image vers le bas
if(Caract==KeyEvent.VK_DOWN)
{
if(y<470)
y = y + 5;
grinch.setLocation(x, y);
}
//on fait bouger l'image vers la droite
if(Caract==KeyEvent.VK_RIGHT)
{
if(x<525)
x = x + 5;
grinch.setLocation(x, y);
}
//on fait bouger l'image vers la gauche
if(Caract==KeyEvent.VK_LEFT)
{
if(x>5)
x = x - 5;
grinch.setLocation(x, y);
}
}
...
} |
Partager