miércoles, 19 de junio de 2019

Creando videojuegos en Rust lang (Parte 30)

En esta entrada seguiremos agregando funcionalidad a nuestro ejemplo de Snake terminado. En esta entrada agregaremos el contador del puntaje del juego y usaremos las librerías de ggez para mostrar este texto en la pantalla del juego. Dentro de los ejemplos en la página de github de "ggez" tenemos un ejemplo de uso de texto usando las librarías que nos proporciona ggez: https://github.com/ggez/ggez/blob/master/examples/text.rs

Crearemos esta versión con sonido usando como base lo que ya tenemos en "hello_ggez11". En esta entrada crearemos "cargo new hello_ggez12". Usaremos el archivo TOML de la entrada anterior:

[package]
name = "hello_ggez12"
version = "0.1.0"
authors = ["MiUsuario <micorreo@correo.com>"]
edition = "2018"

[dependencies]
ggez = "0.5.0-rc.1"
rand = "0.6.5"

Para agregar el texto tenemos que agregar algunas líneas y bloques nuevos de código:
  • Igual al ejemplo de texto usando ggez agregamos las referencias a "ggez::graphics::Text":
    • use ggez::graphics::Text;
      
  • Necesitamos un lugar para guardar el puntaje actual. Pondremos este puntaje actual en el struct del EstadoDelJuego como "puntaje" un "i16":
      struct EstadoDelJuego {
          jugador_x: i16,
          jugador_y: i16,
          cuerpo: VecDeque<(i16, i16)>,
          direccion: Direccion,
          estado: Estado,
          comida_x: i16,
          comida_y: i16,
          sonido: audio::Source,
          puntaje: i16,
          ultima_actualizacion: std::time::Instant
      }
      
  • Como agregamos el puntaje a nuestro struct EstadoDelJuego entonces hay que inicializar este puntaje a cero en nuestra estructura al inicio del juego, es decir, antes de entrar al bucle del juego:
    • let estado_del_juego = &mut EstadoDelJuego {
          jugador_x: DIMENSION_DEL_TABLERO.0 / 4,
          jugador_y: DIMENSION_DEL_TABLERO.1 / 2,
          cuerpo: VecDeque::new(),
          direccion: Direccion::Derecha,
          estado: Estado::Jugando,
          comida_x: ini_comida_x,
          comida_y: ini_comida_y,
          sonido: audio::Source::new(
              contexto, "/sonido.ogg").unwrap(),
          puntaje: 0i16,
          ultima_actualizacion: Instant::now()
      };
      
  • Necesitamos incrementar el contador cada vez que la serpiente coma un pedazo de comida. Esto lo hacemos dentro de nuestro método "Update", dentro del condicional ("if") donde evalúa si la serpiente comió la comida:
    • if self.jugador_x == self.comida_x &&
         self.jugador_y == self.comida_y {
          let _correr_sonido = self.sonido.play_detached();
          //(...) Resto del código
          self.cuerpo.push_front((self.jugador_x, self.jugador_y));
          self.puntaje = self.puntaje + 1;
      } else {
          self.cuerpo.pop_back();
          self.cuerpo.push_front((self.jugador_x, self.jugador_y));
      }
      
  • Finalmente agregamos una secuencia en el método "Draw" para que muestre el puntaje cuando el estado del juego sea "GameOver". Aquí es donde usamos "Text::new" para crear nuestro texto, después simplemente le pasamos esta variable (texto_puntaje) a "graphics::draw":
    • if self.estado == Estado::GameOver {
          let texto_puntaje = Text::new(format!(
              "Game Over\nPuntaje:{}", self.puntaje));
          graphics::draw(
              contexto,
              &texto_puntaje,
              (ggez::mint::Point2 { x: 200.0, y: 0.0 },)
          )?;
      }
      
Con lo anterior nuestro juego ahora tiene un texto que aparece durante los GameOver que nos muestra el puntaje que acumulamos. Este es el código completo:

  1
  2
  3
  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
 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use ggez::{conf, ContextBuilder, Context, 
           event, graphics, GameResult};
