Function macroquad::window::screen_width
source · pub fn screen_width() -> f32
Examples found in repository?
examples/asteroids.rs (line 30)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
fn wrap_around(v: &Vec2) -> Vec2 {
let mut vr = Vec2::new(v.x, v.y);
if vr.x > screen_width() {
vr.x = 0.;
}
if vr.x < 0. {
vr.x = screen_width()
}
if vr.y > screen_height() {
vr.y = 0.;
}
if vr.y < 0. {
vr.y = screen_height()
}
vr
}
#[macroquad::main("Asteroids")]
async fn main() {
let mut ship = Ship {
pos: Vec2::new(screen_width() / 2., screen_height() / 2.),
rot: 0.,
vel: Vec2::new(0., 0.),
};
let mut bullets = Vec::new();
let mut last_shot = get_time();
let mut asteroids = Vec::new();
let mut gameover = false;
let mut screen_center;
loop {
if gameover {
clear_background(LIGHTGRAY);
let mut text = "You Win!. Press [enter] to play again.";
let font_size = 30.;
if asteroids.len() > 0 {
text = "Game Over. Press [enter] to play again.";
}
let text_size = measure_text(text, None, font_size as _, 1.0);
draw_text(
text,
screen_width() / 2. - text_size.width / 2.,
screen_height() / 2. - text_size.height / 2.,
font_size,
DARKGRAY,
);
if is_key_down(KeyCode::Enter) {
ship = Ship {
pos: Vec2::new(screen_width() / 2., screen_height() / 2.),
rot: 0.,
vel: Vec2::new(0., 0.),
};
bullets = Vec::new();
asteroids = Vec::new();
gameover = false;
screen_center = Vec2::new(screen_width() / 2., screen_height() / 2.);
for _ in 0..10 {
asteroids.push(Asteroid {
pos: screen_center
+ Vec2::new(rand::gen_range(-1., 1.), rand::gen_range(-1., 1.))
.normalize()
* screen_width().min(screen_height())
/ 2.,
vel: Vec2::new(rand::gen_range(-1., 1.), rand::gen_range(-1., 1.)),
rot: 0.,
rot_speed: rand::gen_range(-2., 2.),
size: screen_width().min(screen_height()) / 10.,
sides: rand::gen_range(3, 8),
collided: false,
})
}
}
next_frame().await;
continue;
}
let frame_t = get_time();
let rotation = ship.rot.to_radians();
let mut acc = -ship.vel / 100.; // Friction
// Forward
if is_key_down(KeyCode::Up) {
acc = Vec2::new(rotation.sin(), -rotation.cos()) / 3.;
}
// Shot
if is_key_down(KeyCode::Space) && frame_t - last_shot > 0.5 {
let rot_vec = Vec2::new(rotation.sin(), -rotation.cos());
bullets.push(Bullet {
pos: ship.pos + rot_vec * SHIP_HEIGHT / 2.,
vel: rot_vec * 7.,
shot_at: frame_t,
collided: false,
});
last_shot = frame_t;
}
// Steer
if is_key_down(KeyCode::Right) {
ship.rot += 5.;
} else if is_key_down(KeyCode::Left) {
ship.rot -= 5.;
}
// Euler integration
ship.vel += acc;
if ship.vel.length() > 5. {
ship.vel = ship.vel.normalize() * 5.;
}
ship.pos += ship.vel;
ship.pos = wrap_around(&ship.pos);
// Move each bullet
for bullet in bullets.iter_mut() {
bullet.pos += bullet.vel;
}
// Move each asteroid
for asteroid in asteroids.iter_mut() {
asteroid.pos += asteroid.vel;
asteroid.pos = wrap_around(&asteroid.pos);
asteroid.rot += asteroid.rot_speed;
}
// Bullet lifetime
bullets.retain(|bullet| bullet.shot_at + 1.5 > frame_t);
let mut new_asteroids = Vec::new();
for asteroid in asteroids.iter_mut() {
// Asteroid/ship collision
if (asteroid.pos - ship.pos).length() < asteroid.size + SHIP_HEIGHT / 3. {
gameover = true;
break;
}
// Asteroid/bullet collision
for bullet in bullets.iter_mut() {
if (asteroid.pos - bullet.pos).length() < asteroid.size {
asteroid.collided = true;
bullet.collided = true;
// Break the asteroid
if asteroid.sides > 3 {
new_asteroids.push(Asteroid {
pos: asteroid.pos,
vel: Vec2::new(bullet.vel.y, -bullet.vel.x).normalize()
* rand::gen_range(1., 3.),
rot: rand::gen_range(0., 360.),
rot_speed: rand::gen_range(-2., 2.),
size: asteroid.size * 0.8,
sides: asteroid.sides - 1,
collided: false,
});
new_asteroids.push(Asteroid {
pos: asteroid.pos,
vel: Vec2::new(-bullet.vel.y, bullet.vel.x).normalize()
* rand::gen_range(1., 3.),
rot: rand::gen_range(0., 360.),
rot_speed: rand::gen_range(-2., 2.),
size: asteroid.size * 0.8,
sides: asteroid.sides - 1,
collided: false,
})
}
break;
}
}
}
// Remove the collided objects
bullets.retain(|bullet| bullet.shot_at + 1.5 > frame_t && !bullet.collided);
asteroids.retain(|asteroid| !asteroid.collided);
asteroids.append(&mut new_asteroids);
// You win?
if asteroids.len() == 0 {
gameover = true;
}
if gameover {
continue;
}
clear_background(LIGHTGRAY);
for bullet in bullets.iter() {
draw_circle(bullet.pos.x, bullet.pos.y, 2., BLACK);
}
for asteroid in asteroids.iter() {
draw_poly_lines(
asteroid.pos.x,
asteroid.pos.y,
asteroid.sides,
asteroid.size,
asteroid.rot,
2.,
BLACK,
)
}
let v1 = Vec2::new(
ship.pos.x + rotation.sin() * SHIP_HEIGHT / 2.,
ship.pos.y - rotation.cos() * SHIP_HEIGHT / 2.,
);
let v2 = Vec2::new(
ship.pos.x - rotation.cos() * SHIP_BASE / 2. - rotation.sin() * SHIP_HEIGHT / 2.,
ship.pos.y - rotation.sin() * SHIP_BASE / 2. + rotation.cos() * SHIP_HEIGHT / 2.,
);
let v3 = Vec2::new(
ship.pos.x + rotation.cos() * SHIP_BASE / 2. - rotation.sin() * SHIP_HEIGHT / 2.,
ship.pos.y + rotation.sin() * SHIP_BASE / 2. + rotation.cos() * SHIP_HEIGHT / 2.,
);
draw_triangle_lines(v1, v2, v3, 2., BLACK);
next_frame().await
}
}
More examples
examples/basic_shapes.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16
async fn main() {
loop {
clear_background(LIGHTGRAY);
draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
draw_rectangle(screen_width() / 2.0 - 60.0, 100.0, 120.0, 60.0, GREEN);
draw_circle(screen_width() - 30.0, screen_height() - 30.0, 15.0, YELLOW);
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);
next_frame().await
}
}
examples/camera.rs (line 11)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
async fn main() {
loop {
clear_background(LIGHTGRAY);
// Render some primitives in camera space
set_camera(&Camera2D {
zoom: vec2(1., screen_width() / screen_height()),
..Default::default()
});
draw_line(-0.4, 0.4, -0.8, 0.9, 0.05, BLUE);
draw_rectangle(-0.3, 0.3, 0.2, 0.2, GREEN);
draw_circle(0., 0., 0.1, YELLOW);
// Back to screen space, render some text
set_default_camera();
draw_text("HELLO", 30.0, 200.0, 30.0, BLACK);
next_frame().await
}
}
examples/input_keys.rs (line 5)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
async fn main() {
let mut x = screen_width() / 2.0;
let mut y = screen_height() / 2.0;
loop {
clear_background(LIGHTGRAY);
if is_key_down(KeyCode::Right) {
x += 1.0;
}
if is_key_down(KeyCode::Left) {
x -= 1.0;
}
if is_key_down(KeyCode::Down) {
y += 1.0;
}
if is_key_down(KeyCode::Up) {
y -= 1.0;
}
draw_circle(x, y, 15.0, YELLOW);
draw_text("move the ball with arrow keys", 20.0, 20.0, 20.0, DARKGRAY);
next_frame().await
}
}
examples/screen_texture.rs (line 27)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
async fn main() {
let texture: Texture2D = load_texture("examples/chess.png").await.unwrap();
let lens_material = load_material(
ShaderSource::Glsl {
vertex: LENS_VERTEX_SHADER,
fragment: LENS_FRAGMENT_SHADER,
},
MaterialParams {
uniforms: vec![("Center".to_owned(), UniformType::Float2)],
..Default::default()
},
)
.unwrap();
loop {
clear_background(WHITE);
draw_texture_ex(
&texture,
0.0,
0.0,
WHITE,
DrawTextureParams {
dest_size: Some(vec2(screen_width(), screen_height())),
..Default::default()
},
);
let lens_center = mouse_position();
lens_material.set_uniform("Center", lens_center);
gl_use_material(&lens_material);
draw_circle(lens_center.0, lens_center.1, 250.0, RED);
gl_use_default_material();
next_frame().await
}
}
examples/exit_dialog.rs (line 20)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
async fn main() {
prevent_quit();
let mut show_exit_dialog = false;
let mut user_decided_to_exit = false;
loop {
clear_background(GRAY);
if is_quit_requested() {
show_exit_dialog = true;
}
if show_exit_dialog {
let dialog_size = vec2(200., 70.);
let screen_size = vec2(screen_width(), screen_height());
let dialog_position = screen_size / 2. - dialog_size / 2.;
Window::new(hash!(), dialog_position, dialog_size).ui(&mut *root_ui(), |ui| {
ui.label(None, "Do you really want to quit?");
ui.separator();
ui.same_line(60.);
if ui.button(None, "Yes") {
user_decided_to_exit = true;
}
ui.same_line(120.);
if ui.button(None, "No") {
show_exit_dialog = false;
}
});
}
if user_decided_to_exit {
break;
}
next_frame().await
}
}