Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Workshop, part II - Creating a game with Gosu

Ruby Workshop, part II - Creating a game with Gosu

Creating a game with Ruby/Gosu, SINFORM - UESC

Avatar for Roberto Soares

Roberto Soares

September 17, 2013
Tweet

More Decks by Roberto Soares

Other Decks in Programming

Transcript

  1. Biblioteca Gosu Jogos 2D Hello Game! Crie uma pasta para

    o jogo (m k d i r ) Crie um arquivo g a m e . r b r e q u i r e ' g o s u ' c l a s s G a m e < G o s u : : W i n d o w d e f i n i t i a l i z e s u p e r ( 6 0 0 , 6 0 0 , f a l s e ) e n d e n d g a m e = G a m e . n e w g a m e . s h o w 3 / 19
  2. r e q u i r e ' g o

    s u ' Disponibilizar componentes de outros arquivos Importa Gosu::Window c l a s s G a m e < G o s u : : W i n d o w 4 / 19
  3. d e f i n i t i a l

    i z e Construtor. Inicializa o objeto. g a m e = G a m e . n e w Aloca espaço na memória e executa o método initialize. 5 / 19
  4. s u p e r ( 6 0 0 ,

    6 0 0 , f a l s e ) Chama o mesmo método na super classe (ou classe mãe). No caso o construtor. 6 / 19
  5. g a m e . s h o w Executa

    o método s h o w herdado de Gosu::Window. Qualquer método de Gosu::Window pode ser utilizado. 7 / 19
  6. Alterar o título da janela s e l f .

    c a p t i o n = ' S k y C l e a n u p ' g a m e . c a p t i o n = ' S k y C l e a n u p ' caption é um método de instância de Gosu::Window. 8 / 19
  7. Adicionar o background Arquivos no gist. Utilizando a classe Gosu::Image.

    @ b a c k g r o u n d = G o s u : : I m a g e . n e w ( s e l f , " i m a g e s / b g . p n g " , t r u e ) s e l f Referência ao seu objeto, instância de Game. Cada imagem no Gosu deve estar relacionada com uma Janela (Window). d e f i n i t i a l i z e s u p e r ( 6 0 0 , 6 0 0 , f a l s e ) s e l f . c a p t i o n = ' S k y C l e a n u p ' @ b a c k g r o u n d = G o s u : : I m a g e . n e w ( s e l f , " i m a g e s / b g . p n g " , t r u e ) e n d 9 / 19
  8. Desenhando o Background Definimos o método draw. d e f

    d r a w @ b a c k g r o u n d . d r a w ( 0 , 0 , 0 ) e n d x , y , z 10 / 19
  9. Adicionar o avião Criar arquivo 'plane.rb' r e q u

    i r e _ r e l a t i v e ' p l a n e ' c l a s s P l a n e d e f i n i t i a l i z e ( g a m e ) @ g a m e = g a m e @ x = 1 0 0 @ y = 1 0 0 @ s p r i t e = G o s u : : I m a g e . l o a d _ t i l e s ( g a m e , " i m a g e s / p l a n e s . p n g " , 6 6 , 6 7 , t r u e ) [ 0 . . 2 ] e n d d e f d r a w s p r i t e = @ s p r i t e [ G o s u : : m i l l i s e c o n d s / 1 0 0 % @ s p r i t e . s i z e ] s p r i t e . d r a w _ r o t ( @ x , @ y , 1 , 0 ) e n d e n d gist 12 / 19
  10. Alterando a classe Game: d e f i n i

    t i a l i z e s u p e r ( 6 0 0 , 6 0 0 , f a l s e ) # . . . @ p l a n e = P l a n e . n e w ( s e l f ) e n d d e f d r a w @ p l a n e . d r a w @ b a c k g r o u n d . d r a w ( 0 , 0 , 0 ) e n d 13 / 19
  11. Tarefas Alterar a cor do avião (mudando o sprite usado)

    Executar o programa Calcular uma melhor posição inicial do avião Utilizar game.width (largura) e game.height (altura) 14 / 19
  12. Movimentar o avião Método update, executado a cada atualização. d

    e f u p d a t e @ p l a n e . l e f t i f b u t t o n _ d o w n ? G o s u : : K b L e f t @ p l a n e . r i g h t i f b u t t o n _ d o w n ? G o s u : : K b R i g h t e n d 15 / 19
  13. Adicionando asteróides (gist) c l a s s A s

    t e r o i d d e f i n i t i a l i z e ( g a m e ) @ s p r i t e = G o s u : : I m a g e . l o a d _ t i l e s ( g a m e , " i m a g e s / a s t e r o i d s . p n g " , 3 2 0 / 5 , 3 8 4 / 6 , t r u e ) @ x , @ y = 1 0 0 , 1 0 0 e n d d e f u p d a t e e n d d e f d r a w s p r i t e = @ s p r i t e [ G o s u : : m i l l i s e c o n d s / 5 0 % @ s p r i t e . s i z e ] s p r i t e . d r a w _ r o t ( @ x , @ y , 3 , 0 ) e n d e n d Alterando a classe Game: d e f i n i t i a l i z e @ a s t e r o i d = A s t e r o i d . n e w ( s e l f ) e n d d e f u p d a t e @ a s t e r o i d . u p d a t e e n d d e f d r a w @ a s t e r o i d . d r a w e n d 17 / 19
  14. Tarefas Iniciar do topo Queda. (atualizar posição durante update) Iniciar

    numa posição randômica, utilizando Gosu.random(?, ?) Ao sair da tela, deve voltar para o topo em outra posição. Colisão! Exemplo: Gosu::distance(a.x, a.y, b.x, b.y) < distance Verificar durante udpate Adicionar um asteróide a cada 10 segundos Gosu::milliseconds 18 / 19