You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
79 lines
2.2 KiB
15 years ago
|
# The scripts in this tutorial implement the famous minesweeper game
|
||
|
# STEP 1
|
||
|
|
||
|
|
||
|
|
||
|
# First of all we create the main game widget
|
||
|
# The minesweeper widget inherits from the widget class
|
||
|
class(minesweepermain,widget)
|
||
|
{
|
||
|
# The constructor sets the basic widget properties
|
||
|
# and creates the child widgets
|
||
|
constructor()
|
||
|
{
|
||
|
# Set the widget caption
|
||
|
$$->$setCaption("KVIrc's Minesweeper (0.1.0)");
|
||
|
|
||
|
# We will have a variable number of rows , columns and mines
|
||
|
# For now we hardcorde it , later they might become user definable parameters
|
||
|
$$->%rows = 10
|
||
|
$$->%cols = 10
|
||
|
$$->%mines = 10
|
||
|
|
||
|
# The child labels will be put in a layout that will manage automatically their geometries
|
||
|
$$->%layout = $new(layout,$this)
|
||
|
|
||
|
# Time to create the child labels
|
||
|
for(%i = 0;%i < $$->%rows;%i++)
|
||
|
{
|
||
|
for(%j = 0;%j < $$->%cols;%j++)
|
||
|
{
|
||
|
# We use a dictionary to simulate a two dimensional array
|
||
|
# The label references are stored in the dictionary associated to a key
|
||
|
# that is build from the row and column index
|
||
|
$$->%label{%i,%j}=$new(label,$this,"%i_%j")
|
||
|
# Each label must remember its position
|
||
|
$$->%label{%i,%j}->%row = %i
|
||
|
$$->%label{%i,%j}->%col = %j
|
||
|
# We add the labels to the layout grid
|
||
|
$$->%layout->$addWidget($$->%label{%i,%j},%i,%j)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Time to initialize a new game
|
||
|
$$->$newGame();
|
||
|
}
|
||
|
|
||
|
# We need no destructor for now : the child widgets and the layout will be
|
||
|
# destroyed when the user will close the main widget
|
||
|
|
||
|
|
||
|
# Here we start a new game
|
||
|
newGame()
|
||
|
{
|
||
|
# We set the labels
|
||
|
for(%i = 0;%i < $$->%rows;%i++)
|
||
|
{
|
||
|
for(%j = 0;%j < $$->%cols;%j++)
|
||
|
{
|
||
|
# KVIrc is parsed on-the-fly so we use the following line as optimisation.
|
||
|
# parsing %l is really faster than parsing a $$->%label{%i,%j}
|
||
|
%l = $$->%label{%i,%j}
|
||
|
%l->$setFrameStyle(Raised,WinPanel); # raised labels are "unpressed" buttons
|
||
|
%l->%bIsMine = 0 # for now it is NOT a mine
|
||
|
%l->%numMines = 0 # number of adiacent mines , for now 0
|
||
|
%l->%bIsDiscovered = 0 # this label has been pressed ?
|
||
|
%l->$setText("") # set the text to an empty string
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Create an instance of the minesweepermain object
|
||
|
|
||
|
%m = $new(minesweepermain)
|
||
|
%m->$show()
|
||
|
|
||
|
# /parse this file
|
||
|
|