-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsprites.R
More file actions
117 lines (86 loc) · 3.34 KB
/
Copy pathsprites.R
File metadata and controls
117 lines (86 loc) · 3.34 KB
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
library(grid)
library(nara)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load the spritemap for pacman and the ghosts
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# spritemap <- png::readPNG("image/game-sprites.png")
sm <- fastpng::read_png("image/game-sprites.png", type = 'nativeraster')
if (FALSE) {
dim(spritemap)
grid.raster(spritemap)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Extract a sprite from the spritemap
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extract_old <- 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 <- function(row, col) {
sprite <- nara::nr_crop(sm, 456 + (col-1)*16, (row-1)*16, 16, 16)
nara::nr_color_replace(sprite, -16777216L, 0L) # replace 'black' with 'transparent'
sprite
}
if (FALSE) {
extract (5, 2) |> plot(T)
extract2(5, 2) |> plot(T)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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 = ghost1,
ghost2 = ghost2,
ghost3 = ghost3,
ghost4 = ghost4
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Test plot of a single sprite
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (FALSE) {
grid.raster(ghost4$right[[1]], interpolate = FALSE)
grid.newpage(); dev.hold(); grid.raster(pacman$left[[1]], interpolate = FALSE); dev.flush()
}