Bonjour,
j'ai fait un petit code java (je veut crée un jeux avec un pti avion qui tire en 2d)
CODER EN SDLJava
et enfaite mon probleme est que quand je fait espace(sa positionne l'image du missile a l''emplacement de l'avion) et juste après il doit partir devant (comme un missile lancé, il est propulsé) mais, l'image du missile ne se raffraichit qu'une fois que je fait une action ( apuiller sur une touche, bouger la souris... alors que je voudrais qu'elle se rafraichi tout le temps... j'ai esseiller des whiles pour raffraichir mais sa me bug tout ...
import sdljava.SDLException;
import sdljava.SDLMain;
import sdljava.event.SDLEvent;
import sdljava.event.SDLKey;
import sdljava.event.SDLKeyboardEvent;
import sdljava.image.SDLImage;
import sdljava.video.SDLRect;
import sdljava.video.SDLSurface;
import sdljava.video.SDLVideo;
import sdljava.video.*;
public class Jeux {
public static void main(String[] args) throws SDLException, InterruptedException {
// Initialisation SDL.
SDLMain.init(SDLMain.SDL_INIT_VIDEO);
SDLSurface screen = SDLVideo.setVideoMode(640, 480, 32, SDLVideo.SDL_DOUBLEBUF | SDLVideo.SDL_HWSURFACE);
SDLVideo.wmSetCaption("Gekonomani (Création de Banou)", null);
// ----------------------------------------------
// Création du Joueur.
SDLSurface plane = SDLImage.load("images/plane.png");
SDLSurface missile = SDLImage.load("images/missiles.png");
int positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
int positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
int positionMissileX = positionPlaneX;
int positionMissileY = positionPlaneY;
int vie = 3;
boolean refresh = true;
// Acceleration du Joueur desactivée
boolean acceleration = false;
// ----------------------------------------------
/* Variable pour le KeyRepeat (Repetition des touches)
et Variable isRunning activé (programe activé)
*/
boolean isRunning = true;
SDLEvent.enableKeyRepeat(10, 10);
//-----------------------------------------------
// Fin des Initialisation
do
{
SDLEvent event = SDLEvent.waitEvent();
// deplacement des missiles
positionMissileY -= 4;
if(positionMissileY == 0)
{
}
if(positionMissileY == 1)
{
}
if(positionMissileY == 2)
{
}
//remet l'avion au spawn si il depace l'ecran
if(positionPlaneY == 0)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 1)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 2)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 3)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 440)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 441)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 442)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 443)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneY == 444)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 620)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 621)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 622)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 623)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 624)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 0)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 1)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 2)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 3)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
if(positionPlaneX == 4)
{
positionPlaneX = screen.getWidth() / 2 - plane.getWidth() / 2;
positionPlaneY = screen.getHeight() / 2 - plane.getHeight() / 2;
vie -= 1;
}
//----------------------------------------
if (vie == 0)
{
isRunning = false;
}
if (event.getType() == SDLEvent.SDL_QUIT)
{
isRunning = false;
}
if (event.getType() == SDLEvent.SDL_KEYUP)
{
SDLKeyboardEvent keyEventUp = (SDLKeyboardEvent) event;
if( keyEventUp.getSym() == SDLKey.SDLK_LSHIFT)
{
acceleration = false;
}
}
if (event.getType() == SDLEvent.SDL_KEYDOWN)
{
SDLKeyboardEvent keyEvent = (SDLKeyboardEvent) event;
switch (keyEvent.getSym())
{
case SDLKey.SDLK_LSHIFT:
acceleration = true;
break;
case SDLKey.SDLK_ESCAPE:
isRunning = false;
break;
case SDLKey.SDLK_SPACE:
positionMissileX = positionPlaneX;
positionMissileY = positionPlaneY;
break;
case SDLKey.SDLK_UP:
if(acceleration)
{
positionPlaneY -= 4;
}
else
{
positionPlaneY -= 2;
}
break;
case SDLKey.SDLK_DOWN:
if(acceleration)
{
positionPlaneY += 4;
}
else
{
positionPlaneY +=2;
}
break;
case SDLKey.SDLK_LEFT:
if(acceleration)
{
positionPlaneX -= 4;
}
else
{
positionPlaneX -= 2;
}
break;
case SDLKey.SDLK_RIGHT:
if(acceleration)
{
positionPlaneX += 4;
}
else
{
positionPlaneX += 2;
}
break;
}
}
// On met à jour l'affichage
screen.fillRect(screen.mapRGB(255, 255, 255));
blit.blitSurface(positionPlaneX, positionPlaneY, plane, screen);
blit.blitSurface(positionMissileX, positionMissileY, missile, screen);
screen.flip();
Thread.sleep(0);
}
while (isRunning);
// Libération de la mémoire
plane.freeSurface();
missile.freeSurface();
screen.freeSurface();
SDLMain.quit();
}
}
class blit {
static void blitSurface(int x, int y, SDLSurface src, SDLSurface dst) throws SDLException {
SDLRect temp = new SDLRect(x, y);
src.blitSurface(dst, temp);
}
}
J'ai repris le code du SDZ et je le modifie...
Afficher la suite