So this is for BASIC programming, and our assignment was to comment each part of the program and describe what that segment does. The program is for the game Concentration. (A bunch of flipped down cards with the objective to match two of the same) I've finished the assignment, but I'm not sure if it's completely right as I did have a couple areas where I was feeling vague.
Here is what I finished:
CODE
; the following lines … Sets screen resolution, readies the backbuffer, readies the Random num generator and loads the pointer an image
Graphics 800,600
SetBuffer BackBuffer()
SeedRnd MilliSecs()
pointer_img=LoadImage("1.bmp")
; the following lines … Sets up the arrays required to keep track of the cards and their current game statuses.
Dim cards(52)
Dim face_up(52)
Dim card_imgs(52)
Dim on_table(52)
; the following lines … call the init method (described later)
cards_init()
; the following lines … call the shuffle method in order to mix the cards up
cards_shuffle()
; the following lines … initializes last_n
last_n=0
Repeat
; the following lines … refreshes the screen, creates the pointer image at pointer location and places all the valid cards down.
Cls
cards_display()
DrawImage pointer_img,MouseX(),MouseY()
Flip
; the following lines … calls which_one method upon clicking
If MouseHit(1)
n=cards_which_one()
; the following lines … checks to see if the card still exists on the table, in order to 'flip it'
If n>0 Then
face_up(n)=True
; the following lines … calls display method
If last_n=0 Then
last_n=n
Else
cards_display()
Flip
Delay 2000
card=cards(n)
last_card=cards(last_n)
d=Abs(card-last_card)
; the following lines … Basically checks to see if the two chosen cards are matching, and then proceeds to flip them down or remove them.
If d=13 Or d=26 Or d=39 Then
on_table(n)=False
on_table(last_n)=False
Else
face_up(n)=False
face_up(last_n)=False
End If
last_n=0
End If
End If
End If
; the following lines … Game continues until Esc is pressed.
Until KeyHit(1)
End
; the following lines … This function begins caching the card images into the buffer for later use.
Function cards_init()
card_imgs(0)=LoadImage("back.bmp")
For i=1 To 52
cards(i)=i
face_up(i)=False
card_imgs(i)=LoadImage(i+".bmp")
on_table(i)=True
Next
End Function
; the following lines … This function shuffles the deck.
Function cards_shuffle()
For i=1 To 500
r1=Rand(52)
r2=Rand(52)
t=cards(r1)
cards(r1)=cards(r2)
cards(r2)=t
Next
End Function
; the following lines … draws the cards on to the screen.
Function cards_display()
n=1
y=8
For row=1 To 6
x=68
For col=1 To 9
card=0
If on_table(n) Then
If face_up(n) Then card=cards(n)
DrawBlock card_imgs(card),x,y
End If
x=x+74
n=n+1
If n=53 Then Exit
Next
y=y+98
Next
End Function
; the following lines … This function checks to see if the card is face up or not
Function cards_which_one()
card=0
n=1
y=8
For row=1 To 6
x=68
For col=1 To 9
If mouse_in(x,y,x+74,y+98) Then If Not face_up(n) Then card=n
x=x+74
n=n+1
If n=53 Then Exit
Next
y=y+98
Next
Return card
End Function
; the following lines … Enables the mouse pointer.
Function mouse_in(x1,y1,x2,y2)
x=MouseX(): y=MouseY()
m=True
If x<x1 Then m=False
If x>x2 Then m=False
If y<y1 Then m=False
If y>y2 Then m=False
Return m
End Function
A tad long, but basically I was wondering if anyone could tell me if my comments for each section is correct, and if anything is wrong or can be tweaked.
Thanks a bunch.