Programs to Analyze [2/11]
Programs to Analyze
This chapter presents several complete programs for you to analyze and learn from. Many of these programs have been used previously, but analyzing them in their entirety will give you a deeper understanding of how the different pieces fit together.
Start with the first program, a simple example that showcases fundamental concepts. Run it, analyze it, and see how the different elements work together.
This program demonstrates the use of a functional getter, .seta from type color, which is used to set the transparency of a color.
The functional getter color.seta has the following signature:
- colorA color.seta(
- num alpha
- )
In this function, the alpha parameter controls the desired transparency of the resulting color, which is returned as a colorA value. Note that the alpha value must be between 0 and 100.
The next program slowly prints the text of a popular song to the screen, character by character.
The program uses a while loop to continuously print characters to the screen. The execTm() function is used to control the printing speed, which is set to 20 characters per second.
Your task is to analyze the program and understand how it works. Pay attention to the use of string concatenation, the while loop, and the print function to achieve the slow printing effect.
This program demonstrates how to print text to the screen using different fonts and colors, as well as how to create a diagonal line of text.
Pay attention to the use of the print function with various options, such as font, x and y coordinates, and foreground color.
Analyze the program to understand how it works, and see if you can modify it to create different visual effects.
Run this program and observe how the text is printed in a diagonal line, with each character changing color over time.
Analyze the program to understand how the color gradient is achieved using the execTm() function and the modulo operator (%%). Pay attention to how the red and blue components of the color are calculated to create a purplish hue.
As an exercise, try modifying the program to change the color gradient or add other visual effects. You can experiment with different modulo operations, color components, or even add more complex animations.
This program animates a bouncing ball. To make the ball appear lighter, you can adjust the force of gravity by modifying the value that is added to the vertical velocity in each iteration of the loop.
This program displays a plot of a mathematical function f(x) = x × sin(x²), which is defined in the first line of the code. This function is then used to generate the plot.
The user can move the plot left, right, up, or down using the arrow keys, and zoom in or out using the Page Up and Page Down keys.
The program also includes a commented-out section of code that shows an alternative way to handle zooming. Instead of just scaling the unit variable, this code also scales the xp and yp variables, which represent the position of the plot. This has the effect of keeping the plot centered on the screen as the user zooms in or out.
This program displays an arrow from the center to the mouse or touch position.
The DrawArrow procedure takes three parameters: the starting point, ending point, and color. It calculates the vector between the points and draws an arrow, including a small circle at the start, a line segment, and a triangle at the end to form the arrowhead.
This program visualizes vector rotation, allowing you to see how a vector changes when rotated by a certain angle. The program displays an arrow representing the original vector and another arrow representing the rotated vector.
You can move the mouse or use touch to change the direction of the original vector. Pressing the first mouse button or touching with two fingers rotates the vector by a certain angle. The program displays the original vector, the rotated vector, and their lengths, as well as the current rotation angle.
The DrawArrow procedure is used to draw the arrows, taking into account the width and color of the arrow. The program provides a interactive way to understand vector rotation and its effects on the vector's direction and length.
This program animates multiple strings of text, creating a dynamic and engaging visual effect. The animation is achieved through the MovingMsg function, which takes in several parameters, including the initial position, delay, colors, and the text message itself.
The MovingMsg function uses a loop to iterate over each character in the message, calculating its position and color based on the current time and a set of predefined parameters. The InterpolateColor function is used to smoothly transition between two colors, creating a gradient effect.
The program creates a sense of movement by rotating each character around its initial position, using the rotdg function.
A random jumping and rotating zombie animation: the zombie moves to a new random position every second, while simultaneously rotating.
<< F2:Prev - - F4:Next >>