Flappy

Programmation avancée de jeux en PureBasic
boby
Messages : 261
Inscription : jeu. 07/juin/2007 22:54

Flappy

Message par boby »

Bicose waie note...

Je ne sais pas trop si c'est ici qu'il faut poster ce genre d’ânerie mais bon voila j'avais un peut de temps à perdre... Have fun !
(Je rajouterai une mort si j'ai 30 nouvelles minutes à perdre :D )

Code : Tout sélectionner

EnableExplicit
InitSprite()
InitKeyboard()  
Enumeration ;GameStep
  #Flapping
  #Kabooming
  #Attending
EndEnumeration
Structure wall
  x.i
  y1.i
  y2.i
  pt.i
EndStructure
Global  A = 10, B = 24, x = 18, framerate, pushed, window, wall, flappy, rnd, flappyY, event, lasty, GameStep = #Attending, y , score
Global NewList wall.wall()
Declare init()

window = OpenWindow(#PB_Any,0,0,900,500,"Flappy",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(window),0,0,900,500)
flappy = CreateSprite(#PB_Any,50,50,#PB_Sprite_AlphaBlending|#PB_Sprite_PixelCollision)
wall = CreateSprite(#PB_Any,50,400,#PB_Sprite_AlphaBlending|#PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(flappy)) :Circle(25,25,25,$0033CC) : StopDrawing()
StartDrawing(SpriteOutput(wall)) : Box(0,0,50,400,$006600) : StopDrawing()
init()
framerate = ElapsedMilliseconds()
Repeat
  event = WindowEvent()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space) And pushed = 0 : pushed = 1 : EndIf
  Select GameStep
    Case #Flapping
      If pushed = 1 : x = 12 : EndIf
      If ElapsedMilliseconds() >= framerate + 16
        ClearScreen($CC0000)
        If x < 25
          x + 1
        EndIf
        y = A * Sin(2 * #PI / B * x)
        flappyY + y
        ForEach wall()
          wall()\x = wall()\x -5
          If wall()\x < 150 And wall()\pt = 0 : wall()\pt = 1: score + 1 : EndIf
          If wall()\x < -50 
            wall()\x = 950 
            rnd = -Random(390,110)
            If lasty - rnd < -200 : rnd = lasty + 200 : EndIf
            lasty = rnd
            wall()\y1 = rnd 
            wall()\y2 = rnd +600
            wall()\pt = 0
          EndIf
          DisplaySprite(wall,wall()\x,wall()\y1)
          DisplaySprite(wall,wall()\x,wall()\y2)
          DisplaySprite(flappy,150,flappyY)
          If SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y1) Or SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y2)
            GameStep = #Kabooming
          EndIf
        Next 
        StartDrawing(ScreenOutput()) : DrawText(10,10,Str(score)) : StopDrawing()        
      EndIf
      FlipBuffers()
    Case #Kabooming
      Debug "kaboom baby"
      init()
      GameStep = #Attending
      FlipBuffers()
    Case #Attending
      If pushed : GameStep = #Flapping: pushed = 0 : EndIf
  EndSelect
  If pushed : pushed = 2 : EndIf
  If KeyboardReleased(#PB_Key_Space) : pushed = 0 : EndIf
  Delay(1)
Until event = #PB_Event_CloseWindow

Procedure init()
  score = 0
  ClearScreen($CC0000)
  ClearList(wall())
  AddElement(wall())
  wall()\x = 500 
  wall()\y1 = -300
  wall()\y2 = 300
  
  AddElement(wall())
  wall()\x = 1000
  rnd = -Random(390,110)
  wall()\y1 = rnd 
  wall()\y2 = rnd +600
  lasty = rnd
  FlappyY = 250 
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(450,250,"Space to Flapp !",$000000)
  StopDrawing()
  FlipBuffers()
EndProcedure
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Flappy

Message par falsam »

J’espère que tu auras d'autres moments à perdre ^^

Profites en pour travailler la transparences des sprites.
Configuration : Windows 11 Famille 64-bit - PB 6.03 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
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Flappy

Message par djes »

Pas mal pour quelqu'un qui ne sait pas programmer 8O :mrgreen:
Avatar de l’utilisateur
Ar-S
Messages : 9476
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Flappy

Message par Ar-S »

Sympa pour le partage.
Flap Flap heu Clap clap :)
~~~~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
Guillot
Messages : 529
Inscription : jeu. 25/juin/2015 16:18

Re: Flappy

Message par Guillot »

c'est court, fonctionnel
ça fait un bon didacticiel
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Flappy

Message par Kwai chang caine »

Pas si facile que ça le jeu :D
Merci pour le partage 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
vurvur
Messages : 60
Inscription : ven. 29/août/2014 19:52

Re: Flappy

Message par vurvur »

Avec transparence:

Code : Tout sélectionner


EnableExplicit
InitSprite()
InitKeyboard()  
Enumeration  
  #Flapping
  #Kabooming
  #Attending
EndEnumeration
Structure wall
  x.i
  y1.i
  y2.i
  pt.i
EndStructure
Global  A = 10, B = 24, x = 18, framerate, pushed, window, wall, flappy, rnd, flappyY, event, lasty, GameStep = #Attending, y , score
Global NewList wall.wall()
Declare init()

window = OpenWindow(#PB_Any,0,0,900,500,"Flappy",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(window),0,0,900,500)
flappy = CreateSprite(#PB_Any,50,50,  #PB_Sprite_PixelCollision)
wall =   CreateSprite(#PB_Any,50,400, #PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(flappy)) :Circle(25,25,25,$0033CC) : StopDrawing()
StartDrawing(SpriteOutput(wall)) : Box(0,0,50,400,$006600) : StopDrawing()
init()
framerate = ElapsedMilliseconds()
Repeat
  event = WindowEvent()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space) And pushed = 0 : pushed = 1 : EndIf
  Select GameStep
    Case #Flapping
      If pushed = 1 : x = 12 : EndIf
      If ElapsedMilliseconds() >= framerate + 16
        ClearScreen($CC0000)
        If x < 25
          x + 1
        EndIf
        y = A * Sin(2 * #PI / B * x)
        flappyY + y
        ForEach wall()
          wall()\x = wall()\x -5
          If wall()\x < 150 And wall()\pt = 0 : wall()\pt = 1: score + 1 : EndIf
          If wall()\x < -50 
            wall()\x = 950 
            rnd = -Random(390,110)
            If lasty - rnd < -200 : rnd = lasty + 200 : EndIf
            lasty = rnd
            wall()\y1 = rnd 
            wall()\y2 = rnd +600
            wall()\pt = 0
          EndIf
          DisplaySprite(wall,wall()\x,wall()\y1)
          DisplaySprite(wall,wall()\x,wall()\y2)
          DisplayTransparentSprite(flappy,150,flappyY)
          If SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y1) Or SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y2)
            GameStep = #Kabooming
          EndIf
        Next 
        StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent ) : DrawText(10,10,Str(score)) : StopDrawing()        
      EndIf
      FlipBuffers()
    Case #Kabooming
      Debug "kaboom baby"
      init()
      GameStep = #Attending
      FlipBuffers()
    Case #Attending
      If pushed : GameStep = #Flapping: pushed = 0 : EndIf
  EndSelect
  If pushed : pushed = 2 : EndIf
  If KeyboardReleased(#PB_Key_Space) : pushed = 0 : EndIf
  Delay(1)
Until event = #PB_Event_CloseWindow

Procedure init()
  score = 0
  ClearScreen($CC0000)
  ClearList(wall())
  AddElement(wall())
  wall()\x = 500 
  wall()\y1 = -300
  wall()\y2 = 300
  
  AddElement(wall())
  wall()\x = 1000
  rnd = -Random(390,110)
  wall()\y1 = rnd 
  wall()\y2 = rnd +600
  lasty = rnd
  FlappyY = 250 
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(450,250,"Space to Flapp !",$000000)
  StopDrawing()
  FlipBuffers()
EndProcedure

Avatar de l’utilisateur
JohnJohnsonSHERMAN
Messages : 648
Inscription : dim. 13/déc./2015 11:05
Localisation : Allez, cherche...
Contact :

Re: Flappy

Message par JohnJohnsonSHERMAN »

Exellent :)
Pt'être un peu facile?? :mrgreen: Enfin, il ne manque plus que quelques skins et c'est bon, Flappy Bird 2 est là 8)
"Le bug se situe entre la chaise et le clavier"
Votre expert national en bogage et segfaults.

CPU : AMD A8 Quad core - RAM 8Gb - HDD 2To
  • Windows 10 x64 - PB 5.61 x64
  • Linux Ubuntu 16.04 LTS x64 (dual boot) - PB pas encore réinstallé
vurvur
Messages : 60
Inscription : ven. 29/août/2014 19:52

Re: Flappy

Message par vurvur »

JohnJohnsonSHERMAN a écrit :Exellent :)
Pt'être un peu facile?? :mrgreen: Enfin, il ne manque plus que quelques skins et c'est bon, Flappy Bird 2 est là 8)
Suffit de demander :)
Maintenant avec difficulté croissante... Mon record: 44

