Skip to content

Commit d59f217

Browse files
committed
examples: use textures in the sokoban game, instead of colors
1 parent 543a1d2 commit d59f217

File tree

8 files changed

+33
-16
lines changed

8 files changed

+33
-16
lines changed

examples/sokoban/box.png

3.53 KB
Loading

examples/sokoban/box_on_storage.png

4.02 KB
Loading

examples/sokoban/floor.png

3.14 KB
Loading

examples/sokoban/other.png

3.62 KB
Loading

examples/sokoban/player.png

5.7 KB
Loading

examples/sokoban/sokoban.v

+33-16
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import gg
33
import gx
44

55
const csize = 32
6-
const color_box = gx.rgb(139, 69, 19) // Brown
7-
const color_wall = gx.rgb(100, 100, 100) // Gray
8-
const color_floor = gx.rgb(200, 200, 200) // Light gray
9-
const color_player = gx.rgb(0, 0, 255) // Blue
10-
const color_storage = gx.rgb(0, 255, 0) // Green
11-
const color_box_on_storage = gx.rgb(255, 255, 0) // Yellow
126

137
struct Pos {
148
x int
@@ -36,6 +30,14 @@ mut:
3630
levels []string
3731
moves int
3832
ctx &gg.Context = unsafe { nil }
33+
//
34+
id_box int
35+
id_box_on_storage int
36+
id_wall int
37+
id_floor int
38+
id_other int
39+
id_player int
40+
id_storage int
3941
}
4042

4143
fn (mut g Game) parse_level(lnumber int) ! {
@@ -132,24 +134,24 @@ fn (mut g Game) move_player(dir Direction) {
132134
g.moves++
133135
}
134136

135-
fn (g &Game) get_cell_color(pos Pos) gx.Color {
137+
fn (g &Game) get_cell_iid(pos Pos) int {
136138
c := g.warehouse[pos.y][pos.x]
137139
if pos.x == g.player.x && pos.y == g.player.y {
138-
return color_player
140+
return g.id_player
139141
}
140142
for box in g.boxes {
141143
if box.x == pos.x && box.y == pos.y {
142144
if c == `@` {
143-
return color_box_on_storage
145+
return g.id_box_on_storage
144146
}
145-
return color_box
147+
return g.id_box
146148
}
147149
}
148150
match c {
149-
` ` { return color_floor }
150-
`#` { return color_wall }
151-
`@` { return color_storage }
152-
else { return gx.black }
151+
` ` { return g.id_floor }
152+
`#` { return g.id_wall }
153+
`@` { return g.id_storage }
154+
else { return g.id_other }
153155
}
154156
}
155157

@@ -212,8 +214,12 @@ fn (g &Game) draw_frame(_ voidptr) {
212214
for y in 0 .. g.warehouse.len {
213215
for x in 0 .. g.warehouse[y].len {
214216
pos := Pos{x, y}
215-
color := g.get_cell_color(pos)
216-
g.ctx.draw_rect_filled(ox + x * csize, oy + y * csize, csize, csize, color)
217+
iid := g.get_cell_iid(pos)
218+
if iid == g.id_player {
219+
// the player is transparent
220+
g.ctx.draw_image_by_id(ox + x * csize, oy + y * csize, 32, 32, g.id_floor)
221+
}
222+
g.ctx.draw_image_by_id(ox + x * csize, oy + y * csize, 32, 32, iid)
217223
}
218224
}
219225
g.ctx.draw_rect_filled(0, ws.height - 70, ws.width, 70, gx.black)
@@ -231,6 +237,10 @@ fn (g &Game) draw_frame(_ voidptr) {
231237
g.ctx.end()
232238
}
233239

240+
fn (mut g Game) iid(name string) !int {
241+
return g.ctx.create_image(asset.get_path('/', name))!.id
242+
}
243+
234244
fn main() {
235245
mut g := &Game{}
236246
all_level_names := asset.read_bytes('/', '_all_levels.txt')!.bytestr().split_into_lines()
@@ -244,5 +254,12 @@ fn main() {
244254
frame_fn: g.draw_frame
245255
keydown_fn: g.key_down
246256
)
257+
g.id_box = g.iid('box.png')!
258+
g.id_box_on_storage = g.iid('box_on_storage.png')!
259+
g.id_wall = g.iid('wall.png')!
260+
g.id_floor = g.iid('floor.png')!
261+
g.id_other = g.iid('other.png')!
262+
g.id_player = g.iid('player.png')!
263+
g.id_storage = g.iid('storage.png')!
247264
g.ctx.run()
248265
}

examples/sokoban/storage.png

5.04 KB
Loading

examples/sokoban/wall.png

3.16 KB
Loading

0 commit comments

Comments
 (0)