<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>
#game{
width: 200px;
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="game">
<?php
function display() {
$items = array(
'<a href="?item=rock"> <br/><img src= "img/rock.png" width="150" height="150" alt="rock"/></a>',
'<a href="?item=scissors"> <br/><img src="img/scissors.png" width="150" height="150" alt="scissors"></a>',
'<a href="?item=paper"> <br/><img src="img/paper.jpg" width="150" height="150" alt="paper"></a>'
);
foreach ($items as $item => $value):
echo $value;
endforeach;
if (isset($_GET['item']) == true):
//rock paper and scissors types
$item = array('rock', 'paper', 'scissors');
//users choice
$user_Choice = $_GET['item'];
if ($user_Choice == "rock"):
$user_Choice = $items[0];
endif;
if ($user_Choice == "scissors"):
$user_Choice = $items[1];
endif;
if ($user_Choice == "paper"):
$user_Choice = $items[2];
endif;
//random computer generated choice
$random_Num = rand(0, 2);
$computer_Choice = $items[$random_Num];
echo $user_Choice;
echo $computer_Choice;
if ($user_Choice == $items[0] && $computer_Choice == $items[0]):
echo "tie";
endif;
if ($user_Choice == $items[0] && $computer_Choice == $items[2]):
echo "you lose";
endif;
if ($user_Choice == $items[0] && $computer_Choice == $items[1]):
echo "you win";
endif;
if ($user_Choice == $items[2] && $computer_Choice == $items[0]):
echo "you win";
endif;
if ($user_Choice == $items[2] && $computer_Choice == $items[2]):
echo "you tie";
endif;
if ($user_Choice == $items[2] && $computer_Choice == $items[1]):
echo "you lose";
endif;
if ($user_Choice == $items[1] && $computer_Choice == $items[0]):
echo "you lose";
endif;
if ($user_Choice == $items[1] && $computer_Choice == $items[2]):
echo "you win";
endif;
if ($user_Choice == $items[1] && $computer_Choice == $items[1]):
echo "you tie";
endif;
endif;
}
display();
?>
</div>
</body>
</html>
嘿家伙!我在这段代码中遇到了一点问题,这是一个石头剪刀布,我在做一个有趣的东西,然后遇到了一个问题。实际上有几个问题。我首先想知道如何将css合并到php中,我希望能够将我的图片水平而不是垂直地放在彼此旁边。这段代码的第二个问题是,当我点击石头剪刀布时,它会显示用户选择,计算机选择"平局输赢声明",但原始图片留在屏幕上。我如何改变我的代码,以便当我点击石头布或剪刀,原来的3张图片是隐藏的,只剩下的图像是computer_choice和user_choice…谢谢你的阅读,如果你帮助我,谢谢你!
关于css,你可能没有注意到,但它实际上已经在你的代码中,它是由head
部分的style
标签所包含的部分。也就是说,您不需要css来重新排列图片。注意图片数组中的<br/>
标签,它们基本上是html中的新行字符。只要从第二张和第三张图片中删除标签,就可以了。
$items = array(
'<a href="?item=rock"> <br/><img src= "img/rock.png" width="150" height="150" alt="rock"/></a>',
'<a href="?item=scissors"> <img src="img/scissors.png" width="150" height="150" alt="scissors"></a>',
'<a href="?item=paper"> <img src="img/paper.jpg" width="150" height="150" alt="paper"></a>'
);
第二个问题,隐藏图片。有许多不同的方法可以做到这一点,最常见的方法是使用javascript。然而,考虑到你的代码结构,似乎最简单的方法是在打印代码中加上一个检查符。
if (isset($_GET['item']) != true):
foreach ($items as $item => $value):
echo $value;
endforeach;
endif;