© 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.