|
-
Jun 13th, 2006, 11:06 PM
#1
Thread Starter
Lively Member
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:
For A = 0 to 199
if shape1(A).backcolor = shape1(A + 1).backcolor and "user clicked control"_
then "dissappear and slide above block down"
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.
-
Jun 14th, 2006, 06:20 AM
#2
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:
' form code
Option Explicit
Private Const TOTALSHAPES = 200
Private Const SHAPESPERROW = 20
Private Const SHAPESIZE = 32
Dim TotalRows As Long
Private Sub Form_Load()
Dim lngA As Long, lngX As Long, lngY As Long
' change scalemode to pixels
Me.ScaleMode = vbPixels
Picture1.ScaleMode = vbPixels
Picture1.BorderStyle = vbBSNone
' init shapes
Shape1(0).Move 0, 0, SHAPESIZE, SHAPESIZE
Shape1(0).BackStyle = 1
Shape1(0).BorderStyle = 3
For lngA = 1 To (TOTALSHAPES - 1)
' load a new shape object, a copy of Shape1(0)
Load Shape1(lngA)
' do some simple math that determines X and Y location, multiply by shapesize
Shape1(lngA).Move SHAPESIZE * (lngA Mod SHAPESPERROW), SHAPESIZE * (lngA \ SHAPESPERROW), SHAPESIZE, SHAPESIZE
' just set some random color...
Shape1(lngA).BackColor = CLng(Rnd * vbWhite)
' make it visible so we can see it (by default new objects are hidden)
Shape1(lngA).Visible = True
Next lngA
' total rows
TotalRows = lngA \ SHAPESPERROW
' resize the picture area to match the size required to show the blocks
Picture1.Move 0, 0, SHAPESIZE * SHAPESPERROW, TotalRows * SHAPESIZE
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lngX As Long, lngY As Long, lngIndex As Long
lngX = X \ SHAPESIZE
lngY = Y \ SHAPESIZE
' make sure we have valid X and Y
If lngX < 0 Then Exit Sub
If lngY < 0 Then Exit Sub
If lngX >= SHAPESPERROW Then Exit Sub
If lngY >= TotalRows Then Exit Sub
' calculate index
lngIndex = lngY * SHAPESPERROW + lngX
' show which index we are moving at
Me.Caption = "The current index is " & lngIndex
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.
-
Jun 14th, 2006, 11:42 PM
#3
Thread Starter
Lively Member
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.
-
Jun 15th, 2006, 02:15 AM
#4
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
-
Jun 15th, 2006, 11:32 AM
#5
Thread Starter
Lively Member
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.
-
Jun 15th, 2006, 12:13 PM
#6
Re: first game to try
Not any easier than this:
VB Code:
X = Index Mod SHAPESPERROW
Y = Index \ SHAPESPERROW
' the code below makes sure you're not trying to go out of the game field
If X > 0 Then If Shape1(Index - 1).BackColor = Shape1(Index).BackColor Then ' match
If Y > 0 Then If Shape1(Index - SHAPESPERROW).BackColor = Shape1(Index).BackColor Then ' match
If X < SHAPESPERROW - 1 Then If Shape1(Index + 1).BackColor = Shape1(Index).BackColor Then ' match
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:
Dim blnLeft As Boolean, blnTop As Boolean, blnRight As Boolean, blnBottom As Boolean
Dim bytTotalMatch As Byte
X = Index Mod SHAPESPERROW
Y = Index \ SHAPESPERROW
If X > 0 Then blnLeft = Shape1(Index - 1).BackColor = Shape1(Index).BackColor
If Y > 0 Then blnTop = Shape1(Index - SHAPESPERROW).BackColor = Shape1(Index).BackColor
If X < SHAPESPERROW - 1 Then blnRight = Shape1(Index + 1).BackColor = Shape1(Index).BackColor
If Y < TotalRows - 1 Then blnBottom = Shape1(Index + SHAPESPERROW).BackColor = Shape1(Index).BackColor
' a boolean is either False (0) or True (-1)
' Abs strips out negative marker if one is found
' So True becomes 1 and False becomes 0
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:
Dim barMatch() As Byte
ReDim barMatch(TOTALSHAPES - 1)
' Think lngA as Index
For lngA = 0 To UBound(barMatch)
' ... code to fill bytTotalMatch for this array item
barMatch(lngA) = bytTotalMatch
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.
-
Jun 15th, 2006, 02:29 PM
#7
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|