AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

There's a bug in this script
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Thu Mar 13, 2008 4:00 am    Post subject: There's a bug in this script Reply with quote

It also has some minor flaws, but I think it's fun anyway. You might think I put this in the wrong section, but I didn't. Wink
Code:
#SingleInstance Force
#NoEnv

CoordMode Mouse, Screen
SetBatchLines -1
SetWinDelay -1

IfNotExist bug
{
   FileCreateDir bug
   Loop 17
      UrlDownloadToFile http://www.autohotkey.net/~ManaUser/bugs/bug%A_Index%.png, bug\bug%A_Index%.png
}
SetWorkingDir bug

Gui Color, White
Loop 16
{
   Gui Add, Picture, x0 y0 gGrab, bug%A_Index%.png
   GuiControl Hide, bug%A_Index%.png
}
Gui Add, Picture, x0 y0 gWipe, bug17.png
GuiControl Hide, bug17.png
Gui -Caption +ToolWindow +AlwaysOnTop +LastFound
WinSet, TransColor, White
TheBug := WinExist()

Gui Show, w16 h16 y0, NA
WinGetPos X, Y

XSpeeds := "0:1:2:3:3:3:2:1:0:-1:-2:-3:-3:-3:-2:-1"
YSpeeds := "-3:-3:-2:-1:0:1:2:3:3:3:2:1:0:-1:-2:-3"
StringSplit XSpeed, XSpeeds, :
StringSplit YSpeed, YSpeeds, :

Speed := 1
Random Dir, 1, 9
SetTimer Move, 50
GoSub Move

Return


Move:
MouseGetPos,,, UnderMouse
If (TheBug = UnderMouse)
{
   Speed := 3
   SetTimer SlowDown, -525
   SetTimer SlowMore, -1525
}

X += XSpeed%Dir% * Speed
Y += YSpeed%Dir% * Speed

If (X < -16)
   X := -16
Else If (X > A_ScreenWidth)
   X := A_ScreenWidth
If (Y < -16)
   Y := -16
Else If (Y > A_ScreenHeight)
   Y := A_ScreenHeight

GuiControl Hide, bug%Dir%.png
Random Rand, 0, 1
Rand := Rand & !GetKeyState("Left") | GetKeyState("Right")
Dir += Rand ? 1 : -1
If (Dir = 0)
   Dir := 16
Else If (Dir = 17)
   Dir := 1
GuiControl Show, bug%Dir%.png

Gui +LastFound
WinMove, X, Y

Return

SlowDown:
Speed := 2
Return

SlowMore:
Speed := 1
Return

Grab:
SetTimer Move, Off
SetTimer SlowDown, Off
SetTimer SlowMore, Off
SetTimer Hold, 50
Wiggle := 1
If (Dir = 16)
   Dir := 1
Return

Hold:
MouseGetPos X, Y
X -= 8, Y -= 8

Random Rand, 0, 1
If Rand
{
   GuiControl Hide, bug%Dir%.png
   Dir += Wiggle
   GuiControl Show, bug%Dir%.png
   Wiggle *= -1
}

Gui +LastFound
WinMove, X, Y

If GetKeyState("LButton") = 0
{
   SetTimer Hold, Off
   SetTimer Move, 50
}
Return

GuiContextMenu: ;Squish
SetTimer Move, Off
SetTimer Hold, Off
Gui Show, Hide
GuiControl Hide, bug%Dir%.png
GuiControl Show, bug17.png
Gui Show, NA
Sleep 60000
Wipe:
ExitApp
Return


Last edited by ManaUser on Fri Mar 14, 2008 10:31 pm; edited 2 times in total
Back to top
View user's profile Send private message
Rhys



Joined: 17 Apr 2007
Posts: 710
Location: Florida

PostPosted: Thu Mar 13, 2008 5:39 am    Post subject: Reply with quote

This is awesome! Too much fun. I got sad when I clicked it.

I wonder if it'd work with alpha blended png or not. Very clever script! It reminds me of something called Neko I used to run a lot back in the day. The fact that it runs faster when you mouse over it is hilarious.
_________________
[Join IRC!]
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Thu Mar 13, 2008 9:11 pm    Post subject: Reply with quote

Rhys wrote:
This is awesome! Too much fun. I got sad when I clicked it.

Thanks, glad you liked it. And I know what you mean. I'm rather fond of the little guy too. So I've updated it with a kinder, gentler option. You can now pick it up and drag it around. But for sadists you can still exercise your godlike power if a deadly right-click.
Rhys wrote:
I wonder if it'd work with alpha blended png or not. Very clever script! It reminds me of something called Neko I used to run a lot back in the day.

I imagine it could be done, but it would be quite a bit more complicated. I haven't been following the "per-pixel alpha blended GUI demo" closely, but it looks a little daunting.
Rhys wrote:
The fact that it runs faster when you mouse over it is hilarious.

Thanks again, I was particularly pleased with that part too.
Back to top
View user's profile Send private message
Imapw
Guest





PostPosted: Thu Mar 13, 2008 10:06 pm    Post subject: Reply with quote

This thing is cooler then cool.
This thin is åverCool.

if you are going ti make a 2.0 versjon you must start with two bug's,
so when they are close they muliply.

you start the program in the morning and when you are done at work you got
10.000 bugs on your screen
Back to top
Rhys



Joined: 17 Apr 2007
Posts: 710
Location: Florida

PostPosted: Fri Mar 14, 2008 12:58 am    Post subject: Reply with quote

I think it would be cool to create very basic artificial life that lives on your desktop like that - You could program some really basic behaviors like one bug that runs away from another bug that wants to 'eat' it, etc.

I made 10 copies of the script and let them all run at once =)
_________________
[Join IRC!]
Back to top
View user's profile Send private message
Azerty (as guest)
Guest





PostPosted: Fri Mar 14, 2008 2:22 am    Post subject: Reply with quote

That's a funny one

Thanks ManaUser Very Happy
Back to top
tic



Joined: 22 Apr 2007
Posts: 1344

PostPosted: Fri Mar 14, 2008 3:13 am    Post subject: Reply with quote

I was surprised that I relly enjoyed this ManaUser. It was so simple yet so fun. If you permit, I can make an alphablended version that doesnt require any images for you....
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Fri Mar 14, 2008 4:36 am    Post subject: Reply with quote

Rhys wrote:
I think it would be cool to create very basic artificial life that lives on your desktop like that - You could program some really basic behaviors like one bug that runs away from another bug that wants to 'eat' it, etc.

I made 10 copies of the script and let them all run at once =)

Yes, I thought about doing something like that. It's fun just wandering around, but I wish it actually did something once in a while. I'm just not sure what.

I don't know if having several on the screen is a good idea though. I tried that and they start to lose their transparency and stuff after a while. But I haven't been able to figure out exactly why, or what makes it happen.

tic wrote:
I was surprised that I relly enjoyed this ManaUser. It was so simple yet so fun. If you permit, I can make an alphablended version that doesnt require any images for you....

Certainly. I'm sure it would look better that way.

[Note from moderator: I deleted ManaUser's previous "duplicate" posts. I guess the bug ate his edit button...]
Back to top
View user's profile Send private message
_adam
Guest





PostPosted: Fri Mar 14, 2008 5:53 am    Post subject: Reply with quote

if you think it's time to buy a new lcd, run it when the wife is using the pc. Very Happy

I like it Cool
Back to top
imapow



Joined: 13 Mar 2008
Posts: 162
Location: Trøndelag, Norway

PostPosted: Fri Mar 14, 2008 11:52 am    Post subject: Reply with quote

ManaUser wrote:
It's fun just wandering around, but I wish it actually did something once in a while. I'm just not sure what.


  • Sleep.
  • Poop.
  • Multiply.
  • Die.
  • Eat out of desktop items.
  • Jump (bugs dont jump.)
  • Spontaniusly explode.
  • Type in edit felds.
  • Talk.



You must post this topic in the "Bug Reports" forum.
HeHe...
_________________
-._.-¨¯¨-._.-IM@PΩW-._.-¨¯¨-._.-


Last edited by imapow on Fri Mar 14, 2008 12:41 pm; edited 1 time in total
Back to top
View user's profile Send private message
_adam
Guest





PostPosted: Fri Mar 14, 2008 12:35 pm    Post subject: Reply with quote

this actually reminded me of something I had back then, which I was lucky enough to be able to find again.

http://www.slinkycity.com/windows-desktop-sheep.html

I have both the bug and sheep running around Very Happy
Back to top
BoBo¨
Guest





PostPosted: Fri Mar 14, 2008 12:48 pm    Post subject: Reply with quote

AWESOME ! Laughing
Back to top
tic



Joined: 22 Apr 2007
Posts: 1344

PostPosted: Fri Mar 14, 2008 6:17 pm    Post subject: Reply with quote

Here I made this for you. It is nothing too flashy. It could be easily done using pngs, but I just went on the basis of your original script; just drawing the bug with pixels. You can see there is a simple function DrawBug to draw the bug. The variables bug1x and bug1y are the coordinates I am passing to the function to construct the bug. You can see this uses no temporary files and just draws the pixels directly to the screen Smile

With this method it should be easy to draw many bugs
Thanks to Lexikos for correcting me on GdipBitmapSetPixel Smile

Code:
#SingleInstance, Force
#NoEnv
CoordMode, Mouse, Screen
SetBatchLines, -1
SetWinDelay, 0
SetWorkingDir %A_ScriptDir%

bug1x = 5|10|6|9|6|9|7|8|3|4|7|8|5|6|7|8|9|10|11|6|7|8|9|12|4|5|6|7|8|9|12|3|6|7|8|9|10|11|3|6|7|8|9|4|5|6|7|8|9|10|11|12|7|8|12
bug1y = 2|2|3|3|4|4|5|5|6|6|6|6|7|7|7|7|7|7|7|8|8|8|8|8|9|9|9|9|9|9|9|10|10|10|10|10|10|10|11|11|11|11|11|12|12|12|12|12|12|12|12|12|13|13|13

pToken := Gdip_Startup()

Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, 1: Show, w1 h1 Hide
hwnd1 := WinExist()

DrawBug(16, 16, bug1x, bug1y, (A_ScreenWidth-16)//2, (A_ScreenHeight-16)//2, hwnd1)
Return

;###############################################################################################

DrawBug(Width, Height, xCoords, yCoords, xOnScreen, yOnScreen, hwnd, Gui="1", Colour="000000", Transparency="255", Delimiter="|")
{
   StringSplit, xCoords, xCoords, %Delimiter%
   StringSplit, yCoords, yCoords, %Delimiter%
   
   If (xCoords0 != yCoords0)
   Return, "There are not an equal number of x coordinates as there are y coordinates"
   
   BackHidden := A_DetectHiddenWindows
   DetectHiddenWindows, Off

   hbm := CreateDIBSection(Width, Height, 32)
   hdc := DllCall("CreateCompatibleDC", "UInt", 0)
   hbmOld := DllCall("SelectObject", "UInt", hdc, "UInt", hbm)
   G := Gdip_GraphicsFromHDC(hdc)
   DllCall("gdiplus\GdipSetSmoothingMode", "UInt", G, "Int", 2)   
   pBitmap := Gdip_CreateBitmap(Width, Height, 0x26200A)
   
   SetFormat, Integer, Hex
   Transparency += 0
   SetFormat, Integer, D

   x := y := 0
   Match := 1
   Loop
   {      
      If (x = xCoords%Match%) && (y = yCoords%Match%)
      {
         DllCall("gdiplus\GdipBitmapSetPixel", "UInt", pBitmap, "Int", x, "Int", y, "UInt", Transparency Colour)
         Match++
      }
      x++
      If (x = Width)
      x := 0, y := y+1
      If (y = Height)
      Break
   }
   
   Gdip_DrawImage(G, pBitmap, 0, 0, Width, Height, 0, 0, Width, Height, 1.0)
   UpdateLayeredWindow(hwnd, hdc, xOnScreen, yOnScreen, Width, Height)
   Gui, %Gui%: Show, NA
   Gdip_DisposeImage(pBitmap)
   DllCall("DeleteObject", "UInt", hbm)
   DllCall("DeleteDC", "UInt", hdc)
   Gdip_DeleteGraphics(G)
   DetectHiddenWindows, %BackHidden%
   Return, 0
}

UpdateLayeredWindow(hwnd, hdc, LayeredX, LayeredY, LayeredW, LayeredH, Alpha=255)
{
   VarSetCapacity(pt, 8), NumPut(LayeredX, pt, 0), NumPut(LayeredY, pt, 4)
   
   DllCall("UpdateLayeredWindow"
   , "UInt", hwnd
   , "UInt", 0
   , "UInt", &pt
   , "Int64*", LayeredW|LayeredH<<32
   , "UInt", hdc
   , "Int64*", 0
   , "UInt", 0
   , "UInt*", Alpha<<16|1<<24
   , "UInt", 2)
   
   VarSetCapacity(pt, 0)
}

Gdip_DrawImage(pGraphics, pImage, dx, dy, dw, dh, sx="", sy="", sw="", sh="", Trans="1.0")
{
   If (sx != "") && (sy != "") && (sw != "") && (sh != "")
   {
      If (Trans != 1)
      {
         VarSetCapacity(ColorMatrix, 100, 0)
         Loop, 5
         NumPut((A_Index = 4) ? Trans : 1.0, ColorMatrix, (A_Index-1)*24, "Float")

         DllCall("gdiplus\GdipCreateImageAttributes", "UInt*", ImageAttr)
         DllCall("gdiplus\GdipSetImageAttributesColorMatrix", "UInt", ImageAttr, "Int", 1, "Int", 1, "UInt", &ColorMatrix, "Int", 0, "Int", 0)
      }
      
      E := DllCall("gdiplus\GdipDrawImageRectRectI", "UInt", pGraphics, "UInt", pImage, "Int", dx
      , "Int", dy, "Int", dw, "Int", dh, "Int", sx, "Int", sy, "Int", sw
      , "Int", sh, "Int", 2, "UInt", ImageAttr, "UInt", 0, "UInt", 0)   
      DllCall("gdiplus\GdipDisposeImageAttributes", "UInt", ImageAttr)
      DllCall("gdiplus\GdipDeleteMatrix", "UInt", ColorMatrix)
      VarSetCapacity(ColorMatrix, 0)
      Return, E      
   }
   Else
   {
      Return DllCall("gdiplus\GdipDrawImageRectI", "UInt", pGraphics, "UInt", pImage, "Int", dx
        , "Int", dy, "Int", dw, "Int", dh)
   }
}

Gdip_GraphicsFromHDC(hdc)
{
    DllCall("gdiplus\GdipCreateFromHDC", "UInt", hdc, "UInt*", pGraphics)
    Return, pGraphics
}

CreateDIBSection(w, h, bpp, hDC=0, ByRef ppvBits=0)
{
    hdcUsed := hDC ? hDC : DllCall("GetDC", "UInt", 0)
    If (hdcUsed)
    {
        VarSetCapacity(bi, 40, 0)       ; BITMAPINFO(HEADER)
        NumPut(40, bi,  0)              ; biSize
        NumPut(1,  bi, 12, "UShort")    ; biPlanes
        NumPut(0,  bi, 16)              ; biCompression = BI_RGB (none)
        NumPut(w,  bi,  4)              ; biWidth
        NumPut(h,  bi,  8)              ; biHeight
        NumPut(bpp,bi, 14, "UShort")    ; biBitCount
       
        hbmp := DllCall("CreateDIBSection", "UInt" , hDC, "UInt" , &bi, "UInt" , 0, "UInt*", ppvBits, "UInt" , 0, "UInt" , 0)

      DllCall("ReleaseDC", "UInt", 0, "UInt", hdcUsed)
      VarSetCapacity(bi, 0)
        Return, hbmp
    }
    Return, 0
}

Gdip_CreateBitmap(Width, Height, Format)
{
    DllCall("gdiplus\GdipCreateBitmapFromScan0", "Int", Width, "Int", Height, "Int", 0, "Int", Format, "UInt", 0, "UInt*", pBitmap)
    Return pBitmap
}

Gdip_GraphicsFromImage(pImage)
{
    DllCall("gdiplus\GdipGetImageGraphicsContext", "UInt", pImage, "UInt*", pGraphics)
    Return, pGraphics
}

Gdip_DeleteGraphics(pGraphics)
{
   Return, DllCall("gdiplus\GdipDeleteGraphics", "UInt", pGraphics)
}

Gdip_CreateBitmapFromFile(sFile)
{
   VarSetCapacity(wFile, 1023)
   DllCall("kernel32\MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sFile, "Int", -1, "UInt", &wFile, "Int", 512)
   DllCall("gdiplus\GdipCreateBitmapFromFile", "UInt", &wFile, "UInt*", pBitmap)
   Return, pBitmap
}

Gdip_DisposeImage(pImage)
{
   Return, DllCall("gdiplus\GdipDisposeImage", "UInt", pImage)
}

Gdip_Startup()
{
   If !DllCall("GetModuleHandle", "Str", "gdiplus")
   DllCall("LoadLibrary", "Str", "gdiplus")
   VarSetCapacity(si, 16, 0), si := Chr(1)
   DllCall("gdiplus\GdiplusStartup", "UInt*", pToken, "UInt", &si, "UInt", 0)
   VarSetCapacity(si, 0)
   Return, pToken
}

Gdip_Shutdown(pToken)
{
   DllCall("gdiplus\GdiplusShutdown", "UInt", pToken)
   If hModule := DllCall("GetModuleHandle", "Str", "gdiplus")
   DllCall("FreeLibrary", "UInt", hModule)
   Return, 0
}


I find it strange that you do this:

Code:
Loop Parse, XSpeeds, :
   XSpeed%A_Index% := A_LoopField
Loop Parse, YSpeeds, :
   YSpeed%A_Index% := A_LoopField


Why not stringsplit? it is considerably faster, as you are going to be using the created vars and not destroying them after


Last edited by tic on Fri Mar 14, 2008 8:17 pm; edited 1 time in total
Back to top
View user's profile Send private message
Oberon



Joined: 18 Feb 2008
Posts: 458

PostPosted: Fri Mar 14, 2008 8:16 pm    Post subject: Reply with quote

Hah! This is a great script Smile
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1344

PostPosted: Fri Mar 14, 2008 8:51 pm    Post subject: Reply with quote

I also made this handy tool for making those arrays of coordinates for the function in my above post. just drag and drop a png onto the gui. it will only work if the background colour is transparent for the png.


Code:
#SingleInstance, Force
#NoEnv
CoordMode, Mouse, Screen
SetBatchLines, -1
SetWinDelay, 0
SetWorkingDir %A_ScriptDir%

Gui, 1: +AlwaysOnTop
Gui, 1: Add, Text, x10 y10 w100 r1 -Multi -Wrap, xCoords
Gui, 1: Add, Edit, x+0 yp+0 w300 r1 -Multi -Wrap vxCoords
Gui, 1: Add, Text, x10 y+10 w100 r1 -Multi -Wrap, yCoords
Gui, 1: Add, Edit, x+0 yp+0 w300 r1 -Multi -Wrap vyCoords
Gui, 1: Show, AutoSize
Return

GuiDropFiles:
Gui, 1: +OwnDialogs
Loop, Parse, A_GuiEvent, `n
{
   File := A_LoopField
   Break
}
pToken := Gdip_Startup()

pBitmap := Gdip_CreateBitmapFromFile(File)
Width := Gdip_GetImageWidth(pBitmap)
Height := Gdip_GetImageHeight(pBitmap)

x:= y := 0
xCoords := yCoords := ""
Loop
{
   SetFormat, Integer, H
   DllCall("gdiplus\GdipBitmapGetPixel", "UInt", pBitmap, "Int", x, "Int", y, "UInt*", Colour)
   If (Colour != "0x0")
   {
      xCoords .= x "|"
      yCoords .= y "|"
   }
   
   SetFormat, Integer, D
   x++
   If (x = Width)
   x := 0, y := y+1
   If (y = Height)
   Break
}
StringTrimRight, xCoords, xCoords, 1
StringTrimRight, yCoords, yCoords, 1
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
GuiControl,, xCoords, %xCoords%
GuiControl,, yCoords, %yCoords%
Return

;###############################################################################################

Gdip_Startup()
{
   If !DllCall("GetModuleHandle", "Str", "gdiplus")
   DllCall("LoadLibrary", "Str", "gdiplus")
   VarSetCapacity(si, 16, 0), si := Chr(1)
   DllCall("gdiplus\GdiplusStartup", "UInt*", pToken, "UInt", &si, "UInt", 0)
   VarSetCapacity(si, 0)
   Return, pToken
}

Gdip_Shutdown(pToken)
{
   DllCall("gdiplus\GdiplusShutdown", "UInt", pToken)
   If hModule := DllCall("GetModuleHandle", "Str", "gdiplus")
   DllCall("FreeLibrary", "UInt", hModule)
   Return, 0
}

Gdip_CreateBitmapFromFile(sFile)
{
   VarSetCapacity(wFile, 1023)
   DllCall("kernel32\MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sFile, "Int", -1, "UInt", &wFile, "Int", 512)
   DllCall("gdiplus\GdipCreateBitmapFromFile", "UInt", &wFile, "UInt*", pBitmap)
   Return, pBitmap
}

Gdip_GetImageWidth(pImage)
{
   DllCall("gdiplus\GdipGetImageWidth", "Uint", pImage, "UintP", nW)
   Return   nW
}

Gdip_GetImageHeight(pImage)
{
   DllCall("gdiplus\GdipGetImageHeight", "Uint", pImage, "UintP", nH)
   Return   nH
}

Gdip_DisposeImage(pImage)
{
   Return, DllCall("gdiplus\GdipDisposeImage", "UInt", pImage)
}


Last edited by tic on Tue Mar 18, 2008 9:01 pm; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group