-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtutorial-arcade-shooter.html
653 lines (590 loc) · 29.4 KB
/
tutorial-arcade-shooter.html
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<html>
<head>
<title>DragonRuby - Arcade Shooter</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
<script src="tutorial.js"></script>
<link rel="stylesheet" href="tutorial.css" />
</head>
<body>
<h1>DragonRuby Game Toolkit - Arcade Shooter</h1>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="1">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Moveable Player</h1>
<p>We can start by creating a moveable player. Use the arrow keys to move around.</p>
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args, args.state.player
calc args, args.state.player
input args, args.state.player
end
def defaults args
# Initialize player starting values
args.state.player.x ||= 0
args.state.player.y ||= 0
args.state.player.w ||= 15
args.state.player.h ||= 15
args.state.player.path ||= 'sprites/isometric-violet.png'
args.state.player.speed ||= 1
end
def render args, player
# Render the player
args.outputs.sprites << [
player.x,
player.y,
player.w,
player.h,
player.path,
]
end
def calc args, player
# Bound the player to the grid
player.x = player.x.greater(args.grid.left)
player.x = player.x.lesser(args.grid.right - player.w)
player.y = player.y.greater(args.grid.bottom)
player.y = player.y.lesser(args.grid.top - player.h)
end
def input args, player
# Move the player based on user input
player.x += player.speed if args.inputs.keyboard.right
player.x -= player.speed if args.inputs.keyboard.left
player.y += player.speed if args.inputs.keyboard.up
player.y -= player.speed if args.inputs.keyboard.down
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="2">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Player Shooting</h1>
Next, we will add player shooting. Press space to shoot. The label shows the number of bullets that exist.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args, args.state.player
calc args, args.state.player
input args, args.state.player
end
def defaults args
args.state.player.x ||= 0
args.state.player.y ||= 0
args.state.player.w ||= 15
args.state.player.h ||= 15
args.state.player.path ||= 'sprites/isometric-violet.png'
args.state.player.speed ||= 1
# Initialize values for the player's bullets
args.state.player.bullets ||= []
args.state.player.bullet_size ||= 3
args.state.player.bullet_speed ||= 1.5
args.state.player.bullet_path ||= 'sprites/circle-violet.png'
args.state.enemies ||= []
end
def render args, player
args.outputs.sprites << [
player.x,
player.y,
player.w,
player.h,
player.path,
]
# Render the bullets
args.outputs.sprites << player.bullets
# Shows how many bullets are being rendered
args.outputs.labels << [
5, 5.from_top, "bullet count: #{player.bullets.size}", -7.5
]
end
def calc args, player
player.x = player.x.greater(args.grid.left)
player.x = player.x.lesser(args.grid.right - player.w)
player.y = player.y.greater(args.grid.bottom)
player.y = player.y.lesser(args.grid.top - player.h)
player.bullets = player.bullets.find_all do |bullet|
bullet.intersect_rect? args.grid.rect
end
player.bullets.each do |bullet|
bullet.y += player.bullet_speed
# Remove all enemies
args.state.enemies.reject! do |enemy|
# That a bullet is touching
bullet.intersect_rect? enemy[:rect]
end
end
end
def input args, player
player.x += player.speed if args.inputs.keyboard.right
player.x -= player.speed if args.inputs.keyboard.left
player.y += player.speed if args.inputs.keyboard.up
player.y -= player.speed if args.inputs.keyboard.down
# If the space bar is pressed and lifted
if args.inputs.keyboard.key_down.space
# Add a bullet sprite to player.bullets
player.bullets << [
# Centers the bullet on the center of the player
player.x + player.w / 2 - player.bullet_size / 2,
player.y + player.h / 2 - player.bullet_size / 2,
player.bullet_size,
player.bullet_size,
player.bullet_path,
]
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="3">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Enemies</h1>
We can add enemies that the player has to defeat.
Their movement will not be based off of user input.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args
calc args
end
def defaults args
# Create some default values
args.state.enemy_w ||= 15
args.state.enemy_h ||= 15
args.state.enemies ||= []
args.state.enemy_bullets ||= []
# The tick a new enemy should spawn
args.state.enemy_spawn_tick ||= 0
args.state.enemy_bottom ||= args.grid.center.y
args.state.enemy_bullet_size ||= 5
args.state.enemy_bullet_speed ||= 1
args.state.enemy_bullet_path ||= 'sprites/circle-red.png'
end
def render args
# Draw the enemies and enemy bullets
args.outputs.sprites << args.state.enemies
args.outputs.sprites << args.state.enemy_bullets
end
def calc args
spawn_enemies args
update_enemies args
# Move each bullet
args.state.enemy_bullets.reject! do |bullet|
!bullet.intersect_rect? args.grid.rect
end
args.state.enemy_bullets.each do | bullet |
bullet.y -= args.state.enemy_bullet_speed
end
end
def spawn_enemies args
# If it is time to spawn an enemy
if args.state.enemy_spawn_tick == args.state.tick_count
# and if there are less than 3 enemies
if args.state.enemies.size < 3
# Add an enemy
args.state.enemies << {
x: args.grid.right.randomize(:ratio),
y: args.grid.top - args.state.enemy_h,
w: args.state.enemy_w,
h: args.state.enemy_h,
path: 'sprites/isometric-red.png',
dx: args.state.enemy_bullet_speed
.randomize(:sign),
dy: -args.state.enemy_bullet_speed,
bullet_spawn_tick: args.state.tick_count + 60,
}
end
# Set the new time an enemy should spawn
args.state.enemy_spawn_tick += (rand(4) + 1) * 60
end
end
def update_enemies args
args.state.enemies.each do | enemy |
# Move the enemies
enemy[:x] += enemy[:dx]
enemy[:y] += enemy[:dy]
# Bound the enemies to the top half of the screen
# Bounce them off of the edges
if enemy[:x] <= args.grid.left
enemy[:x] = args.grid.left
enemy[:dx] = 1
end
if enemy[:x] >= args.grid.right - enemy[:w]
enemy[:x] = args.grid.right - enemy[:w]
enemy[:dx] = -1
end
if enemy[:y] <= args.state.enemy_bottom
enemy[:y] = args.state.enemy_bottom
enemy[:dy] = 1
end
if enemy[:y] >= args.grid.top - enemy[:h]
enemy[:y] = args.grid.top - enemy[:h]
enemy[:dy] = -1
end
# Spawn bullets
if enemy[:bullet_spawn_tick] == args.state.tick_count
args.state.enemy_bullets << [
# Centers the bullet on the center of the enemy
enemy[:x] + enemy[:w] / 2 -
args.state.enemy_bullet_size / 2,
enemy[:y] + enemy[:h] / 2 -
args.state.enemy_bullet_size / 2,
args.state.enemy_bullet_size,
args.state.enemy_bullet_size,
args.state.enemy_bullet_path,
]
enemy[:bullet_spawn_tick] += 60
end
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="4">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Bringing it all together</h1>
We bring the player and enemies together.
The code is also refactored to improve readability.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args, args.state.player
calc args, args.state.player
input args, args.state.player
end
def defaults args
args.state.player.x ||= 0
args.state.player.y ||= 0
args.state.player.w ||= 15
args.state.player.h ||= 15
args.state.player.path ||= 'sprites/isometric-violet.png'
args.state.player.speed ||= 1
args.state.player.bullets ||= []
args.state.player.bullet_size ||= 3
args.state.player.bullet_speed ||= 1.5
args.state.player.bullet_path ||= 'sprites/circle-violet.png'
args.state.enemy_w ||= 15
args.state.enemy_h ||= 15
args.state.enemies ||= []
args.state.enemy_bullets ||= []
args.state.enemy_spawn_tick ||= 0
args.state.enemy_bottom ||= args.grid.center.y
args.state.enemy_bullet_size ||= 5
args.state.enemy_bullet_speed ||= 1
args.state.enemy_bullet_path ||= 'sprites/circle-red.png'
end
def render args, player
args.outputs.sprites << [
player.x,
player.y,
player.w,
player.h,
player.path,
]
args.outputs.sprites << player.bullets
args.outputs.sprites << args.state.enemies
args.outputs.sprites << args.state.enemy_bullets
end
def calc args, player
bound_player args, player
update_player_bullets args, player
spawn_enemies args
update_enemies args
args.state.enemy_bullets.reject! do |bullet|
!bullet.intersect_rect? args.grid.rect
end
args.state.enemy_bullets.each do | bullet |
bullet.y -= args.state.enemy_bullet_speed
end
end
def bound_player args, player
player.x = player.x.greater(args.grid.left)
player.x = player.x.lesser(args.grid.right - player.w)
player.y = player.y.greater(args.grid.bottom)
player.y = player.y.lesser(args.grid.top - player.h)
end
def update_player_bullets args, player
player.bullets = player.bullets.find_all do |bullet|
bullet.intersect_rect? args.grid.rect
end
player.bullets.each do |bullet|
bullet.y += player.bullet_speed
end
end
def spawn_enemies args
if args.state.enemy_spawn_tick == args.state.tick_count
if args.state.enemies.size < 3
args.state.enemies << {
x: args.grid.right.randomize(:ratio),
y: args.grid.top - args.state.enemy_h,
w: args.state.enemy_w,
h: args.state.enemy_h,
path: 'sprites/isometric-red.png',
dx: args.state.enemy_bullet_speed
.randomize(:sign),
dy: -args.state.enemy_bullet_speed,
bullet_spawn_tick: args.state.tick_count + 60,
}
end
# Set the new time an enemy should spawn
args.state.enemy_spawn_tick += (rand(4) + 1) * 60
end
end
def update_enemies args
args.state.enemies.each do | enemy |
enemy[:x] += enemy[:dx]
enemy[:y] += enemy[:dy]
if enemy[:x] <= args.grid.left
enemy[:x] = args.grid.left
enemy[:dx] = 1
end
if enemy[:x] >= args.grid.right - enemy[:w]
enemy[:x] = args.grid.right - enemy[:w]
enemy[:dx] = -1
end
if enemy[:y] <= args.state.enemy_bottom
enemy[:y] = args.state.enemy_bottom
enemy[:dy] = 1
end
if enemy[:y] >= args.grid.top - enemy[:h]
enemy[:y] = args.grid.top - enemy[:h]
enemy[:dy] = -1
end
if enemy[:bullet_spawn_tick] == args.state.tick_count
args.state.enemy_bullets << [
# Centers the bullet on the center of the enemy
enemy[:x] + enemy[:w] / 2 -
args.state.enemy_bullet_size / 2,
enemy[:y] + enemy[:h] / 2 -
args.state.enemy_bullet_size / 2,
args.state.enemy_bullet_size,
args.state.enemy_bullet_size,
args.state.enemy_bullet_path,
]
enemy[:bullet_spawn_tick] += 60
end
end
end
def input args, player
player.x += player.speed if args.inputs.keyboard.right
player.x -= player.speed if args.inputs.keyboard.left
player.y += player.speed if args.inputs.keyboard.up
player.y -= player.speed if args.inputs.keyboard.down
if args.inputs.keyboard.key_down.space
player.bullets << [
player.x + player.w / 2 - player.bullet_size / 2,
player.y + player.h / 2 - player.bullet_size / 2,
player.bullet_size,
player.bullet_size,
player.bullet_path,
]
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="5">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Adding collisions</h1>
Calculating for collisions can make the players and enemies interact.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args, args.state.player
input args, args.state.player
calc args, args.state.player
end
def defaults args
args.state.player.x ||= 0
args.state.player.y ||= 0
args.state.player.w ||= 15
args.state.player.h ||= 15
# Default value for a player rect
# Used for easy collision handling
args.state.player.rect ||= [
args.state.player.x,
args.state.player.y,
args.state.player.w,
args.state.player.h,
]
args.state.player.path ||= 'sprites/isometric-violet.png'
args.state.player.speed ||= 1
args.state.player.bullets ||= []
args.state.player.bullet_size ||= 3
args.state.player.bullet_speed ||= 1.5
args.state.player.bullet_path ||= 'sprites/circle-violet.png'
args.state.enemy_w ||= 15
args.state.enemy_h ||= 15
args.state.enemies ||= []
args.state.enemy_bullets ||= []
args.state.enemy_spawn_tick ||= 0
args.state.enemy_bottom ||= args.grid.center.y
args.state.enemy_bullet_size ||= 5
args.state.enemy_bullet_speed ||= 1
args.state.enemy_bullet_path ||= 'sprites/circle-red.png'
end
def render args, player
args.outputs.sprites << [
player.x,
player.y,
player.w,
player.h,
player.path,
]
args.outputs.sprites << player.bullets
args.outputs.sprites << args.state.enemies
args.outputs.sprites << args.state.enemy_bullets
end
def calc args, player
bound_player args, player
update_player_bullets args, player
spawn_enemies args
update_enemies args
# updating player.rect whenever the xywh values change
player.rect = [
player.x,
player.y,
player.w,
player.h,
]
args.state.enemy_bullets.reject! do |bullet|
!bullet.intersect_rect? args.grid.rect
end
args.state.enemy_bullets.each do | bullet |
bullet.y -= args.state.enemy_bullet_speed
# Reset the game
# if an enemy bullet hits the player
reset_game(args) if bullet.intersect_rect? player.rect
end
end
def bound_player args, player
player.x = player.x.greater(args.grid.left)
player.x = player.x.lesser(args.grid.right - player.w)
player.y = player.y.greater(args.grid.bottom)
player.y = player.y.lesser(args.grid.top - player.h)
end
def update_player_bullets args, player
player.bullets = player.bullets.find_all do |bullet|
bullet.intersect_rect? args.grid.rect
end
player.bullets.each do |bullet|
bullet.y += player.bullet_speed
# Remove all enemies
args.state.enemies.reject! do |enemy|
# That a bullet is touching
bullet.intersect_rect? enemy[:rect]
end
end
end
def spawn_enemies args
if args.state.enemy_spawn_tick == args.state.tick_count
if args.state.enemies.size < 3
args.state.enemies << {
x: args.grid.right.randomize(:ratio),
y: args.grid.top - args.state.enemy_h,
w: args.state.enemy_w,
h: args.state.enemy_h,
path: 'sprites/isometric-red.png',
dx: args.state.enemy_bullet_speed
.randomize(:sign),
dy: -args.state.enemy_bullet_speed,
bullet_spawn_tick: args.state.tick_count + 60,
}
end
# Set the new time an enemy should spawn
args.state.enemy_spawn_tick += (rand(4) + 1) * 60
end
end
def update_enemies args
args.state.enemies.each do | enemy |
enemy[:x] += enemy[:dx]
enemy[:y] += enemy[:dy]
if enemy[:x] <= args.grid.left
enemy[:x] = args.grid.left
enemy[:dx] = 1
end
if enemy[:x] >= args.grid.right - enemy[:w]
enemy[:x] = args.grid.right - enemy[:w]
enemy[:dx] = -1
end
if enemy[:y] <= args.state.enemy_bottom
enemy[:y] = args.state.enemy_bottom
enemy[:dy] = 1
end
if enemy[:y] >= args.grid.top - enemy[:h]
enemy[:y] = args.grid.top - enemy[:h]
enemy[:dy] = -1
end
if enemy[:bullet_spawn_tick] == args.state.tick_count
args.state.enemy_bullets << [
# Centers the bullet on the center of the enemy
enemy[:x] + enemy[:w] / 2 -
args.state.enemy_bullet_size / 2,
enemy[:y] + enemy[:h] / 2 -
args.state.enemy_bullet_size / 2,
args.state.enemy_bullet_size,
args.state.enemy_bullet_size,
args.state.enemy_bullet_path,
]
enemy[:bullet_spawn_tick] += 60
end
# Creates the enemy rect for collision handling
enemy[:rect] = [
enemy[:x],
enemy[:y],
enemy[:w],
enemy[:h],
]
end
end
def input args, player
player.x += player.speed if args.inputs.keyboard.right
player.x -= player.speed if args.inputs.keyboard.left
player.y += player.speed if args.inputs.keyboard.up
player.y -= player.speed if args.inputs.keyboard.down
if args.inputs.keyboard.key_down.space
player.bullets << [
player.x + player.w / 2 - player.bullet_size / 2,
player.y + player.h / 2 - player.bullet_size / 2,
player.bullet_size,
player.bullet_size,
player.bullet_path,
]
end
end
# Resets the game
def reset_game args
args.state.player.x = 0
args.state.player.y = 0
args.state.player.bullets.clear
args.state.enemies.clear
args.state.enemy_bullets.clear
end
$gtk.reset
</code>
</pre>
</div>
</div>
<footer id="bottom"></footer>
</body>
</html>