forked from coolbutuseless/pacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.R
91 lines (73 loc) · 2.94 KB
/
sprites.R
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
library(grid)
library(nara)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load the spritemap for pacman and the ghosts
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
spritemap <- png::readPNG("image/game-sprites.png")
if (FALSE) {
dim(spritemap)
grid.raster(spritemap)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Extract a sprite from the spritemap
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extract <- function(row, col) {
sprite <- spritemap[1 + (row-1)*16 + 0:15, 457 + (col-1)*16 + 0:15,]
alpha <- sprite[,,1] == 1 | sprite[,,2] == 1 | sprite[,,3] == 1
alpha[] <- as.numeric(alpha)
new <- c(sprite, alpha)
d <- dim(sprite)
d[3] <- 4
dim(new) <- d
nara::array_to_nr(new)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Extract pacman sprites
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pacman <- list(
right = list(extract(1, 1), extract(1, 2), extract(1, 3), extract(1, 2)),
left = list(extract(2, 1), extract(2, 2), extract(1, 3), extract(2, 2)),
up = list(extract(3, 1), extract(3, 2), extract(1, 3), extract(3, 2)),
down = list(extract(4, 1), extract(4, 2), extract(1, 3), extract(4, 2)),
rest = list(extract(1, 3), extract(1, 3), extract(1, 3), extract(1, 3))
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Extract ghost sprites
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ghost1 <- list(
right = list(extract(5, 1), extract(5, 2)),
left = list(extract(5, 3), extract(5, 4)),
up = list(extract(5, 5), extract(5, 6)),
down = list(extract(5, 7), extract(5, 8))
)
ghost2 <- list(
right = list(extract(6, 1), extract(6, 2)),
left = list(extract(6, 3), extract(6, 4)),
up = list(extract(6, 5), extract(6, 6)),
down = list(extract(6, 7), extract(6, 8))
)
ghost3 <- list(
right = list(extract(7, 1), extract(7, 2)),
left = list(extract(7, 3), extract(7, 4)),
up = list(extract(7, 5), extract(7, 6)),
down = list(extract(7, 7), extract(7, 8))
)
ghost4 <- list(
right = list(extract(8, 1), extract(8, 2)),
left = list(extract(8, 3), extract(8, 4)),
up = list(extract(8, 5), extract(8, 6)),
down = list(extract(8, 7), extract(8, 8))
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Combine all ghosts
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ghost <- list(
ghost1, ghost2, ghost3, ghost4
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Test plot of a single sprite
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (FALSE) {
grid.raster(ghost4$right[[1]], interpolate = FALSE)
grid.newpage(); dev.hold(); grid.raster(pacman$rest[[2]], interpolate = FALSE); dev.flush()
}