Why make a Christmas tree in April? I don't know but it's harder than you might think, if you want a good one at least. To make a good Christmas tree you have to have at least four components the trunk, leaves, pot and baubles.
//Trunk
colour(150, 75, 10);
rect(378, 150, 44, 425);
The trunk is simple, a brown coloured rectangle. To make brown with RGB you have to make orange, red plus a little green. Then just reduce the numbers to make brown, which is, to the best of my knowledge, a dark orange.
//Leaves
for (int y = 500; y >= 100; y = y - 20) {
int w = y / 2;
int h = y / 3;
int green = 250 - y * 200 / 500;
colour(0, green, 0);
tri(400 - w / 2, y, 400 + w / 2, y, 400, y - h);
}
The Leaves are not as simple, I did some special things to make the trees leaves look like a golden cyprus.
I am making the tree out of triangles. The loop counts backwards from y = 500 to y = 100, 20 pixels at a time. I am starting at 500, which is near the bottom of the 800 × 600 canvas.
I calculate the width w and height h of the triangle based on the y coordinate, so that the triangles get smaller towards the top of the tree.
To make the tree become gradually lighter closer to the top of the canvas, I vary the amount of green based on the y coordinate. The y coordinate ranges from 100 to 500. First divide by 500 to make it approximately between 0 and 1. Then multiply by 200 to put it on the RGB scale from 0 to 255. Subtract from 250 so that the amount of green ranges roughly between 250 for the light green at the top of the tree, and 50 for the dark green at the bottom of the tree.
//Pot
colour(75, 37, 10);
quad(360, 530, 440, 530, 425, 590, 375, 590);
colour(90, 45, 12);
rect(355, 528, 90, 10);
Now we have a basic pine tree but it doesn't have a place to live... So let's put it in a comically small pot! All we need to do is put a quadrilateral with a smaller base for the body of the pot and a skinny rectangle at the top to be the brim.
//BAUBLES
srandom(6);
for (int i = 0; i < 36; i++) {
if (i % 2) {
colour(255, 0, 0);
} else {
colour(255, 200, 0);
}
int x, y;
int w;
do {
y = random() % 433 + 67;
x = random() % 250 + 275;
w = y / 2;
} while (x < 400 - w/2 || x > 400 + w/2);
circle(x, y, 10);
}
Finally, we have to show that is isn't just a pine tree, so let's add some baubles. To be honest I didn't program this, my dad did, and I have no idea how this was done. So let's hand over to dad:
We want to draw 36 baubles, so we have a loop to count from i = 0 to 35. The odd numbered ones where i % 2 == 1 are red, and the even numbered ones are gold, but we can't make them shiny, at least not today.
We choose a random point in a rectangle that surrounds the tree, its "bounding box", then work out the width of the tree at that y position. If the point is to the left or right of the tree, it tries again with a new random point using a do ... while loop.
Finally, we draw a small circle of radius 10 pixels at the chosen point.
The srandom(6)
call at the top of this section seeds the random number generator. Seeding with a constant value like this gives us the same pattern of baubles each time we run the program. Number 6 happened to give a fairly good looking pattern.
Here is the full code to draw the Christmas tree.
Next post: Why Write a Dev Log?, by Sam
Previous post: Creating Drawings, by Thalia
Contents: Game Dev From Scratch
Top comments (2)
I'm not a C guy, but this is nice.
I'm not a C guy, but this is niC. 😂