Results 1 to 7 of 7

Thread: first game to try

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    125

    first game to try

    Hi, i dont use VB too much but i thought i would try and make a quick little game just for a bit of experience. I figured i should check here for any good and fast methods to work off.

    I want to try and make a block game where you get a stack of blocks, and you have to click the groups of 2 or more to eliminate them, with the goal being to eliminate all of the blocks on the board.

    The only way i know how to create this is by checking each randomly colored square with an "If statement" to see if its neighbor square is the same color when you click it.
    The problem with that is when you have 200 blocks its a bit tedious and I searched but have no idea how to know if a shape was clicked on the program.

    Im sure theres an easier way. I was thinking of a for...loop by checking the shapes array

    VB Code:
    1. For A = 0 to 199
    2.  if shape1(A).backcolor = shape1(A + 1).backcolor and "user clicked control"_
    3. then "dissappear and slide above block down"
    4. Next A

    But I have a few issues there,
    1. I cant check multiple blocks the same way like that when i hit the border
    2. I dont know if i can or how to check if the control was clicked in that way.

    So i know i have some basics still to understand, but this is why im working on it. (I spent a good bit searching around the forum, which did help, but im still stuck with these questions.)

    Any reply is much appreciated.
    --- Science does not explain why things are what they are. What we get from Science is our interpretation of how things do what they do.
    --- No Scientific law of the universe is stable, we did not create it, and we will never understand all of its abilities.
    --- What we determine as reality is a mere assumption of what tomorrow will be based on what yesterday was.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: first game to try

    You can do some math to figure out a clicked area. Here is a simple sample program:

    • Add a picturebox Picture1
    • Add an shape Shape1 to Picture1
    • Set Shape1's property Index to 0


    VB Code:
    1. ' form code
    2. Option Explicit
    3. Private Const TOTALSHAPES = 200
    4. Private Const SHAPESPERROW = 20
    5. Private Const SHAPESIZE = 32
    6.  
    7. Dim TotalRows As Long
    8.  
    9. Private Sub Form_Load()
    10.     Dim lngA As Long, lngX As Long, lngY As Long
    11.     ' change scalemode to pixels
    12.     Me.ScaleMode = vbPixels
    13.     Picture1.ScaleMode = vbPixels
    14.     Picture1.BorderStyle = vbBSNone
    15.     ' init shapes
    16.     Shape1(0).Move 0, 0, SHAPESIZE, SHAPESIZE
    17.     Shape1(0).BackStyle = 1
    18.     Shape1(0).BorderStyle = 3
    19.     For lngA = 1 To (TOTALSHAPES - 1)
    20.         ' load a new shape object, a copy of Shape1(0)
    21.         Load Shape1(lngA)
    22.         ' do some simple math that determines X and Y location, multiply by shapesize
    23.         Shape1(lngA).Move SHAPESIZE * (lngA Mod SHAPESPERROW), SHAPESIZE * (lngA \ SHAPESPERROW), SHAPESIZE, SHAPESIZE
    24.         ' just set some random color...
    25.         Shape1(lngA).BackColor = CLng(Rnd * vbWhite)
    26.         ' make it visible so we can see it (by default new objects are hidden)
    27.         Shape1(lngA).Visible = True
    28.     Next lngA
    29.     ' total rows
    30.     TotalRows = lngA \ SHAPESPERROW
    31.     ' resize the picture area to match the size required to show the blocks
    32.     Picture1.Move 0, 0, SHAPESIZE * SHAPESPERROW, TotalRows * SHAPESIZE
    33. End Sub
    34. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    35.     Dim lngX As Long, lngY As Long, lngIndex As Long
    36.     lngX = X \ SHAPESIZE
    37.     lngY = Y \ SHAPESIZE
    38.     ' make sure we have valid X and Y
    39.     If lngX < 0 Then Exit Sub
    40.     If lngY < 0 Then Exit Sub
    41.     If lngX >= SHAPESPERROW Then Exit Sub
    42.     If lngY >= TotalRows Then Exit Sub
    43.     ' calculate index
    44.     lngIndex = lngY * SHAPESPERROW + lngX
    45.     ' show which index we are moving at
    46.     Me.Caption = "The current index is " & lngIndex
    47. End Sub

    The most important bit for you to learn here is the usage of Mod and \ operators so you can figure out how to get X and Y out of index. You can also see how to convert X and Y into an index.
    Last edited by Merri; Jun 14th, 2006 at 06:25 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    125

    Re: first game to try

    thanks, thats alot of info i can work with.

    You can do some math to figure out a clicked area.
    I still dont understand though, how do i find out which shape was clicked?
    --- Science does not explain why things are what they are. What we get from Science is our interpretation of how things do what they do.
    --- No Scientific law of the universe is stable, we did not create it, and we will never understand all of its abilities.
    --- What we determine as reality is a mere assumption of what tomorrow will be based on what yesterday was.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: first game to try

    You can use MouseUp similar to what I had in MouseMove. Or you could store X and Y positions into form-wide variables (Dim clickX As Single, clickY As Single) in MouseDown so you know where the click happened in Picture1_Click

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    125

    Re: first game to try

    Gotcha,
    Thanks alot that Really helps out and i appreciate it.


    Do you or anyone else know an easier way to check if neighbor blocks are the same color?
    --- Science does not explain why things are what they are. What we get from Science is our interpretation of how things do what they do.
    --- No Scientific law of the universe is stable, we did not create it, and we will never understand all of its abilities.
    --- What we determine as reality is a mere assumption of what tomorrow will be based on what yesterday was.

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: first game to try

    Not any easier than this:

    VB Code:
    1. X = Index Mod SHAPESPERROW
    2. Y = Index \ SHAPESPERROW
    3. ' the code below makes sure you're not trying to go out of the game field
    4. If X > 0 Then If Shape1(Index - 1).BackColor = Shape1(Index).BackColor Then ' match
    5. If Y > 0 Then If Shape1(Index - SHAPESPERROW).BackColor = Shape1(Index).BackColor Then ' match
    6. If X < SHAPESPERROW - 1 Then If Shape1(Index + 1).BackColor = Shape1(Index).BackColor Then ' match
    7. If Y < TotalRows - 1 Then If Shape1(Index + SHAPESPERROW).BackColor = Shape1(Index).BackColor Then ' match

    The code is partly a pseudocode. What happens on match depends on what you want to do. One method you could do is to have four boolean values, so you could see how many matches you get when you combine them together. For example:

    VB Code:
    1. Dim blnLeft As Boolean, blnTop As Boolean, blnRight As Boolean, blnBottom As Boolean
    2. Dim bytTotalMatch As Byte
    3.  
    4. X = Index Mod SHAPESPERROW
    5. Y = Index \ SHAPESPERROW
    6.  
    7. If X > 0 Then blnLeft = Shape1(Index - 1).BackColor = Shape1(Index).BackColor
    8. If Y > 0 Then blnTop = Shape1(Index - SHAPESPERROW).BackColor = Shape1(Index).BackColor
    9. If X < SHAPESPERROW - 1 Then blnRight = Shape1(Index + 1).BackColor = Shape1(Index).BackColor
    10. If Y < TotalRows - 1 Then blnBottom = Shape1(Index + SHAPESPERROW).BackColor = Shape1(Index).BackColor
    11.  
    12. ' a boolean is either False (0) or True (-1)
    13. ' Abs strips out negative marker if one is found
    14. ' So True becomes 1 and False becomes 0
    15. bytTotalMatch = Abs(blnLeft) + Abs(blnTop) + Abs(blnRight) + Abs(blnBottom)

    You also might like to make combos. You could create a new array where you store stuff:

    VB Code:
    1. Dim barMatch() As Byte
    2.  
    3. ReDim barMatch(TOTALSHAPES - 1)
    4.  
    5. ' Think lngA as Index
    6. For lngA = 0 To UBound(barMatch)
    7.  
    8.    ' ... code to fill bytTotalMatch for this array item
    9.  
    10.    barMatch(lngA) = bytTotalMatch
    11. Next lngA

    The code above shows how you can use an array to store information about all matches on the game field. I don't know if you have any use for it, but you might use something similar for calculating combos and such. Most importantly shows you how you can use an array to help you out


    Surprisingly enough I've never made a puzzle or a block game.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    125

    Re: first game to try

    Thats awesome, i knew there had to be a way to use the shape index as the variable i just didnt know the language enough to do it. I'm still working with the first bit of code but when i get to the rest i'll let you know if i have any questions.

    Thanks, you gave me some Major help here.
    --- Science does not explain why things are what they are. What we get from Science is our interpretation of how things do what they do.
    --- No Scientific law of the universe is stable, we did not create it, and we will never understand all of its abilities.
    --- What we determine as reality is a mere assumption of what tomorrow will be based on what yesterday was.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width