© Graeme Stuart 2017

Graeme Stuart, Introducing JavaScript Game Development , https://doi.org/10.1007/978-1-4842-3252-1_5

5. Drawing an Asteroid

Graeme Stuart

(1)Market Harborough, Leicestershire, UK

Asteroids, like ships, can be approximated to a circle for the purposes of collision detection. However, they should be irregular shapes, and each one should be unique. To achieve this we’ll make extensive use of the Math.random method to define the shape of our asteroids.

Drawing Basic Shapes

As a first stab, we’ll divide our circle into segments and draw a straight line from each segment to the next. We can do that by rotating the canvas in steps and drawing a line at each step before closing the path. We’ll want some randomness in the distance of the ...

Get Introducing JavaScript Game Development : Build a 2D Game from the Ground Up now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.