MCPcopy Create free account
hub / github.com/Logicalshift/flo_draw / main

Function main

draw/examples/vectoroids.rs:18–147  ·  view source on GitHub ↗

Demonstration of `flo_draw` that illustrates a way to implement a simple game

()

Source from the content-addressed store, hash-verified

16/// Demonstration of `flo_draw` that illustrates a way to implement a simple game
17///
18pub fn main() {
19 with_2d_graphics(|| {
20 executor::block_on(async {
21 // Set up
22 let (canvas, events) = create_drawing_window_with_events("Vectoroids");
23
24 // Create a tick generator
25 let tick_stream = tick_stream();
26
27 // Combine the tick generator into the events stream
28 let events = events.map(|evt| VectorEvent::DrawEvent(evt));
29 let mut events = stream::select(events, tick_stream);
30
31 // Set up the canvas by declaring the sprites and the background
32 let ship_sprite = SpriteId(0);
33 let bullet_sprite = SpriteId(1);
34 let roid_sprite = SpriteId(2);
35 canvas.draw(|gc| {
36 gc.clear_canvas(Color::Rgba(0.1, 0.1, 0.1, 1.0));
37
38 // Game area is a 1000x1000 square
39 gc.canvas_height(1000.0);
40 gc.center_region(0.0, 0.0, 1000.0, 1000.0);
41
42 // Ship is just a triangle
43 gc.sprite(ship_sprite);
44 gc.clear_sprite();
45
46 gc.new_path();
47 gc.move_to(-10.0, -8.0);
48 gc.line_to(0.0, 12.0);
49 gc.line_to(10.0, -8.0);
50 gc.line_to(8.0, -6.0);
51 gc.line_to(-8.0, -6.0);
52 gc.line_to(-10.0, -8.0);
53
54 gc.line_width(2.0);
55 gc.stroke_color(Color::Rgba(0.8, 0.7, 0.0, 1.0));
56 gc.stroke();
57
58 // Bullet is a square
59 gc.sprite(bullet_sprite);
60 gc.clear_sprite();
61
62 gc.new_path();
63 gc.rect(-1.0, -1.0, 1.0, 1.0);
64 gc.fill_color(Color::Rgba(1.0, 0.8, 0.0, 1.0));
65 gc.fill();
66
67 // 'roids are an irregular shape
68 gc.sprite(roid_sprite);
69 gc.clear_sprite();
70
71 gc.new_path();
72 gc.move_to(0.0, -15.0);
73 gc.line_to(-15.0, -25.0);
74 gc.line_to(-35.0, -15.0);
75 gc.line_to(-20.0, 0.0);

Callers

nothing calls this directly

Calls 15

with_2d_graphicsFunction · 0.85
tick_streamFunction · 0.85
DrawEventEnum · 0.85
SpriteIdClass · 0.85
LayerIdClass · 0.85
mapMethod · 0.80
clear_canvasMethod · 0.80
canvas_heightMethod · 0.80
center_regionMethod · 0.80
spriteMethod · 0.80
clear_spriteMethod · 0.80

Tested by

no test coverage detected