PHP 生成圣诞树代码

wen PHP项目 3

本文目录导读:

PHP 生成圣诞树代码

  1. 基础控制台版本
  2. 彩色HTML版本
  3. 动画效果版本
  4. 函数封装版本(功能完整)
  5. 简洁实用版本
  6. 使用建议:

我来为你提供几个不同版本的PHP圣诞树代码,从简单到进阶:

基础控制台版本

<?php
// 基础圣诞树 - 控制台输出
function drawChristmasTree($height = 10) {
    // 树冠部分
    for ($i = 1; $i <= $height; $i++) {
        echo str_repeat(' ', $height - $i);
        echo str_repeat('*', 2 * $i - 1);
        echo "\n";
    }
    // 树干部分
    for ($i = 0; $i < 2; $i++) {
        echo str_repeat(' ', $height - 2);
        echo "***\n";
    }
}
echo "🎄 圣诞快乐!🎄\n\n";
drawChristmasTree(10);
?>

彩色HTML版本

<?php
// 彩色HTML圣诞树
function drawHTMLTree($height = 12) {
    $html = '<div style="text-align:center; font-family:monospace;">';
    // 树顶星星
    $html .= '<div style="color:gold; font-size:24px;">★</div>';
    // 树冠
    for ($i = 1; $i <= $height; $i++) {
        $leaf = str_repeat('*', 2 * $i - 1);
        $spaces = str_repeat('&nbsp;', $height - $i + 1);
        // 随机装饰颜色
        $colored_parts = '';
        for ($j = 0; $j < strlen($leaf); $j++) {
            $colors = ['green', '#228B22', '#006400', '#32CD32', '#90EE90'];
            $decorations = ['red', 'gold', 'blue', 'purple', 'silver'];
            // 随机添加装饰球
            if (rand(1, 10) > 8) {
                $color = $decorations[array_rand($decorations)];
                $colored_parts .= "<span style='color:$color;'>●</span>";
            } else {
                $color = $colors[array_rand($colors)];
                $colored_parts .= "<span style='color:$color;'>*</span>";
            }
        }
        $html .= "<div style='font-size:20px;'>$spaces$colored_parts</div>";
    }
    // 树干
    for ($i = 0; $i < 3; $i++) {
        $trunk = str_repeat('|', 3);
        $spaces = str_repeat('&nbsp;', $height - 1);
        $html .= "<div style='color:brown; font-size:20px;'>$spaces$trunk</div>";
    }
    // 礼物
    $html .= '<div style="color:red; font-size:30px; margin-top:10px;">🎁</div>';
    $html .= '</div>';
    return $html;
}
echo drawHTMLTree(8);
?>

动画效果版本

