Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-18-2004, 05:55 PM   #1
Mega Man X
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Talking Visual Basic? Anyone?

Hi there!!!

I need some help with Visual Basic. Since this forum says:

Programming [View new posts]
This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
I thought it was worth the shot . I'm trying to make a memory game with Visual Basic to learn how to use the language effectively, plus it's nice to have more things on the resume . So far, it looks like this:

it's geocities, so you may need to copy and paste the above address in a new windows to work :S. Sorry.

As you see, it's a simple memory game with Megaman(!). All 16 default images(with a "Dr") are loaded by default. I'm not even sure if I should do it... Here is where the problem comes... The book I'm reading, does not teach anything about graphics with Visual Basic, and I love graphics and games. So...

1 - How can I load an image and store it into a variable? Which command does it?

Another problem.. I cannot think in a solution for the pseudo-code. I thought a memory game would be easy, but it's driving me crazy . Could anyone give me any suggestion for a pseudo-code? So far, after a week planning this:

I know that I have 8 different images, I need to sort them into 16 squares. I need to randomize the sorting of images every time the game starts. Then I've to check if the player choose two images and If they are the same. If they are, I hide them, if they are not, I flip them back.

Wow, that's is just a very simple game in the easiest language around and I cannot think in a solution. I think I should give up programming. Anyway, is anyone good with Visual Basic or has a good solution for this?

Thanks a bunch in advance!.
Old 03-18-2004, 09:21 PM   #2
Registered: Feb 2004
Location: Texas
Distribution: Debian
Posts: 30

Rep: Reputation: 15
I had to learn VB at work, didn't care for it much but I'm a C guy and I always had the feeling that VB was still a work in progress(incomplete language). Anyway I'm thinking a collection object to store the images in random order. Use a function like the one below to generate a random number from 1 to 16 and use that as the collection key. Course once a key is used then you'll have to keep recalling the Rand function until you get another unique key. After that once a match is made you can replace the "image" held in those collection locations with a generic image to show those locations are no longer valid. Sorry don't know anything bout graphics but I would say use an image control and use the LoadImage() function to load the image to a var. This looks like homework so I hope this helps you get started, but I'm not gonna write the code for ya

'Rand - Return a random number in a given range.
'  Low  - The lower bounds of the range.
'  High - The upper bounds of the range.
'  Returns a random number from Low..High.

Public Function Rand(ByVal Low As Long, _
                     ByVal High As Long) As Long
  Rand = Int((High - Low + 1) * Rnd) + Low
End Function
Old 03-19-2004, 03:06 AM   #3
Mega Man X
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
shortfuse thanks a lot mate!

Yeah, it's some sort of homework . I'm trying, but the book I'm reading is bad and the teacher does not seem to be answering anyone(I am reading Visual Basic by distance course...ghhe). What I'm trying to do so far is:

Option Explicit

Dim myImage(15)

Function init_game()
    Dim idx
    ' initialize default images
    For idx = 0 To 15
        picVGA(idx) = LoadPicture("default.gif")
    Next idx
    ' sort cards
    'For idx = 0 To 7
     '   picVGA(idx).Picture = myImage(0)
    'Next idx
End Function

Private Sub cmdClose_Click()
End Sub

Private Sub Form_Load()

    myImage(0) = LoadPicture("0.gif")
    myImage(1) = LoadPicture("1.gif")
    myImage(2) = LoadPicture("2.gif")
    myImage(3) = LoadPicture("3.gif")
    myImage(4) = LoadPicture("4.gif")
    myImage(5) = LoadPicture("5.gif")
    myImage(6) = LoadPicture("6.gif")
    myImage(7) = LoadPicture("7.gif")


End Sub
As you can see above, the 16 default images are in an array control. So far, it can load and set the 16 default images ( the ones with a "Dr." symbol ). What I don't know how to do is... how to sort the 8 hidden images (that the user has to click to flip them) and randomize it's positions. I cannot think in a way of load those images and let them be hidden. (to load they load fine ).

Also, how would I check if two images matches? Something like:

If click1 == click2 Then

would that work?. Man, I read C and C++, no problems. Java? no problems. Been playing with python and pygame, no problems . How could I get stuck with Visual Basic is beyond my understanding

Thanks a lot for the help!
Old 03-19-2004, 05:08 AM   #4
Registered: Oct 2003
Location: Germany
Distribution: SuSE 8.2, SuSE 9.1 on desktop (besides W2K). SuSE 9.1 on notebook (besides XP).
Posts: 44

Rep: Reputation: 15
I am also not going to write the code. But I have some suggestions on your questions.

(1) To generate a random sequence, you might do a random swap. In an earlier post you have seen how to generate a random number in the range 0..7. Now can write a loop

for I = 0 to 7
R = Rand(0,7)
temp = yourArray(I)
yourArray(I) = yourArray(R)
yourArray(R) = temp
next I

Thus you do 8 random swaps which will shake up your array. To avoid moving bitmaps around in the memory you should perform the swaps on an array of integers (initialise to the values 0..7 in another loop before sorting) and use the resulting randomly sorted array of integers 0..7 for loading your pictures, like

myImage(yourArray(0)) = LoadPicture("0.gif")
...and so on.

(2) To hide a picture, you need another image ("8.gif") which displays the back side. You load the backside image into all your array at first, ignoring the nice integer array (yourArray) you created in (1). After each first click, you load the proper image as shown in (1). After each second click (if pictures do not match) you load the backsides again.

Don't ask how to distinguish between the first and the second click because that would destroy any remaining belief that you have done some programming in any language.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid Question: Microsoft Visual Studio 6.0 (A.K.A. Visual Basic 6) Will it run? S0RD3N Linux - Software 8 05-07-2008 08:42 PM
Visual Basic 6 dwig365 Linux - Software 1 08-28-2004 02:33 PM
Visual Basic 6 DaveyB Linux - Software 2 08-19-2004 12:47 PM
Visual Basic Finnae707 Programming 12 06-17-2003 04:06 AM
Need Visual Basic help icorsi Programming 4 10-09-2001 08:08 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:20 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration