Skip to content

Instantly share code, notes, and snippets.

@Archspect
Created March 27, 2023 16:34
Show Gist options
  • Save Archspect/a292be25ba7f7606f770242cdf27713c to your computer and use it in GitHub Desktop.
Save Archspect/a292be25ba7f7606f770242cdf27713c to your computer and use it in GitHub Desktop.
//! Shows how to render simple primitive shapes with a single color.
use bevy::{
prelude::*,
window::{CursorGrabMode, PresentMode, WindowLevel},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "I am a window!".into(),
resolution: (1008., 567.).into(),
// Tells wasm to resize the window according to the available canvas
fit_canvas_to_parent: true,
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
prevent_default_event_handling: false,
..default()
}),
..default()
}))
//.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(rectangle)
.add_system(Update, keyboard_input)
.run();
}
fn setup(
mut commands: Commands,
)
{
commands.spawn(Camera2dBundle::default());
}
fn rectangle(mut cmd: Commands)
{
cmd.spawn(SpriteBundle {
sprite: Sprite {
color: Color::hex("7700FF").unwrap(),
custom_size: Some(Vec2::new(100.0, 100.0)),
..default()
},
transform: Transform::from_translation(Vec3::new(-50., 0., 0.)),
..default()
});
}
fn keyboard_input_system(keyboard_input: Res<Input<KeyCode>>) {
if keyboard_input.pressed(KeyCode::A) {
info!("'A' currently pressed");
}
if keyboard_input.just_pressed(KeyCode::A) {
info!("'A' just pressed");
}
if keyboard_input.just_released(KeyCode::A) {
info!("'A' just released");
}
}
//! Demonstrates handling a key press/release.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, keyboard_input_system)
.run();
}
/// This system prints 'A' key state
fn keyboard_input_system(keyboard_input: Res<Input<KeyCode>>) {
if keyboard_input.pressed(KeyCode::A) {
info!("'A' currently pressed");
}
if keyboard_input.just_pressed(KeyCode::A) {
info!("'A' just pressed");
}
if keyboard_input.just_released(KeyCode::A) {
info!("'A' just released");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment