Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
726 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  Movement d'un sprite sur l'ecran

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Movement d'un sprite sur l'ecran

n°882357
nimo
New.Internet.Master.Operator
Posté le 25-10-2004 à 18:56:15  profilanswer
 

Voici le code que j'ai ecrit pour afficher des sprites sur l'ecran:
 
 
*******************************************************************

Code :
  1. void initEnemies()
  2. {
  3. int i,x,y;
  4. for (i=0;i<NUM_ENEMIES;i++){
  5.  // set initial position of enemies so that they are randomly clustered on the four corners
  6.  // by setting the x value to range between 0-100 or 540-640
  7.  // and the y value to be between 0-100 or 380 or 480
  8.  x=rand()%640;y=rand()%480;
  9.  while(x>100&&x<540)x=rand()%640;
  10.  while(y>100&&y<380)y=rand()%480;
  11.  DXUSetSprite(&imgEnemy,&enemies[i].sprite,x,y); // set the sprite
  12.  enemies[i].alive=TRUE;       // set alive to true
  13.  // set the floating point position to the sprite initial x,y position
  14.  enemies[i].posx=(double)enemies[i].sprite.x;
  15.  enemies[i].posy=(double)enemies[i].sprite.y;
  16.  // Give the enemy a random direction vector for movement
  17.  // we need a normalised vector ie: which has a length of one so that
  18.  // sqrt(x*x +y*y) = 1
  19.  // assign the x value to be a random value beteen 0 and 0.999
  20.  enemies[i].direction.x=(double)(rand()%100)/100.0;
  21.  // rearrange above equation so calculate y value
  22.  enemies[i].direction.y=sqrt(1.0-(enemies[i].direction.x*enemies[i].direction.x));
  23.  // to describe directions in full 360 degrees both x and y can be positive or negative
  24.  // so randomly assign them to be positve or negative
  25.  if((rand()%10)>5)enemies[i].direction.x= -enemies[i].direction.x;
  26.  if((rand()%10)>5)enemies[i].direction.y= -enemies[i].direction.y;
  27.  // randomly set enemies speed to be between 4 and 6 pixels per cycle
  28.  enemies[i].speed=4+(double)(rand()%3);
  29. }
  30. }


**********************************************************************
 
Mon problem: je voudrai que ces sprites s'afficher un par un sur la parti droite de l'ecran et qu'ils ont un movement direct de droite a gauche
 
Es ce que que'qu'un peu m'aider a changer mon code?
 
Merci


Message édité par nimo le 25-10-2004 à 19:06:50

---------------
NEC 3500 | YAMADA 6600 | NIMO_CORP
mood
Publicité
Posté le 25-10-2004 à 18:56:15  profilanswer
 

n°882358
WhatDe
Posté le 25-10-2004 à 18:57:26  profilanswer
 

C'est toi qui as écrit le code ?

n°882361
nimo
New.Internet.Master.Operator
Posté le 25-10-2004 à 18:58:44  profilanswer
 

c'est un example que j'ai trouver


---------------
NEC 3500 | YAMADA 6600 | NIMO_CORP
n°882363
nimo
New.Internet.Master.Operator
Posté le 25-10-2004 à 19:00:27  profilanswer
 

tu peu m'aider ?


---------------
NEC 3500 | YAMADA 6600 | NIMO_CORP
n°882368
WhatDe
Posté le 25-10-2004 à 19:03:24  profilanswer
 

Non désolé je pige rien au code.

n°882372
nimo
New.Internet.Master.Operator
Posté le 25-10-2004 à 19:05:58  profilanswer
 

Ca genere un array de sprite sur l'ecran ds les 4 coins avec une auto direction.
 
Je voudrai changer ca pour afficher ces sprites a droite de l'ecran et d'avoir une direction simple de droite a gauche de l'ecran mais j'arrive pas
 
So quelqu'un peu aider dit le moi svp
 
Merci


---------------
NEC 3500 | YAMADA 6600 | NIMO_CORP
n°882407
Harkonnen
Modérateur
Un modo pour les bannir tous
Posté le 25-10-2004 à 19:26:50  profilanswer
 

si tu veux afficher les sprites à droite de l'écran, tu dois faire une boucle sur ton nombre de sprites, mettre la posx à 0, et faire varier la posy.
 
exemple :

Code :
  1. for (i=0;i<NUM_ENEMIES;i++)
  2. {
  3.    enemies[i].posx = 0;
  4.    enemies[i].posy = i * 20; // un sprite toutes les 20 lignes
  5. }


 
pour faire bouger les sprites vers la droite, il faut augmenter la posx

Code :
  1. for (i=0;i<NUM_ENEMIES;i++)
  2. {
  3.    enemies[i].posx += 10; // augmente la coordonnée x du sprite de 10 à chaque passage
  4. }


 
   


---------------
J'ai un string dans l'array (Paris Hilton)
n°882467
nimo
New.Internet.Master.Operator
Posté le 25-10-2004 à 20:52:56  profilanswer
 

Merci grace a toi ca marche, voila ce que j'ai changer:
 

