diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..602c9e8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +--- +repos: + - repo: https://github.com/jessp01/pre-commit-golang.git + rev: v0.5.5 + hooks: + - id: go-fmt + - id: go-imports + - id: go-vet + - id: go-lint + # - id: go-critic + - id: go-ineffassign + - id: shellcheck diff --git a/board.go b/board.go index 0e35533..c16936d 100644 --- a/board.go +++ b/board.go @@ -3,8 +3,8 @@ package main import ( "math/rand" - "github.com/faiface/pixel" - "github.com/faiface/pixel/pixelgl" + pixel "github.com/gopxl/pixel" + "github.com/gopxl/pixel/pixelgl" ) // isTouchingFloor checks if the piece that the user is controlling has a piece @@ -80,7 +80,7 @@ func (b Board) checkCollision(s Shape) bool { for i := 0; i < 4; i++ { r := s[i].row c := s[i].col - if r < 0 || r > 21 || c < 0 || c > 9 || b[r][c] != Empty { + if r < 0 || r > 21 || c < 0 || c > BoardCols-1 || b[r][c] != Empty { return true } } @@ -137,7 +137,7 @@ func (b *Board) checkRowCompletion(s Shape) { r := s[i].row emptyFound := false // Look for empty row - for c := 0; c < 10; c++ { + for c := 0; c < BoardCols; c++ { if b[r][c] == Empty { emptyFound = true continue @@ -161,7 +161,7 @@ func (b *Board) checkRowCompletion(s Shape) { // deleteRow remoes a row by shifting everything above it down by one. func (b *Board) deleteRow(row int) { for r := row; r < 21; r++ { - for c := 0; c < 10; c++ { + for c := 0; c < BoardCols; c++ { b[r][c] = b[r+1][c] } } @@ -184,11 +184,12 @@ func (b *Board) fillShape(s Shape, val Block) { // (ie activeShape). func (b *Board) addPiece() { var offset int - if nextPiece == IPiece { + switch nextPiece { + case IPiece: offset = rand.Intn(7) - } else if nextPiece == OPiece { + case OPiece: offset = rand.Intn(9) - } else { + default: offset = rand.Intn(8) } baseShape := getShapeFromPiece(nextPiece) @@ -202,7 +203,7 @@ func (b *Board) addPiece() { // displayBoard displays a particular game board with all of its pieces // onto a given window, win func (b *Board) displayBoard(win *pixelgl.Window) { - boardBlockSize := 20.0 //win.Bounds().Max.X / 10 + boardBlockSize := 20.0 // win.Bounds().Max.X / 10 pic := blockGen(0) imgSize := pic.Bounds().Max.X scaleFactor := float64(boardBlockSize) / float64(imgSize) diff --git a/go.mod b/go.mod index 5937947..e8b2e52 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,19 @@ module github.com/zkry/golang-tetris -go 1.14 +go 1.21 + +toolchain go1.22.2 + +require ( + github.com/gopxl/pixel v1.0.0 + golang.org/x/image v0.13.0 +) require ( - github.com/faiface/pixel v0.9.0 - golang.org/x/image v0.0.0-20200618115811-c13761719519 + github.com/faiface/glhf v0.0.0-20211013000516-57b20770c369 // indirect + github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 // indirect + github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect + github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect + github.com/go-gl/mathgl v1.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect ) diff --git a/go.sum b/go.sum index 12cc705..6100061 100644 --- a/go.sum +++ b/go.sum @@ -1,28 +1,31 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0= -github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/faiface/glhf v0.0.0-20211013000516-57b20770c369 h1:gv4BgP50atccdK/1tZHDyP6rMwiiutR2HPreR/OyLzI= +github.com/faiface/glhf v0.0.0-20211013000516-57b20770c369/go.mod h1:dDdUO+G9ZnJ9sc8nIUvhLkE45k8PEKW6+A3TdWsfpV0= github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q= github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M= -github.com/faiface/pixel v0.9.0 h1:EtOO20jUkJ+SQAtWy19acwmhn/gowQNcfxpvfL8MTE0= -github.com/faiface/pixel v0.9.0/go.mod h1:WkLfLymV31e/Ogv5OR3vtrNxRktTO3WXGWXiiSEg/j4= -github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= -github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= -github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0= -github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= -github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7 h1:THttjeRn1iiz69E875U6gAik8KTWk/JYAHoSVpUxBBI= -github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= +github.com/go-gl/gl v0.0.0-20210905235341-f7a045908259/go.mod h1:wjpnOv6ONl2SuJSxqCPVaPZibGFdSci9HFocT9qtVYM= +github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 h1:zDw5v7qm4yH7N8C8uWd+8Ii9rROdgWxQuGoJ9WDXxfk= +github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= +github.com/go-gl/glfw v0.0.0-20210727001814-0db043d8d5be/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-gl/mathgl v1.0.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= +github.com/go-gl/mathgl v1.1.0 h1:0lzZ+rntPX3/oGrDzYGdowSLC2ky8Osirvf5uAwfIEA= +github.com/go-gl/mathgl v1.1.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/gopxl/pixel v1.0.0 h1:ZON6ll6/tI6sO8fwrlj93GVUcXReTST5//iKv6lcd8g= +github.com/gopxl/pixel v1.0.0/go.mod h1:kPUBG2He7/+alwmi5z0IwnpAc6pw2N7eA08cdBfoE/Q= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34= -golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg= +golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 251fcd9..1eda243 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,9 @@ import ( "os" "time" - "github.com/faiface/pixel" - "github.com/faiface/pixel/pixelgl" - "github.com/faiface/pixel/text" + pixel "github.com/gopxl/pixel" + "github.com/gopxl/pixel/pixelgl" + "github.com/gopxl/pixel/text" "golang.org/x/image/colornames" "golang.org/x/image/font/basicfont" @@ -23,6 +23,9 @@ const BoardRows = 22 // BoardCols is the width of the game board in terms of blocks const BoardCols = 10 +// ResourceDir relative path to dir where images are stored +const ResourceDir = "/resources" + // Point represents a coordinate on the game board with Point{row:0, col:0} // representing the bottom left type Point struct { @@ -31,7 +34,7 @@ type Point struct { } // Board is an array containing the entire game board pieces. -type Board [22][10]Block +type Board [BoardRows][BoardCols]Block // Block represents the color of the block type Block int @@ -90,6 +93,7 @@ var leftRightDelay float64 var moveCounter int var score int var nextPiece Piece +var gameTitle = "Blockfall" var blockGen func(int) pixel.Picture var bgImgSprite pixel.Sprite @@ -106,7 +110,7 @@ func run() { windowWidth := 765.0 windowHeight := 450.0 cfg := pixelgl.WindowConfig{ - Title: "Blockfall", + Title: gameTitle, Bounds: pixel.R(0, 0, windowWidth, windowHeight), VSync: true, } @@ -121,13 +125,13 @@ func run() { if err != nil { panic(err) } - blockGen, err = ss.LoadSpriteSheet(pwd+"/blocks.png", 2, 8) + blockGen, err = ss.LoadSpriteSheet(pwd+ResourceDir+"/blocks.png", 2, 8) if err != nil { panic(err) } // Background image, by ansimuz on opengameart.org - bgPic, err := ss.LoadPicture(pwd + "/parallax-mountain-bg.png") + bgPic, err := ss.LoadPicture(pwd + ResourceDir + "/parallax-mountain-bg.png") if err != nil { panic(err) } @@ -297,5 +301,5 @@ func piece2Block(p Piece) Block { return Green } panic("piece2Block: Invalid piece passed in") - return GraySpecial // Return strange value value + // return GraySpecial // Return strange value value } diff --git a/spritesheet/spritesheet.go b/spritesheet/spritesheet.go index a43f58b..67370a9 100644 --- a/spritesheet/spritesheet.go +++ b/spritesheet/spritesheet.go @@ -6,7 +6,7 @@ import ( "image/color" "os" - "github.com/faiface/pixel" + pixel "github.com/gopxl/pixel" ) // LoadSpriteSheet takes a path to a resource and how it should be divided and returns @@ -48,6 +48,7 @@ func LoadSpriteSheet(path string, row, col int) (func(int) pixel.Picture, error) }, nil } +// LoadPicture load image from path func LoadPicture(path string) (pixel.Picture, error) { file, err := os.Open(path) if err != nil { @@ -61,6 +62,7 @@ func LoadPicture(path string) (pixel.Picture, error) { return pixel.PictureDataFromImage(img), nil } +// GetPlayBGPic shadow board, overlayed over BG image func GetPlayBGPic() pixel.Picture { blackImg := image.NewRGBA(image.Rect(0, 0, 200, 400)) for x := 0; x < 200; x++ { @@ -73,6 +75,7 @@ func GetPlayBGPic() pixel.Picture { return blackPic } +// GetNextPieceBGPic draw next/upcoming shape widget func GetNextPieceBGPic() pixel.Picture { blackImg := image.NewRGBA(image.Rect(0, 0, 100, 100)) for x := 0; x < 100; x++ {