Our use case – a space war game

Throughout this chapter, we will illustrate programming concepts by building parts of a space war game. The design of the game is very simple and straightforward. It consists of game pieces such as spaceships and asteroids that are scattered around a two-dimensional grid. These game pieces are called widgets in our program.

Let's first define our data types as follows:

# Space war game!

mutable struct Position
x::Int
y::Int
end

struct Size
width::Int
height::Int
end

struct Widget
name::String
position::Position
size::Size
end

As data types are central to our design, this warrants a little more explanation:

  • The Position type is used to store the coordinates of a game piece. It is represented by two integers: x and y.
  • The Size type is used to store the size of a game piece. It is represented by two integers: width and height.
  • The Widget type is used to hold a single game piece. It is represented by a name, position, and size.

Note that the Position type is mutable because we expect our game pieces to move around by just updating their coordinates.

Next, we will go over a number of topics related to how functions are defined.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset