Como criar o jogo 2048 em Java 8 e JavaFX

3,316 views

Published on

Todo mundo já jogou 2048. E muita gente sabe programar em Java. Nesta palestra veremos como a versão JavaFX do jogo 2048 foi criada, usando animações da plataforma, estilos CSS, e inclusive os novos recursos da linguagem apresentados no Java 8, como Lambda expressions.

Published in: Technology

Como criar o jogo 2048 em Java 8 e JavaFX

  1. 1. Como Criar o Game 2048 Com JavaFX e Java SE 8 Bruno Borges Principal Product Manager Java Evangelist
  2. 2. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 123 • Java Evangelist • Orale Product Manager • Entusiasta JavaFX e IoT • Onde me encontrar • @brunoborges • plus.google.com/+BrunoBorges Bruno Borges
  3. 3. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 124 Agenda • Jogo 2048 • Da idéia à execução, da execução ao Raspberry Pi • Java para game developers • Java SE 8: Lambda expressions, Stream API • JavaFX 8: Conceitos, CSS, Animações, 3D API • Outros Exemplos
  4. 4. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 125 • Mova os números em uma matriz de 4x4 • Quando números iguais se encontram valores são somados • Vence quando atingir 2048 • 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 O jogo 2048
  5. 5. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 126 Onde jogar 2048? • iOS • Android • Web (Javascript) • gabrielecirulli.github.io/2048/ • JavaFX • PCs, Laptops, Tablets e Smartphones (que suportam Oracle Java SE 8), Raspberry Pi e similares Em quase todos os lugares!
  6. 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 127 • Classes de estrutura • Tile, Location, Direction • GameManager • Animações • Eventos • Estilos CSS 2048FX Criando o jogo em JavaFX
  7. 7. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 128 Java Classes Just a few Direction, Location Tile GameManager SessionManager Game2048
  8. 8. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 129 Grid para o 2048 y(i) x(j) Criando um grid com Lambda Expressions
  9. 9. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1210 Direction e Location Estruturas básicas X Y UP 0 -1 RIGHT 1 0 DOWN 0 1 LEFT -1 0
  10. 10. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1211 Tile Representa um “quadrado” no jogo, com valor atribuído Novo tile aleatório. 90% chance para número “2”.
  11. 11. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1212 Eventos de Teclado Ouvindo eventos em JavaFX para movimentar os Tiles Salva estado atual Recupera último estado Ignora teclas que não sejam setas “arrows” Mapeia setas a Directions, chama GameManager
  12. 12. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1213 Eventos de Touchscreen Ações mapeadas usando Lambda Expressions do Java 8 * Swipe images from quojs.tapquo.com
  13. 13. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1214 Animações Movendo os Tiles de um lado para o outro
  14. 14. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1215 Como saber se o jogo acabou? On Finish
  15. 15. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1216 Mostrar fim do jogo gameOverProperty.set(true); ... Try again?
  16. 16. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1217 Java para Game Developers
  17. 17. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1218 Java SE 8
  18. 18. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1219 Lambda Expressions Listeners Com Java SE 8 Com Java SE 7
  19. 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1220 Lambda Expressions e Stream API Exemplo Com Java SE 8 Com Java SE 7
  20. 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1221 Lambda Expression como Parâmetro Método que recebe uma expressão Lambda
  21. 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1222 Parallel Stream Encontrar Locations disponíveis no grid, usando multi-thread
  22. 22. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1223 JavaFX 8
  23. 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1224 Arquitetura do JavaFX • Prism – renderização nativa • OpenGL (Mac, Linux, Embedded) • Direct 3D (9 em Windows XP/Vista, 11 em Windows 7) • Java 2D (software rendering) • Scene Graph • Nodes • States • Effects
  24. 24. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1225 JavaFX 3D
  25. 25. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1226 26 Criando Formas e Materiais Primitivos
  26. 26. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1227 Colocando Textura em uma Esfera 27
  27. 27. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1228 Colocando Textura em uma Esfera 28
  28. 28. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1229 Colocando Textura em uma Esfera 29
  29. 29. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1230 Para saber mais  Documentação do Java 8:  bit.ly/java8docs  Código-fonte do jogo 2048FX  github.com/brunoborges/fx2048  Twitter: @brunoborges
  30. 30. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | ORACLE e V.OFFICE Parceria WDP (Workforce Development Program) A Oracle University tem o prazer de anunciar a V.OFFICE Treinamentos como nosso mais novo parceiro WDP. A Oracle Corporation lançou o WDP para resolver o déficit global e contínuo de trabalhadores na área de TI e a necessidade de treinamento acessível e de baixo custo nas habilidades correspondentes. O WDP permite que as instituições educacionais participantes forneçam treinamento em Oracle em tempo integral e meio-período a alunos em suas comunidades locais.
  31. 31. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1232 The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
  32. 32. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1233
  33. 33. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1234

×