use std::time::{Duration, Instant};

use rand::prelude::*;

use std::collections::VecDeque;

use ggez::audio;
use ggez::audio::SoundSource;

use ggez::graphics::Text;

const DIMENSION_DEL_TABLERO: (i16, i16) = (10, 10);
const DIMENSION_DE_CELDAS_DEL_TABLERO: (i16, i16) = (32, 32);
const DIMENSION_DE_VENTANA: (f32, f32) = (
    DIMENSION_DEL_TABLERO.0 as f32 * 
    DIMENSION_DE_CELDAS_DEL_TABLERO.0 as f32,
    DIMENSION_DEL_TABLERO.1 as f32 * 
    DIMENSION_DE_CELDAS_DEL_TABLERO.1 as f32,
);

const ACTUALIZACIONES_POR_SEGUNDO: f32 = 8.0;
const MILISEG_POR_ACTUALIZACION: u64 = (
    (1.0 / ACTUALIZACIONES_POR_SEGUNDO) * 1000.0) as u64;

#[derive(PartialEq)]
enum Direccion {
    Arriba,
    Abajo,
    Izquierda,
    Derecha,
}

#[derive(PartialEq)]
enum Estado {
    Jugando,
    GameOver
}

struct EstadoDelJuego {
    jugador_x: i16,
    jugador_y: i16,
    cuerpo: VecDeque<(i16, i16)>,
    direccion: Direccion,
    estado: Estado,
    comida_x: i16,
    comida_y: i16,
    sonido: audio::Source,
    puntaje: i16,
    ultima_actualizacion: std::time::Instant
}

impl ggez::event::EventHandler for EstadoDelJuego {
    fn update(&mut self, 
            _contexto: &mut Context) -> GameResult<()> {
        if (Instant::now() - 
            self.ultima_actualizacion >= 
            Duration::from_millis(MILISEG_POR_ACTUALIZACION)) &
           (self.estado == Estado::Jugando) {
            match self.direccion {
                Direccion::Arriba => {
                self.jugador_y = self.jugador_y - 1;
                if self.jugador_y < 0 {
                    self.estado = Estado::GameOver;
                    self.jugador_y = self.jugador_y + 1;
                } else {
                    for parte in self.cuerpo.iter() {
                        if parte.0 == self.jugador_x &&
                           parte.1 == self.jugador_y {
                            self.estado = Estado::GameOver;
                            self.jugador_y = self.jugador_y + 1;
                            break;
                        }
                    }
                }},
                Direccion::Abajo => {
                self.jugador_y = self.jugador_y + 1;
                if self.jugador_y > DIMENSION_DEL_TABLERO.1 - 1 {
                    self.estado = Estado::GameOver;
                    self.jugador_y = self.jugador_y - 1;
                } else {
                    for parte in self.cuerpo.iter() {
                        if parte.0 == self.jugador_x &&
                           parte.1 == self.jugador_y {
                            self.estado = Estado::GameOver;
                            self.jugador_y = self.jugador_y - 1;
                            break;
                        }
                    }
                }},
                Direccion::Izquierda => {
                self.jugador_x = self.jugador_x - 1;
                if self.jugador_x < 0 {
                    self.estado = Estado::GameOver;
                    self.jugador_x = self.jugador_x + 1;
                } else {
                    for parte in self.cuerpo.iter() {
                        if parte.0 == self.jugador_x &&
                           parte.1 == self.jugador_y {
                            self.estado = Estado::GameOver;
                            self.jugador_x = self.jugador_x + 1;
                            break;
                        }
                    }
                }},
                Direccion::Derecha => {
                self.jugador_x = self.jugador_x + 1;
                if self.jugador_x > DIMENSION_DEL_TABLERO.0 - 1 {
                    self.estado = Estado::GameOver;
                    self.jugador_x = self.jugador_x - 1;
                } else {
                    for parte in self.cuerpo.iter() {
                        if parte.0 == self.jugador_x &&
                           parte.1 == self.jugador_y {
                            self.estado = Estado::GameOver;
                            self.jugador_x = self.jugador_x - 1;
                            break;
                        }
                    }
                }}
            }
            if self.jugador_x == self.comida_x &&
               self.jugador_y == self.comida_y {
                let _correr_sonido = self.sonido.play_detached();
                let mut rng = rand::thread_rng();
                let mut ini_comida_x = rng.gen_range::<i16, i16, i16>(0, 
                    DIMENSION_DEL_TABLERO.0);
                let mut ini_comida_y = rng.gen_range::<i16, i16, i16>(0, 
                    DIMENSION_DEL_TABLERO.1);
                loop {
                    if self.jugador_x != ini_comida_x &&
                       self.jugador_y != ini_comida_y {
                        let mut ocupado = false;
                        for parte_de_cuerpo in self.cuerpo.iter() {
                            if parte_de_cuerpo.0 == ini_comida_x &&
                               parte_de_cuerpo.1 == ini_comida_y {
                                ocupado = true;
                                break;
                            }
                        }
                        if ocupado == false {
                            break;
                        }
                    }
                    ini_comida_x = rng.gen_range::<i16, i16, i16>(0, 
                        DIMENSION_DEL_TABLERO.0);
                    ini_comida_y = rng.gen_range::<i16, i16, i16>(0, 
                        DIMENSION_DEL_TABLERO.1);
                }
                self.comida_x = ini_comida_x;
                self.comida_y = ini_comida_y;
                self.cuerpo.push_front((self.jugador_x, self.jugador_y));
                self.puntaje = self.puntaje + 1;
            } else {
                self.cuerpo.pop_back();
                self.cuerpo.push_front((self.jugador_x, self.jugador_y));
            }
            self.ultima_actualizacion = Instant::now();
        }
        Ok(())
    }
    
