I thought id help the beginner make a checkers game in VB6
and as the universal saying goes: start with something incredibly hard
that you can learn from, learn it, then use it as a reference
so here goes...
all you need is a new form, and nothing else
Code:
set the forms property ScaleMode to 3 (which is pixel)
its recommended that you set the forms backcolor to white
and that you set the caption to "Checkers"
go right into the forms code by pressing F7
go right to the very top and define this:
Code:
DefLng A-Z
this line means that all variables that we define or use later that arent defined as something else like a String (text container) or Integer (16bit number) will become a 32bit Long integer

(Lng=Long)
you dont have to understand this, but its really simple:
Dim X <-- now that X is a Long because DefLng A-z makes this happen
x = 33
Dim X as String <-- now X is a text String and not a Long by default
x = "A33" <-- text
anyways next comes:
Code:
Dim Board(), n, Turn As Boolean
The Board array, a number n and a Boolean variable called Turn that tells us whose turn it is!
now we need to code what happens at the start
Form_Load is an event that triggers when a form is loaded
the form is the window the game will be emulated in
The code looks something like this:
Code:
Private Sub Form_Load()
n = Val(InputBox("Dimensions?", "Checkers", "3"))
If n <= 1 Then End
ReDim Board(1 To n, 1 To n)
End Sub
this isnt really good code, but its simple and understandable if someone other than me explained it, but...
n = [something]
The above line will set the n variable we defined at the top to [something],
and that something is: Val(InputBox("Dimensions?", "Checkers", "3"))
How do i explain this...
Val(something) makes that something turn into a number, even if it is of the text type
Val() is a function that returns something if correct or sometimes even incorrect data is input...
F.ex you should be familiar with Abs(number) or Int(number)
Abs(-2) = 2... abs makes any number positive, if its positive already it returns the very same number back Abs(2) = 2
Int(2.25) = 2... int returns the integer part of a number, it just removes any decimals
..and finally: Val("text") = 0, and Val("234") = 234... val returns a number based on a text!
Hope this is enough for you to get a hint at least, you dont have to understand it fully!
and an inputbox is? well youll know the second you start the game
i dont think i need to explain it because its obvious once you start and look at it
"dimensions?" is the question of the box (note that "" encloses a text-string)
"checkers" is the title of the box, and "3" is the default value!
after that i just added "If n<=1 Then End"
this prevents someone from entering a number too low to play with
and since "" (no text) is considered the number 0 from the Val() function
it also considers when you press Cancel in the inputbox
Cancel makes the inputbox return "" (no text) and Val("") = 0
so we got that covered too
you should have learned about arrays/tables if you want to learn about programming
i dont think theres anything i can say to explain this right now...
but ReDim Board(1 To n, 1 To n) makes the Board() empty array
into a n x n array
say you chose 3 in the inputbox => n = 3
then the array will be re-defined to 3x3 or as the code says 1-3, 1-3
so its a 2dimensional board array
(i should have defined it as a Byte to save space, but it doesnt matter here
this is a small simple game, and we dont have to save anything)
thats the end of the start, theres nothing more to "init" or initialize
now that we know the size of the board and the boards been defined
the rest will go by itself like this:
Code:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim w As Single, h As Single
w = ScaleWidth / n: h = ScaleHeight / n
X1 = Int(X / w) + 1: Y1 = Int(Y / h) + 1
If Board(X1, Y1) = 0 Then
Board(X1, Y1) = IIf(Turn, 2, 1)
Turn = Not Turn
Form_Resize
End If
End Sub
Private Sub Form_Resize()
Dim w As Single, h As Single, r As Single
If WindowState = vbMinimized Then Exit Sub
Cls
w = ScaleWidth / n: h = ScaleHeight / n
r = IIf(w < h, w / 2, h / 2)
For X = 1 To n - 1
Line (X * w, 0)-(X * w, ScaleHeight), 0
Line (0, X * h)-(ScaleWidth, X * h), 0
Next
For X = 0 To n - 1
For Y = 0 To n - 1
If Board(X + 1, Y + 1) <> 0 Then
FillColor = IIf(Board(X + 1, Y + 1) = 1, 0, vbWhite)
Circle (X * w + w / 2, Y * h + h / 2), r, 0
End If
Next
Next
End Sub
The Form_MouseDown event is exactly as it says it is, its when you click on the form, or rather press the button down
(it doesnt cover the release, so its already too late when you click!)
more to come... need a short break
feel free to comment and/or correct