1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
=begin
APIs listing that haven't been encountered in previous sample apps:
- Nested array: An array whose individual elements are also arrays; useful for
storing groups of similar data. Also called multidimensional arrays.
In this sample app, we see nested arrays being used in object definitions.
Notice the parameters for solids, listed below. Parameters 1-3 set the
definition for the rect, and parameter 4 sets the definition of the color.
Instead of having a solid definition that looks like this,
[X, Y, W, H, R, G, B]
we can separate it into two separate array definitions in one, like this
[[X, Y, W, H], [R, G, B]]
and both options work fine in defining our solid (or any object).
- Collections: Lists of data; useful for organizing large amounts of data.
One element of a collection could be an array (which itself contains many elements).
For example, a collection that stores two solid objects would look like this:
[
[100, 100, 50, 50, 0, 0, 0],
[100, 150, 50, 50, 255, 255, 255]
]
If this collection was added to args.outputs.solids, two solids would be output
next to each other, one black and one white.
Nested arrays can be used in collections, as you will see in this sample app.
Reminders:
- args.outputs.solids: An array. The values generate a solid.
The parameters for a solid are
1. The position on the screen (x, y)
2. The width (w)
3. The height (h)
4. The color (r, g, b) (if a color is not assigned, the object's default color will be black)
NOTE: THE PARAMETERS ARE THE SAME FOR BORDERS!
Here is an example of a (red) border or solid definition:
[100, 100, 400, 500, 255, 0, 0]
It will be a solid or border depending on if it is added to args.outputs.solids or args.outputs.borders.
For more information about solids and borders, go to mygame/documentation/03-solids-and-borders.md.
- args.outputs.sprites: An array. The values generate a sprite.
The parameters for sprites are
1. The position on the screen (x, y)
2. The width (w)
3. The height (h)
4. The image path (p)
Here is an example of a sprite definition:
[100, 100, 400, 500, 'sprites/dragonruby.png']
For more information about sprites, go to mygame/documentation/05-sprites.md.
=end
# This code demonstrates the creation and output of objects like sprites, borders, and solids
# If filled in, they are solids
# If hollow, they are borders
# If images, they are sprites
# Solids are added to args.outputs.solids
# Borders are added to args.outputs.borders
# Sprites are added to args.outputs.sprites
# The tick method runs 60 frames every second.
# Your game is going to happen under this one function.
def tick args
border_as_solid_and_solid_as_border args
sprite_as_border_or_solids args
collection_of_borders_and_solids args
collection_of_sprites args
end
# Shows a border being output onto the screen as a border and a solid
# Also shows how colors can be set
def border_as_solid_and_solid_as_border args
border = [0, 0, 50, 50]
args.outputs.borders << border
args.outputs.solids << border
# Red, green, blue saturations (last three parameters) can be any number between 0 and 255
border_with_color = [0, 100, 50, 50, 255, 0, 0]
args.outputs.borders << border_with_color
args.outputs.solids << border_with_color
border_with_nested_color = [0, 200, 50, 50, [0, 255, 0]] # nested color
args.outputs.borders << border_with_nested_color
args.outputs.solids << border_with_nested_color
border_with_nested_rect = [[0, 300, 50, 50], 0, 0, 255] # nested rect
args.outputs.borders << border_with_nested_rect
args.outputs.solids << border_with_nested_rect
border_with_nested_color_and_rect = [[0, 400, 50, 50], [255, 0, 255]] # nested rect and color
args.outputs.borders << border_with_nested_color_and_rect
args.outputs.solids << border_with_nested_color_and_rect
end
# Shows a sprite output onto the screen as a sprite, border, and solid
# Demonstrates that all three outputs appear differently on screen
def sprite_as_border_or_solids args
sprite = [100, 0, 50, 50, 'sprites/ship.png']
args.outputs.sprites << sprite
# Sprite_as_border variable has same parameters (excluding position) as above object,
# but will appear differently on screen because it is added to args.outputs.borders
sprite_as_border = [100, 100, 50, 50, 'sprites/ship.png']
args.outputs.borders << sprite_as_border
# Sprite_as_solid variable has same parameters (excluding position) as above object,
# but will appear differently on screen because it is added to args.outputs.solids
sprite_as_solid = [100, 200, 50, 50, 'sprites/ship.png']
args.outputs.solids << sprite_as_solid
end
# Holds and outputs a collection of borders and a collection of solids
# Collections are created by using arrays to hold parameters of each individual object
def collection_of_borders_and_solids args
collection_borders = [
[
[200, 0, 50, 50], # black border
[200, 100, 50, 50, 255, 0, 0], # red border
[200, 200, 50, 50, [0, 255, 0]], # nested color
],
[[200, 300, 50, 50], 0, 0, 255], # nested rect
[[200, 400, 50, 50], [255, 0, 255]] # nested rect and nested color
]
args.outputs.borders << collection_borders
collection_solids = [
[
[[300, 300, 50, 50], 0, 0, 255], # nested rect
[[300, 400, 50, 50], [255, 0, 255]] # nested rect and nested color
],
[300, 0, 50, 50],
[300, 100, 50, 50, 255, 0, 0],
[300, 200, 50, 50, [0, 255, 0]], # nested color
]
args.outputs.solids << collection_solids
end
# Holds and outputs a collection of sprites by adding it to args.outputs.sprites
# Also outputs a collection with same parameters (excluding position) by adding
# it to args.outputs.solids and another to args.outputs.borders
def collection_of_sprites args
sprites_collection = [
[
[400, 0, 50, 50, 'sprites/ship.png'],
[400, 100, 50, 50, 'sprites/ship.png'],
],
[400, 200, 50, 50, 'sprites/ship.png']
]
args.outputs.sprites << sprites_collection
args.outputs.solids << [
[500, 0, 50, 50, 'sprites/ship.png'],
[500, 100, 50, 50, 'sprites/ship.png'],
[[[500, 200, 50, 50, 'sprites/ship.png']]]
]
args.outputs.borders << [
[
[600, 0, 50, 50, 'sprites/ship.png'],
[600, 100, 50, 50, 'sprites/ship.png'],
],
[600, 200, 50, 50, 'sprites/ship.png']
]
end
|