This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
1133 lines (965 loc) · 33.4 KB
/
README.Rmd
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dev = "ragg_png",
dev.args = list(
background = "#ffffff00"
)
)
```
# A `ggplot2` and `gganimate` Version of Pac-Man <img src="man/figures/ggpacman.gif" align="right" width="120" />
<!-- badges: start -->
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing)
[![GitHub tag](https://img.shields.io/github/tag/mcanouil/ggpacman.svg?label=latest tag)](https://github.com/mcanouil/ggpacman)
[![R build status](https://github.com/mcanouil/ggpacman/workflows/R-CMD-check/badge.svg)](https://github.com/mcanouil/ggpacman/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/ggpacman)](https://CRAN.R-project.org/package=ggpacman)
<!-- badges: end -->
The goal of `ggpacman` is to ...
Make a GIF of the game Pac-Man (*not to develop an R version of Pac-Man ...*).
## Installation
```{r install, eval = FALSE}
# Install ggpacman from CRAN:
install.packages("ggpacman")
# Or the the development version from GitHub:
# install.packages("remotes")
remotes::install_github("mcanouil/ggpacman")
```
## Pac-Man in action
```{r pacman}
library(ggpacman)
animate_pacman(
pacman = pacman,
ghosts = list(blinky, pinky, inky, clyde),
font_family = "xkcd"
)
```
## The Story of `ggpacman`
It started on a Saturday evening ...
It was the 21<sup>st</sup> of March (*for the sake of precision*),
around 10 pm CET (*also for the sake of precision and mostly because it is not relevant*).
I was playing around with my data on 'all' the movies I have seen so far ([mcanouil/IMDbRating](https://github.com/mcanouil/IMDbRating)) and looking on possibly new ideas of visualisation on twitter using `#ggplot2` and `#gganimate` (by the way the first time I played with [`gganimate`](https://gganimate.com/) was at [useR-2018 (Brisbane, Australia)](https://www.r-project.org/conferences/useR-2018/), just before and when @thomasp85 released the actual framework).
The only thing on the feed was "contaminated/deaths and covid-19" curves made with [`ggplot2`](https://ggplot2.tidyverse.org/) and a few with [`gganimate`](https://gganimate.com/) ...
Let's say, it was not as funny and interesting as I was hoping for ...
Then, I've got an idea, what if I can do something funny and not expected with [`ggplot2`](https://ggplot2.tidyverse.org/) and [`gganimate`](https://gganimate.com/)?
My first thought, was let's draw and animate Pac-Man, that should not be that hard!
Well, it was not that easy after-all ...
But, I am going to go through my code here (you might be interested to actually look at the [commits history](https://github.com/mcanouil/ggpacman/commits/master).
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Maybe I went too far with <a href="https://twitter.com/hashtag/ggplot2?src=hash&ref_src=twsrc%5Etfw">#ggplot2</a> and <a href="https://twitter.com/hashtag/gganimate?src=hash&ref_src=twsrc%5Etfw">#gganimate</a> ...😅<br>What do you think <a href="https://twitter.com/hadleywickham?ref_src=twsrc%5Etfw">@hadleywickham</a> & <a href="https://twitter.com/thomasp85?ref_src=twsrc%5Etfw">@thomasp85</a> , did I go too far or not enough ? (I am planning to add the ghosts 😎) <a href="https://t.co/nkfbti1Etd">pic.twitter.com/nkfbti1Etd</a></p>— Mickaël CANOUIL (@mickaelcanouil) <a href="https://twitter.com/mickaelcanouil/status/1241760925499170824?ref_src=twsrc%5Etfw">March 22, 2020</a></blockquote>
1. [The packages](#the-packages)
2. [The maze layer](#the-maze-layer)
1. [The base layer](#the-base-layer)
2. [The grid layer](#the-grid-layer)
3. [The bonus points layer](#the-bonus-points-layer)
3. [Pac-Man character](#pac-man-character)
4. [The Ghosts characters](#the-ghosts-characters)
1. [Body](#body)
2. [Eyes](#eyes)
3. [Ghost shape](#ghost-shape)
5. [How Pac-Man interacts with the maze?](#how-pac-man-interacts-with-the-maze)
1. [Bonus points](#bonus-points)
2. [Ghost `"weak"` and `"eaten"` states](#ghost-weak-and-eaten-states)
6. [Plot time](#Plot-time-to-summarise-a-little-or-a-lot)
### The packages
```{r libraries, results = "hide", message = FALSE, warning = FALSE}
library("stats")
library("utils")
library("rlang")
library("magrittr")
library("dplyr")
library("tidyr")
library("purrr")
library("ggplot2")
library("ggforce")
library("gganimate")
library("ggtext")
```
### The maze layer
#### The base layer
First thing first, I needed to set-up the base layer, meaning, the maze from Pac-Man.
I did start by setting the coordinates of the maze.
```{r base-layer}
base_layer <- ggplot() +
theme_void() +
theme(
legend.position = "none",
plot.background = element_rect(fill = "black", colour = "black"),
panel.background = element_rect(fill = "black", colour = "black"),
) +
coord_fixed(xlim = c(0, 20), ylim = c(0, 26))
```
For later use, I defined some scales (actually those scales, where defined way after chronologically speaking).
I am using those to define sizes and colours for all the geometries I am going to use to achieve the Pac-Man GIF.
```{r colours-mapping}
map_colours <- c(
"READY!" = "goldenrod1",
"wall" = "dodgerblue3", "door" = "dodgerblue3",
"normal" = "goldenrod1", "big" = "goldenrod1", "eaten" = "black",
"Pac-Man" = "yellow",
"eye" = "white", "iris" = "black",
"Blinky" = "red", "Blinky_weak" = "blue", "Blinky_eaten" = "transparent",
"Pinky" = "pink", "Pinky_weak" = "blue", "Pinky_eaten" = "transparent",
"Inky" = "cyan", "Inky_weak" = "blue", "Inky_eaten" = "transparent",
"Clyde" = "orange", "Clyde_weak" = "blue", "Clyde_eaten" = "transparent"
)
```
```{r base-layer-colours}
base_layer <- base_layer +
scale_size_manual(values = c("wall" = 2.5, "door" = 1, "big" = 2.5, "normal" = 0.5, "eaten" = 3)) +
scale_fill_manual(breaks = names(map_colours), values = map_colours) +
scale_colour_manual(breaks = names(map_colours), values = map_colours)
```
```{r base-layer-plot, echo = FALSE}
base_layer
```
My `base_layer` here is not really helpful, so I temporarily added some elements to help me draw everything on it.
*Note*: I won't use it in the following.
```{r base-layer-dev}
base_layer +
scale_x_continuous(breaks = 0:21, sec.axis = dup_axis()) +
scale_y_continuous(breaks = 0:26, sec.axis = dup_axis()) +
theme(
panel.grid.major = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
annotate("rect", xmin = 0, xmax = 21, ymin = 0, ymax = 26, fill = NA)
```
Quite better, isn't it?!
#### The grid layer
Here, I am calling "grid", the walls of the maze.
For this grid, I started drawing the vertical lines on the left side of the maze (as you may have noticed, the first level is symmetrical).
```{r left-vertical}
left_vertical_segments <- tribble(
~x, ~y, ~xend, ~yend,
0, 0, 0, 9,
0, 17, 0, 26,
2, 4, 2, 5,
2, 19, 2, 20,
2, 22, 2, 24,
4, 4, 4, 7,
4, 9, 4, 12,
4, 14, 4, 17,
4, 19, 4, 20,
4, 22, 4, 24,
6, 2, 6, 5,
6, 9, 6, 12,
6, 14, 6, 20,
6, 22, 6, 24,
8, 4, 8, 5,
8, 9, 8, 10,
8, 12, 8, 15,
8, 19, 8, 20,
8, 22, 8, 24
)
```
```{r left-vertical-plot}
base_layer +
geom_segment(
data = left_vertical_segments,
mapping = aes(x = x, y = y, xend = xend, yend = yend),
lineend = "round",
inherit.aes = FALSE,
colour = "white"
)
```
Then, I added the horizontal lines (still only on the left side of the maze)!
```{r left-horizontal}
left_horizontal_segments <- tribble(
~x, ~y, ~xend, ~yend,
0, 0, 10, 0,
2, 2, 8, 2,
0, 4, 2, 4,
8, 4, 10, 4,
0, 5, 2, 5,
8, 5, 10, 5,
2, 7, 4, 7,
6, 7, 8, 7,
0, 9, 4, 9,
8, 9, 10, 9,
8, 10, 10, 10,
0, 12, 4, 12,
8, 12, 10, 12,
0, 14, 4, 14,
8, 15, 9, 15,
0, 17, 4, 17,
6, 17, 8, 17,
2, 19, 4, 19,
8, 19, 10, 19,
2, 20, 4, 20,
8, 20, 10, 20,
2, 22, 4, 22,
6, 22, 8, 22,
2, 24, 4, 24,
6, 24, 8, 24,
0, 26, 10, 26
)
left_segments <- bind_rows(left_vertical_segments, left_horizontal_segments)
```
```{r left-plot}
base_layer +
geom_segment(
data = left_segments,
mapping = aes(x = x, y = y, xend = xend, yend = yend),
lineend = "round",
inherit.aes = FALSE,
colour = "white"
)
```
The maze is slowly appearing, but surely.
As I wrote earlier, the first level is symmetrical, so I used my left lines `left_segments` to compute all the lines on the right `right_segments`.
```{r right}
right_segments <- mutate(
.data = left_segments,
x = abs(x - 20),
xend = abs(xend - 20)
)
```
```{r right-plot}
base_layer +
geom_segment(
data = bind_rows(left_segments, right_segments),
mapping = aes(x = x, y = y, xend = xend, yend = yend),
lineend = "round",
inherit.aes = FALSE,
colour = "white"
)
```
The middle vertical lines were missing, *i.e.*, I did not want to plot them twice, which would have happen, if I added these in `left_segments`.
Also, the "door" of the ghost spawn area is missing.
I added the door and the missing vertical walls in the end.
```{r middle}
centre_vertical_segments <- tribble(
~x, ~y, ~xend, ~yend,
10, 2, 10, 4,
10, 7, 10, 9,
10, 17, 10, 19,
10, 22, 10, 26
)
door_segment <- tibble(x = 9, y = 15, xend = 11, yend = 15, type = "door")
```
Finally, I combined all the segments and drew them all.
```{r maze}
maze_walls <- bind_rows(
left_segments,
centre_vertical_segments,
right_segments
) %>%
mutate(type = "wall") %>%
bind_rows(door_segment)
```
```{r maze-plot}
base_layer +
geom_segment(
data = maze_walls,
mapping = aes(x = x, y = y, xend = xend, yend = yend),
lineend = "round",
inherit.aes = FALSE,
colour = "white"
)
```
The maze is now complete, but no-one can actually see the door, since it appears the same way as the walls.
You may have noticed, I added a column named `type`.
`type` can currently hold two values: `"wall"` and `"door"`.
I am going to use `type` as values for two aesthetics, you may already have guessed which ones.
The answer is the `colour` and `size` aesthetics.
```{r maze-plot-colour}
base_layer +
geom_segment(
data = maze_walls,
mapping = aes(x = x, y = y, xend = xend, yend = yend, colour = type, size = type),
lineend = "round",
inherit.aes = FALSE
)
```
*Note: `maze_walls` is a dataset of `ggpacman` (`data("maze_walls", package = "ggpacman")`).*
#### The bonus points layer
The strategy was quite the same as for the grid layer:
* Setting up the point coordinates for the left side and the middle.
* Compute the coordinates for the right side.
* Use a column `type` for the two types of bonus points, *i.e.*, `"normal"` and `"big"` (the one who weaken the ghosts).
```{r bonus-points}
bonus_points_coord <- function() {
left_bonus_points <- tribble(
~x, ~y, ~type,
1, c(1:3, 7:8, 18:22, 24:25), "normal",
1, c(6, 23), "big",
2, c(1, 3, 6, 8, 18, 21, 25), "normal",
3, c(1, 3:6, 8, 18, 21, 25), "normal",
4, c(1, 3, 8, 18, 21, 25), "normal",
5, c(1, 3:25), "normal",
6, c(1, 6, 8, 21, 25), "normal",
7, c(1, 3:6, 8, 18:21, 25), "normal",
8, c(1, 3, 6, 8, 18, 21, 25), "normal",
9, c(1:3, 6:8, 18, 21:25), "normal"
)
bind_rows(
left_bonus_points,
tribble(
~x, ~y, ~type,
10, c(1, 21), "normal"
),
mutate(left_bonus_points, x = abs(x - 20))
) %>%
unnest("y")
}
maze_points <- bonus_points_coord()
```
```{r maze-point-plot}
maze_layer <- base_layer +
geom_segment(
data = maze_walls,
mapping = aes(x = x, y = y, xend = xend, yend = yend, colour = type, size = type),
lineend = "round",
inherit.aes = FALSE
) +
geom_point(
data = maze_points,
mapping = aes(x = x, y = y, size = type, colour = type),
inherit.aes = FALSE
)
```
```{r maze-layer-show, echo = FALSE}
maze_layer
```
*Note: `maze_points` is a dataset of `ggpacman` (`data("maze_points", package = "ggpacman")`).*
### Pac-Man character
It is now time to draw the main character.
To draw Pac-Man, I needed few things:
* The Pac-Man moves, *i.e.*, all the coordinates where Pac-Man is supposed to be at every `step`.
```{r pacman-position}
data("pacman", package = "ggpacman")
unnest(pacman, c("x", "y"))
```
```{r pacman-position-plot}
maze_layer +
geom_point(
data = unnest(pacman, c("x", "y")),
mapping = aes(x = x, y = y, colour = colour),
size = 4
)
```
* The Pac-Man shape (open and closed mouth). Since, Pac-Man is not a complete circle shape, I used `geom_arc_bar()` (from [`ggforce`](https://ggforce.data-imaginist.com/)), and defined the properties of each state of Pac-Man based on the aesthetics required by this function.
*Note*: At first, I wanted a smooth animation/transition of Pac-Man opening and closing its mouth, this is why there are four `"close_"` states.
```{r pacman-state}
pacman_state <- tribble(
~state, ~start, ~end,
"open_right", 14 / 6 * pi, 4 / 6 * pi,
"close_right", 15 / 6 * pi, 3 / 6 * pi,
"open_up", 11 / 6 * pi, 1 / 6 * pi,
"close_up", 12 / 3 * pi, 0 / 6 * pi,
"open_left", 8 / 6 * pi, - 2 / 6 * pi,
"close_left", 9 / 6 * pi, - 3 / 6 * pi,
"open_down", 5 / 6 * pi, - 5 / 6 * pi,
"close_down", pi, - pi
)
```
```{r pacman-state-plot}
ggplot() +
geom_arc_bar(
data = pacman_state,
mapping = aes(x0 = 0, y0 = 0, r0 = 0, r = 0.5, start = start, end = end),
fill = "yellow",
inherit.aes = FALSE
) +
facet_wrap(vars(state), ncol = 4)
```
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Next mission, should you choose to accept, is to make Pac-Man face the direction of movement🎖</p>— Thomas Lin Pedersen (@thomasp85) <a href="https://twitter.com/thomasp85/status/1241767912370774020?ref_src=twsrc%5Etfw">March 22, 2020</a></blockquote>
Once those things available, how to make Pac-Man look where he is headed?
Short answer, I just computed the differences between two successive positions of Pac-Man and added both open/close state to a new column `state`.
```{r pacman-position-state}
pacman %>%
unnest(c("x", "y")) %>%
mutate(
state_x = sign(x - lag(x)),
state_y = sign(y - lag(y)),
state = case_when(
(is.na(state_x) | state_x %in% 0) & (is.na(state_y) | state_y %in% 0) ~ list(c("open_right", "close_right")),
state_x == 1 & state_y == 0 ~ list(c("open_right", "close_right")),
state_x == -1 & state_y == 0 ~ list(c("open_left", "close_left")),
state_x == 0 & state_y == -1 ~ list(c("open_down", "close_down")),
state_x == 0 & state_y == 1 ~ list(c("open_up", "close_up"))
)
) %>%
unnest("state")
```
Here, in preparation for [`gganimate`](https://gganimate.com/), I also added a column `step` before merging the new upgraded `pacman` (*i.e.*, with the Pac-Man `state` column) with the `pacman_state` defined earlier.
```{r pacman-moves}
pacman_moves <- ggpacman::compute_pacman_coord(pacman)
pacman_moves
```
```{r pacman-moves-plots}
maze_layer +
geom_arc_bar(
data = pacman_moves,
mapping = aes(x0 = x, y0 = y, r0 = 0, r = 0.5, start = start, end = end, colour = colour, fill = colour, group = step),
inherit.aes = FALSE
)
```
You can't see much?!
Ok, perhaps it's time to use [`gganimate`](https://gganimate.com/).
I am going to animate Pac-Man based on the column `step`, which is, if you looked at the code above, just the line number of `pacman_moves`.
```{r pacman-animated}
animated_pacman <- maze_layer +
geom_arc_bar(
data = pacman_moves,
mapping = aes(x0 = x, y0 = y, r0 = 0, r = 0.5, start = start, end = end, colour = colour, fill = colour, group = step),
inherit.aes = FALSE
) +
transition_manual(step)
```
```{r pacman-plot-animated, echo = FALSE, message = FALSE}
animate(
plot = animated_pacman,
width = 3.7 * 2.54,
height = 4.7 * 2.54,
units = "cm",
res = 120,
bg = "black",
duration = 10,
renderer = gifski_renderer()
)
```
*Note: `pacman` is a dataset of `ggpacman` (`data("pacman", package = "ggpacman")`).*
### The Ghosts characters
Time to draw the ghosts, namely: Blinky, Pinky, Inky and Clyde.
#### Body
I started with the body, especially the top and the bottom part of the ghost which are half circle (or at least I chose this) and use again `geom_arc_bar()`.
```{r ghost-arc}
ghost_arc <- tribble(
~x0, ~y0, ~r, ~start, ~end, ~part,
0, 0, 0.5, - 1 * pi / 2, 1 * pi / 2, "top",
-0.5, -0.5 + 1/6, 1 / 6, pi / 2, 2 * pi / 2, "bottom",
-1/6, -0.5 + 1/6, 1 / 6, pi / 2, 3 * pi / 2, "bottom",
1/6, -0.5 + 1/6, 1 / 6, pi / 2, 3 * pi / 2, "bottom",
0.5, -0.5 + 1/6, 1 / 6, 3 * pi / 2, 2 * pi / 2, "bottom"
)
```
```{r ghost-top}
top <- ggplot() +
geom_arc_bar(
data = ghost_arc[1, ],
mapping = aes(x0 = x0, y0 = y0, r0 = 0, r = r, start = start, end = end)
) +
coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1))
```
```{r ghost-top-plot, echo = FALSE}
top
```
I retrieved the coordinates of the created polygon, using `ggplot_build()`.
```{r ghost-top-polygon}
top_polygon <- ggplot_build(top)$data[[1]][, c("x", "y")]
```
And I proceeded the same way for the bottom part of the ghost.
```{r ghost-bottom}
bottom <- ggplot() +
geom_arc_bar(
data = ghost_arc[-1, ],
mapping = aes(x0 = x0, y0 = y0, r0 = 0, r = r, start = start, end = end)
) +
coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1))
```
```{r ghost-bottom-plot, echo = FALSE}
bottom
```
```{r ghost-bottom-polygon}
bottom_polygon <- ggplot_build(bottom)$data[[1]][, c("x", "y")]
```
Then, I just added one point to "properly" link the top and the bottom part.
```{r ghost-body}
ghost_body <- dplyr::bind_rows(
top_polygon,
dplyr::tribble(
~x, ~y,
0.5, 0,
0.5, -0.5 + 1/6
),
bottom_polygon,
dplyr::tribble(
~x, ~y,
-0.5, -0.5 + 1/6,
-0.5, 0
)
)
```
I finally got the whole ghost shape I was looking for.
```{r ghost-body-plot}
ggplot() +
coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1)) +
geom_polygon(
data = ghost_body,
mapping = aes(x = x, y = y),
inherit.aes = FALSE
)
```
*Note: `ghost_body` is a dataset of `ggpacman` (`data("ghost_body", package = "ggpacman")`).*
*Note: `ghost_body` definitely needs some code refactoring.*
#### Eyes
The eyes are quite easy to draw, they are just circles, but ...
As for Pac-Man before, I wanted the ghosts to look where they are headed.
This implies moving the iris one way or the other, and so I defined five states for the iris: right, down, left, up and middle.
```{r ghost-eyes}
ghost_eyes <- tribble(
~x0, ~y0, ~r, ~part, ~direction,
1/5, 1/8, 1/8, "eye", c("up", "down", "right", "left", "middle"),
-1/5, 1/8, 1/8, "eye", c("up", "down", "right", "left", "middle"),
5/20, 1/8, 1/20, "iris", "right",
-3/20, 1/8, 1/20, "iris", "right",
1/5, 1/16, 1/20, "iris", "down",
-1/5, 1/16, 1/20, "iris", "down",
3/20, 1/8, 1/20, "iris", "left",
-5/20, 1/8, 1/20, "iris", "left",
1/5, 3/16, 1/20, "iris", "up",
-1/5, 3/16, 1/20, "iris", "up",
1/5, 1/8, 1/20, "iris", "middle",
-1/5, 1/8, 1/20, "iris", "middle"
) %>%
unnest("direction")
```
```{r ghost-eyes-plot}
map_eyes <- c("eye" = "white", "iris" = "black")
ggplot() +
coord_fixed(xlim = c(-0.5, 0.5), ylim = c(-0.5, 0.5)) +
scale_fill_manual(breaks = names(map_eyes), values = map_eyes) +
scale_colour_manual(breaks = names(map_eyes), values = map_eyes) +
geom_circle(
data = ghost_eyes,
mapping = aes(x0 = x0, y0 = y0, r = r, colour = part, fill = part),
inherit.aes = FALSE,
show.legend = FALSE
) +
facet_wrap(vars(direction), ncol = 3)
```
*Note: `ghost_eyes` is a dataset of `ggpacman` (`data("ghost_eyes", package = "ggpacman")`).*
#### Ghost shape
I had the whole ghost shape and the eyes.
```{r ghost-shape-plot}
ggplot() +
coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1)) +
scale_fill_manual(breaks = names(map_colours), values = map_colours) +
scale_colour_manual(breaks = names(map_colours), values = map_colours) +
geom_polygon(
data = get(data("ghost_body", package = "ggpacman")),
mapping = aes(x = x, y = y),
inherit.aes = FALSE
) +
geom_circle(
data = get(data("ghost_eyes", package = "ggpacman")),
mapping = aes(x0 = x0, y0 = y0, r = r, colour = part, fill = part),
inherit.aes = FALSE,
show.legend = FALSE
) +
facet_wrap(vars(direction), ncol = 3)
```
Again, same as for Pac-Man, in order to know where the ghosts are supposed to look, I computed the differences of each successive positions of the ghosts and I added the corresponding directions.
```{r blinky-ghost}
blinky_ghost <- tibble(x = c(0, 1, 1, 0, 0), y = c(0, 0, 1, 1, 0), colour = "Blinky") %>%
unnest(c("x", "y")) %>%
mutate(
X0 = x,
Y0 = y,
state_x = sign(round(x) - lag(round(x))),
state_y = sign(round(y) - lag(round(y))),
direction = case_when(
(is.na(state_x) | state_x %in% 0) & (is.na(state_y) | state_y %in% 0) ~ "middle",
state_x == 1 & state_y == 0 ~ "right",
state_x == -1 & state_y == 0 ~ "left",
state_x == 0 & state_y == -1 ~ "down",
state_x == 0 & state_y == 1 ~ "up"
)
) %>%
unnest("direction")
```
```{r blinky-ghost-static, echo = FALSE}
blinky_ghost
```
I also added some noise around the position, *i.e.*, four noised position at each actual position of a ghost.
```{r blinky-ghost-plot}
blinky_ghost <- blinky_ghost %>%
mutate(state = list(1:4)) %>%
unnest("state") %>%
mutate(
step = 1:n(),
noise_x = rnorm(n(), mean = 0, sd = 0.05),
noise_y = rnorm(n(), mean = 0, sd = 0.05)
)
```
```{r blinky-ghost-noise, echo = FALSE}
blinky_ghost
```
Then, I added (*in a weird way I might say*) the polygons coordinates for the body and the eyes.
```{r blinky-ghost-state}
blinky_ghost <- blinky_ghost %>%
mutate(
body = pmap(
.l = list(x, y, noise_x, noise_y),
.f = function(.x, .y, .noise_x, .noise_y) {
mutate(
.data = get(data("ghost_body")),
x = x + .x + .noise_x,
y = y + .y + .noise_y
)
}
),
eyes = pmap(
.l = list(x, y, noise_x, noise_y, direction),
.f = function(.x, .y, .noise_x, .noise_y, .direction) {
mutate(
.data = filter(get(data("ghost_eyes")), direction == .direction),
x0 = x0 + .x + .noise_x,
y0 = y0 + .y + .noise_y,
direction = NULL
)
}
),
x = NULL,
y = NULL
)
```
```{r blinky-ghost-show, echo = FALSE}
blinky_ghost
```
For ease, it is now a call to one function directly on the potion matrix of a ghost.
```{r blinky-moves}
blinky_ghost <- tibble(x = c(0, 1, 1, 0, 0), y = c(0, 0, 1, 1, 0), colour = "Blinky")
blinky_moves <- ggpacman::compute_ghost_coord(blinky_ghost)
```
```{r blinky-plot, message = FALSE}
blinky_plot <- base_layer +
coord_fixed(xlim = c(-1, 2), ylim = c(-1, 2)) +
geom_polygon(
data = unnest(blinky_moves, "body"),
mapping = aes(x = x, y = y, fill = colour, colour = colour, group = step),
inherit.aes = FALSE
) +
geom_circle(
data = unnest(blinky_moves, "eyes"),
mapping = aes(x0 = x0, y0 = y0, r = r, colour = part, fill = part, group = step),
inherit.aes = FALSE
)
```
```{r blinky-plot-static, echo = FALSE}
blinky_plot
```
Again, it is better with an animated GIF.
```{r blinky-animated}
animated_blinky <- blinky_plot + transition_manual(step)
```
```{r blinky-plot-animated, echo = FALSE, message = FALSE}
animate(
plot = animated_blinky,
width = 3.7 * 2.54,
height = 3.7 * 2.54,
units = "cm",
res = 120,
bg = "black",
duration = 10,
renderer = gifski_renderer()
)
```
### How Pac-Man interacts with the maze?
#### Bonus points
For ease, I am using some functions I defined to go quickly to the results of the first part of this readme.
The idea here is to look at all the position in common between Pac-Man (`pacman_moves`) and the bonus points (`maze_points`).
Each time Pac-Man was at the same place as a bonus point, I defined a status `"eaten"` for all values of `step` after.
I ended up with a big table with position and the state of the bonus points.
```{r points-eaten}
pacman_moves <- ggpacman::compute_pacman_coord(get(data("pacman", package = "ggpacman")))
right_join(get(data("maze_points")), pacman_moves, by = c("x", "y")) %>%
distinct(step, x, y, type) %>%
mutate(
step = map2(step, max(step), ~ seq(.x, .y, 1)),
colour = "eaten"
) %>%
unnest("step")
```
Again, for ease, I am using a function I defined to compute everything.
```{r create-data}
pacman_moves <- ggpacman::compute_pacman_coord(get(data("pacman", package = "ggpacman")))
bonus_points_eaten <- ggpacman::compute_points_eaten(get(data("maze_points")), pacman_moves)
```
If you don't recall, `maze_layer` already includes a geometry with the bonus points.
```{r reminder-maze, echo = FALSE}
maze_layer
```
I could have change this geometry (*i.e.*, `geom_point()`), but I did not, and draw a new geometry on top of the previous ones.
Do you remember the values of the scale for the size aesthetic?
```{r reminder-scale, eval = FALSE}
scale_size_manual(values = c("wall" = 2.5, "door" = 1, "big" = 2.5, "normal" = 0.5, "eaten" = 3))
```
```{r points-eaten-plot-code}
maze_layer_points <- maze_layer +
geom_point(
data = bonus_points_eaten,
mapping = aes(x = x, y = y, colour = colour, size = colour, group = step),
inherit.aes = FALSE
)
```
```{r points-eaten-plot, echo = FALSE}
maze_layer_points
```
A new animation to see, how the new geometry is overlapping the previous one as `step` increases.
```{r points-eaten-animated}
animated_points <- maze_layer_points + transition_manual(step)
```
```{r points-eaten-plot-animated, echo = FALSE, message = FALSE}
animate(
plot = animated_points,
width = 3.7 * 2.54,
height = 4.7 * 2.54,
units = "cm",
res = 120,
bg = "black",
duration = 10,
renderer = gifski_renderer()
)
```
#### Ghost `"weak"` and `"eaten"` states
The ghosts were more tricky (I know, they are ghosts ...).
I first retrieved all the positions where a `"big"` bonus point was eaten by Pac-Man.
```{r vulnerability}
ghosts_vulnerability <- bonus_points_eaten %>%
filter(type == "big") %>%
group_by(x, y) %>%
summarise(step_init = min(step)) %>%
ungroup() %>%
mutate(
step = map(step_init, ~ seq(.x, .x + 30, 1)),
vulnerability = TRUE,
x = NULL,
y = NULL
) %>%
unnest("step")
```
```{r vulnerability-values, echo = FALSE}
ghosts_vulnerability
```
This is part of a bigger function (I won't dive too deep into it).
```{r compute-function}
ggpacman::compute_ghost_status
```
The goal of this function, is to compute the different states of a ghost, according to the bonus points eaten and, of course, the current Pac-Man position at a determined `step`.
```{r ghost-moves-small}
pacman_moves <- ggpacman::compute_pacman_coord(get(data("pacman", package = "ggpacman")))
bonus_points_eaten <- ggpacman::compute_points_eaten(get(data("maze_points")), pacman_moves)
ghost_moves <- ggpacman::compute_ghost_status(
ghost = get(data("blinky", package = "ggpacman")),
pacman_moves = pacman_moves,
bonus_points_eaten = bonus_points_eaten
)
ghost_moves %>%
filter(state == 1) %>%
distinct(step, direction, colour, vulnerability) %>%
as.data.frame()
```
To simplify a little, below a small example of a ghost moving in one direction with predetermined states.
```{r blinky-moves-state}
blinky_ghost <- bind_rows(
tibble(x = 1:4, y = 0, colour = "Blinky"),
tibble(x = 5:8, y = 0, colour = "Blinky_weak"),
tibble(x = 9:12, y = 0, colour = "Blinky_eaten")
)
blinky_moves <- ggpacman::compute_ghost_coord(blinky_ghost)
```
```{r blinky-moves-small, echo = FALSE}
blinky_moves
```
```{r blinky-plot-state}
blinky_plot <- base_layer +
coord_fixed(xlim = c(0, 13), ylim = c(-1, 1)) +
geom_polygon(
data = unnest(blinky_moves, "body"),
mapping = aes(x = x, y = y, fill = colour, colour = colour, group = step),
inherit.aes = FALSE
) +
geom_circle(
data = unnest(blinky_moves, "eyes"),
mapping = aes(x0 = x0, y0 = y0, r = r, colour = part, fill = part, group = step),
inherit.aes = FALSE
)
```
```{r blinky-plot-line, echo = FALSE, fig.height = 0.5, fig.width = 3.7}
blinky_plot
```
I am sure, you remember all the colours and their mapped values from the beginning, so you probably won't need the following to understand of the ghost disappeared.
```{r reminder-colours, eval = FALSE}
"Blinky" = "red", "Blinky_weak" = "blue", "Blinky_eaten" = "transparent",
```
*Note: yes, `"transparent"` is a colour and a very handy one.*
A new animation to see our little Blinky in action?
```{r blinky-state-animated}
animated_blinky <- blinky_plot + transition_manual(step)
```
```{r blinky-state-plot-animated, echo = FALSE}
animate(
plot = animated_blinky,
width = 3.7 * 2.54,
height = 0.5 * 2.54,
units = "cm",
res = 120,
bg = "black",
duration = 10,
renderer = gifski_renderer()
)
```
### Plot time (to summarise a little (or a lot))
In the current version, nearly everything is either a dataset or a function and could be used like this.
1. Load and compute the data.
```{r plot-time-data}
data("pacman", package = "ggpacman")
data("maze_points", package = "ggpacman")
data("maze_walls", package = "ggpacman")
data("blinky", package = "ggpacman")
data("pinky", package = "ggpacman")
data("inky", package = "ggpacman")
data("clyde", package = "ggpacman")
ghosts <- list(blinky, pinky, inky, clyde)
pacman_moves <- ggpacman::compute_pacman_coord(pacman)
bonus_points_eaten <- ggpacman::compute_points_eaten(maze_points, pacman_moves)
map_colours <- c(
"READY!" = "goldenrod1",
"wall" = "dodgerblue3", "door" = "dodgerblue3",
"normal" = "goldenrod1", "big" = "goldenrod1", "eaten" = "black",
"Pac-Man" = "yellow",
"eye" = "white", "iris" = "black",
"Blinky" = "red", "Blinky_weak" = "blue", "Blinky_eaten" = "transparent",
"Pinky" = "pink", "Pinky_weak" = "blue", "Pinky_eaten" = "transparent",
"Inky" = "cyan", "Inky_weak" = "blue", "Inky_eaten" = "transparent",
"Clyde" = "orange", "Clyde_weak" = "blue", "Clyde_eaten" = "transparent"
)
```
2. Build the base layer with the maze.
```{r plot-time-base}
base_grid <- ggplot() +
theme_void() +
theme(
legend.position = "none",
plot.caption = element_textbox_simple(halign = 0.5, colour = "white"),
plot.caption.position = "plot",
plot.background = element_rect(fill = "black", colour = "black"),