Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- By Oliver @PixelChipCode
- Description
- This code creates a branching crystal growth effect in GameMaker. Clicking anywhere in the room starts a crystal that grows outward with occasional branching, creating a natural, organic visual effect. The crystals grow to a maximum length before stopping, and all growth is drawn on a persistent surface to maintain the visual effect. Pressing the "R" key will restart the room and clear the canvas.
- Instructions for Setup:
- Create two objects: obj_crystal and obj_spawner.
- Attach the following code to their respective events.
- Ensure the obj_spawner is placed in the room to manage the drawing surface and spawning.
- //obj_crystal
- //create event
- growth_speed = .5; // Speed of growth (slower for more control)
- branch_chance = 0.02; // Reduce the chance of branching to avoid excessive density
- growth_length = irandom_range(12, 50); // Randomize length to make each branch different
- current_length = 0;
- direction = irandom(360); // Start with a random direction
- turn_rate = random_range(-1, 1); // Slight random adjustment to make growth organic
- //step event
- // Grow the crystal if it hasn't reached its maximum length
- if (current_length < growth_length) {
- // Gradually adjust the growth direction for more natural branching
- direction += random_range(-1, 1) + turn_rate;
- // Calculate growth direction
- var dx = lengthdir_x(growth_speed, direction);
- var dy = lengthdir_y(growth_speed, direction);
- var prev_x = x;
- var prev_y = y;
- x += dx;
- y += dy;
- current_length += growth_speed;
- // Draw the growth onto the surface
- if (surface_exists(global.crystal_surface)) {
- surface_set_target(global.crystal_surface);
- draw_set_color(c_white);
- draw_line_width(prev_x, prev_y, x, y, 1); // Use thin lines for a delicate look
- surface_reset_target();
- }
- // Occasionally create a new branch, but only if this branch is still growing
- if (random(1) < branch_chance && current_length > 10) { // Only branch after some initial growth
- var new_direction = direction + irandom_range(-30, 30); // Less variation for more controlled branching
- var new_crystal = instance_create_layer(x, y, "Instances", obj_crystal);
- new_crystal.direction = new_direction;
- }
- }
- //draw event
- if (surface_exists(global.crystal_surface)) {
- draw_surface(global.crystal_surface, 0, 0);
- }
- // obj_spawner
- //create event
- // Create a surface for drawing the crystallization effect
- global.crystal_surface = -1;
- //step event
- if(mouse_check_button(mb_left)){
- // If the surface exists, start drawing on it
- if (surface_exists(global.crystal_surface)) {
- surface_set_target(global.crystal_surface);
- // Set draw color for crystals
- draw_set_color(c_white);
- // Draw starting point
- draw_circle(mouse_x, mouse_y, 2, false);
- // Reset the surface target to the application (main screen)
- surface_reset_target();
- // Create an instance of obj_crystal at click point
- instance_create_layer(mouse_x, mouse_y, "Instances", obj_crystal);
- }
- }
- if(keyboard_check_pressed(ord("R"))){
- room_restart();
- }
- //room start event
- if (surface_exists(global.crystal_surface)) {
- surface_free(global.crystal_surface);
- }
- global.crystal_surface = surface_create(room_width, room_height);
Advertisement
Add Comment
Please, Sign In to add comment