comment créer un sprite?

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
gadjet35
Messages : 190
Inscription : ven. 21/oct./2005 7:49
Localisation : Quelque part en france !

comment créer un sprite?

Message par gadjet35 »

:?: comment créer un sprite? :?:

je sais qu'il faut insérer: createsprite()
mais après comment créer son sprite? :?: :?:
Avatar de l’utilisateur
Chris
Messages : 3731
Inscription : sam. 24/janv./2004 14:54
Contact :

Message par Chris »

Un exemple

Code : Tout sélectionner

#Window = 0
#Width = 600
#Height = 600
#Timer = 0

#Sprite = 0

;- Initialisation de DirectX
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Erreur", "Impossible d'initialiser DirectX", 0)
  CloseWindow(#Window) : End
EndIf


;- Ouverture de la fenêtre et de l'écran
hwnd = OpenWindow(#Window, 0, 0, #Width, #Height, #PB_Window_TitleBar | #PB_Window_ScreenCentered, "")
OpenWindowedScreen(hwnd, 0, 0, #Width, #Height, 0, 0, 0)

;- Création d'un sprite
If CreateSprite(#Sprite, 64,64)
  StartDrawing(SpriteOutput(#Sprite))
  Circle(32,32,32,RGB($FF,$0,$0))
  StopDrawing()
EndIf

Repeat
  ClearScreen(0, 0, 0) : ExamineKeyboard()
  
  DisplaySprite(#Sprite, 50,50)
  
  FlipBuffers()
  While WindowEvent() : Wend
  Delay(1)
  If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf
Until quit = 1
comtois
Messages : 5186
Inscription : mer. 21/janv./2004 17:48
Contact :

Message par comtois »

et comme tu demandais comment utiliser le clavier , j'ai repris l'exemple de Chris en ajoutant le déplacement du sprite au clavier.

Code : Tout sélectionner

#Window = 0
#Width = 600
#Height = 600
#Timer = 0

#Sprite = 0

;- Initialisation de DirectX
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Erreur", "Impossible d'initialiser DirectX", 0)
  CloseWindow(#Window) : End
EndIf


;- Ouverture de la fenêtre et de l'écran
hwnd = OpenWindow(#Window, 0, 0, #Width, #Height, #PB_Window_TitleBar | #PB_Window_ScreenCentered, "")
OpenWindowedScreen(hwnd, 0, 0, #Width, #Height, 0, 0, 0)

;- Création d'un sprite
If CreateSprite(#Sprite, 64,64)
  StartDrawing(SpriteOutput(#Sprite))
  Circle(32,32,32,RGB($FF,$0,$0))
  StopDrawing()
EndIf

Repeat
  ClearScreen(0, 0, 0)
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up) 
  	y - 1
  EndIf
  If KeyboardPushed(#PB_Key_Down) 
  	y + 1
  EndIf
  If KeyboardPushed(#PB_Key_Left) 
  	x - 1
  EndIf
  If KeyboardPushed(#PB_Key_Right) 
  	x + 1
  EndIf    
  DisplaySprite(#Sprite, x,y)
 
  FlipBuffers()
  While WindowEvent() : Wend
  Delay(1)
  If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf
Until quit = 1 
http://purebasic.developpez.com/
Je ne réponds à aucune question technique en PV, utilisez le forum, il est fait pour ça, et la réponse peut profiter à tous.
Nfred
Messages : 16
Inscription : mer. 20/avr./2005 10:35

sprite assistance

Message par Nfred »

j'ai repris ce p'tit code que j'ai pris soin de modifier et commenter pour les débutants qui souhaiterai faire une gestion de sprite :

Code : Tout sélectionner

#Window = 0 
#Width = 600 
#Height = 600 
#Timer = 0 

Enumeration ; déclaration des sprites
  #SpriteB   ; bas
  #SpriteD   ; droite
  #SpriteG   ; gauche
  #SpriteH   ; haut
  #SpriteBD  ; bas&droite
  #SpriteHD  ; haut&droite
  #SpriteBG  ; bas&gauche
  #SpriteHG  ; haut&gauche
EndEnumeration

;{- Initialisation de DirectX 
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 
  MessageRequester("Erreur", "Impossible d'initialiser DirectX", 0) 
  CloseWindow(#Window) : End 
EndIf 
;}

;{- Ouverture de la fenêtre et de l'écran 
hwnd = OpenWindow(#Window, 0, 0, #Width, #Height, #PB_Window_TitleBar | #PB_Window_ScreenCentered, "") 
OpenWindowedScreen(hwnd, 0, 0, #Width, #Height, 0, 0, 0) 
;}

;{- Création des sprites
If CreateSprite(#SpriteB, 64,64) 
  StartDrawing(SpriteOutput(#SpriteB)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,0,32,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteH, 64,64) 
  StartDrawing(SpriteOutput(#SpriteH)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,0,-32,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteG, 64,64) 
  StartDrawing(SpriteOutput(#SpriteG)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,-32,0,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteD, 64,64) 
  StartDrawing(SpriteOutput(#SpriteD)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,32,0,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteBD, 64,64) 
  StartDrawing(SpriteOutput(#SpriteBD)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,32,32,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteHD, 64,64) 
  StartDrawing(SpriteOutput(#SpriteHD)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,32,-32,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteBG, 64,64) 
  StartDrawing(SpriteOutput(#SpriteBG)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,-32,32,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
If CreateSprite(#SpriteHG, 64,64) 
  StartDrawing(SpriteOutput(#SpriteHG)) 
  Circle(32,32,32,RGB($FF,$0,$0)) 
  Line(32,32,-32,-32,RGB($00,$0,$0))
  StopDrawing() 
EndIf 
;}

Repeat 
  ClearScreen(0, 0, 0) 
  ExamineKeyboard() 
  If KeyboardPushed(#PB_Key_Pad8) ;{ haut
    y - 1 
    Sprite = #SpriteH
    If y <0
      y=0
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad2) ;{ bas
    y + 1 
    Sprite = #SpriteB
    If y > 535
      y=535
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad4) ;{ gauche
    x - 1 
    Sprite = #SpriteG
    If x<0
      x=0
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad6) ;{ droite
    x + 1 
    Sprite = #SpriteD
    If x>535
      x=535
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad3) ;{ bas&droite
    x + 1 
    y + 1
    Sprite = #SpriteBD
    If x>535
      x=535
    EndIf
    If y > 535
      y=535
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad9) ;{ haut&droite
    x + 1 
    y - 1
    Sprite = #SpriteHD
    If x>535
      x=535
    EndIf
    If y <0
      y=0
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad7) ;{ haut&gauche
    x - 1 
    y - 1
    Sprite = #SpriteHG
    If x<0
      x=0
    EndIf
    If y <0
      y=0
    EndIf
  ;}
  ElseIf KeyboardPushed(#PB_Key_Pad1) ;{ bas&gauche
    x - 1 
    y + 1
    Sprite = #SpriteBG
    If x<0
      x=0
    EndIf
    If y > 535
      y=535
    EndIf
  EndIf ;}
  DisplaySprite(Sprite, x,y) 
  
  FlipBuffers() 
  While WindowEvent() : Wend 
  Delay(10) 
  If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf 
Until quit = 1 

voili :P
Nfred
Messages : 16
Inscription : mer. 20/avr./2005 10:35

oubli

Message par Nfred »

Ce code fonctionne avec les touches du pavé numérique
Backup
Messages : 14526
Inscription : lun. 26/avr./2004 0:40

Message par Backup »

et voici un exemple de collision

tu dirige ta balle avec la souris sur la brique rouge au centre de l'ecran
la ligne en haut a gauche te dira ou tu la touche !
le bouton droit de la souris pour quitter !


; prg realisé par Dobro

#dobro =1
#Police =1
#Sprite =1
Declare.s super_collision(sprite_numero1, x_sprite1, y_sprite1, sprite_numero2, x_sprite2, y_sprite2)
; cette procedure renvoie du quelle coté le sprite a été touché !

; sprite_numero1= le numero du sprite 1 a tester
; largeur_spr1 = largeur en pixel du sprite numero 1
; hauteur_spr1 = hauteur en pixel du sprite numero 1
; x_sprite1 = coordonée X du sprite numero 1
; y_sprite1=coordonée Y du sprite numero 1
; --------------------------------------------------------------------------
; sprite_numero2= le numero du sprite 2 a tester
; largeur_spr2 = largeur en pixel du sprite numero 2
; hauteur_spr12 = hauteur en pixel du sprite numero 2
; x_sprite2 = coordonée X du sprite numero 2
; y_sprite2 coordonée Y du sprite numero 2

Enumeration
   #sprite_cible
   #sprite_souris
   #sprite_text
EndEnumeration

Structure sprite
  x.w
  y.w
EndStructure
Dim sprite.sprite(1)
Dim ecran(640,400)
For x = 0 To 640 ; un écran de couleurs aléatoires
   For y = 0 To 400
    r= Random (255)
    g= Random (255)
    b= Random (255)
    c= RGB (r,g,b)
    ecran(x,y)= c
   Next y
Next x
; ***********************************
Resultat = InitSprite ()
FontID = LoadFont ( #Police , "arial" , 18, #PB_Font_Bold )
EcranX = GetSystemMetrics_ ( #SM_CXSCREEN ): ;=largeur de l'ecran
EcranY = GetSystemMetrics_ ( #SM_CYSCREEN ): ;=hauteur de l'ecran
   WindowID = OpenWindow (1, 0, 0, 800, 600, #PB_Window_SystemMenu|#PB_Window_BorderLess |#PB_Window_ScreenCentered , "hello" )
  
   WindowID = WindowID (1)
  Result = OpenWindowedScreen ( WindowID ,0,0, 800, 600, 1, 0,0)
   CreateSprite ( #sprite_cible , 128, 64) ; sprite exemple
   StartDrawing ( SpriteOutput ( #sprite_cible ) ) ; on dessine dedans
   Box (0, 0, 128, 64, RGB ($FF,$0,$80))
   StopDrawing ()
   CreateSprite ( #sprite_souris , 32, 32) ; sprite souris
   StartDrawing ( SpriteOutput ( #sprite_souris ) ) ; on dessine dedans
   ;Box(0, 0, 64, 64,RGB($13,$F8,$7))
   Circle (16, 16, 16, RGB ($13,$F8,$7))
   StopDrawing ()
  bord$= "aucun bord"
   CreateSprite ( #sprite_text , 150,14) ; le text
   StartDrawing ( SpriteOutput ( #sprite_text ) ) ; on dessine dedans
   DrawText (bord$)
   StopDrawing ()
  Resultat = InitMouse ()
   Repeat
     ExamineMouse ()
    Event= WindowEvent ()
     DisplaySprite ( #sprite_cible , WindowWidth () /2, WindowHeight ()/2)
    sprite(1)\x= WindowWidth ()/2
    sprite(1)\y= WindowHeight ()/2
     DisplaySprite ( #sprite_souris , MouseX (), MouseY ())
     DisplaySprite ( #sprite_text , 10, 10)
    bord$=super_collision( #sprite_cible , sprite(1)\x,sprite(1)\y, #sprite_souris , MouseX (), MouseY ())
     StartDrawing ( SpriteOutput ( #sprite_text ) ) ; on dessine dedans
     DrawingMode (0)
     DrawText (bord$)
     DrawText ( " " )
     StopDrawing ()
     If MouseButton (2)
       End
     EndIf
     FlipBuffers (): ; affiche l'ecran
     ClearScreen (0, 0, 0) : ;efface l'ecran
   Until Event= #PB_Event_CloseWindow
  
   Procedure.s super_collision(sprite_numero1, x_sprite1, y_sprite1, sprite_numero2, x_sprite2, y_sprite2)
    spr1_milieu_x=x_sprite1+ SpriteWidth (sprite_numero1) /2
    spr1_milieu_y=y_sprite1+ SpriteHeight (sprite_numero1)/2
    spr2_milieu_x=x_sprite2+ SpriteWidth (sprite_numero2)/2
    spr2_milieu_y=y_sprite2+ SpriteHeight (sprite_numero2)/2
    spr1_hauteur= SpriteHeight (sprite_numero1)
    spr1_largeur= SpriteWidth (sprite_numero1)
    spr2_hauteur= SpriteHeight (sprite_numero2)
    spr2_largeur= SpriteWidth (sprite_numero2)
    bord$= ""
     If SpriteCollision (sprite_numero1,x_sprite1, y_sprite1,sprite_numero2,x_sprite2, y_sprite2)
       If bord$<> ""
         Goto dobrosuite
       EndIf
       If (spr2_milieu_x)>x_sprite1 And (spr2_milieu_x)<(x_sprite1+spr1_largeur) And (spr2_milieu_y)>(y_sprite1+spr1_hauteur )
        bord$= "bas"
         Goto dobrosuite
       EndIf
       If (spr2_milieu_x)>x_sprite1 And (spr2_milieu_x)<(x_sprite1+spr1_largeur) And (spr2_milieu_y)<y_sprite1
        bord$= "haut"
         Goto dobrosuite
       EndIf
       If (spr2_milieu_y)>y_sprite1 And (spr2_milieu_y)<(y_sprite1+spr1_hauteur) And (spr2_milieu_x)>(x_sprite1+spr1_largeur)
        bord$= "droit"
         Goto dobrosuite
       EndIf
       If (spr2_milieu_y)>y_sprite1 And (spr2_milieu_y)<(y_sprite1+spr1_hauteur) And (spr2_milieu_x)<x_sprite1
        bord$= "gauche"
         Goto dobrosuite
       EndIf
       If (spr2_milieu_x)<x_sprite1 And (spr2_milieu_y)<y_sprite1
        bord$= "haut-gauche"
       EndIf
       If (spr2_milieu_x)>x_sprite1+spr1_largeur And (spr2_milieu_y)<y_sprite1
        bord$= "haut-droit"
       EndIf
       If (spr2_milieu_y)>y_sprite1+spr1_hauteur And (spr2_milieu_x)<x_sprite1
        bord$= "bas-gauche"
       EndIf
       If (spr2_milieu_y)>y_sprite1+spr1_hauteur And (spr2_milieu_x)>x_sprite1+spr1_largeur
        bord$= "bas-droit"
       EndIf
     EndIf
    dobrosuite:
     ProcedureReturn bord$
   EndProcedure
  
  
  
  


:D
Répondre