<?php
// 闪烁动画圣诞树
function drawAnimTree($height = 10) {
    ?>
    <style>
        @keyframes twinkle {
            0% { opacity: 1; }
            50% { opacity: 0.3; }
            100% { opacity: 1; }
        }
        .tree-line {
            font-family: 'Courier New', monospace;
            font-size: 18px;
            line-height: 1.4;
        }
        .star {
            color: gold;
            animation: twinkle 1s infinite;
            font-size: 30px;
        }
        .light-red { color: #ff0000; animation: twinkle 0.7s infinite; }
        .light-gold { color: #ffd700; animation: twinkle 1.2s infinite; }
        .light-blue { color: #0000ff; animation: twinkle 0.9s infinite; }
    </style>
    <div style="text-align:center; background: #0a0a2e; padding: 20px; border-radius: 10px;">
        <div class="star">✦ ✦ ✦</div>
    <?php
    for ($i = 1; $i <= $height; $i++) {
        $spaces = str_repeat('&nbsp;', $height - $i + 3);
        echo "<div class='tree-line'>$spaces";
        for ($j = 0; $j < 2 * $i - 1; $j++) {
            if (rand(1, 10) > 7) {
                $lights = ['light-red', 'light-gold', 'light-blue'];
                $class = $lights[array_rand($lights)];
                echo "<span class='$class'>●</span>";
            } else {
                echo "<span style='color: darkgreen;'>*</span>";
            }
        }
        echo "</div>";
    }
    // 树干
    for ($i = 0; $i < 2; $i++) {
        echo "<div class='tree-line'>";
        echo str_repeat('&nbsp;', $height + 2);
        echo "<span style='color: #8B4513;'>|||</span>";
        echo "</div>";
    }
    echo "</div>";
}
drawAnimTree(10);
?>

函数封装版本(功能完整)

<?php
class ChristmasTree {
    private $height;
    private $colors = [
        'green' => ['#006400', '#228B22', '#32CD32'],
        'decorations' => ['#FF0000', '#FFD700', '#0000FF', '#FF69B4', '#FFA500']
    ];
    public function __construct($height = 10) {
        $this->height = $height;
    }
    // 生成星形
    private function drawStar() {
        echo "<div style='text-align:center; color:gold; font-size:24px;'>";
        echo "★";
        echo "</div>";
    }
    // 生成树枝
    private function drawBranch($row) {
        $spaces = str_repeat('&nbsp;', $this->height - $row + 2);
        echo "<div style='text-align:center; font-family:monospace; font-size:16px;'>";
        echo $spaces;
        for ($i = 0; $i < 2 * $row - 1; $i++) {
            if (rand(1, 8) > 6) {
                $color = $this->colors['decorations'][array_rand($this->colors['decorations'])];
                echo "<span style='color:$color;'>●</span>";
            } else {
                $color = $this->colors['green'][array_rand($this->colors['green'])];
                echo "<span style='color:$color;'>▲</span>";
            }
        }
        echo "</div>";
    }
    // 生成树干
    private function drawTrunk() {
        for ($i = 0; $i < 3; $i++) {
            echo "<div style='text-align:center;'>";
            echo str_repeat('&nbsp;', $this->height);
            echo "<span style='color:#8B4513; font-size:16px;'>|||</span>";
            echo "</div>";
        }
    }
    // 生成完整的树
    public function render() {
        $html = "<div style='background: linear-gradient(to bottom, #001133, #000022); padding: 20px; border-radius: 10px;'>";
        echo $html;
        $this->drawStar();
        for ($i = 1; $i <= $this->height; $i++) {
            $this->drawBranch($i);
        }
        $this->drawTrunk();
        echo "</div>";
    }
}
// 使用示例
$tree = new ChristmasTree(8);
$tree->render();
?>

简洁实用版本

<?php
function xmas_tree($rows = 8) {
    echo "<pre style='text-align:center; color:green;'>";
    // 树顶
    echo "<span style='color:gold;'>★</span>\n";
    // 树冠
    for ($i = 0; $i < $rows; $i++) {
        $spaces = str_repeat(' ', $rows - $i);
        $stars = str_repeat('*', (2 * $i) + 1);
        // 添加装饰灯(随机位置)
        $chars = str_split($stars);
        foreach ($chars as $key => $char) {
            if ($key % 2 == 0 && rand(1, 5) == 3) {
                $colors = ['red', 'gold', 'blue'];
                echo "<span style='color: {$colors[array_rand($colors)]};'>o</span>";
            } else {
                echo $char;
            }
        }
        echo "\n";
    }
    // 树干
    for ($i = 0; $i < 2; $i++) {
        echo str_repeat(' ', $rows - 1);
        echo "***\n";
    }
    echo "</pre>";
    echo "<p style='text-align:center; color:red; font-size:20px;'>Merry Christmas! 🎄</p>";
}
xmas_tree(10);
?>

使用建议:

  1. 终端环境:使用版本1或版本5
  2. 网页展示:使用版本2、3或4
  3. 需要动画效果:使用版本3
  4. 面向对象编程:使用版本4

这些代码都带有装饰效果,可以根据需要调整树的高度和装饰风格,祝编码愉快!🎄✨

抱歉,评论功能暂时关闭!