Quantcast
Viewing all articles
Browse latest Browse all 56

[VB6 ]World War 3 points game Source + EXE

Hi,

This is a little game I have been working on over the past couple of days.

vb Code:
  1. 'You need
  2. ' 4 CommandButtons "cmdAbout", "cmdHow", "cmdAdd" and "cmd Subtract"
  3. ' A listbox called "lstCountries" set to style "Checkbox".
  4.  
  5. 'The form code
  6. Option Explicit
  7. Dim index As Integer, i As Integer, holder As String, var(20) As Variant, country As String, zero As Boolean
  8.  
  9. Private Sub cmdAbout_Click()
  10. MsgBox ("World War 3 Forum Game"& vbCrLf & "Based on the Forum game [url]http://www.gtaforums.com/index.php?[/url] showtopic=566787&st=0" & vbCrLf & Please visit either [url]http://aaronspehr.net/[/url] or [url]http://www.vbforums.com/showthread.php?728099-World-War-3-points-game&p=4465297#post4465297[/url] to download the lastest version. & vbCrLf & "Copyright 2013 by Nightwalker83")
  11. End Sub
  12.  
  13. Private Sub cmdAdd_Click()
  14.  addpoint
  15. End Sub
  16.  
  17. Private Sub cmdHow_Click()
  18. MsgBox ("How to play " & App.ProductName & vbCrLf & "Select a country then select either add or subtract a vote if a country reaches 0 votes they lose the game.")
  19. End Sub
  20.  
  21. Private Sub cmdSubtract_Click()
  22. subtractpoint
  23. End Sub
  24.  
  25. Private Sub Form_Load()
  26.     'Subclass the "Form", to Capture the Listbox Notification Messages ...
  27.     lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedList)
  28.     var(0) = 5
  29.     lstCountries.AddItem "Australia   - " & var(0), 0
  30.      var(1) = 5
  31.     lstCountries.AddItem "France  - " & var(1), 1
  32.      var(2) = 5
  33.     lstCountries.AddItem "Germany  - " & var(2), 2
  34.     i = 0
  35. End Sub
  36.  
  37. Private Sub Form_Unload(Cancel As Integer)
  38.     'Release the SubClassing, Very Important to Prevent Crashing!
  39.     Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
  40. End Sub
  41.  
  42. Private Sub lstCountries_Click()
  43.    holder = lstCountries.Text
  44.    country = (Replace(holder, Right(holder, 3), ""))
  45. End Sub
  46.  
  47. Private Sub varcheck(pos As Integer)
  48. If pos = 0 Then
  49. lstCountries.RemoveItem (lstCountries.ListIndex)
  50. cmdSubtract.Enabled = False
  51. zero = True
  52. Else
  53. zero = False
  54. End If
  55. End Sub
  56.  
  57. Private Sub addpoint()
  58. If holder = "" Then Exit Sub
  59.       List1.RemoveItem List1.ListIndex
  60.         Select Case Trim(country)
  61.         Case "Australia"
  62.         varcheck (var(0))
  63.         var(0) = var(0) + 1
  64.          List1.AddItem country & var(0), List1.ListIndex
  65.          List1.ItemData(List1.NewIndex) = QBColor(10)
  66.         Case "France"
  67.         varcheck (var(1))
  68.         var(1) = var(1) + 1
  69.          List1.AddItem country & var(1), List1.ListIndex
  70.          List1.ItemData(List1.NewIndex) = QBColor(10)
  71.         Case "Germany"
  72.         varcheck (var(2))
  73.         var(2) = var(2) + 1
  74.        List1.AddItem country & var(2), List1.ListIndex
  75.        List1.ItemData(List1.NewIndex) = QBColor(10)
  76.         End Select
  77.         i = Right(List1.Text, 1)
  78.         MsgBox (country & " point added")
  79.         Command2.Enabled = True
  80. End Sub
  81.  
  82. Private Sub subtractpoint()
  83. If holder = "" Then Exit Sub
  84.         Select Case Trim(country)
  85.         Case "Australia"
  86.          varcheck (var(0))
  87.          If zero Then Exit Sub
  88.             lstCountries.RemoveItem lstCountries.ListIndex
  89.          var(0) = var(0) - 1
  90.          lstCountries.AddItem country & var(0), lstCountries.ListIndex
  91.          lstCountries.ItemData(lstCountries.NewIndex) = QBColor(12)
  92.         Case "France"
  93.         varcheck (var(1))
  94.         If zero Then Exit Sub
  95.             lstCountries.RemoveItem lstCountries.ListIndex
  96.         var(1) = var(1) - 1
  97.          lstCountries.AddItem country & var(1), lstCountries.ListIndex
  98.          lstCountries.ItemData(lstCountries.NewIndex) = QBColor(12)
  99.         Case "Germany"
  100.         varcheck (var(2))
  101.            If zero Then Exit Sub
  102.             lstCountries.RemoveItem lstCountries.ListIndex
  103.         var(2) = var(2) - 1
  104.        lstCountries.AddItem country & var(2), lstCountries.ListIndex
  105.        lstCountries.ItemData(lstCountries.NewIndex) = QBColor(12)
  106.         End Select
  107.         i = Right(lstCountries.Text, 1)
  108.         MsgBox (country & " point subtracted")
  109. End Sub
  110.  
  111. 'The module code
  112. ' from thevbprogrammer.com
  113.  
  114. Option Explicit
  115.  
  116. Public Type RECT
  117.         Left As Long
  118.         Top As Long
  119.         Right As Long
  120.         Bottom As Long
  121. End Type
  122.  
  123. Public Type DRAWITEMSTRUCT
  124.         CtlType As Long
  125.         CtlID As Long
  126.         itemID As Long
  127.         itemAction As Long
  128.         itemState As Long
  129.         hwndItem As Long
  130.         hdc As Long
  131.         rcItem As RECT
  132.         ItemData As Long
  133. End Type
  134.  
  135. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  136.  
  137. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  138. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  139. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  140. Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
  141. Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
  142. Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  143. Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
  144. Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
  145. Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
  146. Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
  147. Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
  148.  
  149. Public Const COLOR_HIGHLIGHT = 13
  150. Public Const COLOR_HIGHLIGHTTEXT = 14
  151. Public Const COLOR_WINDOW = 5
  152. Public Const COLOR_WINDOWTEXT = 8
  153. Public Const LB_GETTEXT = &H189
  154. Public Const WM_DRAWITEM = &H2B
  155. Public Const GWL_WNDPROC = (-4)
  156. Public Const ODS_FOCUS = &H10
  157. Public Const ODT_LISTBOX = 2
  158.  
  159. Public lPrevWndProc As Long
  160.  
  161. Public Function SubClassedList(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  162.     Dim tItem As DRAWITEMSTRUCT
  163.     Dim sBuff As String * 255
  164.     Dim sItem As String
  165.     Dim lBack As Long
  166.  
  167.     If Msg = WM_DRAWITEM Then
  168.         'Redraw the listbox
  169.         'This function only passes the Address of the DrawItem Structure, so we need to
  170.         'use the CopyMemory API to Get a Copy into the Variable we setup:
  171.         Call CopyMemory(tItem, ByVal lParam, Len(tItem))
  172.         'Make sure we're dealing with a Listbox
  173.         If tItem.CtlType = ODT_LISTBOX Then
  174.             'Get the Item Text
  175.             Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
  176.             sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
  177.             If (tItem.itemState And ODS_FOCUS) Then
  178.                 'Item has Focus, Highlight it, I'm using the Default Focus
  179.                 'Colors for this example.
  180.                 lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
  181.                 Call FillRect(tItem.hdc, tItem.rcItem, lBack)
  182.                 Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
  183.                 Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
  184.                 TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
  185.                 DrawFocusRect tItem.hdc, tItem.rcItem
  186.             Else
  187.                 'Item Doesn't Have Focus, Draw it's Colored Background
  188.                 'Create a Brush using the Color we stored in ItemData
  189.                 lBack = CreateSolidBrush(tItem.ItemData)
  190.                 'Paint the Item Area
  191.                 Call FillRect(tItem.hdc, tItem.rcItem, lBack)
  192.                 'Set the Text Colors
  193.                 Call SetBkColor(tItem.hdc, tItem.ItemData)
  194.                 Call SetTextColor(tItem.hdc, IIf(tItem.ItemData = vbBlack, vbWhite, vbBlack))
  195.                 'Display the Item Text
  196.                 TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
  197.             End If
  198.             Call DeleteObject(lBack)
  199.             'Don't Need to Pass a Value on as we've just handled the Message ourselves
  200.             SubClassedList = 0
  201.             Exit Function
  202.  
  203.         End If
  204.  
  205.     End If
  206.     SubClassedList = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
  207. End Function

The first version is only single player but I am thinking of making it multi-player maybe through the use of a database.


Nightwalker

Viewing all articles
Browse latest Browse all 56

Trending Articles