    fn draw(&mut self, 
            contexto: &mut Context) -> GameResult<()> {
        graphics::clear(contexto, [0.0, 1.0, 0.0, 1.0].into());
        // Dibujar el cuerpo de la serpiente
        for parte_del_cuerpo in self.cuerpo.iter() {
            let rectangulo_cuerpo = ggez::graphics::Rect::new(
                (parte_del_cuerpo.0 *
                 DIMENSION_DE_CELDAS_DEL_TABLERO.0) as f32, 
                (parte_del_cuerpo.1 *
                 DIMENSION_DE_CELDAS_DEL_TABLERO.1) as f32,
                DIMENSION_DE_CELDAS_DEL_TABLERO.0 as f32,
                DIMENSION_DE_CELDAS_DEL_TABLERO.1 as f32);
            let grafico_de_cuerpo = graphics::Mesh::new_rectangle(
                contexto,
                graphics::DrawMode::fill(),
                rectangulo_cuerpo,
                [9.0, 0.1, 0.0, 1.0].into())?;
            graphics::draw(contexto, 
                &grafico_de_cuerpo, 
                (ggez::mint::Point2 { x: 0.0, y: 0.0 },))?;
        }
        // Dibujar el jugador
        let rectangulo_jugador = ggez::graphics::Rect::new(
            (self.jugador_x *
             DIMENSION_DE_CELDAS_DEL_TABLERO.0) as f32, 
            (self.jugador_y *
             DIMENSION_DE_CELDAS_DEL_TABLERO.1) as f32,
            DIMENSION_DE_CELDAS_DEL_TABLERO.0 as f32,
            DIMENSION_DE_CELDAS_DEL_TABLERO.1 as f32);
        let grafico_de_jugador = graphics::Mesh::new_rectangle(
            contexto,
            graphics::DrawMode::fill(),
            rectangulo_jugador,
            [0.3, 0.3, 0.0, 1.0].into())?;
        graphics::draw(contexto, 
            &grafico_de_jugador, 
            (ggez::mint::Point2 { x: 0.0, y: 0.0 },))?;
        // Dibujar la comida
        let rectangulo_comida = ggez::graphics::Rect::new(
            (self.comida_x *
             DIMENSION_DE_CELDAS_DEL_TABLERO.0) as f32, 
            (self.comida_y *
             DIMENSION_DE_CELDAS_DEL_TABLERO.1) as f32,
            DIMENSION_DE_CELDAS_DEL_TABLERO.0 as f32,
            DIMENSION_DE_CELDAS_DEL_TABLERO.1 as f32);
        let grafico_de_comida = graphics::Mesh::new_rectangle(
            contexto,
            graphics::DrawMode::fill(),
            rectangulo_comida,
            [0.0, 0.0, 1.0, 1.0].into())?;
        graphics::draw(contexto, 
            &grafico_de_comida, 
            (ggez::mint::Point2 { x: 0.0, y: 0.0 },))?;
        if self.estado == Estado::GameOver {
            let texto_puntaje = Text::new(format!(
                "Game Over\nPuntaje:{}", self.puntaje));
            graphics::draw(
                contexto,
                &texto_puntaje,
                (ggez::mint::Point2 { x: 200.0, y: 0.0 },)
            )?;
        }
        // Mandando al generador de gráficos
        graphics::present(contexto)?;
        ggez::timer::yield_now();
        Ok(())
    }
    
    fn key_down_event(
        &mut self,
        _ctx: &mut Context,
        keycode: ggez::event::KeyCode,
        _keymod: ggez::event::KeyMods,
        _repeat: bool,
    ) {
        match keycode {
            ggez::event::KeyCode::Up => {
                if self.direccion != Direccion::Abajo {
                    self.direccion = Direccion::Arriba
                }
            },
            ggez::event::KeyCode::Down => {
                if self.direccion != Direccion::Arriba {
                    self.direccion = Direccion::Abajo
                }
            },
            ggez::event::KeyCode::Left => {
                if self.direccion != Direccion::Derecha {
                    self.direccion = Direccion::Izquierda
                }
            },
            ggez::event::KeyCode::Right => {
                if self.direccion != Direccion::Izquierda {
                    self.direccion = Direccion::Derecha
                }
            },
            _ => (),
        }
        ()
    }
}

fn main() {
    let mut rng = rand::thread_rng();
    let mut ini_comida_x = rng.gen_range::<i16, i16, i16>(0, 
        DIMENSION_DEL_TABLERO.0);
    let mut ini_comida_y = rng.gen_range::<i16, i16, i16>(0, 
        DIMENSION_DEL_TABLERO.1);
    loop {
        if DIMENSION_DEL_TABLERO.0 / 4 != ini_comida_x &&
           DIMENSION_DEL_TABLERO.1 / 2 != ini_comida_y {
            break;
        }
        ini_comida_x = rng.gen_range::<i16, i16, i16>(0, 
            DIMENSION_DEL_TABLERO.0);
        ini_comida_y = rng.gen_range::<i16, i16, i16>(0, 
            DIMENSION_DEL_TABLERO.1);
    }
    let mut configuracion = conf::Conf::new();
    configuracion.window_setup = conf::WindowSetup::default()
        .title("Snake");
    configuracion.window_mode = conf::WindowMode::default()
        .dimensions(DIMENSION_DE_VENTANA.0, 
                    DIMENSION_DE_VENTANA.1);
    let (ref mut contexto, 
         ref mut bucle_de_juego) = ContextBuilder::new(
            "hello_ggez", "autor")
        .conf(configuracion).build().unwrap();
    let estado_del_juego = &mut EstadoDelJuego {
        jugador_x: DIMENSION_DEL_TABLERO.0 / 4,
        jugador_y: DIMENSION_DEL_TABLERO.1 / 2,
        cuerpo: VecDeque::new(),
        direccion: Direccion::Derecha,
        estado: Estado::Jugando,
        comida_x: ini_comida_x,
        comida_y: ini_comida_y,
        sonido: audio::Source::new(
            contexto, "/sonido.ogg").unwrap(),
        puntaje: 0i16,
        ultima_actualizacion: Instant::now()
    };
    event::run(contexto, bucle_de_juego, 
        estado_del_juego).unwrap();
}

Esta es una captura de la pantalla del game over:

Navegación:
Primera parte
Siguiente parte
Parte anterior

Fuentes (inglés):

No hay comentarios.:

Publicar un comentario