Code :
  1. void initEnemies()
  2. {
  3. int i,x,y;
  4. for (i=0;i<NUM_ENEMIES;i++){
  5.  DXUSetSprite(&imgEnemy,&enemies[i].sprite,x,y); // set the sprite
  6.  enemies[i].alive=TRUE;
  7.  enemies[i].posx = 0;
  8.  enemies[i].posy = i * 20; 
  9.  enemies[i].posx=(double)enemies[i].sprite.x;
  10.  enemies[i].posy=(double)enemies[i].sprite.y;
  11.  enemies[i].direction.x-=5;
  12.  enemies[i].direction.y=(double)(rand()%100)/100.0;
  13.  enemies[i].speed=2+(double)(rand()%3);
  14. }
  15. }


 
mon autre problem c'est que quand je tire une bullet, elle ne va pas horizontal mais elle va en vertical
 
Es ce que je code que j'ai et mauvai?
 

Code :
  1. void initBullets()
  2. {
  3. int i;
  4. for (i=0;i<NUM_BULLETS;i++){
  5.  DXUSetSprite(&imgBullet,&bullets[i].sprite,0,0); // set the sprite
  6.  bullets[i].posx=bullets[i].sprite.x;    // set floating point position
  7.  bullets[i].posy=bullets[i].sprite.y;
  8.  bullets[i].alive=FALSE;        // set alive to false
  9.  bullets[i].speed=10;        // set speed to 10 (faster than enemies)
  10. }
  11. }


Code :
  1. void renderBullets()
  2. {
  3. int i,j;
  4. for (i=0;i<NUM_BULLETS;i++){
  5.  if(bullets[i].alive){
  6.   // bullet is alive so move its position by mutiplying the x and y components
  7.   // by its speed. Note that these values are stored as doubles for accuracy
  8.   bullets[i].posx=bullets[i].posx+(bullets[i].direction.x*bullets[i].speed);
  9.   bullets[i].posy=bullets[i].posy+(bullets[i].direction.y*bullets[i].speed);
  10.   // round the floating point position by casting as int ready for drawing
  11.   bullets[i].sprite.x=(int)bullets[i].posx;
  12.   bullets[i].sprite.y=(int)bullets[i].posy;
  13.   //if the bullet has gone off the screen thenkill it
  14.   if(bullets[i].posx<0||bullets[i].posx>640||bullets[i].posy<0||bullets[i].posy>480){
  15.    bullets[i].alive=FALSE;
  16.   }
  17.   else {
  18.    // otherwise draw it
  19.    DXUDrawSprite(&bullets[i].sprite);
  20.    // check for collision of bullets and enemies
  21.    for (j=0;j<currentTotalEnemies;j++){
  22.     if((PPSpriteCollision(&bullets[i].sprite,&enemies[j].sprite))
  23.      &&enemies[j].alive&&bullets[i].alive)
  24.      {
  25.      // on collision kill enemy
  26.      enemies[j].alive=FALSE;
  27.      // invcrement enemiesKilled counter for this level
  28.      enemiesKilled++;
  29.      // invcrement enemiesKilled counter for all levels
  30.      totalEnemiesKilled++;
  31.      // kill the bullet
  32.      bullets[i].alive=FALSE;
  33.      // start an explosion  
  34.      explosions[explodeCounter].alive=TRUE;
  35.      explosions[explodeCounter].sprite.x=enemies[j].sprite.x;
  36.      explosions[explodeCounter].sprite.y=enemies[j].sprite.y;
  37.      explosions[explodeCounter].scale.x=1.0;
  38.      explosions[explodeCounter].scale.y=1.0;
  39.      explosions[explodeCounter].scaleInc=0.1;
  40.      explosions[explodeCounter].transparency=255;
  41.      // increment explosions counter, and reset it to 0 if itexceeds arhero bounds
  42.      explodeCounter++;
  43.      if(explodeCounter>=NUM_EXPLOSIONS)explodeCounter=0;
  44.     }
  45.    }
  46.   }
  47.  }
  48. }
  49. }


Code :
  1. void releaseBullets()
  2. {
  3. int i;
  4. for (i=0;i<NUM_BULLETS;i++){
  5.   DXUReleaseSprite(&bullets[i].sprite);
  6.   DXUReleaseImage(&bullets[i].image);
  7. }
  8. }


 
Merci


Message édité par nimo le 25-10-2004 à 20:56:11

---------------
NEC 3500 | YAMADA 6600 | NIMO_CORP

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Movement d'un sprite sur l'ecran

 

Sujets relatifs
[Xlib] création d'une fenêtre en plein écranOuverture d'une animation flash en plein ecran
attribution valeur d'une variable fonction de la resolution ecranEcran tactile grâce à JAVA
[VBS] Afficher le resultat d'une requete à l'écranfond d'ecran
récupérer la couleur d'un pixel sur l'écran ?impression differente de l'ecran
Comment cacher proprement la barre de la fenetre win en Plein Ecran ?recuperation de données saisies a l'ecran puis les mettre dans un fich
Plus de sujets relatifs à : Movement d'un sprite sur l'ecran


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR