Mouse Input

Mouse Position

x, y := p8.GetMouseXY()

Returns the current mouse position in game coordinates.

Example: Cursor

func (g *game) Draw() {
    p8.Cls(0)
    
    mx, my := p8.GetMouseXY()
    p8.Circ(mx, my, 2, 7)  // Draw cursor
}

Mouse Buttons

ConstantButton
p8.ButtonMouseLeftLeft click
p8.ButtonMouseRightRight click
p8.ButtonMouseMiddleMiddle click
p8.ButtonMouseWheelUpScroll up
p8.ButtonMouseWheelDownScroll down

Click Detection

func (g *game) Update() {
    mx, my := p8.GetMouseXY()
    
    if p8.Btnp(p8.ButtonMouseLeft) {
        // Left mouse button just clicked
        g.handleClick(mx, my)
    }
    
    if p8.Btn(p8.ButtonMouseLeft) {
        // Left mouse button held (dragging)
        g.handleDrag(mx, my)
    }
}

Scroll Wheel

func (g *game) Update() {
    if p8.Btn(p8.ButtonMouseWheelUp) {
        g.zoom++
    }
    if p8.Btn(p8.ButtonMouseWheelDown) {
        g.zoom--
    }
}

Example: Paint Program

type game struct {
    lastX, lastY int
}

func (g *game) Update() {
    mx, my := p8.GetMouseXY()
    
    if p8.Btn(p8.ButtonMouseLeft) {
        // Draw line from last position to current
        p8.Line(g.lastX, g.lastY, mx, my, 7)
    }
    
    g.lastX = mx
    g.lastY = my
}