Advertisement
tic

Asteroids

tic
Jun 17th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance, Force
  2. #NoEnv
  3. SetBatchLines, -1
  4. #Persistent
  5.  
  6. ;#Include, Gdip.ahk
  7.  
  8. If !pToken := Gdip_Startup()
  9. {
  10.     MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
  11.     ExitApp
  12. }
  13. OnExit, Exit
  14.  
  15. Run, Notepad,,, PID
  16. WinWait ahk_pid %PID%
  17. WinGet, list, List, ahk_pid %PID%
  18. Loop, %list%
  19. {
  20.     WinGetTitle, title, % "ahk_id " list%A_Index%       ;%
  21.     if InStr(title, "Notepad")
  22.         hwnd1 := list%A_Index%
  23. }
  24. if !hwnd1
  25. {
  26.     MsgBox, 48, No window found!, No matching Notepad window found!
  27.     ExitApp
  28. }
  29. WinGetPos,,, Width, Height, ahk_id %hwnd1%
  30.  
  31. hwnddc := GetDC(hwnd1)
  32. hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
  33. G := Gdip_GraphicsFromHDC(hdc), Gdip_SetSmoothingMode(G, 4)
  34.  
  35. pBrushSHIP1 := Gdip_BrushCreateSolid(0xffa39f9f)
  36. pBrushSHIP2 := Gdip_BrushCreateSolid(0xff7f7575)
  37. pBrushSHIP3 := Gdip_BrushCreateSolid(0xff4c4a4a)
  38.  
  39. pBitmapBackground := CreateBackground(Width, Height)
  40.  
  41. Ship = 5|0|20|10|0|-50|-20|10
  42. x := Width//2, y := Height//2, Vx := Vy := Va := 0
  43. SetTimer, Update, 30
  44. return
  45.  
  46. ;#######################################################################
  47.  
  48. Update:
  49. arr := ""
  50. Va += GetKeyState("Left") ? -0.1 : GetKeyState("Right") ? 0.1 : 0
  51. Loop, Parse, Ship, |
  52.     Mod(A_Index, 2) ? xt := A_LoopField : arr .= xt*cos(Va)-A_LoopField*Sin(Va) "|" xt*Sin(Va)+A_LoopField*Cos(Va) "|"
  53. StringTrimRight, arr, arr, 1
  54.  
  55. Vs := GetKeyState("Up") ? 0.4 : GetKeyState("Down") ? -0.2 : 0
  56. Vx := Vx*0.95+Sin(Va)*Vs, Vy := Vy*0.95-Cos(Va)*Vs
  57. x := x < 0 ? Width : x > Width ? 0 : x+Vx , y := y < 0 ? Height : y > Height ? 0 : y+Vy
  58.  
  59. Gdip_DrawImage(G, pBitmapBackground, 0, 0, Width, Height)
  60.  
  61. StringSplit, narr, arr, |
  62. 1narr := narr1+x "," narr2+y "|" narr3+x "," narr4+y "|" narr5+x "," narr6+y
  63. 2narr := narr5+x "," narr6+y "|" narr7+x "," narr8+y "|" narr1+x "," narr2+y
  64. 3narr := narr1+x "," narr2+y "|" narr3+x "," narr4+y "|" narr1+x "," ((narr4-narr2)//2)+y "|" narr7+x "," narr8+y
  65.  
  66. Gdip_FillPolygon(G, pBrushSHIP1, 1narr)
  67. Gdip_FillPolygon(G, pBrushSHIP2, 2narr)
  68. Gdip_FillPolygon(G, pBrushSHIP3, 3narr)
  69. Gdip_DrawImage(G, pBitmap1)
  70. BitBlt(hwnddc, 0, 0, Width, Height, hdc, 0, 0)
  71. return
  72.  
  73. ;#######################################################################
  74.  
  75. CreateBackground(Width, Height)
  76. {
  77.     pBitmap := Gdip_CreateBitmap(Width, Height)
  78.     G := Gdip_GraphicsFromImage(pBitmap), Gdip_SetSmoothingMode(G, 4)
  79.  
  80.     pBrushBlack := Gdip_BrushCreateSolid(0xff000000)
  81.     Gdip_FillRectangle(G, pBrushBlack, 0, 0, Width, Height)
  82.     Gdip_DeleteBrush(pBrushBlack)
  83.  
  84.     pBrushStar := Gdip_BrushCreateSolid(0xffeeeeee)
  85.     Loop, % (Width*Height)/400      ;%
  86.     {
  87.         Random, s, 1, 4
  88.         Random, x, -s//2, Width+(s//2)
  89.         Random, y, -s//2, Height+(s//2)
  90.         Gdip_FillEllipse(G, pBrushStar, x, y, s, s)
  91.     }
  92.     Gdip_DeleteGraphics(G)
  93.     Gdip_DeleteBrush(pBrushStar)
  94.     return pBitmap
  95. }
  96.  
  97. ;#######################################################################
  98.  
  99. GuiClose:
  100. Exit:
  101. Gdip_DeleteBrush(pBrushSHIP1), Gdip_DeleteBrush(pBrushSHIP2), Gdip_DeleteBrush(pBrushSHIP3)
  102. Gdip_DisposeImage(pBitmapBackground)
  103. SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
  104. Gdip_DeleteGraphics(G)
  105. Gdip_Shutdown(pToken)
  106. WinKill, ahk_id %hwnd1%
  107. ExitApp
  108. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement