Advertisement
KingAesthetic

PHP Example 2

Sep 27th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. // function that simulates a combat system.
  3. function simulateCombat($playerHealth, $enemyHealth) {
  4.     srand(time());
  5.  
  6.     while ($playerHealth > 0 && $enemyHealth > 0) {
  7.         $playerAttack = rand(1, 20);
  8.         $enemyAttack = rand(1, 20);
  9.  
  10.         echo "Player attacks for " . $playerAttack . " damage!" . PHP_EOL;
  11.         $enemyHealth -= $playerAttack;
  12.  
  13.         if ($enemyHealth <= 0) {
  14.             echo "Enemy defeated! Player wins!" . PHP_EOL;
  15.             break;
  16.         }
  17.  
  18.         echo "Enemy counterattacks for " . $enemyAttack . " damage!" . PHP_EOL;
  19.         $playerHealth -= $enemyAttack;
  20.  
  21.         if ($playerHealth <= 0) {
  22.             echo "Player defeated! Enemy wins!" . PHP_EOL;
  23.             break;
  24.         }
  25.     }
  26. }
  27.  
  28. // function that generates random loot from treasure chests.
  29. function generateLoot($playerLuck) {
  30.     srand(time());
  31.  
  32.     $lootValue = rand(0, 99);
  33.     $actualLoot = $lootValue * $playerLuck / 100;
  34.  
  35.     echo "You found loot worth " . $actualLoot . " gold!" . PHP_EOL;
  36. }
  37.  
  38. // define the structure for handling skill leveling.
  39. class Player {
  40.     public $level;
  41.     public $experience;
  42.  
  43.     public function __construct($level, $experience) {
  44.         $this->level = $level;
  45.         $this->experience = $experience;
  46.     }
  47. }
  48.  
  49. // player levels up!
  50. function levelUp($player) {
  51.     $player->level++;
  52.     $player->experience = 0;
  53.     echo "Congratulations! You leveled up to level " . $player->level . "!" . PHP_EOL;
  54. }
  55.  
  56. // function that simulates an enemy encounter.
  57. function enemyEncounter($enemyName, $enemyHealth) {
  58.     echo "You encounter a " . $enemyName . "!" . PHP_EOL;
  59.     echo "Enemy health: " . $enemyHealth . PHP_EOL;
  60. }
  61.  
  62. $playerHealth = 100;
  63. $enemyHealth = 80;
  64. simulateCombat($playerHealth, $enemyHealth);
  65.  
  66. $playerLuck = 75;
  67. generateLoot($playerLuck);
  68.  
  69. $player = new Player(1, 0);
  70. $player->experience += 100;
  71. if ($player->experience >= 1000) {
  72.     levelUp($player);
  73. }
  74.  
  75. $enemyName = "Orc";
  76. $enemyHealth2 = 120;
  77. enemyEncounter($enemyName, $enemyHealth2);
  78.  
  79. // tic tac toe
  80. $board = array_fill(0, 3, array_fill(0, 3, ' '));
  81. $board[1][1] = 'X';
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement