Simon Says

Programmation avancée de jeux en PureBasic
Avatar de l’utilisateur
venom
Messages : 3072
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Simon Says

Message par venom »

Bonjour,

voilà je me suis amusé a codé un petit "simon"
Il n'y a rien d'extraordinaire dans ce code, donc rien d'extraordinaire a dire dessus :lol:

Règle du jeu :
Source wikipedia a écrit :Le jeu du Simon trouve son origine dans le jeu pour enfant Jacques a dit, d'où il tire également son nom, puisque dans les pays anglophones ce n'est pas Jacques mais Simon (Simon Says…) qui donne les ordres.

Le jeu, éclaire une des quatre couleurs et produit un son toujours associé à cette couleur. Le joueur doit alors appuyer sur la touche de la couleur qui vient de s'allumer.

Le jeu répète la même couleur et le même son, puis ajoute au hasard une nouvelle couleur. Le joueur doit reproduire cette nouvelle séquence. Chaque fois que le joueur reproduit correctement la séquence, le jeu ajoute une nouvelle couleur.
Il vous faut juste cliquer et avoir de la mémoire. :roll:

Source + fichiers

Code seul :

Code : Tout sélectionner

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; +     Autor :         Venom                                                                     +
; +     Project name :  Simon Says                                                                +
; +     Version :       V 1.0                                                                     +
; +     Compilator :    PureBasic V5.73                                                           +
; +     Date :          16/01/2021                                                                +
; +     OS :            Windows 10                                                                +
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;- Window Constants
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Enumeration
  #Window_0
EndEnumeration


; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;- Gadgets Constants
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Enumeration
  #GreenNight
  #RedNight
  #YellowNight
  #BlueNight
  #GreenLight
  #RedLight
  #YellowLight
  #BlueLight
  
  #Sound_1
  #Sound_2
  #Sound_3
  #Sound_4
  #Sound_Over
  
  #Button_1
  #Button_2
  #Button_3
  #Button_4
EndEnumeration


EnableExplicit


Global EventID
Global a, NbCoups = 0, CoupEnCours = 0, ID_Button_1 = 1, ID_Button_2 = 2, ID_Button_3 = 3, ID_Button_4 = 4, Time = 500, TimeGamer = 200,TimeOver = 1000
Global DossierGraphique$ = "gfx\"
Global DossierSons$ = "sfx\"
Global Dim TabGame(99)


Declare LoadFilesAndTab()
Declare Intro(*Valeur)
Declare Over(*Valeur)
Declare PlayTab(*valeur)


; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;- ouverture de la fenetre
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  If OpenWindow(#Window_0, 0, 0, 300, 300, "Simon Says", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
   LoadFilesAndTab()
    
    ButtonImageGadget(#Button_1, 0, 0, 150, 150, ImageID(#GreenNight))   
    ButtonImageGadget(#Button_2, 150, 0, 150, 150, ImageID(#RedNight))   
    ButtonImageGadget(#Button_3, 0, 150, 150, 150, ImageID(#YellowNight))   
    ButtonImageGadget(#Button_4, 150, 150, 150, 150, ImageID(#BlueNight))

     CreateThread(@Intro(), 23)

  EndIf 


; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;- Boucle principale
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  Repeat
   EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
          
        Case #Button_1
         SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#GreenLight))
          PlaySound(#Sound_1, #PB_Sound_MultiChannel, 100)
           Delay(TimeGamer) 
         SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#GreenNight))
          Delay(TimeGamer)
         
         If TabGame(CoupEnCours) = ID_Button_1
           
          If CoupEnCours = NbCoups
            SetWindowTitle(#Window_0, "Simon Says | tour : "+Str(NbCoups+1)+" terminé")
             NbCoups = NbCoups+1
              CreateThread(@PlayTab(), 23)
               CoupEnCours = 0
          ElseIf  CoupEnCours < NbCoups And TabGame(CoupEnCours) = ID_Button_1
            CoupEnCours = CoupEnCours+1
          EndIf
          
         Else
          PlaySound(#Sound_Over, #PB_Sound_MultiChannel, 100)
           SetWindowTitle(#Window_0, "Simon Says | game over")
            Delay(TimeOver)
             CreateThread(@Over(), 23)
         EndIf 
         
         
        Case #Button_2
         SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#RedLight))
          PlaySound(#Sound_2, #PB_Sound_MultiChannel, 100)
           Delay(TimeGamer)
         SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#RedNight))
          Delay(TimeGamer)
         
         If TabGame(CoupEnCours) = ID_Button_2
           
          If CoupEnCours = NbCoups
            SetWindowTitle(#Window_0, "Simon Says | tour : "+Str(NbCoups+1)+" terminé")
             NbCoups = NbCoups+1
              CreateThread(@PlayTab(), 23)
               CoupEnCours = 0
          ElseIf  CoupEnCours < NbCoups And TabGame(CoupEnCours) = ID_Button_2
            CoupEnCours = CoupEnCours+1
          EndIf
          
         Else
          PlaySound(#Sound_Over, #PB_Sound_MultiChannel, 100)
           SetWindowTitle(#Window_0, "Simon Says | game over")
            Delay(TimeOver)
             CreateThread(@Over(), 23)  
         EndIf  
          
        Case #Button_3
         SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#YellowLight))
          PlaySound(#Sound_3, #PB_Sound_MultiChannel, 100)
           Delay(TimeGamer)
         SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#YellowNight))
          Delay(TimeGamer)
         
         If TabGame(CoupEnCours) = ID_Button_3
           
          If CoupEnCours = NbCoups
            SetWindowTitle(#Window_0, "Simon Says | tour : "+Str(NbCoups+1)+" terminé")
             NbCoups = NbCoups+1
              CreateThread(@PlayTab(), 23)
               CoupEnCours = 0
          ElseIf  CoupEnCours < NbCoups And TabGame(CoupEnCours) = ID_Button_3
            CoupEnCours = CoupEnCours+1
          EndIf
          
         Else
          PlaySound(#Sound_Over, #PB_Sound_MultiChannel, 100)
           SetWindowTitle(#Window_0, "Simon Says | game over")
            Delay(TimeOver)
             CreateThread(@Over(), 23)
         EndIf 
          
        Case #Button_4
         SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#BlueLight))
          PlaySound(#Sound_4, #PB_Sound_MultiChannel, 100)
           Delay(TimeGamer)
         SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#BlueNight))
          Delay(TimeGamer)
         
         If TabGame(CoupEnCours) = ID_Button_4
           
          If CoupEnCours = NbCoups
            SetWindowTitle(#Window_0, "Simon Says | tour : "+Str(NbCoups+1)+" terminé")
             NbCoups = NbCoups+1
              CreateThread(@PlayTab(), 23)
               CoupEnCours = 0
          ElseIf  CoupEnCours < NbCoups And TabGame(CoupEnCours) = ID_Button_4
            CoupEnCours = CoupEnCours+1
          EndIf

         Else
          PlaySound(#Sound_Over, #PB_Sound_MultiChannel, 100)
           SetWindowTitle(#Window_0, "Simon Says | game over")
            Delay(TimeOver)
             CreateThread(@Over(), 23)
         EndIf  

      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow



; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;- Procedures
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Procedure LoadFilesAndTab()
 UsePNGImageDecoder()

 If InitSound() = 0
  MessageRequester("Erreur", "Impossible d'initialisé le son", 0)
   End
 EndIf
  
 ; chargement des images
 LoadImage(#GreenNight, DossierGraphique$+"GreenNight.png", 0)
 LoadImage(#RedNight, DossierGraphique$+"RedNight.png", 0)
 LoadImage(#YellowNight, DossierGraphique$+"YellowNight.png", 0)
 LoadImage(#BlueNight, DossierGraphique$+"BlueNight.png", 0)
 ;
 LoadImage(#GreenLight, DossierGraphique$+"Green.png", 0)
 LoadImage(#RedLight, DossierGraphique$+"Red.png", 0)
 LoadImage(#YellowLight, DossierGraphique$+"Yellow.png", 0)
 LoadImage(#BlueLight, DossierGraphique$+"Blue.png", 0)
    
 ; Chargement des sons
 LoadSound(#Sound_1, DossierSons$+"1.wav")
 LoadSound(#Sound_2, DossierSons$+"2.wav")
 LoadSound(#Sound_3, DossierSons$+"3.wav")
 LoadSound(#Sound_4, DossierSons$+"4.wav")
 LoadSound(#Sound_Over, DossierSons$+"over.wav")
 
 
 ; rempli le tableau aléatoire entre 1 et 4
  For a = 0 To 99
   TabGame(a) = Random(4, 1)
  Next 
EndProcedure
  
  
Procedure Intro(*Valeur)
 Delay(TimeGamer)
  SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#GreenLight)); allume la case green
   PlaySound(#Sound_1, #PB_Sound_MultiChannel, 100); joue le son
    Delay(TimeGamer)
  SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#RedLight)); allume la case red
   PlaySound(#Sound_2, #PB_Sound_MultiChannel, 100); joue le son
    Delay(TimeGamer)
  SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#YellowLight)); allume la case yellow
   PlaySound(#Sound_3, #PB_Sound_MultiChannel, 100); joue le son
    Delay(TimeGamer)
  SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#BlueLight)); allume la case blue
   PlaySound(#Sound_4, #PB_Sound_MultiChannel, 100); joue le son
    Delay(Time) 
  SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#GreenNight)); eteint la case green
  SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#RedNight)); eteint la case red
  SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#YellowNight)); eteint la case yellow
  SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#BlueNight)); eteint la case blue
    Delay(Time)
  
  ;- demarre la game
  CreateThread(@PlayTab(), 23)
EndProcedure


Procedure Over(*Valeur)
 Delay(Time)
  SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#GreenLight)); allume la case green
   PlaySound(#Sound_4, #PB_Sound_MultiChannel, 100); joue le son
    Delay(TimeGamer)
  SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#RedLight)); allume la case red
   PlaySound(#Sound_3, #PB_Sound_MultiChannel, 100); joue le son
    Delay(TimeGamer)
  SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#YellowLight)); allume la case yellow
   PlaySound(#Sound_2, #PB_Sound_MultiChannel, 100); joue le son
    Delay(TimeGamer)
  SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#BlueLight)); allume la case blue
   PlaySound(#Sound_1, #PB_Sound_MultiChannel, 100); joue le son
 Delay(Time)
   End 
EndProcedure


Procedure PlayTab(*valeur)
Delay(TimeGamer)
For a = 0 To NbCoups

  If TabGame(a) = 1
   SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#GreenLight))
   PlaySound(#Sound_1, #PB_Sound_MultiChannel, 100)
    Delay(Time) 
   SetGadgetAttribute(#Button_1, #PB_Button_Image, ImageID(#GreenNight))
  ElseIf TabGame(a) = 2
   SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#RedLight))
   PlaySound(#Sound_2, #PB_Sound_MultiChannel, 100)
    Delay(Time)
   SetGadgetAttribute(#Button_2, #PB_Button_Image, ImageID(#RedNight))
  ElseIf TabGame(a) = 3
   SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#YellowLight))
   PlaySound(#Sound_3, #PB_Sound_MultiChannel, 100)
    Delay(Time)
   SetGadgetAttribute(#Button_3, #PB_Button_Image, ImageID(#YellowNight))
  ElseIf TabGame(a) = 4
   SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#BlueLight))
   PlaySound(#Sound_4, #PB_Sound_MultiChannel, 100)
    Delay(Time)
   SetGadgetAttribute(#Button_4, #PB_Button_Image, ImageID(#BlueNight))
  EndIf 
 Delay(Time)
Next
EndProcedure






@++
Dernière modification par venom le mer. 20/janv./2021 22:08, modifié 1 fois.
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
Ar-S
Messages : 9477
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Simon Says

Message par Ar-S »

Sympa. Petites coquilles :

Terminer => Terminé
Manque l’icône du jeu dans l'archive
Sinon pas mal de répétions dans le code mais ça marche

Je trouve par contre étrange le fait que tu crées X fois le thread CreateThread(@PlayTab(),23) sans jamais le tuer.

Merci pour le partage :)
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Avatar de l’utilisateur
venom
Messages : 3072
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: Simon Says

Message par venom »

Merci de tes remarques Ar-S.

J'ai corriger la faute et ajouter l’icône a l'archive :wink: Pour le killthread, je n'ai pas regarder... :oops:
Disons que pour le moment ça fonctionne :roll: :D






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Simon Says

Message par Kwai chang caine »

Sympa :D
J'en avais un de ce style y'a environ 50 ans :oops:
Merci pour le partage 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Répondre