The thing is that don't know how to make it work ->
For example lets say we have, but i don't want to be all 25% each
Buffs:
-Invulnerable -> 15%
-Speed Down -> 30%
-U can eat Yourself ->15%
-Change Color -> 40%
Debuffs:
-Crazy snake (switch between controls) -> 15%
-Speed Up -> 20%
-Invisible Tail -> 20%
-Doubles your size -> 15%
How can i make it work that way with autoit (?
I don't want someone to share a code and thats it, a good example or explanation will be good.
Finally i want to know if the drawing functions are okay that way or i'm overdrawing all the time wasting cpu usage...
Here is the code if anyone wants to have fun for a while :d
Code:
#include <GDIPlus.au3>
#include <Misc.au3>
#include <GUIConstants.au3>
Global $snakeBody[5][2] ; we will save here every node of the snake
Global $x, $y ;snake position of course
Global $tailLength; snake size -> head + nº nodes
Global $moving ; we are moving or not and what direction
Global $appleX, $appleY, $buffX, $buffY ;positions
Global $speed ;snake speed
Global $Dead ;dead or not
Global $Time, $TimeBuff
Global $HandleTimer, $buffTimer
Global $buffON ;we have a buff or not
Global $BuffDurationTimer
Global $BuffDuration
Global $SNAKESTATE
Global $VK_LEFT = 25 ;global cause we want to change them if we have crazy debuff
Global $VK_UP = 26
Global $VK_RIGHT = 27
Global $VK_DOWN = 28
#Region Constants
Global Const $LEFT = 1
Global Const $RIGHT = 2
Global Const $UP = 3
Global Const $DOWN = 4
Global Const $pauseStr = "Press any key to begin"
Global Const $BUFF_SPEED_DOWN = 1 ;35% ->How to make this work with random probability...
Global Const $BUFF_EAT = 2 ;10%
Global Const $BUFF_MORELUCK = 3 ;10%
Global Const $BUFF_INVENSIBLE = 4 ;10%
Global Const $BUFF_NEW_COLOR = 5 ;35%
Global Const $DEBUFF_SPEED_UP = 6 ;20%
Global Const $DEBUFF_INVISIBLE = 7 ;20%
Global Const $DEBUFF_CRAZY = 8 ;15%
Global Const $DEBUFF_BADLUCK = 9 ;10%
Global Const $DEBUFF_FATBOY = 10 ;35%
#EndRegion Constants
#Region Gdi Crap
$size = 300
$form = GUICreate("", $size, $size, -1, -1, $WS_POPUP)
GUISetState()
_GDIPlus_Startup()
$graphic = _GDIPlus_GraphicsCreateFromHWND($form)
$pen = _GDIPlus_PenCreate(0xFF000000, 2)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($size, $size, $graphic)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap) ;buffer for drawing
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
$image = _GDIPlus_ImageLoadFromFile("grid.png")
$apple = _GDIPlus_ImageLoadFromFile("apple.png");
$BodyColor = _GDIPlus_BrushCreateSolid(0xFF00FF00)
$HeadColor = _GDIPlus_BrushCreateSolid(0xFFFFAEC9)
$hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 18, 2)
#EndRegion Gdi Crap
;CreateGridImage() -> if u need the grid remove the semicolon
Func _SetupVars()
$x = 15
$y = 15
$tailLength = 3
$moving = 0
$speed = 50
$Dead = False
$Time = 3000;10000 + Random(0,3000)
$HandleTimer = TimerInit()
$buffON = False
$invisibleDuration = 5000
$TimeBuff = 5000
$SNAKESTATE = -1
$BuffDuration = 8000
DrawApple()
EndFunc ;==>_SetupVars
Func CreateGridImage()
_GDIPlus_GraphicsClear($hBuffer, 0xFF0000FF)
Local $x = 0
Local $y = 0
For $i = 1 To 400
_GDIPlus_GraphicsDrawRect($hBuffer, $x, $y, 15, 15, $pen)
If Mod($i, 20) = 0 Then
$x = 0
$y += 15
ContinueLoop
EndIf
$x += 15
Next
_GDIPlus_GraphicsDrawImageRect($graphic, $hBitmap, 0, 0, $size, $size)
_GDIPlus_ImageSaveToFile($hBitmap, "grid.png")
Exit
EndFunc ;==>CreateGridImage
#Region DrawFunctions
Func DrawGrid()
_GDIPlus_GraphicsClear($hBuffer, 0xFF0000FF)
_GDIPlus_GraphicsDrawImageRect($hBuffer, $image, 0, 0, $size, $size)
EndFunc ;==>DrawGrid
Func FillGrid($x, $y, $head = False)
If $head Then
_GDIPlus_GraphicsFillRect($hBuffer, $x + 2, $y + 2, 13, 13, $HeadColor)
Else
_GDIPlus_GraphicsFillRect($hBuffer, $x + 2, $y + 2, 13, 13, $BodyColor)
EndIf
EndFunc ;==>FillGrid
Func DrawSnake($snakeX, $snakeY)
For $i = $tailLength To 0 Step -1
$snakeBody[0][0] = $snakeX
$snakeBody[0][1] = $snakeY
$snakeBody[$i + 1][0] = $snakeBody[$i][0] ;pos from tail -> head
$snakeBody[$i + 1][1] = $snakeBody[$i][1]
If $i = 0 Then
FillGrid($snakeX, $snakeY, True) ;draw the head
ContinueLoop
EndIf
FillGrid($snakeBody[$i][0], $snakeBody[$i][1]) ;draw the body
If $snakeX = $appleX And $snakeY = $appleY Then DrawApple();eat hit test
If $snakeX = $snakeBody[$i][0] And $snakeY = $snakeBody[$i][1] And $i <> 0 And $moving <> 0 And $SNAKESTATE <> $BUFF_INVENSIBLE Then $Dead = True;you cannot crash with your head or can u?
If $snakeX = $buffX And $snakeY = $buffY And $buffON = True Then BuffCreate() ;if we catch the buff we get a buff or debuff
Next
_GDIPlus_GraphicsDrawImageRect($hBuffer, $apple, $appleX, $appleY, 15, 15) ;draw apple
_GDIPlus_GraphicsDrawImageRect($graphic, $hBitmap, 0, 0, 300, 300); draw the entire graphic -> ALWAYS LAST AT THE MAIN LOOP TOO
EndFunc ;==>DrawSnake
Func DrawApple()
Do
$appleX = Random(0, 285, 1)
$appleY = Random(0, 285, 1)
Until Mod($appleX, 15) = 0 And Mod($appleY, 15) = 0 ; we want our apple pos to be multiple of 15 , if not the snake hit test will fail
$tailLength += 1
ReDim $snakeBody[UBound($snakeBody) + 1][2] ;add 1 element to our snakebody array
EndFunc ;==>DrawApple
Func DrawBuff()
Do
$buffX = Random(0, 285, 1)
$buffY = Random(0, 285, 1)
Until Mod($buffX, 15) = 0 And Mod($buffY, 15) = 0 ;same here as above
_GDIPlus_GraphicsFillEllipse($hBuffer, $buffX, $buffY, 15, 15, $hBrush) ;we draw the buff :c
EndFunc ;==>DrawBuff
Func DrawString($string, $x, $y)
$tLayout = _GDIPlus_RectFCreate($x, $y, 0, 0)
$aInfo = _GDIPlus_GraphicsMeasureString($hBuffer, $string, $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDrawStringEx($hBuffer, $string, $hFont, $aInfo[0], $hFormat, $hBrush)
EndFunc ;==>DrawString
#EndRegion DrawFunctions
;if u are reading this u maybe look at my code :d , god bless u xDD leave a comment below :p
Func NewBuff() ;function to add to the field a buff or not --- 33% chance, or thats what i believe
$buffON = False
Local $probability = 0.33 ;33%
Local $newbuff = Random(0, 1)
If $newbuff <= $probability Then ;create or not a buff
$buffON = True
$buffTimer = TimerInit()
DrawBuff()
EndIf
EndFunc ;==>NewBuff
Func BuffCreate() ;create a buff or a debuff when the user picks it
$buffON = False
Local $luck = 0.05 ;5%
Local $debuff = 0.5; 50%;
Local $isbuff = False
Local $random = Random(0, 1)
If ($debuff + $luck) > ($random) Then $isbuff = True ;is a buff or is a debuff
If $isbuff Then
;create buff
;number between -> 1 to 5 -> i don't want this to be random at all i want to make it work with percent of having that buff, but don't know how to do it XD
Else
;create debuff
;number between -> 6 to 10
EndIf
;ApplyBuff(somebuffIndex)
EndFunc ;==>BuffCreate
Func ApplyBuff($buf)
$SNAKESTATE = $buf
$BuffDurationTimer = TimerInit()
EndFunc ;==>ApplyBuff
Main()
Func Main()
_SetupVars()
While Not $Dead
If _IsPressed($VK_LEFT) And $moving <> $RIGHT Then $moving = $LEFT
If _IsPressed($VK_UP) And $moving <> $DOWN Then $moving = $UP
If _IsPressed($VK_RIGHT) And $moving <> $LEFT Then $moving = $RIGHT
If _IsPressed($VK_DOWN) And $moving <> $UP Then $moving = $DOWN
If $moving = $LEFT Then $x -= 15 ;snake min distance to cover 15
If $moving = $UP Then $y -= 15
If $moving = $RIGHT Then $x += 15
If $moving = $DOWN Then $y += 15
If $x > ($size - 15) Then $x = 0
If $x < 0 Then $x = ($size - 15)
If $y > ($size - 15) Then $y = 0
If $y < 0 Then $y = ($size - 15)
Sleep($speed) ;changing the sleep time will change the speed :c
DrawGrid() ;first we draw de grid then the snake , then the snake and apple
If $moving = 0 Then DrawString($pauseStr, ($size / 2) - (StringLen($pauseStr) * 5.5), 150) ;game paused string
BuffTimerThread() ;when the time is reached it calls the function to create a buff ,a 33% determinates wheter to appear a buff or not
BuffDrawingTime();The time the buff will be there for the snake to catch it
BuffDuration() ; wheter a buff or debuff duration
DrawSnake($x, $y)
Sleep(50) ; removing it will make it go much faster
WEnd
_DeleteAll() ;you kill your snake, how could u :(
EndFunc ;==>Main
#Region TimerThreads
Func BuffTimerThread() ;time between trying to create a buff
If TimerDiff($HandleTimer) > $Time Then
$Time = 10000 + Random(0, 3000)
NewBuff()
$HandleTimer = TimerInit()
EndIf
EndFunc ;==>BuffTimerThread
Func BuffDrawingTime() ;time that buff will stay in the grid
If ($buffON) Then ;buff created?
If TimerDiff($buffTimer) > $TimeBuff Then
$buffTimer = TimerInit()
$buffON = False
EndIf
_GDIPlus_GraphicsFillEllipse($hBuffer, $buffX, $buffY, 15, 15, $hBrush) ;Draw buff
EndIf
EndFunc ;==>BuffDrawingTime
Func BuffDuration()
If $SNAKESTATE <> -1 Then
Switch $SNAKESTATE ;set buff
Case $DEBUFF_INVISIBLE
_GDIPlus_BrushSetSolidColor($BodyColor, 0x000000)
Case $DEBUFF_CRAZY
$VK_LEFT = 27
$VK_DOWN = 26
$VK_RIGHT = 25
$VK_UP = 28
EndSwitch
If TimerDiff($BuffDurationTimer) > $BuffDuration Then ;reset buff
Switch $SNAKESTATE
Case $DEBUFF_INVISIBLE
_GDIPlus_BrushSetSolidColor($BodyColor, 0xFF00FF00)
Case $DEBUFF_CRAZY
Global $VK_LEFT = 25
Global $VK_UP = 26
Global $VK_RIGHT = 27
Global $VK_DOWN = 28
EndSwitch
$SNAKESTATE = -1
EndIf
EndIf
EndFunc ;==>BuffDuration
#EndRegion TimerThreads
Func _DeleteAll()
_GDIPlus_GraphicsDispose($graphic)
_GDIPlus_GraphicsDispose($hBuffer)
_GDIPlus_ImageDispose($image)
_GDIPlus_ImageDispose($apple)
_GDIPlus_ImageDispose($hBitmap)
_GDIPlus_BrushDispose($HeadColor)
_GDIPlus_BrushDispose($BodyColor)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_Shutdown()
EndFunc ;==>_DeleteAll
Need files:
Optional ->
I know the code looks bad, very sorry about that :c