Code : Tout sélectionner


EnableExplicit
InitSprite()
InitKeyboard()  
Enumeration  
  #Flapping
  #Kabooming
  #Attending
EndEnumeration
Structure wall
  x.i
  y1.i
  y2.i
  pt.i
EndStructure
Global  A = 10, B = 24, x = 18, framerate, pushed, window, wall, flappy, rnd, flappyY, event, lasty, GameStep = #Attending, y , score
Global NewList wall.wall()
Declare init()

window = OpenWindow(#PB_Any,0,0,900,500,"Flappy",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(window),0,0,900,500)
flappy = CreateSprite(#PB_Any,50,50,  #PB_Sprite_PixelCollision)
wall =   CreateSprite(#PB_Any,50,400, #PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(flappy)) 
Circle(25,25,25,$0033CC) 
Circle(35,25,5 ,$8899CC) 
Circle(45,25,5 ,$8899CC) 
StopDrawing()
StartDrawing(SpriteOutput(wall)) : Box(0,0,50,400,$006600) : StopDrawing()
init()
framerate = ElapsedMilliseconds()
Repeat
  event = WindowEvent()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space) And pushed = 0 : pushed = 1 : EndIf
  Select GameStep
    Case #Flapping
      If pushed = 1 : x = 12 : EndIf
      If ElapsedMilliseconds() >= framerate + 16
        ClearScreen($CC3C3C)
        If x < 25
          x + 1
        EndIf
        y = A * Sin(2 * #PI / B * x)
        flappyY + y
        ForEach wall()
          wall()\x = wall()\x - 6 - score / 5
          If wall()\x < 150 And wall()\pt = 0 : wall()\pt = 1: score + 1 : EndIf
          If wall()\x < -50 
            wall()\x = 950 
            rnd = -Random(390,110)
            If lasty - rnd < -200 : rnd = lasty + 200 : EndIf
            lasty = rnd
            wall()\y1 = rnd 
            wall()\y2 = rnd +600
            wall()\pt = 0
          EndIf
          DisplaySprite(wall,wall()\x,wall()\y1)
          DisplaySprite(wall,wall()\x,wall()\y2)
          DisplayTransparentSprite(flappy,150,flappyY)
          If SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y1) Or SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y2)
            GameStep = #Kabooming
          EndIf
        Next 
        StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent ) : DrawText(10,10,Str(score)) : StopDrawing()        
      EndIf
      FlipBuffers()
    Case #Kabooming
      Debug "kaboom baby - " + score 
      init()
      GameStep = #Attending
      FlipBuffers()
    Case #Attending
      If pushed : GameStep = #Flapping: pushed = 0 : EndIf
  EndSelect
  If pushed : pushed = 2 : EndIf
  If KeyboardReleased(#PB_Key_Space) : pushed = 0 : EndIf
  Delay(1)
Until event = #PB_Event_CloseWindow

Procedure init()
  score = 0
  ClearScreen($CC3C3C)
  ClearList(wall())
  AddElement(wall())
  wall()\x = 500 
  wall()\y1 = -300
  wall()\y2 = 300
  
  AddElement(wall())
  wall()\x = 1000
  rnd = -Random(390,110)
  wall()\y1 = rnd 
  wall()\y2 = rnd +600
  lasty = rnd
  FlappyY = 250 
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(450,250,"Space to Flapp !",$000000)
  StopDrawing()
  FlipBuffers()
EndProcedure

Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Flappy

Message par djes »

Excellent :mrgreen:
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Flappy

Message par Kwai chang caine »

Ca commence à prendre forme :D
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Avatar de l’utilisateur
Ar-S
Messages : 9476
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Flappy

Message par Ar-S »

Centrage du texte, embellissement du sprite et petite animation tant qu'on y est :)

Images + code : http://share.ldvmultimedia.com/FlappyV3.zip

Code seule

Code : Tout sélectionner

EnableExplicit
InitSprite()
InitKeyboard() 
UsePNGImageDecoder()
Enumeration 
  #Flapping
  #Kabooming
  #Attending
  #T
EndEnumeration
Structure wall
  x.i
  y1.i
  y2.i
  pt.i
EndStructure
Global  Count = 0, A = 10, B = 24, x = 18, framerate, pushed, window, wall, flappy, flappy1,flappy2, LenT, rnd, flappyY, event, lasty, GameStep = #Attending, y , score
Global NewList wall.wall()
Declare init()
Declare Flap()

window = OpenWindow(#PB_Any,0,0,900,500,"Flappy",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
AddWindowTimer(window,#T,100)

BindEvent(#PB_Event_Timer, @flap())

OpenWindowedScreen(WindowID(window),0,0,900,500)

wall =   CreateSprite(#PB_Any,50,400, #PB_Sprite_PixelCollision)

flappy1 = LoadSprite(#PB_Any, "i1.png",#PB_Sprite_AlphaBlending|#PB_Sprite_PixelCollision)
flappy2 = LoadSprite(#PB_Any, "i2.png",#PB_Sprite_AlphaBlending|#PB_Sprite_PixelCollision)



StartDrawing(SpriteOutput(wall)) : Box(0,0,50,400,$006600) : StopDrawing()

init()
framerate = ElapsedMilliseconds()
Repeat
  event = WindowEvent()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space) And pushed = 0 : pushed = 1 : EndIf
  Select GameStep
    Case #Flapping
      If pushed = 1 : x = 12 :  EndIf
      If ElapsedMilliseconds() >= framerate + 16
        
        ClearScreen($CC3C3C)
        If x < 25
          x + 1
        EndIf
        y = A * Sin(2 * #PI / B * x)
        flappyY + y
        ForEach wall()
          wall()\x = wall()\x - 6 - score / 5
          If wall()\x < 150 And wall()\pt = 0 : wall()\pt = 1: score + 1 : EndIf
          If wall()\x < -50
            wall()\x = 950
            rnd = -Random(390,110)
            If lasty - rnd < -200 : rnd = lasty + 200 : EndIf
            lasty = rnd
            wall()\y1 = rnd
            wall()\y2 = rnd +600
            wall()\pt = 0
          EndIf
          DisplaySprite(wall,wall()\x,wall()\y1)
          DisplaySprite(wall,wall()\x,wall()\y2)
          DisplayTransparentSprite(flappy,150,flappyY)
          If SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y1) Or SpritePixelCollision(flappy,150,flappyY,wall,wall()\x,wall()\y2)
            GameStep = #Kabooming
          EndIf
        Next
        StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent ) : DrawText(10,10,Str(score)) : StopDrawing()       
      EndIf
      FlipBuffers()
    Case #Kabooming
      Debug "kaboom baby - " + score
      init()
      GameStep = #Attending
      FlipBuffers()
    Case #Attending
      If pushed : GameStep = #Flapping: pushed = 0 : EndIf
  EndSelect
  If pushed : pushed = 2 : EndIf
  
  If KeyboardReleased(#PB_Key_Space) : pushed = 0 : EndIf
  Delay(1)
Until event = #PB_Event_CloseWindow

Procedure init()
  score = 0
  ClearScreen($CC3C3C)
  ClearList(wall())
  AddElement(wall())
  wall()\x = 500
  wall()\y1 = -300
  wall()\y2 = 300
 
  AddElement(wall())
  wall()\x = 1000
  rnd = -Random(390,110)
  wall()\y1 = rnd
  wall()\y2 = rnd +600
  lasty = rnd
  FlappyY = 250
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Transparent)
  LenT = TextWidth("Space to Flapp !")
  DrawText(450-(Lent/2),250,"Space to Flapp !",$000000)
  StopDrawing()
  FlipBuffers()
EndProcedure

Procedure Flap()
; Ar-S
  Count+1
  If count=1
    flappy = flappy2
  Else
    flappy = flappy1
    count = 0
  EndIf
EndProcedure


~~~~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
boby
Messages : 261
Inscription : jeu. 07/juin/2007 22:54

Re: Flappy

Message par boby »

Qu'est-ce que j'ai fait... XD Vous êtes de grand malades

Vurvur je n'arrive pas à voir la différence qui fait que la transparence fonctionne chez toi... Quelqu'un pourrait m'éclairer ?

J'ai pas trop eu le temps de bosser dessus mais j'en était rendu là avant de venir sur le forum, j'ai visiblement pas du tout comprendre...

Code : Tout sélectionner

InitSprite()
OpenWindow(0,0,0,500,500,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,500,500)
TransparentSpriteColor(#PB_Default,$000000)
CreateSprite(0,50,50,#PB_Sprite_AlphaBlending)
StartDrawing(SpriteOutput(0))
Circle(25,25,25,$0000CC)
StopDrawing()
;TransparentSpriteColor(0,$000000)
Repeat
  ClearScreen($cc0000)
  DisplayTransparentSprite(0,250,250)
  FlipBuffers()
  Delay(1)
Until WindowEvent() = #PB_Event_CloseWindow
#Sprite The sprite to use. If #PB_Default is used, then the default color (black - RGB(0,0,0)) is changed to the new given one and all future loaded or created sprites will use this color as the transparent one.
Je suis en pure 5.51, je ne vois pas ce qui n'est pas bon...
Avatar de l’utilisateur
Ar-S
Messages : 9476
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Flappy

Message par Ar-S »

DisplaySprite(flappy,150,flappyY) => DisplayTransparentSprite(flappy,150,flappyY)
~~~~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
boby
Messages : 261
Inscription : jeu. 07/juin/2007 22:54

Re: Flappy

Message par boby »

Bah ça je l'avais essayé, mais ça ne suffit pas, il y a visiblement quelque chose que j'ai loupé...
Répondre