In the course of training in programming in Visual Basic,
Delphi or Java trainees are usually offered to write as the first program the program Hello Word. It is
possibly right. As a rule you need only 2-3 code lines to write the program. Would you believe that your second program in any of the languages can contain about 1900 code lines, use scores of objects, process
signals from keyboard and work with Timer? And would you believe that programming in one of the languages (Visual Basic, Delphi or Java) you would master in 2-3 days two other languages from the list?
To answer the
questions, please, read the document.
Manual on development of a logic game in Java is identical to the ebook versions for VB and Delphi. It would help you to master new programming languages in a short time. The ebook includes Word document with step-by-step description
of the process of program development, screenshots, procedure codes and 12 complete listings in Java (Microsoft Visual J++ 6.0) illustrating all stages of programming in Java-Application.
Author: Valery V Shmeleff Moscow Russia http://www.oflameron.ru , http://www.oflameron.com and http://www.shmeleff.com
Oflameron game author, game code developer, VB, Delphi, Assembler, Java, PHP programmer.
Game is conducted on a game field, which is optional size field (limited only to display size or convenience of programming). Since later on we’ll describe
the process of developing the game graphic and PDA-versions, we have chosen 8x5 for the game table.
Boxes have various meanings:
[-1,-5,-10,-15,-25] - deduct the indicated points from your account.
[1,5,10,15,25] - add the indicated points to your account.
[B] - deducts 200 points from your account.
[P] - adds 100 points to your account.
[T] - adds 500 points to your account.
[Z] - zeroes your account.
[X] - add additional marker. Deducts 500 point as pay.
[END] - ends the game.
The player can perform only 2 actions on the game field:
1 – put “marker” on any cell of the game field bottom line only once at the
game start (by clicking on the cell field).
2 – on putting the “marker”, the player can move it right or left along the game field bottom line by means of left and right arrows (keyboard button).
All cell nominal values to be “visited” by the player “marker” will be added to score. “Marker” is a cell highlighted in blue . To put a marker, click on any cell of the game field bottom line. “Marker” may be put only once. Then it can be moved many times.
As soon as the player has put the marker, game starts. Game is conducted for time. A linear color indicator counts time intervals, at which all lines of game field are moved one line down. At that, a cell with a losing value may fall on the player “marker”. The player goal is to move the marker along winning cells, increase the score as possible, and watch the “marker” not to be “caught” by the cell from above with a losing nominal value (e.g., |Z|- score zeroing).
Game is not as simple as it may seem. Its program has additional functions: acceleration and slowing down of
game, changing winning
cells frequency at the player large score. Game Oflameron may be realized in its static version, with no account of time. This version may have its benefits in the game process.
Game goal is to increase the score. Game (theoretically endless) may be stopped in the following situations: 1) the player “marker” falls to a cell with |End| nominal 2) the player
score becomes negative 3) game has time limitation. Such flexibility of game algorithm allows to create a great number of its versions – with accumulation of “lives”, introduction
of extra bonuses (cell nominal values), loading graphic design from the Internet, network versions for 2 players (static version) and paper version.
Create Project as Windows Application and Form1, its size being 264õ164. A Label1 – Label55 element is to be placed at the Form.
Where Label1 – Label6 is a Level column, Label 7 – Label 46 are nominal playing area cells, Label 47-48 present control footing information. A lower colour indicator is formed by
Label 49-Label 55.
Number arrays correspond to a Level element column and a playing area.
Array int level[] = new int[4]; //Level array
Game field
public int field5[] = new int[8]; // Array for the playing area for level 5 (upper line)
public int field4[] = new int[8]; // Array for the playing area for level 4
public int field3[] = new int[8]; // Array for the playing area for level 3
public int field2[] = new int[8]; // Array for the playing area for level 2
public int field1[] = new int[8]; // Array for the playing area for level 1
The game is started by a payer’s marker being placed in any cell of the bottom row (by clicking a mouse). The cell is highlighted in blue
:
and the nominal of the cell occupied is added to or subtracted from a player’s score. A lower colour linear indicator
is becoming shorter. Before the indicator disappears a player can move a game marker along the bottom row to the right or to the left off its current position by pressing |Right|
or |Left| keyboard arrows. Nominals of all cells that the player goes through are added to or subtracted from a player’s score. As soon as the linear colour indicator disappears a
player cannot move his marker for a moment, while nominals of all cells are shifted top down for one row. A playing area top row is filled with new values by a random-number
generator. At that the nominal of a cell that was shifted from above to the one where the marker is placed, is also added to the score, thus changing your score considerably.
After that a linear color indicator is restored and is getting shorter again, while a player can move the marker again.
The game score is tracked in an orange cell:
The game goes on until a player’s score equals to zero or the marker gets into a cell with an |End| nominal.
The game is not as simple as it may seem. It is very easy to move a marker to the cells |Z| (score mulling) or |End| (end of the game).
Here a programming process of a “text-only version” of a logical tabular game "Oflameron" is described. The cell nominals
are presented at the screen as symbols, while the cells are Label elements.
A version of the game can be easily made graphical if you place on the playing area the array of Picture Box
elements corresponding to each Label elements in “text” version and create in graphic editor images corresponding to the nominal values of cells. The next
edition of the programming manual will also describe in detail the process of a mobile phone game development.
Unlike versions in
Visual Basic and Delphi,
the version in JAVA has no multidimensional arrays. This is why several one-dimensional arrays (according to the lines in the game table) will correspond to
the game area cells.
public int field5[] = new int[8]; // Array for the playing area for level 5 (upper line)
public int field4[] = new int[8]; // Array for the playing area for level 4
public int field3[] = new int[8]; // Array for the playing area for level 3
public int field2[] = new int[8]; // Array for the playing area for level 2
public int field1[] = new int[8]; // Array for the playing area for level 1
Use two variable
public int NCounter = 0; //Score meter value
public int RndE; //Variable for random numbers
On the start of the program in Visual
Basic and Delphi versions the fixed initial value of the score was set. Let us add simple lines here for the program to start with a random initial
score of a player - from 0 to 400
RndE = (int)(400*Math.random()); //Generates a random number = from 0 to 400 for initial SCORE value
NCounter = RndE; //Initial score value
Let us fill in game array cells field1[], field2[], field3[], field4[], field5[] with values from the generator of random numbers (the code in given in bold).
public Form1() { initForm(); RndE = (int)(400*Math.random()); //Generates a random number = from 0 to 400
NCounter = RndE; //Initial score value
// Let us fill cell arrays filed1 - filed5 with random number generator values
for (int i=0; i<8; i++) { field1[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field2[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field3[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field4[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field5[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
} fdraw(); //Draw cells values
}
Draw the values of the arrays field1 - field5 in the cells to control filling of arrays
public void fdraw() { //Draw the values of the arrays field1 - field5 in the cells
//to control filling of arrays
cs = cs + field1[1]; //Let us check on of the array elements. It can be any element.
label8.setText(cs); // Let us write its value in Label8 to have a look
}
I. e., the array sell shall be just written down in the text of the form cell. this way may be not the best but it provides for the opportunity to check the work of already written procedures with minimum efforts.
A complete JAVA project of this game development stage is in the file
Now it is necessary to create a procedure to analyze field1 – field5 arrays cell content, and depending on their values, to form a required symbol value (nominal) of a playing area cell and
specify a required symbol and cell background color.
Now, overwrite Fdraw() procedure in another one. Create another Label56 to temporarily store a generated cell nominal and its colour attributes (highlighted at the figure).
Let us add the used colors to the variable declaration (bold):
public int level[] = new int[4]; //Array for Level
public int field5[] = new int[8]; //Array for the playing area for level 5 (upper line)
public int field4[] = new int[8]; //Array for the playing area for level 4
public int field3[] = new int[8]; //Array for the playing area for level 3
public int field2[] = new int[8]; //Array for the playing area for level 2
public int field1[] = new int[8]; //Array for the playing area for level 1
public int NCounter = 0; //Score meter value
public int RndE; //Variable for random numbers
public int i;
public String cs = ""; //Line for work with Label
Color black = new Color(0x000000); //Color of number cell symbols, cells "T", "P" and "B"
Color white = new Color(0xffffff); //Color of symbols of cells "Z" and "End"
Color plus1 = new Color(0xC0FFFF); //Color of the background for number cells
Color plusT = new Color(0xA3C9A9); //Color of the background for winning cells “T”
Color plusP = new Color(0xFFC0C0); //Color of the background for winning cells "P"
Color plusB = new Color(0x00FFFF); //Color of the background for cells "B"
Color plusZ = new Color(0x000000); //Color of the background for cells "Z"
Color plusEnd = new Color(0x0000FF); //Color of the background for cells "End"
Color dg; //For background color values communication
Color dk; //For symbol color values communication
The procedure of calculation of the nominal value of game area cell and its color attributes (cell background color and font color) shall be calculated basing on array element value. Example for processing of
elements of the array field1[] corresponding to cells of the bottom line of the game area - see code
Now let us add copying of the value and attributes of “temporary” cell Label56 in the specific cells of the playing area. Let us consider the example for copying of
elements of one array field1[] to the bottom line of the playing area with determination of cells nominal values and their color attributes
public void Field_Fill() { //Procedure of copying of the nominal values and the attributes from Label56
//to the playing area cells Label7 - Label14
for (int i=0; i<8; i++) { Color_Chars(i); //Write the nominal values and the attributes of the cell down to Label56
cs = label56.getText(); //Write the nominal value from Label56 to the variable cs
if (i == 0) label7.setText(cs); if (i == 1) label8.setText(cs); if (i == 2) label9.setText(cs); if (i == 3) label10.setText(cs); if (i == 4) label11.setText(cs); if (i == 5) label12.setText(cs); if (i == 6) label13.setText(cs); if (i == 7) label14.setText(cs);
dg = label56.getBackColor(); //Write BackColor from Label56 to the variable dg
if (i == 0) label7.setBackColor(dg); if (i == 1) label8.setBackColor(dg); if (i == 2) label9.setBackColor(dg); if (i == 3) label10.setBackColor(dg); if (i == 4) label11.setBackColor(dg); if (i == 5) label12.setBackColor(dg); if (i == 6) label13.setBackColor(dg); if (i == 7) label14.setBackColor(dg);
dk = label56.getForeColor(); //Write ForeColor from Label56 to the variable dk
if (i == 0) label7.setForeColor(dk); if (i == 1) label8.setForeColor(dk); if (i == 2) label9.setForeColor(dk); if (i == 3) label10.setForeColor(dk); if (i == 4) label11.setForeColor(dk); if (i == 5) label12.setForeColor(dk); if (i == 6) label13.setForeColor(dk); if (i == 7) label14.setForeColor(dk); cs = ""; } }
It is possible not to use the "temporary" cell Label56 at all (see the file
but use the set of variables.
This way has be chosen only for the sake of compatibility of manuals for Visual Basic, Delphi è Java.
A complete JAVA project of this game development stage is in the file vjp4.zip
The above mentioned procedures would fill with values only the bottom line of the playing area. Let us write procedures to fill in all lines of the playing area.
The procedure for work with the array field2[] (the second line from the bottom of the playing area) would be similar to the procedure public void Color_Chars(int i) for field1[].
See procedure code - in file color_chars2.txt
Procedures would be identical for all other arrays.
The procedure public void Field_Fill() shall be modified accordingly - see procedure code in file public_void_field_fill.txt
Such variant of filling a playing area with START values (at the start of the game) cannot be considered as an optimal one. However it is very demonstrable for algorithm understanding.
The procedure causing initial filling of arrays and cells of the paying area with values would be as follows
public Form1() { initForm(); RndE = (int)(400*Math.random()); //Generates a random number = from 0 to 400
NCounter = RndE; //Initial score value
// Let us fill cell arrays filed1 - filed5 with random number generator values
for (int i=0; i<8; i++) { field1[i] = (int)(19*Math.random()); field2[i] = (int)(19*Math.random()); field3[i] = (int)(19*Math.random()); field4[i] = (int)(19*Math.random()); field5[i] = (int)(19*Math.random()); } //fdraw(); //Draw cells values – For adjustment
Field_Fill(); //Fill the playing area cells with values
}
A complete JAVA project of this game development stage is in the file vjp5.zip
To observe how it all works let us add one more procedure - private void Form1_click(Object source, Event e) (for some time. For debug):
private void Form1_click(Object source, Event e) { // Let us fill cell arrays filed1 - filed5 with random number generator values
for (int i=0; i<8; i++) { field1[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field2[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field3[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field4[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
field5[i] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
} Field_Fill(); }
Now, clicking the mouse to Form1 field (only forms) you can see if the cell nominals values and their colour attributes are changed correctly.
A complete JAVA project of this game development stage is in the file
private void Form1_click(Object source, Event e) procedure can be deleted.
Now develop public void Num_Move() procedure to overwrite playing area cells’ values and color attributes in a row-wise top-down way and to fill a playing area top row
with new values (i.e., new cell values are input in the game from the top row). The procedure contains copying operators. You can develop a more optimal copying algorithm.
public void Num_Move() {//in case of cells shift downwards
//Procedure for rewriting of the nominal values and the attributes
for (int i=0; i<8; i++) { field1[i] = field2[i]; field2[i] = field3[i]; field3[i] = field4[i]; field4[i] = field5[i]; } // Let us fill cell array filed5 with new random number generator values
Up_Str_App(); Field_Fill(); //Draw cells
}
Where
public void Up_Str_App() { // Let us fill cell array filed5 with new random number generator values
for (int j=0; j<8; j++) { field5[j] = (int)(19*Math.random()); //Generates a random number = from 0 to 19
} }
fills the upper line (array field5[j]) with new values.
To see how top-down copying values and cell attributes works, create a temporary click handling procedure private void Form1_click(Object source, Event e) at Form1:
Save_Color() procedure is used to restore cell color when moving the marker horizontally (along the bottom row of the playing area).
Create a markersave variable to memorize an array ELEMENT NUMBERsavecolor(j), the variable corresponding to the marker cell (i.e. the cell, in which the marker has been placed).
The possibility of placing a marker on one cell only can be checked. Now it is necessary to provide a marker being saved (in terms of cell turning blue) when shifting cells top – down.
The procedure public void Marker_Reset() of marker color restoring after playing area cells shifting top-down:
public void Marker_Reset() { // color restoring after playing area cells shifting top-down
if (markersave == 0) label7.setBackColor(blue); //Restore the blue color of the cell
if (markersave == 1) label8.setBackColor(blue); if (markersave == 2) label9.setBackColor(blue); if (markersave == 3) label10.setBackColor(blue); if (markersave == 4) label11.setBackColor(blue); if (markersave == 5) label12.setBackColor(blue); if (markersave == 6) label13.setBackColor(blue); if (markersave == 7) label14.setBackColor(blue); }
Now activate Marker_Reset() procedure to operate in a top-down shift procedure. In the bottom of private void Form1_click(Object source, Event e) procedure insert the following code (marked as red):
private void Form1_click(Object source, Event e) { //Shift the cells downwards and fill in the upper line
Num_Move(); Marker_Reset(); //Restore the color of the marked cell
}
A complete JAVA project of this game development stage is in the file vjp8.zip
Write a score count procedure when placing a marker in the beginning of the game - see code in file public_void_Marker_Count.txt
Now attach a score count procedure in each of click procedures for bottom row cells:
private void label7_click(Object source, Event e) { if (firstset == 0) Save_Color(); //Save the colors of the bottom line cells
if (firstset == 0) label7.setBackColor(blue); if (firstset == 0) markersave = 0; //The marker in the cell corresponding to savecolor(0)
if (firstset == 0) firstset = 1; //Do not process any more
Marker_Count(); }
For other bottom row cells the procedure is exactly the same.
Now write a score counting procedure public void Dn_Count() at cells’ shifting top-down. Procedure looks like extremely simple:
public void Dn_Count() { n = 0; //Temporarily to resolve work of procedure Set_Marker_Count
Marker_Count(); //Procedure of calculation of count after installation of a marker
}
To demonstrate the procedure operation, insert the procedure call in a top-down cell row shifting procedure private void Form1_click(Object source, Event e)
private void Form1_click(Object source, Event e) { //Shift the cells downwards and fill in the upper lin
Num_Move(); Marker_Reset(); //Restore the color of the marked cell
Dn_Count(); //To count up glasses at moving lines from above - downwards
}
A complete JAVA project of this game development stage is in the file
Create a procedure of linear colour indicator operation private void timer1_timer(Object source, Event e). Add Timer1 to the form and enable it at once
private void timer1_timer(Object source, Event e) { IndLent = IndLent + 1; //Switch off one more element of color indicator
if (IndLent == 9) timer1.setInterval(500); //Restore the interval of the indicator operation
if (IndLent == 9) IndLent = 0; //Full size of the indicator
Print_Ind(); //Let us draw the indicator
}
The procedure of the indicator drawing public void Print_Ind()
public void Print_Ind() { // Draw indicator
if (IndLent == 1) label55.setVisible(false); if (IndLent == 2) label54.setVisible(false); if (IndLent == 3) label53.setVisible(false); if (IndLent == 4) label52.setVisible(false); if (IndLent == 5) label51.setVisible(false); if (IndLent == 6) label50.setVisible(false); if (IndLent == 7) label49.setVisible(false); if (IndLent == 7) timer1.setInterval(100); //Decrease the interval for the indicator to restore quicker
if (IndLent == 0) { label55.setVisible(true); //Draw all the elements of the indicator
label54.setVisible(true); //Draw all the elements of the indicator
label53.setVisible(true); //Draw all the elements of the indicator
label52.setVisible(true); //Draw all the elements of the indicator
label51.setVisible(true); //Draw all the elements of the indicator
label50.setVisible(true); //Draw all the elements of the indicator
label49.setVisible(true); //Draw all the elements of the indicator
} }
A complete JAVA project of this game development stage is in the file
Now you should disable the timer and enable it after the market has been placed in a playing area bottom row.
Insert timer enabling in a marker placing procedure in a playing area bottom row (to be more exact – in a score counting
procedure after marker placing Marker_Count(), since the procedure is common for all cells of a lower level). At the end of the procedure
Marker_Count() there is a line:
timer1.setEnabled(true); // 'The timer is ON since the marker is put
When a linear color indicator disappears completely, it is necessary to shift down all rows with playing area cells (private void Form1_click(Object source, Event e) procedure) to the marker, count scores and fill in a playing area top row with new nominal values.
Timer1 operation procedure looks like the following way:
private void timer1_timer(Object source, Event e) { IndLent = IndLent + 1; //Switch off one more element of color indicator
if (IndLent == 9) timer1.setInterval(500); //Restore the interval of the indicator operation
if (IndLent == 9) Form1_click(this, e); //Shift the cells downwards
if (IndLent == 9) IndLent = 0; //Full size of the indicator Print_Ind();//Let us draw the indicator }
(In this listing repeated click on the field of Form1 is not prohibited. To amend this situation you shall create another procedure that does not process the events of Form 1. And then to copy the same code that in private void Form1_click() ).
Now it is necessary to write handling of pressing down the key and marker’s moving to the [right] / [left]
The fact is that processing of keyboard button strokes is the main difference of versions of programming manuals in different languages - Visual Basic, Delphi and JAVA
Let us see Events for Form1 (see [1]) and select the event keyDown (see [2]). Double click [2] and you will get the perform of the procedure:
Now you can see how it works. The code of the respective button will be written in the field label1. For the buttons «Right Arrow” and “Left Arrow” codes 37 and 39 are applicable.
A complete JAVA project of this game development stage is in the file vjp11.zip
Write more of the procedure to have the code necessary for work.
The variable markersave shows unambiguously in which cell of the bottom line of the playing area the marker is positioned. Let us write the procedure for the marker movements to right-left in the bottom line:
private void Form1_keyDown(Object source, KeyEvent e) { keyCode = e.getKeyCode(); if (keyCode == 37) Move_Point_Left(); //Move left
if (keyCode == 39) Move_Point_Right(); //Move right
cs = cs + keyCode; label1.setText(cs); }
(Part of the code is marked red – the code has been taken unchanged from the procedures private void label7_click(Object source, Event e) - private void label14_click(Object source, Event e) - see code in file keyboard.txt
Now it is necessary to create only one procedure public void Level_Count() – a procedure of shifting the numbering of current gaming level |Level|:
Let us engage the procedure of layers shift in the work, e.g.:
public void Dn_Count() { //Counting procedure
n = 0; //Allow temporarily Set_Marker_Count
Marker_Count(); //Calculate score on the marker placement
Level_Count(); // Procedure of shifting the numbering of current gaming Level
}
A complete JAVA project of this game development stage is in the file vjp12.zip
Now, this is a demonstrative moment: all software-realization procedures of the game Oflameron are ready. In other words, you have created a completely operable logical gaming program in JAVA. It is still early to introduce it as a commercial product as it still contains many faults.
Next programming manual will be on transformation of JAVA version of the game into J2ME.
The following manuals are to be published:
– technology of creation Adware - programs in Visual Basic, Delphi è C++
- development of programs for foreign language studies (for programmers in VB, Delphi, Java è PHP)