Browse Source

Add hold box

master v1.0.0
Joop Kiefte 9 months ago
parent
commit
c3da6b22c7
3 changed files with 34 additions and 5 deletions
  1. +9
    -5
      main.go
  2. +2
    -0
      tris/core.go
  3. +23
    -0
      tris/hold.go

+ 9
- 5
main.go View File

@ -89,13 +89,15 @@ func main() {
sscore := fmt.Sprintf("score %d", score)
slines := fmt.Sprintf("lines %d", linescleared)
if !harddrop {
fpos(0, 0, f.Add(p))
ppos(0, 0, "Hold (c)")
npos(3, 0, tris.HoldBox)
fpos(0, 10, f.Add(p))
var next tris.Field
b, next = b.Next(5)
npos(0, 24, next)
ppos(1, 32, sscore)
ppos(3, 32, slevel)
ppos(5, 32, slines)
npos(0, 34, next)
ppos(1, 42, sscore)
ppos(3, 42, slevel)
ppos(5, 42, slines)
key = GetKey(t)
}
switch key[0] {
@ -119,6 +121,8 @@ func main() {
rot = (p.Rot + 1) % 4
case 'z':
rot = (p.Rot + 3) % 4
case 'c':
b, p = b.Swap(p)
case ' ':
if !harddrop {
dropfrom = p.Y


+ 2
- 0
tris/core.go View File

@ -20,6 +20,7 @@ type Piece [4]uint16
// A table that represents each piece from https://tetris.fandom.com/wiki/SRS
// in binary starting with 1 top left going per row, here in hexadecimal shorthand.
var (
EmptyPiece = Piece{}
IPiece = Piece{0x0F00, 0x4444, 0x00F0, 0x2222}
JPiece = Piece{0x1700, 0x6220, 0x0740, 0x2230}
LPiece = Piece{0x4700, 0x2260, 0x0710, 0x3220}
@ -130,6 +131,7 @@ func (b Bag) Randomize() Bag {
}
func (b Bag) Pick() (Bag, Placement) {
Swapped = false
if len(b) == 0 {
b = NewBag()
}


+ 23
- 0
tris/hold.go View File

@ -0,0 +1,23 @@
package tris
var HoldPiece Piece
var HoldBox Field
var Swapped bool
func (b Bag) Swap(p Placement) (Bag, Placement) {
if Swapped {
return b, p
}
var tempp Placement
if HoldPiece == EmptyPiece {
b, tempp = b.Pick()
HoldPiece = tempp.piece
}
p.piece, HoldPiece = HoldPiece, p.piece
Swapped = true
HoldBox, _ = Placement{piece: HoldPiece, X: 0, Y:0}.Field(2,4)
p.Rot, p.X, p.Y = 0, 3, -2
return b, p
}

Loading…
Cancel
Save