|
| 1 | +""" |
| 2 | +
|
| 3 | +raylib version: 5.5.0.3 |
| 4 | +
|
| 5 | +raylib [core] example - random sequence |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from pyray import * |
| 10 | + |
| 11 | + |
| 12 | +class ColorRect: |
| 13 | + color: Color |
| 14 | + rect: Rectangle |
| 15 | + |
| 16 | + |
| 17 | +def generate_random_color_rect_sequence( |
| 18 | + rect_count: int, rect_width: float, screen_width: int, screen_height: float |
| 19 | +) -> list: |
| 20 | + rectangles = [ColorRect() for _ in range(rect_count)] |
| 21 | + |
| 22 | + seq = load_random_sequence(rect_count, 0, rect_count - 1) |
| 23 | + rect_seq_width = rect_count * rect_width |
| 24 | + start_x = (screen_width - rect_seq_width) * 0.5 |
| 25 | + |
| 26 | + for i in range(rect_count): |
| 27 | + rect_height = remap(seq[i], 0, rect_count - 1, 0, screen_height) |
| 28 | + rectangles[i].color = Color( |
| 29 | + get_random_value(0, 255), |
| 30 | + get_random_value(0, 255), |
| 31 | + get_random_value(0, 255), |
| 32 | + 255, |
| 33 | + ) |
| 34 | + rectangles[i].rect = Rectangle( |
| 35 | + start_x + i * rect_width, |
| 36 | + screen_height - rect_height, |
| 37 | + rect_width, |
| 38 | + rect_height, |
| 39 | + ) |
| 40 | + return rectangles |
| 41 | + |
| 42 | + |
| 43 | +def shuffle_color_rect_sequence(rectangles: list, rect_count: int): |
| 44 | + seq = load_random_sequence(rect_count, 0, rect_count - 1) |
| 45 | + |
| 46 | + for i in range(rect_count): |
| 47 | + r1 = rectangles[i] |
| 48 | + r2 = rectangles[seq[i]] |
| 49 | + |
| 50 | + tmp_color = r1.color |
| 51 | + tmp_h = r1.rect.height |
| 52 | + tmp_y = r1.rect.y |
| 53 | + |
| 54 | + r1.color = r2.color |
| 55 | + r1.rect.height = r2.rect.height |
| 56 | + r1.rect.y = r2.rect.y |
| 57 | + r2.color = tmp_color |
| 58 | + r2.rect.height = tmp_h |
| 59 | + r2.rect.y = tmp_y |
| 60 | + |
| 61 | + |
| 62 | +screen_width = 800 |
| 63 | +screen_height = 450 |
| 64 | + |
| 65 | +init_window(screen_width, screen_height, "raylib [core] example - random sequence") |
| 66 | +set_target_fps(60) |
| 67 | +rect_count = 20 |
| 68 | +rect_size = screen_width / rect_count |
| 69 | +rectangles = generate_random_color_rect_sequence( |
| 70 | + rect_count, rect_size, screen_width, 0.75 * screen_height |
| 71 | +) |
| 72 | + |
| 73 | +while not window_should_close(): |
| 74 | + |
| 75 | + if is_key_pressed(KeyboardKey.KEY_SPACE): |
| 76 | + shuffle_color_rect_sequence(rectangles, rect_count) |
| 77 | + |
| 78 | + if is_key_pressed(KeyboardKey.KEY_UP): |
| 79 | + rect_count += 1 |
| 80 | + rect_size = screen_width / rect_count |
| 81 | + rectangles = generate_random_color_rect_sequence( |
| 82 | + rect_count, rect_size, screen_width, 0.75 * screen_height |
| 83 | + ) |
| 84 | + |
| 85 | + if is_key_pressed(KeyboardKey.KEY_DOWN): |
| 86 | + if rect_count >= 4: |
| 87 | + rect_count -= 1 |
| 88 | + rect_size = screen_width / rect_count |
| 89 | + rectangles = generate_random_color_rect_sequence( |
| 90 | + rect_count, rect_size, screen_width, 0.75 * screen_height |
| 91 | + ) |
| 92 | + |
| 93 | + begin_drawing() |
| 94 | + |
| 95 | + clear_background(RAYWHITE) |
| 96 | + |
| 97 | + for i in range(rect_count): |
| 98 | + draw_rectangle_rec(rectangles[i].rect, rectangles[i].color) |
| 99 | + draw_text( |
| 100 | + "Press SPACE to shuffle the current sequence", |
| 101 | + 10, |
| 102 | + screen_height - 96, |
| 103 | + 20, |
| 104 | + BLACK, |
| 105 | + ) |
| 106 | + draw_text( |
| 107 | + "Press UP to add a rectangle and generate a new sequence", |
| 108 | + 10, |
| 109 | + screen_height - 64, |
| 110 | + 20, |
| 111 | + BLACK, |
| 112 | + ) |
| 113 | + draw_text( |
| 114 | + "Press DOWN to remove a rectangle and generate a new sequence", |
| 115 | + 10, |
| 116 | + screen_height - 32, |
| 117 | + 20, |
| 118 | + BLACK, |
| 119 | + ) |
| 120 | + |
| 121 | + draw_text(f"Count: {rect_count} rectangles", 10, 10, 20, MAROON) |
| 122 | + draw_fps(screen_width - 80, 10) |
| 123 | + end_drawing() |
| 124 | +close_window() |
0 commit comments