[ok]Comment affiicher un texte pour quelques secondes

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Bernie
Messages : 282
Inscription : mar. 22/mars/2016 10:12
Localisation : En France

[ok]Comment affiicher un texte pour quelques secondes

Message par Bernie »

Bonsoir
comment faire pour que mon texte soit afficher quelques secondes sur l"écran et disparait ?

merci


Code : Tout sélectionner

EnableExplicit
If  InitSprite()=0 Or InitKeyboard()=0 Or InitSound()=0
  MessageRequester("Erreur", "Impossible d'initialiser le jeux ")
  End
EndIf

;--- Les Constantes 
Enumeration Windows
  #Main_Window
EndEnumeration

Enumeration texte
  #TexteRejouer
  EndEnumeration


  Global gEvent,gWidth=800,gHeight=600,gfont
  gFont = LoadFont(0, "Arial", 18, #PB_Font_Bold )



;---Création de la surface du jeu
OpenWindow(#Main_Window,0,0,gWidth,gHeight,"test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Main_Window),0,0,gWidth,gHeight)

Procedure Affichetext()
  CreateSprite(#TexteRejouer,90,50,#PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(#TexteRejouer))
DrawingFont(gFont)
DrawText(0,10,"TEST  ",RGBA(255,255,255,255),RGBA(0,0,0,0));
StopDrawing()
  EndProcedure

Affichetext()



Repeat
  Repeat
    gEvent=WindowEvent()
    Select gEvent
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until gEvent=0
  ;--- Partie 2D
  ClearScreen(RGB(0, 0, 0))
  DisplayTransparentSprite(#TexteRejouer, 150, 10)
    ExamineKeyboard()
    FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
End
Dernière modification par Bernie le dim. 01/mai/2016 6:59, modifié 1 fois.
Avatar de l’utilisateur
falsam
Messages : 7324
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Comment affiicher un texte pour quelques secondes à l'ec

Message par falsam »

Hello Bernie.

Objectif afficher le texte durant 5 secondes .

On déclare une variable TopTimer = #True

Code : Tout sélectionner

Global TopTimer.b = #True
On ajoute un timer à la fenêtre qui se déclenchera au bout de 5 secondes

Code : Tout sélectionner

AddWindowTimer(#Main_Window, #TimerText, 5000) 
Quand le timer a atteint les 5 secondes, on le détruit.

Code : Tout sélectionner

RemoveWindowTimer(#Main_Window, #TimerText)
Tant que TopTimer est à #True, on affiche le texte.

Code : Tout sélectionner

If TopTimer = #True
  DisplayTransparentSprite(#TexteRejouer, 150, 10)
EndIf
Le code .

Code : Tout sélectionner

EnableExplicit
If  InitSprite()=0 Or InitKeyboard()=0 Or InitSound()=0
  MessageRequester("Erreur", "Impossible d'initialiser le jeux ")
  End
EndIf

;--- Les Constantes 
Enumeration Timer
  #TimerText
EndEnumeration

Enumeration Windows
  #Main_Window
EndEnumeration

Enumeration texte
  #TexteRejouer
EndEnumeration

;TopTimer
Global TopTimer.b = #True

Global gEvent,gWidth=800,gHeight=600,gfont

gFont = LoadFont(0, "Arial", 18, #PB_Font_Bold )

;---Création de la surface du jeu
OpenWindow(#Main_Window,0,0,gWidth,gHeight,"test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Main_Window),0,0,gWidth,gHeight)

;Ajouter un timer qui se déclenchera chaque seconde (1 seconde = 1000 millisecondes)
AddWindowTimer(#Main_Window, #TimerText, 5000) 

Procedure Affichetext()
  CreateSprite(#TexteRejouer,90,50,#PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(#TexteRejouer))
  DrawingFont(gFont)
  DrawText(0,10,"TEST  ",RGBA(255,255,255,255),RGBA(0,0,0,0));
  StopDrawing()
EndProcedure

Affichetext()

Repeat
  Repeat
    gEvent=WindowEvent()
    Select gEvent
      Case #PB_Event_Timer 
        TopTimer = #False
        
        ;Le timer est déclenché : Il sera détruit afin de ne plus afficher le texte
        RemoveWindowTimer(#Main_Window, #TimerText)
        
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until gEvent=0
  ;--- Partie 2D
  ClearScreen(RGB(0, 0, 0))
  
  ;Si on a pas atteint les 5 secondes, on affiche le texte
  If TopTimer = #True
    DisplayTransparentSprite(#TexteRejouer, 150, 10)
  EndIf
  
  ExamineKeyboard()
  FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
End
Configuration : Windows 11 Famille 64-bit - PB 6.20 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
microdevweb
Messages : 1802
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: Comment affiicher un texte pour quelques secondes à l'ec

Message par microdevweb »

Remarque: J'ai utilisé ton code, j'ai juste ajouté quelques code.

2 solutions

Première solution avec un compteur

Code : Tout sélectionner

EnableExplicit
If  InitSprite()=0 Or InitKeyboard()=0 Or InitSound()=0
    MessageRequester("Erreur", "Impossible d'initialiser le jeux ")
    End
EndIf

;--- Les Constantes 
Enumeration Windows
    #Main_Window
EndEnumeration

Enumeration texte
    #TexteRejouer
EndEnumeration


Global gEvent,gWidth=800,gHeight=600,gfont
gFont = LoadFont(0, "Arial", 18, #PB_Font_Bold )
Global Currentime,TimeElapsed,Chrono=1000 ; 1000 millisecondes soit une seconde


;---Création de la surface du jeu
OpenWindow(#Main_Window,0,0,gWidth,gHeight,"test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Main_Window),0,0,gWidth,gHeight)

Procedure Affichetext()
    CreateSprite(#TexteRejouer,90,50,#PB_Sprite_AlphaBlending)
    StartDrawing(SpriteOutput(#TexteRejouer))
    DrawingFont(gFont)
    DrawText(0,10,"TEST  ",RGBA(255,255,255,255),RGBA(0,0,0,0));
    StopDrawing()
EndProcedure

Affichetext()


; On relève le temps avent la boucle principale
Currentime=ElapsedMilliseconds()
Repeat
    Repeat
        gEvent=WindowEvent()
        Select gEvent
            Case #PB_Event_CloseWindow
                End
        EndSelect
    Until gEvent=0
    ;--- Partie 2D
    ClearScreen(RGB(0, 0, 0))
    ; ICi je calcul le temps écoulé
    TimeElapsed=ElapsedMilliseconds()-Currentime
    ; Ici je teste que le temps est inférieur à la limite décidée
    If TimeElapsed<Chrono
        DisplayTransparentSprite(#TexteRejouer, 150, 10)
    EndIf
    ExamineKeyboard()
    FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
End
Deuxième solution en utilisant un WindwosTime

Code : Tout sélectionner

EnableExplicit
If  InitSprite()=0 Or InitKeyboard()=0 Or InitSound()=0
    MessageRequester("Erreur", "Impossible d'initialiser le jeux ")
    End
EndIf

;--- Les Constantes 
Enumeration Windows
    #Main_Window
EndEnumeration
#Timer=0
Enumeration texte
    #TexteRejouer
EndEnumeration


Global gEvent,gWidth=800,gHeight=600,gfont
Global DisplayTxtOn=#True
gFont = LoadFont(0, "Arial", 18, #PB_Font_Bold )
; Une procédure qu sera appelée quand le temps est écoulé
Procedure CallBackTimer()
    DisplayTxtOn=#False
    RemoveWindowTimer(#Main_Window,#Timer)
EndProcedure
;---Création de la surface du jeu
OpenWindow(#Main_Window,0,0,gWidth,gHeight,"test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Main_Window),0,0,gWidth,gHeight)
; J'atoute un timer à la fenêtre
AddWindowTimer(#Main_Window,#Timer,1000)
BindEvent(#PB_Event_Timer,@CallBackTimer(),#Main_Window)
Procedure Affichetext()
    CreateSprite(#TexteRejouer,90,50,#PB_Sprite_AlphaBlending)
    StartDrawing(SpriteOutput(#TexteRejouer))
    DrawingFont(gFont)
    DrawText(0,10,"TEST  ",RGBA(255,255,255,255),RGBA(0,0,0,0));
    StopDrawing()
EndProcedure

Affichetext()


Repeat
    Repeat
        gEvent=WindowEvent()
        Select gEvent
            Case #PB_Event_CloseWindow
                End
        EndSelect
    Until gEvent=0
    ;--- Partie 2D
    ClearScreen(RGB(0, 0, 0))
    ; Je n'afiche le texte que si le flag est vrai
    If DisplayTxtOn=#True
        DisplayTransparentSprite(#TexteRejouer, 150, 10)
    EndIf
    ExamineKeyboard()
    FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
End
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
microdevweb
Messages : 1802
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: Comment affiicher un texte pour quelques secondes à l'ec

Message par microdevweb »

Oups falsam, nos réponses se sont croisées :lol:
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Bernie
Messages : 282
Inscription : mar. 22/mars/2016 10:12
Localisation : En France

Re: Comment affiicher un texte pour quelques secondes à l'ec

Message par Bernie »

Merci à vous 2 pour vos réponses
Répondre