examples: change the name convention: Num -> Count

This commit is contained in:
Hajime Hoshi 2022-07-13 01:55:03 +09:00
parent 16ff5c5039
commit 36b7b85477
8 changed files with 52 additions and 52 deletions

View File

@ -35,7 +35,7 @@ const (
frameOY = 32
frameWidth = 32
frameHeight = 32
frameNum = 8
frameCount = 8
)
var (
@ -55,7 +55,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
op.GeoM.Translate(screenWidth/2, screenHeight/2)
i := (g.count / 5) % frameNum
i := (g.count / 5) % frameCount
sx, sy := frameOX+i*frameWidth, frameOY
screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
}

View File

@ -22,7 +22,7 @@ const maxFlushCount = 20
// Field represents a game field with block states.
type Field struct {
blocks [fieldBlockNumX][fieldBlockNumY]BlockType
blocks [fieldBlockCountX][fieldBlockCountY]BlockType
flushCount int
onEndFlushAnimating func(int)
}
@ -30,13 +30,13 @@ type Field struct {
// IsBlocked returns a boolean value indicating whether
// there is a block at position (x, y) on the field.
func (f *Field) IsBlocked(x, y int) bool {
if x < 0 || fieldBlockNumX <= x {
if x < 0 || fieldBlockCountX <= x {
return true
}
if y < 0 {
return false
}
if fieldBlockNumY <= y {
if fieldBlockCountY <= y {
return true
}
return f.blocks[x][y] != BlockTypeNone
@ -116,7 +116,7 @@ func (f *Field) SetEndFlushAnimating(fn func(lines int)) {
// flushable returns a boolean value indicating whether
// there is a flushable line in the field.
func (f *Field) flushable() bool {
for j := fieldBlockNumY - 1; 0 <= j; j-- {
for j := fieldBlockCountY - 1; 0 <= j; j-- {
if f.flushableLine(j) {
return true
}
@ -127,7 +127,7 @@ func (f *Field) flushable() bool {
// flushableLine returns a boolean value indicating whether
// the line j is flushabled or not.
func (f *Field) flushableLine(j int) bool {
for i := 0; i < fieldBlockNumX; i++ {
for i := 0; i < fieldBlockCountX; i++ {
if f.blocks[i][j] == BlockTypeNone {
return false
}
@ -140,13 +140,13 @@ func (f *Field) setBlock(x, y int, blockType BlockType) {
}
func (f *Field) endFlushAnimating() int {
flushedLineNum := 0
for j := fieldBlockNumY - 1; 0 <= j; j-- {
if f.flushLine(j + flushedLineNum) {
flushedLineNum++
flushedLineCount := 0
for j := fieldBlockCountY - 1; 0 <= j; j-- {
if f.flushLine(j + flushedLineCount) {
flushedLineCount++
}
}
return flushedLineNum
return flushedLineCount
}
// flushLine flushes the line j if possible, and if the line is flushed,
@ -155,17 +155,17 @@ func (f *Field) endFlushAnimating() int {
// flushLine returns a boolean value indicating whether
// the line is actually flushed.
func (f *Field) flushLine(j int) bool {
for i := 0; i < fieldBlockNumX; i++ {
for i := 0; i < fieldBlockCountX; i++ {
if f.blocks[i][j] == BlockTypeNone {
return false
}
}
for j2 := j; 1 <= j2; j2-- {
for i := 0; i < fieldBlockNumX; i++ {
for i := 0; i < fieldBlockCountX; i++ {
f.blocks[i][j2] = f.blocks[i][j2-1]
}
}
for i := 0; i < fieldBlockNumX; i++ {
for i := 0; i < fieldBlockCountX; i++ {
f.blocks[i][0] = BlockTypeNone
}
return true
@ -204,13 +204,13 @@ func flushingColor(rate float64) ebiten.ColorM {
func (f *Field) Draw(r *ebiten.Image, x, y int) {
fc := flushingColor(float64(f.flushCount) / maxFlushCount)
for j := 0; j < fieldBlockNumY; j++ {
for j := 0; j < fieldBlockCountY; j++ {
if f.flushableLine(j) {
for i := 0; i < fieldBlockNumX; i++ {
for i := 0; i < fieldBlockCountX; i++ {
drawBlock(r, f.blocks[i][j], i*blockWidth+x, j*blockHeight+y, fc)
}
} else {
for i := 0; i < fieldBlockNumX; i++ {
for i := 0; i < fieldBlockCountX; i++ {
drawBlock(r, f.blocks[i][j], i*blockWidth+x, j*blockHeight+y, ebiten.ColorM{})
}
}

View File

@ -187,8 +187,8 @@ func (s *GameScene) drawBackground(r *ebiten.Image) {
}
const (
fieldWidth = blockWidth * fieldBlockNumX
fieldHeight = blockHeight * fieldBlockNumY
fieldWidth = blockWidth * fieldBlockCountX
fieldHeight = blockHeight * fieldBlockCountY
)
func (s *GameScene) choosePiece() *Piece {

View File

@ -161,8 +161,8 @@ func init() {
const (
blockWidth = 10
blockHeight = 10
fieldBlockNumX = 10
fieldBlockNumY = 20
fieldBlockCountX = 10
fieldBlockCountY = 20
)
func drawBlock(r *ebiten.Image, block BlockType, x, y int, clr ebiten.ColorM) {
@ -180,7 +180,7 @@ func drawBlock(r *ebiten.Image, block BlockType, x, y int, clr ebiten.ColorM) {
func (p *Piece) InitialPosition() (int, int) {
size := len(p.blocks)
x := (fieldBlockNumX - size) / 2
x := (fieldBlockCountX - size) / 2
y := 0
Loop:
for j := 0; j < size; j++ {

View File

@ -39,7 +39,7 @@ const (
const (
tileSize = 16
tileXNum = 25
tileXCount = 25
)
const (
@ -175,8 +175,8 @@ func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64((i%worldSizeX)*tileSize), float64((i/worldSizeX)*tileSize))
sx := (t % tileXNum) * tileSize
sy := (t / tileXNum) * tileSize
sx := (t % tileXCount) * tileSize
sy := (t / tileXCount) * tileSize
g.world.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
}
}

View File

@ -33,8 +33,8 @@ const (
screenWidth = 640
screenHeight = 480
gridSize = 10
xNumInScreen = screenWidth / gridSize
yNumInScreen = screenHeight / gridSize
xGridCountInScreen = screenWidth / gridSize
yGridCountInScreen = screenHeight / gridSize
)
const (
@ -83,8 +83,8 @@ func (g *Game) collidesWithSelf() bool {
func (g *Game) collidesWithWall() bool {
return g.snakeBody[0].X < 0 ||
g.snakeBody[0].Y < 0 ||
g.snakeBody[0].X >= xNumInScreen ||
g.snakeBody[0].Y >= yNumInScreen
g.snakeBody[0].X >= xGridCountInScreen ||
g.snakeBody[0].Y >= yGridCountInScreen
}
func (g *Game) needsToMoveSnake() bool {
@ -96,8 +96,8 @@ func (g *Game) reset() {
g.apple.Y = 3 * gridSize
g.moveTime = 4
g.snakeBody = g.snakeBody[:1]
g.snakeBody[0].X = xNumInScreen / 2
g.snakeBody[0].Y = yNumInScreen / 2
g.snakeBody[0].X = xGridCountInScreen / 2
g.snakeBody[0].Y = yGridCountInScreen / 2
g.score = 0
g.level = 1
g.moveDirection = dirNone
@ -130,8 +130,8 @@ func (g *Game) Update() error {
}
if g.collidesWithApple() {
g.apple.X = rand.Intn(xNumInScreen - 1)
g.apple.Y = rand.Intn(yNumInScreen - 1)
g.apple.X = rand.Intn(xGridCountInScreen - 1)
g.apple.Y = rand.Intn(yGridCountInScreen - 1)
g.snakeBody = append(g.snakeBody, Position{
X: g.snakeBody[len(g.snakeBody)-1].X,
Y: g.snakeBody[len(g.snakeBody)-1].Y,
@ -195,8 +195,8 @@ func newGame() *Game {
moveTime: 4,
snakeBody: make([]Position, 1),
}
g.snakeBody[0].X = xNumInScreen / 2
g.snakeBody[0].Y = yNumInScreen / 2
g.snakeBody[0].X = xGridCountInScreen / 2
g.snakeBody[0].Y = yGridCountInScreen / 2
return g
}

View File

@ -31,7 +31,7 @@ const (
screenWidth = 640
screenHeight = 480
scale = 64
starsNum = 1024
starsCount = 1024
)
type Star struct {
@ -69,12 +69,12 @@ func (s *Star) Draw(screen *ebiten.Image) {
}
type Game struct {
stars [starsNum]Star
stars [starsCount]Star
}
func NewGame() *Game {
g := &Game{}
for i := 0; i < starsNum; i++ {
for i := 0; i < starsCount; i++ {
g.stars[i].Init()
}
return g
@ -82,14 +82,14 @@ func NewGame() *Game {
func (g *Game) Update() error {
x, y := ebiten.CursorPosition()
for i := 0; i < starsNum; i++ {
for i := 0; i < starsCount; i++ {
g.stars[i].Update(float64(x*scale), float64(y*scale))
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
for i := 0; i < starsNum; i++ {
for i := 0; i < starsCount; i++ {
g.stars[i].Draw(screen)
}
}

View File

@ -64,20 +64,20 @@ func (g *Game) Update() error {
func (g *Game) Draw(screen *ebiten.Image) {
w, _ := tilesImage.Size()
tileXNum := w / tileSize
tileXCount := w / tileSize
// Draw each tile with each DrawImage call.
// As the source images of all DrawImage calls are always same,
// this rendering is done very efficiently.
// For more detail, see https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#Image.DrawImage
const xNum = screenWidth / tileSize
const xCount = screenWidth / tileSize
for _, l := range g.layers {
for i, t := range l {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64((i%xNum)*tileSize), float64((i/xNum)*tileSize))
op.GeoM.Translate(float64((i%xCount)*tileSize), float64((i/xCount)*tileSize))
sx := (t % tileXNum) * tileSize
sy := (t / tileXNum) * tileSize
sx := (t % tileXCount) * tileSize
sy := (t / tileXCount) * tileSize
screen.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
}
}