完善 w4-life 游戏交互

This commit is contained in:
chai2010
2024-09-10 19:38:33 +08:00
parent 108477b31a
commit c59309e63c
3 changed files with 28 additions and 0 deletions

View File

@@ -21,6 +21,9 @@ func BitImage.At(x, y: int) => bool {
}
func BitImage.Set(x, y: int, c: bool) {
if x < 0 || x >= this.Width || y < 0 || y >= this.Height {
return
}
idx := (y*this.Width + x) / 8
if c {
this.Pix[idx] = byte(setBit(uint(this.Pix[idx]), uint(x%8)))

View File

@@ -45,6 +45,14 @@ func LifeInit {
}
}
func LifeSetRect(x0, y0, x1, y1: int) {
for x := x0; x < x1; x++ {
for y := y0; y < y1; y++ {
cells0.Set(x, y, true)
}
}
}
type DIR :struct {
x: int
y: int

View File

@@ -18,5 +18,22 @@ func Update {
LifeStart()
}
mouseX := int(wasm4.GetMouseX())
mouseY := int(wasm4.GetMouseY())
if byte(wasm4.GetMouseButtons()) == wasm4.MOUSE_LEFT {
LifeSetRect(mouseX-2, mouseY-2, mouseX+2, mouseY+2)
}
LifeStep()
drawMouse(mouseX, mouseY)
}
func drawMouse(mouseX, mouseY: int) {
if mouseX < 0 || mouseY < 0 || mouseX >= wasm4.SCREEN_SIZE || mouseY >= wasm4.SCREEN_SIZE {
return
}
wasm4.SetDrawColors(2)
wasm4.Rect(mouseX-1, mouseY-1, 3, 3)
}