Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Crab Example</title>
- <style>
- #crabSection { position:relative; }
- .dot { margin: 0px 0px 0px 0px; position:absolute; }
- </style>
- </head>
- <body>
- <div>
- <h1>Dots on the crab example</h1>
- </div>
- <div id="crabSection">
- <img src="crab.png">
- <?php
- $crab = imagecreatefrompng("crab.png");
- $dot = imagecreatefrompng("dot.png");
- $numDesiredDots = 10;
- $numCreatedDots = 0;
- $crabWidth = imagesx($crab);
- $crabHeight = imagesy($crab);
- $dotWidth = imagesx($dot);
- $dotHeight = imagesy($dot);
- $spawnableWidth = $crabWidth - $dotWidth;
- $spawnableHeight = $crabHeight - $dotHeight;
- srand(time());
- $testingForDotSubpart = imagecreatetruecolor($dotWidth, $dotHeight);
- $validCoordinates = array();
- $invalidCoordinates = array();
- $colorWereLookingFor = 0xFF; // ARGB - our crab is blue
- while($numCreatedDots < $numDesiredDots)
- {
- $randomX = rand() % $spawnableWidth;
- $randomY = rand() % $spawnableHeight;
- imagecopy($testingForDotSubpart, $crab, 0, 0, $randomX, $randomY, $dotWidth, $dotHeight);
- $valid = true;
- for($x = 0; $x < $dotWidth; $x++)
- {
- for($y = 0; $y < $dotHeight; $y++)
- {
- if(imagecolorat($testingForDotSubpart, $x, $y) != $colorWereLookingFor)
- {
- $valid = false;
- break 2;
- }
- }
- }
- if($valid)
- {
- array_push($validCoordinates, array('x' => $randomX, 'y' => $randomY));
- $numCreatedDots++;
- }
- else
- {
- // you can get rid of this else, it's just to show you how many fails there are
- array_push($invalidCoordinates, array('x' => $randomX, 'y' => $randomY));
- }
- }
- imagedestroy($testingForDotSubpart);
- imagedestroy($dot);
- imagedestroy($crab);
- echo "<p>Valid Coords: <br>";
- foreach($validCoordinates as $coord)
- {
- echo "X: " . $coord['x'] . " Y: " . $coord['y'] . "<br>\n";
- }
- echo "<br>Invalid Coords " . count($invalidCoordinates) . "</p>\n";
- // Now add the dots on the crab!
- for($i = 0; $i < count($validCoordinates); $i++)
- {
- $coord = $validCoordinates[$i];
- echo "<div class='dot' style='left:".$coord['x'].";top:".$coord['y'].";'><a href='javascript:alert(".$i.");'><img src='dot.png'></a></div>\n";
- }
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement