Hallo,
heut schreib ich mal wieder ein kleines Tutorial..
Entstanden ist es aus langeweile, hab auch ned lang fürs Script gebraucht..
Es geht um eine Schatzsuche. Ein Schatz wurde im Sand versteckt und die User deiner Seite können versuchen ihn zu finden ;)
Dazu brauchen wir erstmal eine Grafik von Sand. zB die hier: http://commons.wikimedia.org/wiki/File: Sand.jpg
Die Grafik würd ich allerdings noch verkleinern, zB auf 500x750 pixel
Jetzt zum Script. Ich hab das ganze schön als Klasse mit Scleifchen und Konfetti verpackt..
Daneben stehen Kommentare, das ganze sollte selbsterklärend sein. Falls ihr doch noch Fragen habt, könnt ihr sie mir natürlich gerne stellen, ich reis euch nich den Kopf ab.. Vielleicht :P
Hier das Script:
<?php class Schatzsuche { public $x = null; public $y = null; public $found = false; protected $schatzWidth = 0; // Breite des Schatzes in Pixeln protected $schatzHeight = 0; // Hoehe des Schatzes in Pixeln protected $schatzX = 0; // Koordinaten, wo der Schatz versteckt ist protected $schatzY = 0; // genauer gesagt die linke obere Ecke des Schatzes public function __construct ($schatzX, $schatzY, $schatzWidth, $schatzHeight) { $this->schatzX = $schatzX; $this->schatzY = $schatzY; $this->schatzWidth = $schatzWidth; $this->schatzHeight = $schatzHeight; } public function search ($x = null, $y = null) { if (($x === null or $y === null) and !empty($_POST['sand_x']) and !empty($_POST['sand_y'])) { $x = intval($_POST['sand_x']); $y = intval($_POST['sand_y']); } if ($x === null or $y === null) return false; // abbrechen, wenn keine koordinaten uebergeben $this->x = $x; $this->y = $y; if ($this->x >= $this->schatzX and $this->x <= ($this->schatzX + $this->schatzWidth)) { if ($this->y >= $this->schatzY and $this->y <= ($this->schatzY + $this->schatzHeight)) { $this->found = true; } } } } // Parameter: Koordinaten wo der Schatz versteckt ist, Breite des Schatzes in Pixeln, Hoehe des Schatzes in Pixeln $schatzsuche = new Schatzsuche(50, 200, 20, 20); $schatzsuche->search();?>
<!DOCTYPE html>
<html>
<head>
<title>Schatzsuche</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Schatzsuche</h1>
<p>Klick einfach in den Sand um nachzuschaun, ob der Schatz sich da befindet..</p>
<?php if ($schatzsuche->x !== null and $schatzsuche->y !== null and $schatzsuche->found) { ?>
<p>Du hast den Schatz gefunden!</p>
<?php } else if ($schatzsuche->x !== null and $schatzsuche->y !== null) { ?>
<p>Leider war er da nich.. Nich aufgeben ;)</p>
<?php } ?>
<div class="schatzsuche">
<form method="post" action="index.php">
<input type="image" name="sand" src="sand.png" alt="Sand" />
</form>
</div>
</body>
</html>
Alles anzeigen
Das ganze kommt am besten untereinander in eine Datei. Ich habs hier nur in 2 Blöcke aufgeteilt, damits besser gehighlightet wird..
Ihr müsst das ganze natürlich noch anpassen.. also das Ziel der Form, den Grafiknamen, etc..
Nicht zu vergessen auch die Position und Größe des Schatzes
Ich hoff euch gefällt die Schatzsuche ;)