[EX] Snake Game

Partagez vos scripts, et vos applications AutoIt.
Règles du forum
.
Répondre
Avatar du membre
Numeric
Niveau 5
Niveau 5
Messages : 132
Enregistré le : mer. 23 mars 2016 08:17
Status : Hors ligne

[EX] Snake Game

#1

Message par Numeric »

Plongez dans une expérience de jeu immersive avec Snake Game, où la puissance du mode orienté objet d'AutoItObject.au3 transforme ce classique intemporel en une expérience de jeu modernisée et captivante. Découvrez comment cette approche novatrice donne vie à un jeu emblématique d'une manière totalement nouvelle.

Fonctionnalités :

1. Architecture Orientée Objet : Snake Game est construit sur l'architecture orientée objet robuste d'AutoItObject.au3, offrant une structure de code claire et modulaire pour une gestion efficace des entités du jeu telles que le serpent, la nourriture et l'interface graphique.

2. Contrôles Intuitifs : Profitez de contrôles intuitifs et réactifs grâce à l'intégration transparente des fonctions de gestion des événements d'AutoIt, offrant une expérience de jeu fluide et immersive.

3. Personnalisation Avancée : Explorez une variété de fonctionnalités personnalisables, notamment des niveaux de difficulté ajustables, des paramètres de vitesse modifiables et des options de personnalisation de l'interface utilisateur, vous permettant de créer une expérience de jeu adaptée à vos préférences.

4. Gestion des Objets : Avec AutoItObject.au3, la gestion des objets est simplifiée, permettant une manipulation efficace des entités du jeu telles que les segments du serpent, la nourriture et les obstacles, facilitant ainsi le développement et la maintenance du jeu.

5. Optimisation des Performances :Profitez de performances optimales grâce à l'efficacité de l'approche orientée objet, qui permet une utilisation efficace des ressources système et une exécution fluide du jeu même sur des plateformes moins puissantes.

Pourquoi s'intéresser à Snake Game?

- Exploration de la Programmation Orientée Objet en AutoIt: Snake Game offre une opportunité unique d'explorer les principes de la programmation orientée objet à travers un jeu interactif et divertissant.

- Défi Personnel et Créativité : Testez vos compétences de développement et exprimez votre créativité en personnalisant et en améliorant le jeu selon vos propres spécifications, offrant ainsi une expérience de jeu unique et stimulante.
partagez vos expériences et astuces pour créer des jeux innovants et captivants avec AutoItObject.au3.


Snake Game illustre parfaitement la puissance et la flexibilité du mode orienté objet d'AutoItObject.au3 en offrant une expérience de jeu captivante et moderne. Plongez dans cet univers fascinant où la programmation devient un art, et où chaque ligne de code contribue à créer une expérience de jeu inoubliable.

#include-once
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
; #INDEX# =======================================================================================================================
; Title .........: Snake Game
; AutoIt Version : 3.3
; AutoItObject Version : v1.2.8.2
; Language ......: English
; Description ...: A simple Snake game implementation using AutoIt->AutoItObject.au3.
; Dependencies ..: AutoItObject.au3
; Author ........: Numeric
; ===============================================================================================================================
#include <GUIConstantsEx.au3>
#include "AutoItObject.au3"
#include <Print.au3>
#include <Misc.au3>

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

_AutoItObject_Startup()

Func Snake($oPoint = 0, $Board = Default)
   Local $aRandomDirectionTab = [-1, 0, 1]
   Local $dx = $aRandomDirectionTab[Int(Random(0, 2, 1))]
   Local $dy = $aRandomDirectionTab[Int(Random(0, 2, 1))]
   Local $iLowSpeed = 100
   Local $foodCounter = 0

   If $Board = Default Then $Board = GUICreate("Snake", 400, 400)
   GUISetBkColor(0x000000)

   GUISetState()
   Local $aBoardPos = WinGetClientSize($Board)   ;WinGetPos($Board)
   If @error Then Return SetError(1, 0, False) ; window not found
   Local $iBoardWidth = $aBoardPos[0]
   Local $iBoardHeight = $aBoardPos[1]

   Local $X = Random(10, $iBoardWidth - 10, 1)
   Local $Y = Random(10, $iBoardHeight - 10, 1)

   If $oPoint = 0 Then $oPoint = Point($X, $Y)
   Local $aMap[]
   MapAppend($aMap, $oPoint)
   Local $oFood = Point(Random(10, $iBoardWidth - 10), Random(10, $iBoardHeight - 10), 39219)

   Local $sClass = _AutoItObject_Class()
   With $sClass
      .Create()
      .AddProperty("head", $ELSCOPE_PRIVATE, $oPoint)
      .AddProperty("aMap", $ELSCOPE_PRIVATE, $aMap)
      .AddProperty("food", $ELSCOPE_PRIVATE, $oFood)
      .AddProperty("foodCounter", $ELSCOPE_PRIVATE, $foodCounter)
      .AddProperty("dx", $ELSCOPE_PRIVATE, $dx)
      .AddProperty("dy", $ELSCOPE_PRIVATE, $dy)
      .AddProperty("Board", $ELSCOPE_PRIVATE, $Board)
      .AddProperty("speedLevel", $ELSCOPE_PRIVATE, $iLowSpeed)
      .AddProperty("iBoardWidth", $ELSCOPE_PRIVATE, $iBoardWidth)
      .AddProperty("iBoardHeight", $ELSCOPE_PRIVATE, $iBoardHeight)

      .AddMethod("getBoard", "_getBoard")
      .AddMethod("setSpeedLevel", "_setSpeedLevel")
      .AddMethod("getSpeedLevel", "_getSpeedLevel")
      .AddMethod("runGameLoop", "_runGameLoop")


      .AddMethod("setdx", "_setdx")
      .AddMethod("setdy", "_setdy")
      .AddMethod("getdx", "_getdx")
      .AddMethod("getdy", "_getdy")

      ;  .AddMethod("start", "_start")
      .AddMethod("sleepW", "_sleepW")
      .AddMethod("getMap", "_getMap")
      .AddMethod("setMap", "_setMap")
      .AddMethod("getHead", "_getHead")
      .AddMethod("getBoardHeight", "_getBoardHeight")
      .AddMethod("getBoardWidth", "_getBoardWidth")
      .AddMethod("getFoodCounter", "_getFoodCounter")
      .AddMethod("setFoodCounter", "_setFoodCounter", True)
      .AddMethod("move", "_move", True)
      .AddMethod("generateFood", "_generateFood")
      .AddMethod("setFood", "_setFood")
      .AddMethod("getFood", "_getFood")
      .AddDestructor("_destructor")
   EndWith
   Return $sClass.Object
EndFunc   ;==>Snake

Func _destructor($this)
   ConsoleWrite("Destructor calling" & @CRLF)
   Local $aMap = $this.getMap()
   For $oPoint In $aMap
      $oPoint = 0
   Next
   $aMap = 0
   $this.setMap(0)
EndFunc   ;==>_destructor


Func _getFoodCounter($this)
   Return $this.foodCounter
EndFunc   ;==>_getFoodCounter

Func _setFoodCounter($this, $foodCounter)
   $this.foodCounter = $foodCounter
EndFunc   ;==>_setFoodCounter

Func _setSpeedLevel($this, $iLevel)
   $this.speedLevel = $iLevel
EndFunc   ;==>_setSpeedLevel

Func _getSpeedLevel($this)
   Return $this.speedLevel
EndFunc   ;==>_getSpeedLevel

Func _getBoard($this)
   Return $this.Board
EndFunc   ;==>_getBoard

Func _runGameLoop($this)
   While 1
      If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
      If _IsPressed(25) Then
         $this.setdx(-1)
         $this.setdy(0)
      EndIf
      If _IsPressed(27) Then
         $this.setdx(1)
         $this.setdy(0)
      EndIf
      If _IsPressed(26) Then
         $this.setdx(0)
         $this.setdy(-1)
      EndIf
      If _IsPressed(28) Then
         $this.setdx(0)
         $this.setdy(1)
      EndIf

      If _IsPressed(53) Then
         $this.sleepW()
      EndIf
      $this.move()
      Sleep($this.getSpeedLevel())
   WEnd

EndFunc   ;==>_runGameLoop

Func _getBoardWidth($this)
   Return $this.iBoardWidth
EndFunc   ;==>_getBoardWidth

Func _getBoardHeight($this)
   Return $this.iBoardHeight
EndFunc   ;==>_getBoardHeight

Func _generateFood($this)
   Local $oFood = Point(Random(10, $this.getBoardWidth() - 10), Random(10, $this.getBoardHeight() - 10), 39219)
   $this.setFood($oFood)
EndFunc   ;==>_generateFood

Func _setFood($this, $oFood)
   $this.food = $oFood
EndFunc   ;==>_setFood

Func _getFood($this)
   Return $this.food
EndFunc   ;==>_getFood

Func _getHead($this)
   Return $this.head
EndFunc   ;==>_getHead

Func _setdx($this, $dx)
   $this.dx = $dx
EndFunc   ;==>_setdx

Func _setdy($this, $dy)
   $this.dy = $dy
EndFunc   ;==>_setdy

Func _getdx($this)
   Return $this.dx
EndFunc   ;==>_getdx

Func _getdy($this)
   Return $this.dy
EndFunc   ;==>_getdy

Func _getMap($this)
   Return $this.aMap
EndFunc   ;==>_getMap

Func _setMap($this, $aMap)
   $this.aMap = $aMap
EndFunc   ;==>_setMap

Func _move($this)
   Local $aMap = $this.getMap()
   Local $head = $aMap[0]
   Local $newX = $head.getXPos() + $this.getdx() * 10
   Local $newY = $head.getYPos() + $this.getdy() * 10

   ; Vérifier si la tête du serpent sort de l'écran
   If $newX <= 0 Or $newX >= $this.getBoardWidth() - 10 Or $newY <= 0 Or $newY >= $this.getBoardHeight() - 10 Then
      Return SetError(1, 0, False)
   EndIf

   Local $oFood = $this.getFood()
   If $head.checkCollision($oFood) Then
      $this.setFoodCounter($this.getFoodCounter() + 1)
      Local $lastSegment = $aMap[UBound($aMap) - 1]
      Local $lastX = $lastSegment.getXPos() + $this.getdx() * 10
      Local $lastY = $lastSegment.getYPos() + $this.getdy() * 10
      MapAppend($aMap, Point($lastX, $lastY))

      $this.setMap($aMap)
      If $this.getFoodCounter() = 4 Then
         Local $newSpeedLevel = $this.getSpeedLevel() - 20
         If $newSpeedLevel < 0 Then $newSpeedLevel = 1
         $this.setSpeedLevel($newSpeedLevel)
         $this.setFoodCounter(0)
      EndIf

      GUICtrlDelete($oFood.getID())
      $this.generateFood()
   EndIf

   ; Mettre à jour la position de la tête du serpent
   $head.setXPos($newX)
   $head.setYPos($newY)

   ; Déplacer les segments du serpent
   For $i = UBound($aMap) - 1 To 1 Step -1
      $aMap[$i].setXPos($aMap[$i - 1].getXPos())
      $aMap[$i].setYPos($aMap[$i - 1].getYPos())
   Next
EndFunc   ;==>_move

;attention ça crash windows
Func _sleepW(ByRef $this)
   Do
      Do
         Sleep(100)
      Until _IsPressed(25) Or _IsPressed(26) Or _IsPressed(27) Or _IsPressed(28)
      If _IsPressed(25) Then
         $this.setdx(-1)
         $this.setdy(0)
      EndIf
      If _IsPressed(27) Then
         $this.setdx(1)
         $this.setdy(0)
      EndIf
      If _IsPressed(26) Then
         $this.setdx(0)
         $this.setdy(-1)
      EndIf
      If _IsPressed(28) Then
         $this.setdx(0)
         $this.setdy(1)
      EndIf
   Until _IsPressed(25) Or _IsPressed(26) Or _IsPressed(27) Or _IsPressed(28)
EndFunc   ;==>_sleepW

Func Point($X = 0, $Y = 0, $iClolor = 16777215)
   Local $idLabel = GUICtrlCreateLabel("", $X, $Y, 10, 10, 0x0001) ; $BS_CENTER
   GUICtrlSetBkColor($idLabel, $iClolor)

   Local $cPoint = _AutoItObject_Class()
   With $cPoint
      .Create()
      .AddProperty("___sType___", $ELSCOPE_PRIVATE, "Point")
      .AddProperty("X", $ELSCOPE_PRIVATE, $X)
      .AddProperty("Y", $ELSCOPE_PRIVATE, $Y)
      .AddProperty("iColor", $ELSCOPE_PRIVATE, $iClolor)
      .AddProperty("idLabel", $ELSCOPE_PRIVATE, $idLabel)
      .AddMethod("getObjType", "_getObjType")
      .AddMethod("setXPos", "_setXPos")
      .AddMethod("setYPos", "_setYPos")
      .AddMethod("getID", "_getID")
      .AddMethod("setID", "_setID")
      .AddMethod("getXPos", "_getXPos")
      .AddMethod("getYPos", "_getYPos")
      .AddMethod("checkCollision", "_checkCollision")
   EndWith
   Return $cPoint.Object
EndFunc   ;==>Point

Func _getObjType($this)
   Return $this.___sType___
EndFunc   ;==>_getObjType

Func _checkCollision($this, $oPoint)
   If Not IsObj($oPoint) Then Return SetError(1, 0, False)
   Local $sType = $oPoint.getObjType()
   If $sType <> "Point" Then Return SetError(2, 0, False)

   Local $aPos1 = ControlGetPos("", "", $this.getID())
   Local $aPos2 = ControlGetPos("", "", $oPoint.getID())
   If $aPos1[0] + $aPos1[2] >= $aPos2[0] And $aPos1[0] <= $aPos2[0] + $aPos2[2] And $aPos1[1] + $aPos1[3] >= $aPos2[1] And $aPos1[1] <= $aPos2[1] + $aPos2[3] Then
      Return True
   EndIf
   Return False
EndFunc   ;==>_checkCollision

Func _setXPos($this, $iX)
   $this.X = $iX
   GUICtrlSetPos($this.idLabel, $iX, $this.getYPos())
EndFunc   ;==>_setXPos

Func _setYPos($this, $iY)
   $this.Y = $iY
   GUICtrlSetPos($this.idLabel, $this.getXPos(), $iY)
EndFunc   ;==>_setYPos

Func _getXPos($this)
   Return $this.X
EndFunc   ;==>_getXPos

Func _getYPos($this)
   Return $this.Y
EndFunc   ;==>_getYPos

Func _getID($this)
   Return $this.idLabel
EndFunc   ;==>_getID

Func _setID($this, $idLabel)
   $this.idLabel = $idLabel
EndFunc   ;==>_setID


;***************************************************
Global $sNake = Snake()
$sNake.runGameLoop()
$sNake = 0
;**************************************************

_AutoItObject_Shutdown()
De 0 et 1 vers les étoiles , tout part du Binaire, Numeric
Répondre