Hallo zusammen,
ich würde gern ein 2-Spieler-Backgammon-Spiel mit PHP/MySQL entwickeln.
Beim Klicken auf ein <input type='image' src='...'> werden die x- und y- Koordinaten übertragen.
Das brachte mich auf folgende Idee:
Ich generiere als Image-Source ein PNG, dass per GD-Funktionen erstellt wird.
Folgender Code demonstriert die Funktionsweise mit einem Spielstein.
PHP-Code:
<?php
// index.php
if($_POST['kord_x']) // Wo wurde geklickt?
{
$x = $_POST['kord_x'];
$y = $_POST['kord_y'];
if($y <= 170)
{
for($i = 0; $i < 210; $i += 35)
{
if($x >= 15 + $i && $x <= 45 + $i)
{
$pos = 12 - $i / 35;
}
if($x >= 255 + $i && $x <= 285 + $i)
{
$pos = 6 - $i / 35;
}
}
}
elseif($y >= 230)
{
for($i = 0; $i < 210; $i += 35)
{
if($x >= 15 + $i && $x <= 45 + $i)
{
$pos = 13 + $i / 35;
}
if($x >= 255 + $i && $x <= 285 + $i)
{
$pos = 19 + $i / 35;
}
}
}
}
echo "<font size='2' face='Verdana'>Zunge Nr.: ".$pos."</font>";
echo "<form action='index.php'method='post'>";
echo "<input type='image' src='brett.php?pos=".$pos."' name='kord'>";
echo "</form>";
?>
<?php
// Das Spielbrett:
// brett.php
// Spielbrett zeichnen
$brett_breite = 480;
$brett_hoehe = 400;
$im = imagecreate($brett_breite,$brett_hoehe);
$brett_farbe = imagecolorallocate($im, 125, 125, 125);
for($i = 0; $i < 2; $i++)
{
for($j = 0; $j < 12; $j++)
{
$abstand_zunge_x = 35;
$extra_abstand = 0;
$j > 5 ? $extra_abstand = 30 : "";
$i == 0 ? $mess_p = array(15 + $extra_abstand + $j * $abstand_zunge_x,10, 45 + $extra_abstand + $j * $abstand_zunge_x,10, 30 + $extra_abstand + $j * $abstand_zunge_x,160) : $mess_p = array(30 + $extra_abstand + $j * $abstand_zunge_x,240, 45 + $extra_abstand + $j * $abstand_zunge_x,390, 15 + $extra_abstand + $j * $abstand_zunge_x,390);
if($j % 2 == $i)
{
$hell = imagecolorallocate($im, 200, 200, 200);
imagefilledpolygon($im, $mess_p, 3 , $hell);
}
else
{
$dunkel = imagecolorallocate($im, 100, 100, 100);
imagefilledpolygon($im, $mess_p, 3 , $dunkel);
}
}
}
// Spielstein positionieren
if($_GET['pos'] > 0)
{
if($_GET['pos'] <= 12)
{
$pos = (12 - $_GET['pos']) * 35;
$pos_y = 0;
}
if($_GET['pos'] <= 6)
{
$pos += 30;
$pos_y = 0;
}
if($_GET['pos'] > 12)
{
$pos = ($_GET['pos'] - 13) * 35;
$pos_y = 350;
}
if($_GET['pos'] >= 19)
{
$pos += 30;
}
$stein_hell = imagecolorallocate($im, 255, 255, 255);
imagearc($im, 30 + $pos, 25 + $pos_y, 30, 30, 0, 360, $stein_hell);
imagefill($im, 30 + $pos, 25 + $pos_y, $stein_hell);
}
imagepng($im);
imagedestroy($im);
?>
Was haltet Ihr von diesem Ansatz?
Lauf ich da in eine Sackgasse?
Bin für jeden Tipp dankbar.
Grüsse
chris17