PHP开发利器-PRADO 1.6范例
ado
第三章 范例:the Hangman Game
这一章将会讲一下开发一个基于Web的Hangman Game(猜词游戏)。这里面会用到一些PRADO的组件。这个游戏说明了显示状态使PRADO页面具有很强的交互性。
这个游戏只包括了一个页面来实现以下的功能:当用户第一次访问的时候,允许用户选择三种不同难度来开始这个游戏,不同的难度对应着不同的可以猜错的次数。游戏开始后,页面的上半部分会显示要猜得单词,没有猜中的字母用下划线显示。页面的下半部分列出了26个字母,用户可以点击对应字母来选择要猜的字母。如果用户放弃游戏或者错误次数超过了难度限制,会显示失败信息;如果猜对了,会显示成功信息。任何时候用户都可以重新开始游戏。
范例中会使用的 PRADO 组件包括:
- TRadioButton: 显示一个单选框。
- TPanel: 显示 元素。
- TLabel: 显示文本。
- TButton: 显示一个提交按钮。
- TLinkButton: 显示一个提交操作的超链接。
- TForm: 显示 元素。
这些定义好的组件通过属性和事件已经封装好了很多功能。比如,可以设置TPanel 的Visible
属性来控制整个元素是不是可见。可以给TButton 组件的OnClick 事件指定一个响应函数,这样当用户点击按钮的时候就会自动调用响应函数。请参考PRADO API文档中关于这些组件更详细的信息。游戏中的一些数据在游戏过程中需要一值保持着的。因为这里只用了一个页面,所以可以使用显示状态而不需要使用session来保存这些数据。下面这些数据需要保存在显示状态中:
- Word:要猜得单词。
- GuessWord: 正在猜的单词(没有猜中用下划线显示)。
- Level:游戏的难度级别。
- Misses: 猜错的次数。
他们都被定义在页面的属性中(当然也可以不这么做)。
我们需要创建6个文件。假定这个应用放在Web服务器的根目录下,这些文件是:
- hangman.php : 应用的主入口;
- WEB-INF/hangman.spec: 应用的配置文件;
- WEB-INF/hangman/HomePage.php: 页面类文件;
- WEB-INF/hangman/HomePage.spec: 页面规格文件;
- WEB-INF/hangman/HomePage.tpl: 页面模板文件;
- WEB-INF/data/hangman.txt: 包含要猜得单词的文本文件。
下面主要讲一下HomePage 类的三个文件,其他文件和"Hello,world!"例子是类似的。
3.1 HomePage.tpl
首先来说一些页面模板文件。这个过程也是PRADO应用开发的过程。首先在页面模板中组合PRADO组件,然后就可以测试一下这个页面的显示样子,最后编写页面类文件中需要的代码。这是一个典型的RAD(快速应用开发)的开发过程。
这个模板文件的位置是 examples/hangman/HomePage.tpl .
可以看到这个模板被分成4个组件startPanel, guessPanel, winPanel, 和losePanel ;他们将根据游戏的状态来决定是否可视。
在 startPanel中,使用了三个 TRadioButton 组件让用户可以选择游戏的难度。TButton 的OnClick 事件指定了一个响应函数onSelectLevel ,当用户选择难度后点击了按钮就会被触发。如果用户没有选择难度,TLabel 组件会显示一个错误提示信息。
在 guessPanel中,三个表达式标签用于显示页面的 GuessWord, Misses,和 Level 属性。 同时还有一系列的TLinkButton 组件来显示字母A到Z,它们用于让用户点击选择猜哪一个字母。不管是猜对了还是猜错了,以后这个字母就不允许再被点击了。注意,所有的这些TLinkButton 组件都有同样的一个事件OnClick 的响应函数onGuessWord 。
winPanel 和 losePanel 会显示成功和错误的信息。如果用户点击了面板上的TLinkButton ,OnClick 事件的响应函数onStartAgain 会被触发。
3.2 HomePage.spec
这个页面规格文件在 examples/hangman/HomePage.spec。它很简单,就是定义了4个页面的属性: Misses, Level, GuessWord 和 Word .
3.3 HomePage.php
这个页面类文件在 examples/hangman/HomePage.php .
页面类 HomePage 继承了框架提供的 TPage 类。
getter和setter函数用来定义页面属性。注意: getViewState() 和 setViewState() 函数被调用了,用来返回和保持属性的数据。这两个函数在TPage 的父类TComponent 中实现。它们是显示状态机制的主要代码。保存在显示状态中的数据在页面的postback中是一直保存着的。
接下来实现了4个事件响应函数: onSelectLevel(), onGuessWord(), onGiveUp() 和
onStartAgain()。它们对应着在页面模板中指定的OnClick 事件响应函数。四个函数的具体内容就不再解释了,一看就明白。现在你可以试试这个游戏,访问 /examples/hangman.php">URL:http://
/examples/hangman.php 。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode
