Top Banner
Como Criar o Game 2048 Com JavaFX e Java SE 8 Bruno Borges Principal Product Manager Java Evangelist
33

Como criar o jogo 2048 em Java 8 e JavaFX

Nov 11, 2014

Download

Technology

Bruno Borges

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Como criar o jogo 2048 em Java 8 e JavaFX

Como Criar o Game 2048Com JavaFX e Java SE 8Bruno BorgesPrincipal Product ManagerJava Evangelist

Page 2: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 3: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 4: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 5: Como criar o jogo 2048 em Java 8 e JavaFX

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!

Page 6: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 7: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 128

Java ClassesJust a few

Direction, Location

Tile

GameManager

SessionManager

Game2048

Page 8: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 129

Grid para o 2048y(i)

x(j)

Criando um grid com Lambda Expressions

Page 9: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1210

Direction e LocationEstruturas básicas

X Y

UP 0 -1

RIGHT 1 0

DOWN 0 1

LEFT -1 0

Page 10: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1211

TileRepresenta um “quadrado” no jogo, com valor atribuído

Novo tile aleatório. 90% chance para número “2”.

Page 11: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1212

Eventos de TecladoOuvindo 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

Page 12: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1213

Eventos de TouchscreenAções mapeadas usando Lambda Expressions do Java 8

* Swipe images from quojs.tapquo.com

Page 13: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1214

AnimaçõesMovendo os Tiles de um lado para o outro

Page 14: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 15: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1216

Mostrar fim do jogogameOverProperty.set(true); ... Try again?

Page 16: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1217

Java para Game Developers

Page 17: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1218

Java SE 8

Insert Picture Here

Page 18: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1219

Lambda ExpressionsListeners

Com Java SE 8

Com Java SE 7

Page 19: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1220

Lambda Expressions e Stream APIExemplo

Com Java SE 8

Com Java SE 7

Page 20: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1221

Lambda Expression como ParâmetroMétodo que recebe uma expressão Lambda

Page 21: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1222

Parallel StreamEncontrar Locations disponíveis no grid, usando multi-thread

Page 22: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1223

JavaFX 8

Insert Picture Here

Page 23: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 24: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1225

JavaFX 3D

Page 25: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1226 26

Insert Chart Here

Criando Formas e Materiais Primitivos

Page 26: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1227

Colocando Textura em uma Esfera

27

Page 27: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1228

Colocando Textura em uma Esfera

28

Page 28: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1229

Colocando Textura em uma Esfera

29

Page 29: Como criar o jogo 2048 em Java 8 e JavaFX

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

Page 30: Como criar o jogo 2048 em Java 8 e JavaFX

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.

Page 31: Como criar o jogo 2048 em Java 8 e JavaFX

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.

Page 32: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1233

Page 33: Como criar o jogo 2048 em Java 8 e JavaFX

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1234