所以我对一个我想为一小群在线朋友开发的游戏有一个想法,而且我实际上非常喜欢html和css以及一些javascript / jquery。我想要的建议是,基本框架的这个样本是否足够功能,允许我继续这个想法,或者我是否应该广泛地改变一些东西。我是PHP的新手,但我知道我需要一种服务器端语言来使游戏更具动态性,并防止他们仔细阅读代码以获得答案......
基于文本的游戏是否可行,使用这个简单的框架作为起点,还是应该使用完全不同的东西?
编辑:游戏是渐进式的,因此一旦到达下一个级别,他们就不需要访问之前的级别。检查结果将通过Ajax / Jquery传输到模拟控制台,以便他们实时查看。
<?php
session_start();
//Text sent through jquery and ajax, checked for a passcode to access a higher level directly since I'm using sessions
switch($_POST['text']){
case a:
$_SESSION['auth_level'] = 2;
break;
case b:
$_SESSION['auth_level'] = 3;
break;
case c:
$_SESSION['auth_level'] = 4;
break;
case d:
$_SESSION['auth_level'] = 5;
break;
case e:
$_SESSION['auth_level'] = 6;
break;
default:
$_SESSION['auth_level'] = 1;
break;
};
//Giving them conditional access to commands based on their authorization level
if($_SESSION['auth_level'] = 1) {
switch($_POST['text']){
case a:
echo "";
break;
case b:
echo "";
break;
case c:
echo "";
break;
case d:
echo "";
break;
case e:
echo "";
break;
case f:
echo "You are now authorized for level 2!";
$_SESSION['auth_level'] = 2;
break;
}
elseif($_SESSION['auth_level'] = 2) {
switch($_POST['text']){
case a:
echo "";
break;
case b:
echo "";
break;
case c:
echo "";
break;
case d:
echo "";
break;
case e:
echo "";
break;
case f:
echo "You are now authorized for level 3!";
$_SESSION['auth_level'] = 3;
break;
}
elseif($_SESSION['auth_level'] = 3) {
switch($_POST['text']){
case a:
echo "";
break;
case b:
echo "";
break;
case c:
echo "";
break;
case d:
echo "";
break;
case e:
echo "";
break;
case f:
echo "You are now authorized for level 4!";
$_SESSION['auth_level'] = 4;
break;
}
//For anything not designated as a command
else {
echo $_POST['text'] . " is not a valid command or access code"
?>
答案 0 :(得分:0)
评论有点长,所以我会尽力帮忙。
理想情况下,如果您尝试实施基于文本的游戏,用户输入命令或输入关卡代码,您应该考虑将它们分成两个不同的字段 - 类似于$_POST['level_code']
,这与他们的'命令'。
session_start();
// If a user doesn't have an auth_level, give them the default
if(!isset($_SESSION['auth_level'])) {
$_SESSION['auth_level'] = 1;
}
// If a user posts a level code, do something with it
if(isset($_POST['level_code']) {
switch ($_POST['level_code']) {
case 'levelcode_a':
// Give your user feedback as well if you want
$_SESSION['auth_level'] = 1;
break;
case 'levelcode_b':
$_SESSION['auth_level'] = 2;
break;
// etc, etc
case 'levelcode_c':
default:
// User didn't send a level code so don't do anything
}
}
if (!isset($_POST['text'])) {
exit("No additional codes were given");
}
// Then do your application logic
switch ($_SESSION['auth_level']) {
case 1:
switch ($_POST['text']) {
case 'a':
// Some game activity
break;
case 'b':
// Some game activity
break;
}
// Enable level 1 actions
break;
case 2:
// Enable level 2 actions
break;
default:
// Don't do anything
}
因此,如果您只想创建一个处理所有游戏活动的单一脚本,那么它本质上就是交换机内的一个开关。
还有其他模式,但这相对容易保持头脑。我很可能将辅助switch
传递给一个函数,这样你可以构建类似do_level_1_action(user_input)
的东西(如果它们是1级等等),它可能有助于为你分离逻辑。 / p>