func mleft() { if !collide(draw.Pt(pos.X-pcsz, pos.Y), piece) && !collide(draw.Pt(pos.X-pcsz, pos.Y+pcsz-DY), piece) { undrawpiece() pos.X -= pcsz drawpiece() display.FlushImage() } }
func newBox(cvs *canvas.Canvas, r draw.Rectangle) *box { var b box red := image.Uniform{image.Red} b.n = canvas.NewLine(red, r.Min, draw.Pt(r.Max.X, r.Min.Y), 1) b.e = canvas.NewLine(red, draw.Pt(r.Max.X, r.Min.Y), r.Max, 1) b.s = canvas.NewLine(red, r.Max, draw.Pt(r.Min.X, r.Max.Y), 1) b.w = canvas.NewLine(red, draw.Pt(r.Min.X, r.Max.Y), r.Min, 1) cvs.AddItem(b.n) cvs.AddItem(b.e) cvs.AddItem(b.s) cvs.AddItem(b.w) return &b }
func (ctxt *context) interactiveJulia(m draw.Mouse, mc <-chan draw.Mouse) { var i canvas.ImageItem i.IsOpaque = true i.R = draw.Rect(0, 0, ThumbSize, ThumbSize).Add(ctxt.cvs.Rect().Max).Sub(draw.Pt(ThumbSize, ThumbSize)) i.Image = image.NewRGBA(image.Rect(0, 0, ThumbSize, ThumbSize)) ctxt.cvs.AddItem(&i) defer func() { ctxt.cvs.Delete(&i) ctxt.cvs.Flush() }() delta := ctxt.mouseDelta() for { f := ctxt.f.Associated(m.Point.Add(delta)) if f == nil { for m.Buttons != 0 { m = <-mc } return } r := draw.Rect(0, 0, ThumbSize, ThumbSize) f = f.Resize(r) ctxt.cvs.Atomically(func(flush canvas.FlushFunc) { NewTile(r, f, i.Image.(*image.RGBA), true) flush(i.Bbox(), nil) }) ctxt.cvs.Flush() if m.Buttons == 0 { ctxt.julia(m.Point) return } m = <-mc } }
func horiz() bool { var lev [MAXN]int h := 0 for i := 0; i < NY; i++ { for j := 0; board[i][j] != 0; j++ { if j == NX-1 { lev[h] = i h++ break } } } if h == 0 { return false } r := rboard newscreen = false for j := 0; j < h; j++ { r.Min.Y = rboard.Min.Y + lev[j]*pcsz r.Max.Y = r.Min.Y + pcsz draw.Draw(screen, r, draw.White, whitemask, draw.ZP) display.FlushImage() } PlaySound(whoosh) for i := 0; i < 3; i++ { pause(250) if newscreen { drawboard() break } for j := 0; j < h; j++ { r.Min.Y = rboard.Min.Y + lev[j]*pcsz r.Max.Y = r.Min.Y + pcsz draw.Draw(screen, r, draw.White, whitemask, draw.ZP) } display.FlushImage() } r = rboard for j := 0; j < h; j++ { i := NY - lev[j] - 1 score(250 + 10*i*i) r.Min.Y = rboard.Min.Y r.Max.Y = rboard.Min.Y + lev[j]*pcsz draw.Draw(screen, r.Add(draw.Pt(0, pcsz)), screen, nil, r.Min) r.Max.Y = rboard.Min.Y + pcsz draw.Draw(screen, r, draw.White, nil, draw.ZP) for k := lev[j] - 1; k >= 0; k-- { board[k+1] = board[k] } board[0] = [NX]byte{} } display.FlushImage() return true }
func movepiece() bool { var mask image.Image if collide(draw.Pt(pos.X, pos.Y+pcsz), piece) { return false } if collider(pos, br2.Max) { mask = bb2mask } draw.Draw(screen, br2.Add(pos), bb2, mask, bb2r.Min) pos.Y += DY display.FlushImage() return true }
func choosepiece() { for { i := rand.Intn(len(pieces)) setpiece(&pieces[i]) pos = rboard.Min pos.X += rand.Intn(NX) * pcsz if !collide(draw.Pt(pos.X, pos.Y+pcsz-DY), piece) { break } } drawpiece() display.FlushImage() }
func drawboard() { draw.Border(screen, rboard.Inset(-2), 2, draw.Black, draw.ZP) draw.Draw(screen, draw.Rect(rboard.Min.X, rboard.Min.Y-2, rboard.Max.X, rboard.Min.Y), draw.White, nil, draw.ZP) for i := 0; i < NY; i++ { for j := 0; j < NX; j++ { if board[i][j] != 0 { drawsq(screen, draw.Pt(rboard.Min.X+j*pcsz, rboard.Min.Y+i*pcsz), int(board[i][j]-16)) } } } score(0) if suspended { draw.Draw(screen, screenr, draw.White, whitemask, draw.ZP) } }
func setpiece(p *Piece) { draw.Draw(bb, bbr, draw.White, nil, draw.ZP) draw.Draw(bbmask, bbr, draw.Transparent, nil, draw.ZP) br = draw.Rect(0, 0, 0, 0) br2 = br piece = p if p == nil { return } var op draw.Point var r draw.Rectangle r.Min = bbr.Min for i, pt := range p.d { r.Min.X += pt.X * pcsz r.Min.Y += pt.Y * pcsz r.Max.X = r.Min.X + pcsz r.Max.Y = r.Min.Y + pcsz if i == 0 { draw.Draw(bb, r, draw.Black, nil, draw.ZP) draw.Draw(bb, r.Inset(1), txpix[piece.tx], nil, draw.ZP) draw.Draw(bbmask, r, draw.Opaque, nil, draw.ZP) op = r.Min } else { draw.Draw(bb, r, bb, nil, op) draw.Draw(bbmask, r, bbmask, nil, op) } if br.Max.X < r.Max.X { br.Max.X = r.Max.X } if br.Max.Y < r.Max.Y { br.Max.Y = r.Max.Y } } br.Max = br.Max.Sub(bbr.Min) delta := draw.Pt(0, DY) br2.Max = br.Max.Add(delta) r = br.Add(bb2r.Min) r2 := br2.Add(bb2r.Min) draw.Draw(bb2, r2, draw.White, nil, draw.ZP) draw.Draw(bb2, r.Add(delta), bb, nil, bbr.Min) draw.Draw(bb2mask, r2, draw.Transparent, nil, draw.ZP) draw.Draw(bb2mask, r, draw.Opaque, bbmask, bbr.Min) draw.Draw(bb2mask, r.Add(delta), draw.Opaque, bbmask, bbr.Min) }
func (b *box) setRect(r draw.Rectangle) { b.n.SetEndPoints(r.Min, draw.Pt(r.Max.X, r.Min.Y)) b.e.SetEndPoints(draw.Pt(r.Max.X, r.Min.Y), r.Max) b.s.SetEndPoints(r.Max, draw.Pt(r.Min.X, r.Max.Y)) b.w.SetEndPoints(draw.Pt(r.Min.X, r.Max.Y), r.Min) }
func centre(r draw.Rectangle) draw.Point { return draw.Pt((r.Min.X+r.Max.X)/2, (r.Min.Y+r.Max.Y)/2) }