Flappy

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

Re: Flappy

Message par boby »

Kaboom baby !

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 cloud, white, black, cloudloop
Global Dim cloud(2,1)
Global NewList wall.wall()
Declare init()
Declare Kaboom()
cloud(0,0) = 80 : cloud(1,0) = 480 : cloud(2,0) = 880
cloud(0,1) = Random(50) : cloud(1,1) = Random(50) : cloud(2,1) = Random(50)
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)
cloud = CreateSprite(#PB_Any,200,130)
white = CreateSprite(#PB_Any,50,50)
black = CreateSprite(#PB_Any,50,50)
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()
StartDrawing(SpriteOutput(white)) : Circle(25,25,25,$FFFFFF) : StopDrawing()
StartDrawing(SpriteOutput(black)) : Circle(25,25,25,$100000) : StopDrawing()
StartDrawing(SpriteOutput(cloud))
Circle(35,65,35,$FFCCFF)
Circle(80,40,40,$FFCCFF)
Circle(75,95,30,$FFCCFF)
Circle(110,30,30,$FFCCFF)
Circle(130,70,50,$FFCCFF)
Circle(175,80,20,$FFCCFF)
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
        For cloudloop = 0 To 2
          cloud(cloudloop,0) - 1
          If cloud(cloudloop,0) < -200
            cloud(cloudloop,0) = 980
            cloud(cloudloop,1) = Random(50)
          EndIf
        Next cloudloop
        DisplayTransparentSprite(cloud,cloud(0,0),cloud(0,1))
        RotateSprite(cloud,180,#PB_Absolute)
        DisplayTransparentSprite(cloud,cloud(1,0),cloud(1,1))
        RotateSprite(cloud,0,#PB_Absolute)
        DisplayTransparentSprite(cloud,cloud(2,0),cloud(2,1))
        ForEach wall()
          wall()\x = wall()\x - 7
          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 +550
            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),$000000) : StopDrawing()       
      EndIf
      StartDrawing(ScreenOutput())
      Box(0,480,900,20,$006633)
      Box(0,475,900,5,$006699)
      StopDrawing()
      FlipBuffers()
    Case #Kabooming
      If ElapsedMilliseconds() >= framerate + 32
        Kaboom() : framerate = ElapsedMilliseconds()
      EndIf
    Case #Attending
      If pushed : GameStep = #Flapping: pushed = 0: score = 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()
  ClearScreen($CC3C3C)
  ClearList(wall())
  AddElement(wall())
  wall()\x = 500
  wall()\y1 = -300
  wall()\y2 = 250
 
  AddElement(wall())
  wall()\x = 1000
  rnd = -Random(390,110)
  wall()\y1 = rnd
  wall()\y2 = rnd +550
  lasty = rnd
  FlappyY = 250
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(WindowWidth(window)/2 - TextWidth("Space to Flapp !")/2,250,"Space to Flapp !",$000000)
  DrawText(WindowWidth(window)/2 - TextWidth(" Score : "+Str(score))/2,270," Score : "+Str(score),$000000)
  StopDrawing()
  DisplayTransparentSprite(cloud,cloud(0,0),cloud(0,1))
  RotateSprite(cloud,180,#PB_Absolute)
  DisplayTransparentSprite(cloud,cloud(1,0),cloud(1,1))
  RotateSprite(cloud,0,#PB_Absolute)
  DisplayTransparentSprite(cloud,cloud(2,0),cloud(2,1))
  StartDrawing(ScreenOutput())
  Box(0,480,900,20,$006633)
  Box(0,475,900,5,$006699)
  StopDrawing()
  FlipBuffers()
EndProcedure

Procedure Kaboom()
  Static Kaboom = 15, kaboomstat, flapX = 150
  Protected rnd
  Kaboom - 1
  rnd = Random(20)
  ClearScreen($CC3C3C)
  DisplayTransparentSprite(cloud,cloud(0,0),cloud(0,1))
  RotateSprite(cloud,180,#PB_Absolute)
  DisplayTransparentSprite(cloud,cloud(1,0),cloud(1,1))
  RotateSprite(cloud,0,#PB_Absolute)
  DisplayTransparentSprite(cloud,cloud(2,0),cloud(2,1))  
  If kaboomstat
    flapX + rnd
    flappyY - rnd
    DisplayTransparentSprite(black,flapX,flappyY)
    ForEach wall()
      DisplaySprite(wall,wall()\x+rnd/2,wall()\y1)
      DisplaySprite(wall,wall()\x+rnd/2,wall()\y2)
    Next
    DisplayTransparentSprite(black,flapX,flappyY)
    kaboomstat = 0
  Else
    flapX - rnd
    flappyY + rnd
    ForEach wall()
      DisplaySprite(wall,wall()\x-rnd/2,wall()\y1)
      DisplaySprite(wall,wall()\x-rnd/2,wall()\y2)
    Next
    DisplayTransparentSprite(white,flapX,flappyY)
    kaboomstat = 1
  EndIf
  StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent ) : DrawText(10,10,Str(score),$000000) : StopDrawing() 
  StartDrawing(ScreenOutput())
  Box(0,480,900,20,$006633)
  Box(0,475,900,5,$006699)
  StopDrawing()
  FlipBuffers()
  If Kaboom = 0
    Kaboom = 15
    flapX = 150
    init()
    GameStep = #Attending
  EndIf
EndProcedure
Avatar de l’utilisateur
Ar-S
Messages : 9472
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Flappy

Message par Ar-S »

Plus dure cette version :)
Tu n'aimes pas mon sprite animé ^^
~~~~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 »

Ouais j'ai resserrer un peut l'espacement, c'était trop simple.
Pour "ma" version je voulais rester sur du "code only" (pis j'adore les 2 pitis nyeux qu'a rajouté vurvur XD)
Avatar de l’utilisateur
Ar-S
Messages : 9472
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Flappy

Message par Ar-S »

Tu devrais proposer 2 ou 3 niveaux de difficultés au démarrage, ainsi tu jouerais juste sur l'espacement et la vitesse ^^ parce que là je le trouve chaud quand même :D
~~~~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 »

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 cloud, white, black, cloudloop, space = 595, difficult = 1, speed = 5
Global Dim cloud(2,1)
Global NewList wall.wall()
Declare init()
Declare Kaboom()
cloud(0,0) = 80 : cloud(1,0) = 480 : cloud(2,0) = 880
cloud(0,1) = Random(50) : cloud(1,1) = Random(50) : cloud(2,1) = Random(50)
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)
cloud = CreateSprite(#PB_Any,200,130)
white = CreateSprite(#PB_Any,50,50)
black = CreateSprite(#PB_Any,50,50)
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()
StartDrawing(SpriteOutput(white)) : Circle(25,25,25,$FFFFFF) : StopDrawing()
StartDrawing(SpriteOutput(black)) : Circle(25,25,25,$100000) : StopDrawing()
StartDrawing(SpriteOutput(cloud))
Circle(35,65,35,$FFCCFF)
Circle(80,40,40,$FFCCFF)
Circle(75,95,30,$FFCCFF)
Circle(110,30,30,$FFCCFF)
Circle(130,70,50,$FFCCFF)
Circle(175,80,20,$FFCCFF)
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
        For cloudloop = 0 To 2
          cloud(cloudloop,0) - 1
          If cloud(cloudloop,0) < -200
            cloud(cloudloop,0) = 980
            cloud(cloudloop,1) = Random(50)
          EndIf
        Next cloudloop
        DisplayTransparentSprite(cloud,cloud(0,0),cloud(0,1))
        RotateSprite(cloud,180,#PB_Absolute)
        DisplayTransparentSprite(cloud,cloud(1,0),cloud(1,1))
        RotateSprite(cloud,0,#PB_Absolute)
        DisplayTransparentSprite(cloud,cloud(2,0),cloud(2,1))
        ForEach wall()
          wall()\x = wall()\x - speed
          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 +space
            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),$000000) : StopDrawing()       
      EndIf
      StartDrawing(ScreenOutput())
      Box(0,480,900,20,$006633)
      Box(0,475,900,5,$006699)
      StopDrawing()
      FlipBuffers()
    Case #Kabooming
      If ElapsedMilliseconds() >= framerate + 32
        Kaboom() : framerate = ElapsedMilliseconds()
      EndIf
    Case #Attending
      If pushed 
        init() : GameStep = #Flapping : pushed = 0 : score = 0 
      EndIf
      If KeyboardReleased(#PB_Key_Down) And difficult < 4 : difficult + 1 : space - 20 : speed + 1 : EndIf
      If KeyboardReleased(#PB_Key_Up) And difficult > 1 : difficult - 1 : space + 20 : speed - 1 :EndIf 
      ClearScreen($CC3C3C)
      StartDrawing(ScreenOutput())
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText(WindowWidth(window)/2 - TextWidth("Space to Flapp !")/2,150,"Space to Flapp !",$000000)
      DrawText(WindowWidth(window)/2 - TextWidth(" Score : "+Str(score))/2,170," Score : "+Str(score),$000000)
      DrawText(WindowWidth(window)/2 - TextWidth("Easy as fuck")/2,200,"Easy as fuck",$000000)
      DrawText(WindowWidth(window)/2 - TextWidth("Medium")/2,230,"Medium",$000000)
      DrawText(WindowWidth(window)/2 - TextWidth("Hard as hell")/2,260,"Hard as hell",$000000)
      DrawText(WindowWidth(window)/2 - TextWidth("You Shall not PASS !!")/2,290,"You Shall not PASS !!",$0000FF)
      DrawingMode(#PB_2DDrawing_Outlined)
      Box(WindowWidth(window)/2 - TextWidth("You Shall not PASS !!")/2-5,165+difficult*30,TextWidth("You Shall not PASS !!")+10,TextHeight("P")+10,$00CC00)
      StopDrawing()
      DisplayTransparentSprite(cloud,cloud(0,0),cloud(0,1))
      RotateSprite(cloud,180,#PB_Absolute)
      DisplayTransparentSprite(cloud,cloud(1,0),cloud(1,1))
      RotateSprite(cloud,0,#PB_Absolute)
      DisplayTransparentSprite(cloud,cloud(2,0),cloud(2,1))
      StartDrawing(ScreenOutput())
      Box(0,480,900,20,$006633)
      Box(0,475,900,5,$006699)
      StopDrawing()
      FlipBuffers()      
  EndSelect
  If pushed : pushed = 2 : EndIf
  If KeyboardReleased(#PB_Key_Space) : pushed = 0 : EndIf
  Delay(1)
Until event = #PB_Event_CloseWindow

Procedure init()
  
  ClearList(wall())
  AddElement(wall())
  wall()\x = 500
  wall()\y1 = -300
  wall()\y2 = -300 + space

  AddElement(wall())
  wall()\x = 1000
  rnd = -Random(390,110)
  wall()\y1 = rnd
  wall()\y2 = rnd +space
  lasty = rnd
  FlappyY = 250

EndProcedure

Procedure Kaboom()
  Static Kaboom = 15, kaboomstat, flapX = 150
  Protected rnd
  Kaboom - 1
  rnd = Random(20)
  ClearScreen($CC3C3C)
  DisplayTransparentSprite(cloud,cloud(0,0),cloud(0,1))
  RotateSprite(cloud,180,#PB_Absolute)
  DisplayTransparentSprite(cloud,cloud(1,0),cloud(1,1))
  RotateSprite(cloud,0,#PB_Absolute)
  DisplayTransparentSprite(cloud,cloud(2,0),cloud(2,1)) 
  If kaboomstat
    flapX + rnd
    flappyY - rnd
    DisplayTransparentSprite(black,flapX,flappyY)
    ForEach wall()
      DisplaySprite(wall,wall()\x+rnd/2,wall()\y1)
      DisplaySprite(wall,wall()\x+rnd/2,wall()\y2)
    Next
    DisplayTransparentSprite(black,flapX,flappyY)
    kaboomstat = 0
  Else
    flapX - rnd
    flappyY + rnd
    ForEach wall()
      DisplaySprite(wall,wall()\x-rnd/2,wall()\y1)
      DisplaySprite(wall,wall()\x-rnd/2,wall()\y2)
    Next
    DisplayTransparentSprite(white,flapX,flappyY)
    kaboomstat = 1
  EndIf
  StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent ) : DrawText(10,10,Str(score),$000000) : StopDrawing()
  StartDrawing(ScreenOutput())
  Box(0,480,900,20,$006633)
  Box(0,475,900,5,$006699)
  StopDrawing()
  FlipBuffers()
  If Kaboom = 0
    Kaboom = 15
    flapX = 150
    ;init()
    GameStep = #Attending
  EndIf
EndProcedure
Un truc du genre ?
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 »

Cool !!! 8)
ImageLe bonheur est une route...
Pas une destination

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

Re: Flappy

Message par JohnJohnsonSHERMAN »

C'est de mieux en mieux !! Exellent ! :P :)
"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é
Répondre