Chapter 3 Example: theHangmanGame will show you how to develop a Web-based HangmanGame ). Some PRADO components are used here. This game shows that the display status makes the PRADO page highly interactive. This Game only includes one page to implement the following functions: When a user visits the Game for the first time, the user can select chapter 3 Example: the Hangman Game
This chapter describes how to develop a Web-based Hangman Game ). Some PRADO components are used here. This game shows that the display status makes the PRADO page highly interactive.
This game only includes one page to implement the following features: When users access the game for the first time, they are allowed to select three different levels of difficulty to start the game, different levels of difficulty correspond to different number of wrong guesses. After the game starts, the upper part of the page displays the words to be guessed, and the letters to be guessed are underlined. The lower part of the page lists 26 letters. you can click the corresponding letter to select the expected letter. If the user gives up the game or the number of errors exceeds the difficulty limit, the failure information is displayed. If you guessed it, the success information is displayed. Users can start the game again at any time.
The PRADO components used in the example include:
- TRadioButton: displays a single worker.
- TPanel: Display
Element.
- TLabel: displays text.
- TButton: displays a submit button.
- TLinkButton: displays a hyperlink for submitting an operation.
- TForm: Display Element.
These defined components have encapsulated many functions through attributes and events. For example, you can setTPanelOfVisible
Attribute to control the entire
Whether the element is visible. You canTButtonComponentOnClickThe event specifies a response function. when you click a button, the response function is automatically called. For more information about these components, see The prado api documentation.
Some data in the game needs to be maintained in the game process. Because only one page is used here, you can use the display status instead of the session to save the data. The following data needs to be saved in the display status:
- Word: to guess the Word.
- GuessWord: the word being guessed (no guesses are displayed with an underscore ).
- Level: the difficulty Level of the game.
- Misses: number of guesses.
They are all defined in the page attributes (or not ).
We need to create 6 files. Assume that the application is placed in the root directory of the Web server. the files are:
- Hangman. php: Main entry of the application;
- WEB-INF/hangman. spec: Application configuration file;
- WEB-INF/hangman/HomePage. php: Page files;
- WEB-INF/hangman/HomePage. spec: Page specification file;
- WEB-INF/hangman/HomePage. tpl: Page template file;
- WEB-INF/data/hangman.txt: Text file containing the words to guess.
The following describes howHomePageClass, other files and "Hello, world! "The example is similar.
3.1HomePage. tpl
First, some page template files. This process is also the process of PRADO application development. First, combine the PRADO component in the page template, then you can test the display of this page, and finally write the code required in the page class file. This is a typical RAD (rapid application development) development process.
The location of this template file isExamples/hangman/HomePage. tpl.
The template is divided into four components.StartPanel,GuessPanel,WinPanel, AndLosePanelThey will determine whether the game is visible based on the game status.
InStartPanel, ThreeTRadioButtonThe component allows users to choose the difficulty of the game.TButtonOfOnClickThe event specifies a response function.OnSelectLevelWhen the user selects the difficulty, clicking the button will be triggered. If the user does not select the difficulty,TLabelThe component displays an error message.
InGuessPanel, Three expression labels are used to displayGuessWord,Misses, AndLevelAttribute. There are also a seriesTLinkButtonComponent to display the letters A to Z. They are used for the user to click to select which letter to guess. Whether you have guessed the letter or guessed it wrong, you will not be allowed to click it again in the future. Note that all of theseTLinkButtonComponents all have the same eventOnClickResponse FunctionsOnGuessWord.
WinPanelAndLosePanelThe success and error messages are displayed. If you clickTLinkButton,OnClickEvent Response functionsOnStartAgainWill be triggered.
3.2HomePage. spec
The page specification file is inExamples/hangman/HomePage. spec. It is very simple, that is, it defines the attributes of four pages:Misses,Level,GuessWordAndWord.
3.3HomePage. php
This page class fileExamples/hangman/HomePage. php.
Page typeHomePageInheritsTPageClass.
Getter and setter functions are used to define page attributes. Note:GetViewState ()AndSetViewState ()The function is called to return and maintain attribute data. These two functions areTPageParent classTComponent. They are the main code for displaying the status mechanism. The data saved in the displayed status is always saved in the page postback.
The following four event response functions are implemented:OnSelectLevel (),OnGuessWord (),OnGiveUp ()And
OnStartAgain (). They correspond toOnClickEvent response function. The specific content of the four functions is no longer explained.
Now you can try this game and access/examples/hangman. php ">URL:Http :// /Examples/hangman. php .