Skip to content

Commit 171c68b

Browse files
authored
Merge pull request #27 from hamao0820/hamao0820/drawing
Add new example "drawing"
2 parents 5b50d5b + b21a7d3 commit 171c68b

File tree

3 files changed

+132
-13
lines changed

3 files changed

+132
-13
lines changed

Makefile

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
smoketest: FORCE
22
mkdir -p out
3-
tinygo build -o ./out/all.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/all/
4-
tinygo build -o ./out/flappygopher.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/flappygopher/
5-
tinygo build -o ./out/jumpingopher.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/jumpingopher/
6-
tinygo build -o ./out/blocks.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/blocks/
7-
tinygo build -o ./out/example_GeoM.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./examples/GeoM/
8-
tinygo build -o ./out/all.gopher-badge.uf2 --size short --target gopher-badge ./games/all/
9-
tinygo build -o ./out/all.pybadge.uf2 --size short --target pybadge ./games/all/
10-
tinygo build -o ./out/all.wioterminal.uf2 --size short --target wioterminal ./games/all/
11-
tinygo build -o ./out/all.macropad-rp2040.uf2 --size short --target macropad-rp2040 ./games/all/
12-
tinygo build -o ./out/all.gopher-board-i2c.uf2 --size short --target ./targets/gopher-board-i2c.json ./games/all/
13-
tinygo build -o ./out/all.gopher-board-spi.uf2 --size short --target ./targets/gopher-board-spi.json ./games/all/
14-
tinygo build -o ./out/all.wasm --size short --target wasm --no-debug --panic trap ./games/all/
15-
tinygo build -o ./out/goradius.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/goradius/
3+
tinygo build -o ./out/all.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/all/
4+
tinygo build -o ./out/flappygopher.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/flappygopher/
5+
tinygo build -o ./out/jumpingopher.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/jumpingopher/
6+
tinygo build -o ./out/blocks.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/blocks/
7+
tinygo build -o ./out/example_GeoM.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./examples/GeoM/
8+
tinygo build -o ./out/example_drawing.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./examples/drawing/
9+
tinygo build -o ./out/all.gopher-badge.uf2 --size short --target gopher-badge ./games/all/
10+
tinygo build -o ./out/all.pybadge.uf2 --size short --target pybadge ./games/all/
11+
tinygo build -o ./out/all.wioterminal.uf2 --size short --target wioterminal ./games/all/
12+
tinygo build -o ./out/all.macropad-rp2040.uf2 --size short --target macropad-rp2040 ./games/all/
13+
tinygo build -o ./out/all.gopher-board-i2c.uf2 --size short --target ./targets/gopher-board-i2c.json ./games/all/
14+
tinygo build -o ./out/all.gopher-board-spi.uf2 --size short --target ./targets/gopher-board-spi.json ./games/all/
15+
tinygo build -o ./out/all.wasm --size short --target wasm --no-debug --panic trap ./games/all/
16+
tinygo build -o ./out/goradius.zero-kb02.uf2 --size short --target ./targets/zero-kb02.json ./games/goradius/
1617

1718
FORCE:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package drawing
2+
3+
import (
4+
"github.com/sago35/koebiten"
5+
"tinygo.org/x/drivers/pixel"
6+
)
7+
8+
var (
9+
white = pixel.NewMonochrome(0xFF, 0xFF, 0xFF)
10+
black = pixel.NewMonochrome(0x00, 0x00, 0x00)
11+
)
12+
13+
type Pointer struct {
14+
x, y int
15+
}
16+
17+
type Game struct {
18+
pointer Pointer
19+
thick int
20+
canvas *koebiten.Image
21+
}
22+
23+
func NewGame() *Game {
24+
return &Game{
25+
pointer: Pointer{64, 32},
26+
canvas: koebiten.NewImage(128, 64),
27+
}
28+
}
29+
30+
func (g *Game) Update() error {
31+
if koebiten.IsKeyPressed(koebiten.KeyArrowLeft) {
32+
g.pointer.x--
33+
}
34+
if koebiten.IsKeyPressed(koebiten.KeyArrowRight) {
35+
g.pointer.x++
36+
}
37+
if koebiten.IsKeyPressed(koebiten.KeyArrowUp) {
38+
g.pointer.y--
39+
}
40+
if koebiten.IsKeyPressed(koebiten.KeyArrowDown) {
41+
g.pointer.y++
42+
}
43+
44+
if koebiten.IsKeyPressed(koebiten.KeyRotaryLeft) {
45+
g.thick--
46+
if g.thick < 0 {
47+
g.thick = 0
48+
}
49+
}
50+
if koebiten.IsKeyPressed(koebiten.KeyRotaryRight) {
51+
g.thick++
52+
if g.thick > 10 {
53+
g.thick = 10
54+
}
55+
}
56+
57+
if koebiten.IsKeyPressed(koebiten.Key0) {
58+
g.draw(g.canvas, g.pointer.x, g.pointer.y, black)
59+
}
60+
if koebiten.IsKeyPressed(koebiten.Key1) || koebiten.IsKeyPressed(koebiten.Key2) ||
61+
koebiten.IsKeyPressed(koebiten.Key3) || koebiten.IsKeyPressed(koebiten.Key4) ||
62+
koebiten.IsKeyPressed(koebiten.Key5) || koebiten.IsKeyPressed(koebiten.Key6) ||
63+
koebiten.IsKeyPressed(koebiten.Key7) || koebiten.IsKeyPressed(koebiten.Key8) ||
64+
koebiten.IsKeyPressed(koebiten.Key9) || koebiten.IsKeyPressed(koebiten.Key10) ||
65+
koebiten.IsKeyPressed(koebiten.Key11) {
66+
g.draw(g.canvas, g.pointer.x, g.pointer.y, white)
67+
}
68+
69+
return nil
70+
}
71+
72+
func (g *Game) Draw(screen *koebiten.Image) {
73+
g.canvas.DrawImage(screen, koebiten.DrawImageOptions{})
74+
koebiten.DrawFilledCircle(screen, g.pointer.x, g.pointer.y, g.thick+1, white)
75+
}
76+
77+
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
78+
return 128, 64
79+
}
80+
81+
func (g *Game) draw(canvas *koebiten.Image, x, y int, color pixel.BaseColor) {
82+
koebiten.DrawFilledCircle(g.canvas, x, y, g.thick, color)
83+
}
84+
85+
func isAnyKeyPressed() bool {
86+
keys := []koebiten.Key{
87+
koebiten.Key0, koebiten.Key1, koebiten.Key2, koebiten.Key3,
88+
koebiten.Key4, koebiten.Key5, koebiten.Key6, koebiten.Key7,
89+
koebiten.Key8, koebiten.Key9, koebiten.Key10, koebiten.Key11,
90+
}
91+
for _, key := range keys {
92+
if koebiten.IsKeyPressed(key) {
93+
return true
94+
}
95+
}
96+
return false
97+
}

examples/drawing/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/sago35/koebiten"
7+
"github.com/sago35/koebiten/examples/drawing/drawing"
8+
"github.com/sago35/koebiten/hardware"
9+
)
10+
11+
func main() {
12+
koebiten.SetHardware(hardware.Device)
13+
koebiten.SetWindowSize(128, 64)
14+
koebiten.SetWindowTitle("Drawing")
15+
16+
game := drawing.NewGame()
17+
18+
if err := koebiten.RunGame(game); err != nil {
19+
log.Fatal(err)
20+
}
21+
}

0 commit comments

Comments
 (0)