54
Mims H. Wright Freelance Software Developer [email protected] @mimshwright github.com/mimshwright/

Mimstris: Building an arcade puzzle game in React / Redux

Embed Size (px)

Citation preview

Page 1: Mimstris: Building an arcade puzzle game in React / Redux

Mims H. Wright Freelance Software Developer

[email protected] @mimshwright

github.com/mimshwright/

Page 2: Mimstris: Building an arcade puzzle game in React / Redux

Mimstris:Building an arcade puzzle

game in React / Redux

Page 3: Mimstris: Building an arcade puzzle game in React / Redux

Why? How?

What?

Choosing to make Tetris

Challenges of coding a game in react

Takeaways from the experience

Page 4: Mimstris: Building an arcade puzzle game in React / Redux

Why?

Page 5: Mimstris: Building an arcade puzzle game in React / Redux
Page 6: Mimstris: Building an arcade puzzle game in React / Redux
Page 7: Mimstris: Building an arcade puzzle game in React / Redux

That sounds fun• Play with my son

• Make crazy shapes and stuff • Try out some new frameworks, etc.

Page 8: Mimstris: Building an arcade puzzle game in React / Redux

JavaScript circa 2014

Page 9: Mimstris: Building an arcade puzzle game in React / Redux
Page 10: Mimstris: Building an arcade puzzle game in React / Redux
Page 11: Mimstris: Building an arcade puzzle game in React / Redux
Page 12: Mimstris: Building an arcade puzzle game in React / Redux

const game = () =>

Needs to be ES2017 cuz it’s 2017, obvs.

Page 13: Mimstris: Building an arcade puzzle game in React / Redux
Page 14: Mimstris: Building an arcade puzzle game in React / Redux

ReductrisI’ll call it…

Page 15: Mimstris: Building an arcade puzzle game in React / Redux

👈

Page 16: Mimstris: Building an arcade puzzle game in React / Redux

Reductris Reactris

I’ll call it…

Page 17: Mimstris: Building an arcade puzzle game in React / Redux
Page 18: Mimstris: Building an arcade puzzle game in React / Redux

Reductris Reactris Mimstris

I’ll call it…

Page 19: Mimstris: Building an arcade puzzle game in React / Redux

How?

Page 20: Mimstris: Building an arcade puzzle game in React / Redux

1. 2. 3.

Building the game

Rendering to canvas

The Redux Store

Page 21: Mimstris: Building an arcade puzzle game in React / Redux

1. 2. 3.

Building the game

Rendering to canvas

The Redux Store

Page 22: Mimstris: Building an arcade puzzle game in React / Redux

board = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],]

Page 23: Mimstris: Building an arcade puzzle game in React / Redux

piece = [ [ 0, 1, 0 ], [ 0, 1, 0 ], [ 0, 1, 1 ], ]

Page 24: Mimstris: Building an arcade puzzle game in React / Redux

board = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],]

Page 25: Mimstris: Building an arcade puzzle game in React / Redux

“Game Loop”

init()

update()

draw()

every frame

Page 26: Mimstris: Building an arcade puzzle game in React / Redux

• Timing of piece movement (time since last update) • Responding to user input (see pressed later) • Piece matrix position and rotation • Collision detection (for each square, if they are both non-zero) • Clearing lines (If a whole row is non-zero) • Score keeping • Configuration

Inside update()

Page 27: Mimstris: Building an arcade puzzle game in React / Redux

Push Method (event-driven)

Page 28: Mimstris: Building an arcade puzzle game in React / Redux

Pull Method (polling)

Page 29: Mimstris: Building an arcade puzzle game in React / Redux

Pressed.js

Page 30: Mimstris: Building an arcade puzzle game in React / Redux

I had a problem.

I saw there were several good solutions.

I made my own that was sliiiightly different.

Page 31: Mimstris: Building an arcade puzzle game in React / Redux

pressed.js

Tracks which keys are currently pressed

Page 32: Mimstris: Building an arcade puzzle game in React / Redux

1. 2. 3.

Building the game

Rendering to canvas

The Redux Store

Page 33: Mimstris: Building an arcade puzzle game in React / Redux

init()

Before React

update()

draw()

init()

update()

react

draw()

every frame every frame

componentDidUpdate()

render()

React + Redux

init()

update()

reduxstore

react

draw()

every frame

componentDidUpdate()

subscribe() / connect()

dispatch()

Page 34: Mimstris: Building an arcade puzzle game in React / Redux
Page 35: Mimstris: Building an arcade puzzle game in React / Redux

canvasRenderer.js

drawGame(board, piece)

clear()

drawMatrix(board)

drawBlock(x,y,color)

drawMatrix(piece)

drawBlock(x,y,color)

Page 36: Mimstris: Building an arcade puzzle game in React / Redux

1. 2. 3.

Building the game

Rendering to canvas

The Redux Store

Page 37: Mimstris: Building an arcade puzzle game in React / Redux
Page 38: Mimstris: Building an arcade puzzle game in React / Redux

What?

Page 40: Mimstris: Building an arcade puzzle game in React / Redux

Takeaways…

Page 41: Mimstris: Building an arcade puzzle game in React / Redux

• Babel / ES6 • React • Redux (incl. reselect, redux-acitons, etc.) • Ducks • Lodash • Writing an npm module • Mocha, Chai, Tape, AVA • Atom

Things I tried for the first time…

Page 42: Mimstris: Building an arcade puzzle game in React / Redux

😍 ES6 😍

Page 43: Mimstris: Building an arcade puzzle game in React / Redux

Config files!

Page 44: Mimstris: Building an arcade puzzle game in React / Redux

• rendering • matrix maths

Use modules to handle isolated functionality

Makes it much easier to refactor later

Page 45: Mimstris: Building an arcade puzzle game in React / Redux

Weird pieces are fun but they’re not in the game for a reason.

Page 46: Mimstris: Building an arcade puzzle game in React / Redux

• Actions • Action Creators • Reducers • Selectors

Ducks is a guideline for combining…

…into a single file.

( Ask me about boilerplate-reducer)

Ducks.

Page 47: Mimstris: Building an arcade puzzle game in React / Redux

Stress saver

Page 48: Mimstris: Building an arcade puzzle game in React / Redux

just works. Drawing to the canvas in React

Page 49: Mimstris: Building an arcade puzzle game in React / Redux

Unit test early… for your sanity

Page 50: Mimstris: Building an arcade puzzle game in React / Redux

a robot who is clicking on things

update() is

Page 51: Mimstris: Building an arcade puzzle game in React / Redux

Memoize!

Page 52: Mimstris: Building an arcade puzzle game in React / Redux

Make a thing for fun.

Page 53: Mimstris: Building an arcade puzzle game in React / Redux

mimstris.surge.shgithub.com/mimshwright/mimstris

Page 54: Mimstris: Building an arcade puzzle game in React / Redux

Mims H. Wright Freelance Software Developer

[email protected] @mimshwright

github.com/mimshwright/