[Ex] Coding Challenge : Star Field

Partagez vos scripts, et vos applications AutoIt.
Règles du forum
.
Répondre
Avatar du membre
TommyDDR
Modérateur
Modérateur
Messages : 2086
Enregistré le : mar. 22 juil. 2008 21:55
Localisation : Nantes
Status : Hors ligne

[Ex] Coding Challenge : Star Field

#1

Message par TommyDDR »

Je vous mets à disposition l'adaptation AutoIt du code de la chaine The Coding Train sur le champ d'étoiles (mode hyper-espace, quantum drive) : https://www.youtube.com/watch?v=17WoOqgXsRM

Pas de but spécial à ce programme si ce n'est la contemplation.

La vitesse est contrôlée par la souris (à gauche de la fenêtre = fixe, à droite = rapide).
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <Math.au3>

Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
Opt("MouseCoordMode", 2)

Global $gui
Global $taille[2] = [600, 600]
Global $NB_STARS = 100
Global $maxSize = 10
Global $maxSpeed = 100
Global $speed = $maxSpeed

Enum $C_STAR_X, $C_STAR_Y, $C_STAR_Z, $C_STAR_PZ, $C_STAR_MAX

Global $stars[$NB_STARS][$C_STAR_MAX]

_GDIPlus_Startup()

$gui = GUICreate("", $taille[0], $taille[1])
GUISetOnEvent($GUI_EVENT_CLOSE, "quit", $gui)
GUISetState(@SW_SHOW, $gui)

Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gui)
Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics($taille[0], $taille[1], $hGraphic)
Global $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

Global $blackBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
Global $whiteBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
Global $whitePen = _GDIPlus_PenCreate(0xFFFFFFFF)

OnAutoItExitRegister(freeRessources)

setup()

While(True)
   loop()
WEnd

Func setup()
   For $i = 0 To $NB_STARS - 1
      _Array_Set($stars, createStar(), $i)
   Next
EndFunc

Func loop()
   Local $mgp = MouseGetPos(0)
   $mgp = _Min(_Max($mgp, 0), $taille[0])
   $speed = map($mgp, 0, 600, 0, $maxSpeed)
   _GDIPlus_GraphicsFillRect($hBackbuffer, 0, 0, $taille[0], $taille[1], $blackBrush)
   For $i = 0 To $NB_STARS - 1
      update($i)
      show($i, $hBackbuffer)
   Next
   _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $taille[0], $taille[1])
   Sleep(1)
EndFunc

Func update($i)
   $stars[$i][$C_STAR_Z] -= $speed
   If($stars[$i][$C_STAR_Z] <= 0) Then
      $stars[$i][$C_STAR_X] = Random(-$taille[0]/4, $taille[0]/4, 1)
      $stars[$i][$C_STAR_Y] = Random(-$taille[1]/4, $taille[1]/4, 1)
      $stars[$i][$C_STAR_Z] = Random(0, $taille[0], 1)
      $stars[$i][$C_STAR_PZ] = $stars[$i][$C_STAR_Z]
   EndIf
EndFunc

Func show($i, $hGraphic)
   Local $x = $stars[$i][$C_STAR_X]
   Local $y = $stars[$i][$C_STAR_Y]
   Local $z = $stars[$i][$C_STAR_Z]
   Local $pz = $stars[$i][$C_STAR_PZ]

   Local $sx = map($x / $z, 0, 1, 0, $taille[0]);
   Local $sy = map($y / $z, 0, 1, 0, $taille[1]);

   Local $size = map($z, 0, $taille[0], $maxSize, 0)

   _GDIPlus_GraphicsFillEllipse($hGraphic, $taille[0]/2 + $sx - $size/2, $taille[1]/2 + $sy - $size/2, $size, $size, $whiteBrush)

   Local $px = map($x / $pz, 0, 1, 0, $taille[0])
   Local $py = map($y / $pz, 0, 1, 0, $taille[1])

   _GDIPlus_GraphicsDrawLine($hGraphic, $taille[0]/2 + $px, $taille[1]/2 + $py, $taille[0]/2 + $sx, $taille[1]/2 + $sy, $whitePen)
EndFunc

Func createStar()
   Local $star[$C_STAR_MAX]
   $star[$C_STAR_X] = Random(-$taille[0]/2, $taille[0]/2, 1)
   $star[$C_STAR_Y] = Random(-$taille[1]/2, $taille[1]/2, 1)
   $star[$C_STAR_Z] = Random(0, $taille[0], 1)
   $star[$C_STAR_PZ] = $star[$C_STAR_Z]
   Return $star
EndFunc

Func map($x, $rangex1, $rangex2, $rangey1, $rangey2)
   Return (($x - $rangex1) / ($rangex2 - $rangex1)) * ($rangey2 - $rangey1) + $rangey1
EndFunc

Func _Array_Set(ByRef $array, $add, $indice)
   If(Not(IsArray($array) And IsArray($add))) Then Return SetError(1, 0, -1)
   Local $ubound = _Max(UBound($array, 2), UBound($add, 1))
   ReDim $array[UBound($array, 1)][$ubound]
   ReDim $add[$ubound]
   For $j = 0 To UBound($array, 2)-1
      $array[$indice][$j] = $add[$j]
   Next
EndFunc

Func quit()
   Exit
EndFunc

Func freeRessources()
   _GDIPlus_BrushDispose($blackBrush)
   _GDIPlus_BrushDispose($whiteBrush)
   _GDIPlus_PenDispose($whitePen)
   _GDIPlus_GraphicsDispose($hBackbuffer)
   _GDIPlus_BitmapDispose($hBitmap)
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_Shutdown()
EndFunc
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Avatar du membre
walkson
Modérateur
Modérateur
Messages : 1020
Enregistré le : ven. 12 août 2011 19:49
Localisation : Hurepoix
Status : Hors ligne

Re: [Ex] Coding Challenge : Star Field

#2

Message par walkson »

Tommy
#include "./include/_ArrayEx.au3" c'est le même à cette adresse https://www.autoitscript.fr/forum/viewt ... 29#p105439 ?
Cordialement,
Walkson
"Horas non numero nisi serenas " Le canon de midi
(Je ne compte que les heures heureuses)
Avatar du membre
TommyDDR
Modérateur
Modérateur
Messages : 2086
Enregistré le : mar. 22 juil. 2008 21:55
Localisation : Nantes
Status : Hors ligne

Re: [Ex] Coding Challenge : Star Field

#3

Message par TommyDDR »

Oops, j'ai mis la mauvaise version, j'ai mis à jour le 1er post pour ne plus avoir besoin de l'include (mais oui c'est bien celui là ;) )
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Répondre