Introducing Dragonfly Cloud! Learn More

Question: How do you use colors in GameMaker?

Answer

In GameMaker, colors can be used to enhance the visual aesthetics of games. You can set the color of sprites, backgrounds, fonts, and other elements.

  1. Setting Colors for Drawing: When drawing shapes, text, or sprites, you can set the drawing color using the draw_set_color(color) function. The color argument is typically defined using GameMaker's built-in color constants (like c_red, c_green, etc.), or with custom colors created using the make_color_rgb(red, green, blue) function.

    draw_set_color(c_red); // Set the drawing color to red draw_rectangle(x1, y1, x2, y2, false); // Draw a red rectangle
  2. Creating Custom Colors: You can create your own colors by specifying the red, green, and blue components with values between 0 and 255.

    var myColor = make_color_rgb(128, 64, 255); // Create a custom purple color draw_set_color(myColor); draw_circle(x, y, radius, false); // Draw a circle with the custom color
  3. Changing the Blend Mode: Blending modes determine how colors mix when drawn on top of each other. You can change blend modes using gpu_set_blendmode(bm_mode).

    gpu_set_blendmode(bm_add); // Set the blend mode to additive blending draw_sprite(spr_Example, 0, x, y); // Draw a sprite with additive blending gpu_set_blendmode(bm_normal); // Reset to normal blending mode
  4. Using Color with Fonts: When drawing text, you can also use draw_set_color() to define the text color before calling draw_text().

    draw_set_color(c_blue); // Set the text color to blue draw_text(x, y, "Hello World!"); // Draw blue text at position (x, y)
  5. Alpha Transparency: In addition to RGB (red, green, blue), you can also specify alpha transparency when creating a color using make_color_rgba(). Alpha values range from 0 (completely transparent) to 255 (completely opaque).

    var myTransparentColor = make_color_rgba(255, 255, 255, 128); // Create a semi-transparent white color draw_set_color(myTransparentColor); draw_rectangle(x1, y1, x2, y2, true); // Draw a semi-transparent rectangle
  6. Gradient Effects: GameMaker also allows you to draw gradients by specifying different colors for different points.

    draw_gradient_horiz(x1, y1, x2, y2, c_red, c_blue); // Horizontal gradient from red to blue

Remember that the exact representation of colors may vary depending on the display settings and hardware.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.