Page 1 sur 1

Clone de flappy bird

Posté : dim. 22 sept. 2019 22:31
par marcgforce
Bonjour,

C'est moi qui ai rien fait, je l'ai trouvé sur pastebin, comme c'est un clone de flappybird et que ça peut intéresser quelqu'un qui fait des recherches sur les collisions et les objets graphiques, je le dépose ici :
#include <WinAPI.au3>
#include <misc.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Array.au3>
OnAutoItExitRegister("GDI_CleanUp")

Global $ObjectsHeight = 155
Global $ObjectsSpace = 180
Global $ObjectsLimit = 585
Global $ObjectsW = 72
Global $ObjectsSpeed = 3
Global $Objects[3][2]

Global $BirdW = 57
Global $BirdH = 40
Global $BirdDropSpeed = 6
Global $Birds[100][2], $BirdNumber = 1
Global $BirdsState[$BirdNumber + 1][2]
Global $BirdsFitness[$BirdNumber]

Global $Gen = 1

   Global $hDC, $hHBitmap, $buffer, $context
Global $hGraphic, $hBrushObject, $hBrushBird, $hPenBlack, $hPenRed, $g_hBitmap, $g_hGfxCtxt

Const $cBlack        = 0xFF000000
Const $cRed          = 0x44FF0000
Const $cObject       = 0x5500BB00
Const $cBird         = 0x55FF0000
Const $cBackground      = 0xFFFFFFFF

Global $Gui
Global $GuiW = 470, $GuiH = 700

$Gui = GUICreate("FlappyBird lololol", $GuiW, $GuiH)
GUISetState()

GDI_StartUp()

CreateFirstObjects()
CreateBird()
$timeHold = TimerInit()
$gameSpeed = 10
Global $holdSpace =False

while True
   If $holdSpace And _IsPressed(20) = False Then
      $holdSpace = False
   ElseIf $holdSpace = False And _IsPressed(20) Then
      $holdSpace = True
      $BirdsState[1][0] = 1
   EndIf

   If TimerDiff($timeHold) > $gameSpeed Then
      MoveObjects()
      MoveBirds()
      $timeHold = TimerInit()
   EndIf
   DrawObjects()
   CheckAlive()
   Switch GUIGetMsg()
      Case -3
         Exit
   EndSwitch
WEnd

Func MoveBirds()
   Local $isNext = False
   For $i = 1 to $Birds[0][0]
      If $Birds[$i][0] < -3 - $BirdW Then ContinueLoop
      If $BirdsState[$i][0] = - 1 Then ContinueLoop
      For $o = 0 to 2
         If $Birds[$i][0] + $BirdW >= $Objects[$o][0] And $Birds[$i][0] <= $Objects[$o][0] + $ObjectsW And ($Birds[$i][1] < $Objects[$o][1] Or ($Birds[$i][1] + $BirdH) > ($Objects[$o][1] + $ObjectsHeight)) Then
            $BirdsState[$i][0] = -1

            ; ====================================================  TÍNH FITNESS  ========================================================
            If $Birds[$i][1] < $Objects[$o][1] Then
               $BirdsFitness[$i - 1] += ($Objects[$o][1] - $Birds[$i][1]) / $Objects[$o][1]
            ElseIf $birds[$i][1] > $Objects[$o][1] + $ObjectsHeight Then
               $BirdsFitness[$i - 1] += ($Birds[$i][1] - ($Objects[$o][1] + $ObjectsHeight)) / ($ObjectsLimit - $Objects[$o][1] + $ObjectsHeight)
            Else
               $BirdsFitness += 100
            EndIf

            ;Set fitness
            ; ============================================================================================================

            $isNext = True
            ExitLoop
         EndIf
      Next
      If $isNext = True then
         $isNext = False
         ContinueLoop
      EndIf
      If $BirdsState[$i][0] > 0 Then
         Switch $BirdsState[$i][0]
            Case 1
               $Birds[$i][1] -= $BirdDropSpeed*1.5
               $BirdsState[$i][1] += 1
               if $BirdsState[$i][1] >= 6 Then
                  $BirdsState[$i][1] = 0
                  $BirdsState[$i][0] += 1
               EndIf
               If $Birds[$i][1] <= 0 Then $Birds[$i][1] = 0
            Case 2
               $Birds[$i][1] -= $BirdDropSpeed
               $BirdsState[$i][1] += 1
               if $BirdsState[$i][1] >= 3 Then
                  $BirdsState[$i][1] = 0
                  $BirdsState[$i][0] += 1
               EndIf
               If $Birds[$i][1] <= 0 Then $Birds[$i][1] = 0
            Case 3
               $Birds[$i][1] -= $BirdDropSpeed / 2
               $BirdsState[$i][1] += 1
               if $BirdsState[$i][1] >= 3 Then
                  $BirdsState[$i][1] = 0
                  $BirdsState[$i][0] += 1
               EndIf
               If $Birds[$i][1] <= 0 Then $Birds[$i][1] = 0
            Case 4
               $BirdsState[$i][1] += 1
               if $BirdsState[$i][1] >= 2 Then
                  $BirdsState[$i][1] = 0
                  $BirdsState[$i][0] += 1
               EndIf
            Case 5
               $Birds[$i][1] += $BirdDropSpeed/2
               $BirdsState[$i][1] += 1
               if $BirdsState[$i][1] >= 3 Then
                  $BirdsState[$i][1] = 0
                  $BirdsState[$i][0] += 1
               EndIf
            Case 6
               $Birds[$i][1] += $BirdDropSpeed
               $BirdsState[$i][1] += 1
               if $BirdsState[$i][1] >= 6 Then
                  $BirdsState[$i][1] = 0
                  $BirdsState[$i][0] = 0
               EndIf
         EndSwitch
      Else
         If $Birds[$i][1] + $BirdH + $BirdDropSpeed >= $ObjectsLimit Then
            $Birds[$i][1] = $ObjectsLimit - $BirdH
         Else
            $Birds[$i][1] += $BirdDropSpeed * 1.5
         EndIf
      EndIf
   Next
EndFunc

Func MoveObjects()
   For $i = 0 to 2
      $Objects[$i][0] -= $ObjectsSpeed
      If $Objects[$i][0] <= - $ObjectsW Then
         $Objects[$i][0] = $Objects[(($i - 1 < 0) ? 2 : $i - 1)][0] + $ObjectsSpace + $ObjectsW
         $Objects[$i][1] = Random(100, $ObjectsLimit - $ObjectsHeight - 100, 1)
      EndIf
   Next
   For $i = 1 to $Birds[0][0]
      If $BirdsState[$i][0] = -1 Then
         $Birds[$i][0] -= $ObjectsSpeed
      Else
         $BirdsFitness[$i - 1] += $ObjectsSpeed
      EndIf
   Next

EndFunc

Func CreateFirstObjects()
   For $i = 0 to 2
      $Objects[$i][0] = 600 + $i * ($ObjectsSpace + $ObjectsW)
      $Objects[$i][1] = Random(100, $ObjectsLimit - $ObjectsHeight - 100, 1)
   Next
EndFunc

Func CreateBird()
   Local $centerX = ($GuiW - $BirdW)/2
   Local $centerY = ($ObjectsLimit - $BirdH)/2
   $Birds[0][0] = $BirdNumber
   For $i = 1 to $Birds[0][0]
      $Birds[$i][0] = $centerX
      $Birds[$i][1] = $centerY
      $BirdsState[$i][0] = 6
      $BirdsState[$i][1] = 0
      $BirdsFitness[$i - 1] = 0
   Next
EndFunc

Func DrawObjects()
   _GDIPlus_GraphicsClear($context, $cBackground)
   For $i = 0 to 2
      _GDIPlus_GraphicsFillRect($context, $Objects[$i][0], 0, $ObjectsW, $Objects[$i][1], $hBrushObject)
      _GDIPlus_GraphicsFillRect($context, $Objects[$i][0], $Objects[$i][1] + $ObjectsHeight, $ObjectsW, $ObjectsLimit - $Objects[$i][1] - $ObjectsHeight, $hBrushObject)

      _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] + 20, 0, $ObjectsW - 30, $Objects[$i][1], $hBrushObject)
      _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] + 20, $Objects[$i][1] + $ObjectsHeight, $ObjectsW - 30, $ObjectsLimit - $Objects[$i][1] - $ObjectsHeight, $hBrushObject)

      _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] - 5, $Objects[$i][1] - 15, $ObjectsW + 10, 15, $hBrushObject)
      _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] - 5, $Objects[$i][1] + $ObjectsHeight, $ObjectsW + 10, 15, $hBrushObject)

      _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0], -2, $ObjectsW, $Objects[$i][1] + 2 - 15, $hPenBlack)
      _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0], $Objects[$i][1] + $ObjectsHeight + 15, $ObjectsW, $ObjectsLimit - $Objects[$i][1] - $ObjectsHeight - 15, $hPenBlack)

      _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0] - 5, $Objects[$i][1] - 15, $ObjectsW + 10, 15, $hPenBlack)
      _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0] - 5, $Objects[$i][1] + $ObjectsHeight, $ObjectsW + 10, 15, $hPenBlack)
   Next
   For $i = 1 to $Birds[0][0]
      _GDIPlus_GraphicsFillEllipse($context, $Birds[$i][0], $Birds[$i][1], $BirdW, $BirdH, $hBrushBird)

      _GDIPlus_GraphicsDrawEllipse($context, $Birds[$i][0], $Birds[$i][1], $BirdW, $BirdH, $hPenBlack)
   Next
   _GDIPlus_GraphicsDrawLine($context, 0, $ObjectsLimit, $GuiW, $ObjectsLimit, $hPenBlack)

   _GDIPlus_GraphicsDrawString($context, "GEN: " & $Gen, 30, $ObjectsLimit + 10)
;~    _GDIPlus_GraphicsDrawImageRect($hGraphic, $g_hBitmap, 0, 0, $GuiW, $GuiH)

   _WinAPI_BitBlt($hDC, 0, 0, $GuiW, $GuiH, $buffer, 0, 0, $SRCCOPY)
EndFunc

Func CheckAlive()
   For $i = 1 to $Birds[0][0]
      If $BirdsState[$i][0] <> - 1 then Return
   Next

   CreateFirstObjects()
   CreateBird()
   $Gen += 1
EndFunc


Func GDI_StartUp()
   _GDIPlus_Startup()

    $hDC = _WinAPI_GetDC($GUI)
    $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $GuiW, $GuiH)

    $buffer = _WinAPI_CreateCompatibleDC($hDC)
    _WinAPI_SelectObject($buffer, $hHBitmap)

    $context = _GDIPlus_GraphicsCreateFromHDC($buffer)
    _GDIPlus_GraphicsSetSmoothingMode($context, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetPixelOffsetMode($context, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)



   $hBrushObject = _GDIPlus_BrushCreateSolid($cObject)
   $hBrushBird = _GDIPlus_BrushCreateSolid($cBird)
   $hPenBlack = _GDIPlus_PenCreate($cBlack)
   $hPenRed = _GDIPlus_PenCreate($cRed)
EndFunc
Func GDI_CleanUp()
    _GDIPlus_BrushDispose($hBrushObject)
    _GDIPlus_BrushDispose($hBrushBird)
    _GDIPlus_PenDispose($hPenBlack)
    _GDIPlus_PenDispose($hPenRed)
    _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_BitmapDispose($g_hBitmap)
   _GDIPlus_ImageDispose($g_hGfxCtxt)
    _GDIPlus_Shutdown()
EndFunc