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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
require 'app/nokia.rb'
def tick args
# =======================================================================
# ==== HELLO WORLD ======================================================
# =======================================================================
# Steps to get started:
# 1. ~def tick args~ is the entry point for your game.
# 2. There are quite a few code samples below, remove the "##"
# before each line and save the file to see the changes.
# 3. 0, 0 is in bottom left and 83, 47 is in top right corner.
# 4. Be sure to come to the discord channel if you need
# more help: [[http://discord.dragonruby.org]].
# Commenting and uncommenting code:
# - Add a "#" infront of lines to comment out code
# - Remove the "#" infront of lines to comment out code
# Invoke the hello_world subroutine/method
hello_world args # <---- add a "#" to the beginning of the line to stop running this subroutine/method.
# =======================================================================
# ==== HOW TO RENDER A LABEL ============================================
# =======================================================================
# Uncomment the line below to invoke the how_to_render_a_label subroutine/method.
# Note: The method is defined in this file with the signature ~def how_to_render_a_label args~
# Scroll down to the method to see the details.
# Remove the "#" at the beginning of the line below
# how_to_render_a_label args # <---- remove the "#" at the beginning of this line to run the method
# =======================================================================
# ==== HOW TO RENDER A FILLED SQUARE (SOLID) ============================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_render_solids args
# =======================================================================
# ==== HOW TO RENDER AN UNFILLED SQUARE (BORDER) ========================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_render_borders args
# =======================================================================
# ==== HOW TO RENDER A LINE =============================================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_render_lines args
# =======================================================================
# == HOW TO RENDER A SPRITE =============================================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_render_sprites args
# =======================================================================
# ==== HOW TO MOVE A SPRITE BASED OFF OF USER INPUT =====================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_move_a_sprite args
# =======================================================================
# ==== HOW TO ANIMATE A SPRITE (SEPERATE PNGS) ==========================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_animate_a_sprite args
# =======================================================================
# ==== HOW TO ANIMATE A SPRITE (SPRITE SHEET) ===========================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_animate_a_sprite_sheet args
# =======================================================================
# ==== HOW TO DETERMINE COLLISION =============================================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_determine_collision args
# =======================================================================
# ==== HOW TO CREATE BUTTONS ==================================================
# =======================================================================
# Remove the "#" at the beginning of the line below
# how_to_create_buttons args
# ==== The line below renders a debug grid, mouse information, and current tick
# render_debug args
end
# =======================================================================
# ==== HELLO WORLD ======================================================
# =======================================================================
def hello_world args
args.nokia.solids << { x: 0, y: 64, w: 10, h: 10, r: 255 }
args.nokia.labels << {
x: 42,
y: 46,
text: "nokia 3310 jam 3",
size_enum: NOKIA_FONT_SM,
alignment_enum: 1,
r: 0,
g: 0,
b: 0,
a: 255,
font: NOKIA_FONT_PATH
}
args.nokia.sprites << {
x: 42 - 10,
y: 26 - 10,
w: 20,
h: 20,
path: 'sprites/monochrome-ship.png',
a: 255,
angle: args.state.tick_count % 360
}
end
# =======================================================================
# ==== HOW TO RENDER A LABEL ============================================
# =======================================================================
def how_to_render_a_label args
# NOTE: Text is aligned from the TOP LEFT corner
# Render an EXTRA LARGE/XL label (remove the "#" in front of each line below)
args.nokia.labels << { x: 0, y: 46, text: "Hello World",
size_enum: NOKIA_FONT_XL,
r: 0, g: 0, b: 0, a: 255,
font: NOKIA_FONT_PATH }
# Render a LARGE/LG label (remove the "#" in front of each line below)
args.nokia.labels << { x: 0, y: 29, text: "Hello World",
size_enum: NOKIA_FONT_LG,
r: 0, g: 0, b: 0, a: 255,
font: NOKIA_FONT_PATH }
# Render a MEDIUM/MD label (remove the "#" in front of each line below)
args.nokia.labels << { x: 0, y: 16, text: "Hello World",
size_enum: NOKIA_FONT_MD,
r: 0, g: 0, b: 0, a: 255,
font: NOKIA_FONT_PATH }
# Render a SMALL/SM label (remove the "#" in front of each line below)
args.nokia.labels << { x: 0, y: 7, text: "Hello World",
size_enum: NOKIA_FONT_SM,
r: 0, g: 0, b: 0, a: 255,
font: NOKIA_FONT_PATH }
# You are provided args.nokia.default_label which returns a Hash that you
# can ~merge~ properties with
# Example 1
args.nokia.labels << args.nokia
.default_label
.merge(text: "Default")
# Example 2
args.nokia.labels << args.nokia
.default_label
.merge(x: 31,
text: "Default")
end
# =============================================================================
# ==== HOW TO RENDER FILLED SQUARES (SOLIDS) ==================================
# =============================================================================
def how_to_render_solids args
# Render a square at 0, 0 with a width and height of 1
args.nokia.solids << { x: 0, y: 0, w: 1, h: 1 }
# Render a square at 1, 1 with a width and height of 2
args.nokia.solids << { x: 1, y: 1, w: 2, h: 2 }
# Render a square at 3, 3 with a width and height of 3
args.nokia.solids << { x: 3, y: 3, w: 3, h: 3 }
# Render a square at 6, 6 with a width and height of 4
args.nokia.solids << { x: 6, y: 6, w: 4, h: 4 }
end
# =============================================================================
# ==== HOW TO RENDER UNFILLED SQUARES (BORDERS) ===============================
# =============================================================================
def how_to_render_borders args
# Render a square at 0, 0 with a width and height of 3
args.nokia.borders << { x: 0, y: 0, w: 3, h: 3, a: 255 }
# Render a square at 3, 3 with a width and height of 3
args.nokia.borders << { x: 3, y: 3, w: 4, h: 4, a: 255 }
# Render a square at 5, 5 with a width and height of 4
args.nokia.borders << { x: 7, y: 7, w: 5, h: 5, a: 255 }
end
# =============================================================================
# ==== HOW TO RENDER A LINE ===================================================
# =============================================================================
def how_to_render_lines args
# Render a horizontal line at the bottom
args.nokia.lines << { x: 0, y: 0, x2: 83, y2: 0 }
# Render a vertical line at the left
args.nokia.lines << { x: 0, y: 0, x2: 0, y2: 47 }
# Render a diagonal line starting from the bottom left and going to the top right
args.nokia.lines << { x: 0, y: 0, x2: 83, y2: 47 }
end
# =============================================================================
# == HOW TO RENDER A SPRITE ===================================================
# =============================================================================
def how_to_render_sprites args
# Loop 10 times and create 10 sprites in 10 positions
# Render a sprite at the bottom left with a width and height of 5 and a path of 'sprites/monochrome-ship.png'
10.times do |i|
args.nokia.sprites << {
x: i * 8.4,
y: i * 4.8,
w: 5,
h: 5,
path: 'sprites/monochrome-ship.png'
}
end
# Given an array of positions create sprites
positions = [
{ x: 20, y: 32 },
{ x: 45, y: 15 },
{ x: 72, y: 23 },
]
positions.each do |position|
# use Ruby's ~Hash#merge~ function to create a sprite
args.nokia.sprites << position.merge(path: 'sprites/monochrome-ship.png',
w: 5,
h: 5)
end
end
# =============================================================================
# ==== HOW TO ANIMATE A SPRITE (SEPERATE PNGS) ==========================
# =============================================================================
def how_to_animate_a_sprite args
# STEP 1: Define when you want the animation to start. The animation in this case will start in 3 seconds
start_animation_on_tick = 180
# STEP 2: Get the frame_index given the start tick.
sprite_index = start_animation_on_tick.frame_index count: 7, # how many sprites?
hold_for: 8, # how long to hold each sprite?
repeat: true # should it repeat?
# STEP 3: frame_index will return nil if the frame hasn't arrived yet
if sprite_index
# if the sprite_index is populated, use it to determine the sprite path and render it
sprite_path = "sprites/explosion-#{sprite_index}.png"
args.nokia.sprites << { x: 42 - 16,
y: 47 - 32,
w: 32,
h: 32,
path: sprite_path }
else
# if the sprite_index is nil, render a countdown instead
countdown_in_seconds = ((start_animation_on_tick - args.state.tick_count) / 60).round(1)
args.nokia.labels << args.nokia
.default_label
.merge(x: 0,
y: 18,
text: "Count Down: #{countdown_in_seconds.to_sf}",
alignment_enum: 0)
end
# render the current tick and the resolved sprite index
args.nokia.labels << args.nokia
.default_label
.merge(x: 0,
y: 11,
text: "Tick: #{args.state.tick_count}")
args.nokia.labels << args.nokia
.default_label
.merge(x: 0,
y: 5,
text: "sprite_index: #{sprite_index}")
end
# =============================================================================
# ==== HOW TO ANIMATE A SPRITE (SPRITE SHEET) =================================
# =============================================================================
def how_to_animate_a_sprite_sheet args
# STEP 1: Define when you want the animation to start. The animation in this case will start in 3 seconds
start_animation_on_tick = 180
# STEP 2: Get the frame_index given the start tick.
sprite_index = start_animation_on_tick.frame_index count: 7, # how many sprites?
hold_for: 8, # how long to hold each sprite?
repeat: true # should it repeat?
# STEP 3: frame_index will return nil if the frame hasn't arrived yet
if sprite_index
# if the sprite_index is populated, use it to determine the source rectangle and render it
args.nokia.sprites << {
x: 42 - 16,
y: 47 - 32,
w: 32,
h: 32,
path: "sprites/explosion-sheet.png",
source_x: 32 * sprite_index,
source_y: 0,
source_w: 32,
source_h: 32
}
else
# if the sprite_index is nil, render a countdown instead
countdown_in_seconds = ((start_animation_on_tick - args.state.tick_count) / 60).round(1)
args.nokia.labels << args.nokia
.default_label
.merge(x: 0,
y: 18,
text: "Count Down: #{countdown_in_seconds.to_sf}",
alignment_enum: 0)
end
# render the current tick and the resolved sprite index
args.nokia.labels << args.nokia
.default_label
.merge(x: 0,
y: 11,
text: "tick: #{args.state.tick_count}")
args.nokia.labels << args.nokia
.default_label
.merge(x: 0,
y: 5,
text: "sprite_index: #{sprite_index}")
end
# =============================================================================
# ==== HOW TO STORE STATE, ACCEPT INPUT, AND RENDER SPRITE BASED OFF OF STATE =
# =============================================================================
def how_to_move_a_sprite args
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 46, text: "Use Arrow Keys",
alignment_enum: 1)
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 41, text: "Or WASD",
alignment_enum: 1)
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 36, text: "Or Click",
alignment_enum: 1)
# set the initial values for x and y using ||= ("or equal operator")
args.state.ship.x ||= 0
args.state.ship.y ||= 0
# if a mouse click occurs, update the ship's x and y to be the location of the click
if args.nokia.mouse_click
args.state.ship.x = args.nokia.mouse_click.x
args.state.ship.y = args.nokia.mouse_click.y
end
# if a or left arrow is pressed/held, decrement the ships x position
if args.nokia.keyboard.left
args.state.ship.x -= 1
end
# if d or right arrow is pressed/held, increment the ships x position
if args.nokia.keyboard.right
args.state.ship.x += 1
end
# if s or down arrow is pressed/held, decrement the ships y position
if args.nokia.keyboard.down
args.state.ship.y -= 1
end
# if w or up arrow is pressed/held, increment the ships y position
if args.nokia.keyboard.up
args.state.ship.y += 1
end
# render the sprite to the screen using the position stored in args.state.ship
args.nokia.sprites << {
x: args.state.ship.x,
y: args.state.ship.y,
w: 5,
h: 5,
path: 'sprites/monochrome-ship.png',
# parameters beyond this point are optional
angle: 0, # Note: rotation angle is denoted in degrees NOT radians
r: 255,
g: 255,
b: 255,
a: 255
}
end
# =======================================================================
# ==== HOW TO DETERMINE COLLISION =======================================
# =======================================================================
def how_to_determine_collision args
# Render the instructions
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 46, text: "Click Anywhere",
alignment_enum: 1)
# if a mouse click occurs:
# - set ship_one if it isn't set
# - set ship_two if it isn't set
# - otherwise reset ship one and ship two
if args.nokia.mouse_click
# is ship_one set?
if !args.state.ship_one
args.state.ship_one = { x: args.nokia.mouse_click.x - 5,
y: args.nokia.mouse_click.y - 5,
w: 10,
h: 10 }
# is ship_one set?
elsif !args.state.ship_two
args.state.ship_two = { x: args.nokia.mouse_click.x - 5,
y: args.nokia.mouse_click.y - 5,
w: 10,
h: 10 }
# should we reset?
else
args.state.ship_one = nil
args.state.ship_two = nil
end
end
# render ship one if it's set
if args.state.ship_one
# use Ruby's .merge method which is available on ~Hash~ to set the sprite and alpha
# render ship one
args.nokia.sprites << args.state.ship_one.merge(path: 'sprites/monochrome-ship.png')
end
if args.state.ship_two
# use Ruby's .merge method which is available on ~Hash~ to set the sprite and alpha
# render ship two
args.nokia.sprites << args.state.ship_two.merge(path: 'sprites/monochrome-ship.png')
end
# if both ship one and ship two are set, then determine collision
if args.state.ship_one && args.state.ship_two
# collision is determined using the intersect_rect? method
if args.state.ship_one.intersect_rect? args.state.ship_two
# if collision occurred, render the words collision!
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 5,
text: "Collision!",
alignment_enum: 1)
else
# if collision occurred, render the words no collision.
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 5,
text: "No Collision.",
alignment_enum: 1)
end
else
# if both ship one and ship two aren't set, then render --
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 6,
text: "--",
alignment_enum: 1)
end
end
# =============================================================================
# ==== HOW TO CREATE BUTTONS ==================================================
# =============================================================================
def how_to_create_buttons args
# Define a button style
args.state.button_style = { w: 82, h: 10, }
# Render instructions
args.state.button_message ||= "Press a Button!"
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 82,
text: args.state.button_message,
alignment_enum: 1)
# Creates button one using a border and a label
args.state.button_one_border = args.state.button_style.merge( x: 1, y: 32)
args.nokia.borders << args.state.button_one_border
args.nokia.labels << args.nokia
.default_label
.merge(x: args.state.button_one_border.x + 2,
y: args.state.button_one_border.y + NOKIA_FONT_SM_HEIGHT + 2,
text: "Button One")
# Creates button two using a border and a label
args.state.button_two_border = args.state.button_style.merge( x: 1, y: 20)
args.nokia.borders << args.state.button_two_border
args.nokia.labels << args.nokia
.default_label
.merge(x: args.state.button_two_border.x + 2,
y: args.state.button_two_border.y + NOKIA_FONT_SM_HEIGHT + 2,
text: "Button Two")
# Initialize the state variable that tracks which button was clicked to "" (empty stringI
args.state.last_button_clicked ||= "--"
# If a click occurs, check to see if either button one, or button two was clicked
# using the inside_rect? method of the mouse
# set args.state.last_button_clicked accordingly
if args.nokia.mouse_click
if args.nokia.mouse_click.inside_rect? args.state.button_one_border
args.state.last_button_clicked = "One Clicked!"
elsif args.nokia.mouse_click.inside_rect? args.state.button_two_border
args.state.last_button_clicked = "Two Clicked!"
else
args.state.last_button_clicked = "--"
end
end
# Render the current value of args.state.last_button_clicked
args.nokia.labels << args.nokia
.default_label
.merge(x: 42,
y: 5,
text: args.state.last_button_clicked,
alignment_enum: 1)
end
def render_debug args
if !args.state.grid_rendered
(NOKIA_HEIGHT + 1).map_with_index do |i|
args.outputs.static_debug << {
x: NOKIA_X_OFFSET,
y: NOKIA_Y_OFFSET + (i * NOKIA_ZOOM),
x2: NOKIA_X_OFFSET + NOKIA_ZOOMED_WIDTH,
y2: NOKIA_Y_OFFSET + (i * NOKIA_ZOOM),
r: 128,
g: 128,
b: 128,
a: 80
}.line
end
(NOKIA_WIDTH + 1).map_with_index do |i|
args.outputs.static_debug << {
x: NOKIA_X_OFFSET + (i * NOKIA_ZOOM),
y: NOKIA_Y_OFFSET,
x2: NOKIA_X_OFFSET + (i * NOKIA_ZOOM),
y2: NOKIA_Y_OFFSET + NOKIA_ZOOMED_HEIGHT,
r: 128,
g: 128,
b: 128,
a: 80
}.line
end
end
args.state.grid_rendered = true
args.state.last_click ||= 0
args.state.last_up ||= 0
args.state.last_click = args.state.tick_count if args.nokia.mouse_down # you can also use args.nokia.click
args.state.last_up = args.state.tick_count if args.nokia.mouse_up
args.state.label_style = { size_enum: -1.5 }
args.state.watch_list = [
"args.state.tick_count is: #{args.state.tick_count}",
"args.nokia.mouse_position is: #{args.nokia.mouse_position.x}, #{args.nokia.mouse_position.y}",
"args.nokia.mouse_down tick: #{args.state.last_click || "never"}",
"args.nokia.mouse_up tick: #{args.state.last_up || "false"}",
]
args.outputs.debug << args.state
.watch_list
.map_with_index do |text, i|
{
x: 5,
y: 720 - (i * 18),
text: text,
size_enum: -1.5,
r: 255, g: 255, b: 255
}.label
end
args.outputs.debug << {
x: 640,
y: 25,
text: "INFO: dev mode is currently enabled. Comment out the invocation of ~render_debug~ within the ~tick~ method to hide the debug layer.",
size_enum: -0.5,
alignment_enum: 1,
r: 255, g: 255, b: 255
}.label
end
def snake_demo args
end
$gtk.reset
|