mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
examples: change the name convention: Num -> Count
This commit is contained in:
parent
16ff5c5039
commit
36b7b85477
@ -35,7 +35,7 @@ const (
|
|||||||
frameOY = 32
|
frameOY = 32
|
||||||
frameWidth = 32
|
frameWidth = 32
|
||||||
frameHeight = 32
|
frameHeight = 32
|
||||||
frameNum = 8
|
frameCount = 8
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -55,7 +55,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
|
op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
|
||||||
op.GeoM.Translate(screenWidth/2, screenHeight/2)
|
op.GeoM.Translate(screenWidth/2, screenHeight/2)
|
||||||
i := (g.count / 5) % frameNum
|
i := (g.count / 5) % frameCount
|
||||||
sx, sy := frameOX+i*frameWidth, frameOY
|
sx, sy := frameOX+i*frameWidth, frameOY
|
||||||
screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
|
screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ const maxFlushCount = 20
|
|||||||
|
|
||||||
// Field represents a game field with block states.
|
// Field represents a game field with block states.
|
||||||
type Field struct {
|
type Field struct {
|
||||||
blocks [fieldBlockNumX][fieldBlockNumY]BlockType
|
blocks [fieldBlockCountX][fieldBlockCountY]BlockType
|
||||||
flushCount int
|
flushCount int
|
||||||
onEndFlushAnimating func(int)
|
onEndFlushAnimating func(int)
|
||||||
}
|
}
|
||||||
@ -30,13 +30,13 @@ type Field struct {
|
|||||||
// IsBlocked returns a boolean value indicating whether
|
// IsBlocked returns a boolean value indicating whether
|
||||||
// there is a block at position (x, y) on the field.
|
// there is a block at position (x, y) on the field.
|
||||||
func (f *Field) IsBlocked(x, y int) bool {
|
func (f *Field) IsBlocked(x, y int) bool {
|
||||||
if x < 0 || fieldBlockNumX <= x {
|
if x < 0 || fieldBlockCountX <= x {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if y < 0 {
|
if y < 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if fieldBlockNumY <= y {
|
if fieldBlockCountY <= y {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return f.blocks[x][y] != BlockTypeNone
|
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
|
// flushable returns a boolean value indicating whether
|
||||||
// there is a flushable line in the field.
|
// there is a flushable line in the field.
|
||||||
func (f *Field) flushable() bool {
|
func (f *Field) flushable() bool {
|
||||||
for j := fieldBlockNumY - 1; 0 <= j; j-- {
|
for j := fieldBlockCountY - 1; 0 <= j; j-- {
|
||||||
if f.flushableLine(j) {
|
if f.flushableLine(j) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -127,7 +127,7 @@ func (f *Field) flushable() bool {
|
|||||||
// flushableLine returns a boolean value indicating whether
|
// flushableLine returns a boolean value indicating whether
|
||||||
// the line j is flushabled or not.
|
// the line j is flushabled or not.
|
||||||
func (f *Field) flushableLine(j int) bool {
|
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 {
|
if f.blocks[i][j] == BlockTypeNone {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -140,13 +140,13 @@ func (f *Field) setBlock(x, y int, blockType BlockType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Field) endFlushAnimating() int {
|
func (f *Field) endFlushAnimating() int {
|
||||||
flushedLineNum := 0
|
flushedLineCount := 0
|
||||||
for j := fieldBlockNumY - 1; 0 <= j; j-- {
|
for j := fieldBlockCountY - 1; 0 <= j; j-- {
|
||||||
if f.flushLine(j + flushedLineNum) {
|
if f.flushLine(j + flushedLineCount) {
|
||||||
flushedLineNum++
|
flushedLineCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return flushedLineNum
|
return flushedLineCount
|
||||||
}
|
}
|
||||||
|
|
||||||
// flushLine flushes the line j if possible, and if the line is flushed,
|
// 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
|
// flushLine returns a boolean value indicating whether
|
||||||
// the line is actually flushed.
|
// the line is actually flushed.
|
||||||
func (f *Field) flushLine(j int) bool {
|
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 {
|
if f.blocks[i][j] == BlockTypeNone {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for j2 := j; 1 <= j2; j2-- {
|
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]
|
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
|
f.blocks[i][0] = BlockTypeNone
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -204,13 +204,13 @@ func flushingColor(rate float64) ebiten.ColorM {
|
|||||||
|
|
||||||
func (f *Field) Draw(r *ebiten.Image, x, y int) {
|
func (f *Field) Draw(r *ebiten.Image, x, y int) {
|
||||||
fc := flushingColor(float64(f.flushCount) / maxFlushCount)
|
fc := flushingColor(float64(f.flushCount) / maxFlushCount)
|
||||||
for j := 0; j < fieldBlockNumY; j++ {
|
for j := 0; j < fieldBlockCountY; j++ {
|
||||||
if f.flushableLine(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)
|
drawBlock(r, f.blocks[i][j], i*blockWidth+x, j*blockHeight+y, fc)
|
||||||
}
|
}
|
||||||
} else {
|
} 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{})
|
drawBlock(r, f.blocks[i][j], i*blockWidth+x, j*blockHeight+y, ebiten.ColorM{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,8 +187,8 @@ func (s *GameScene) drawBackground(r *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
fieldWidth = blockWidth * fieldBlockNumX
|
fieldWidth = blockWidth * fieldBlockCountX
|
||||||
fieldHeight = blockHeight * fieldBlockNumY
|
fieldHeight = blockHeight * fieldBlockCountY
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *GameScene) choosePiece() *Piece {
|
func (s *GameScene) choosePiece() *Piece {
|
||||||
|
@ -159,10 +159,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
blockWidth = 10
|
blockWidth = 10
|
||||||
blockHeight = 10
|
blockHeight = 10
|
||||||
fieldBlockNumX = 10
|
fieldBlockCountX = 10
|
||||||
fieldBlockNumY = 20
|
fieldBlockCountY = 20
|
||||||
)
|
)
|
||||||
|
|
||||||
func drawBlock(r *ebiten.Image, block BlockType, x, y int, clr ebiten.ColorM) {
|
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) {
|
func (p *Piece) InitialPosition() (int, int) {
|
||||||
size := len(p.blocks)
|
size := len(p.blocks)
|
||||||
x := (fieldBlockNumX - size) / 2
|
x := (fieldBlockCountX - size) / 2
|
||||||
y := 0
|
y := 0
|
||||||
Loop:
|
Loop:
|
||||||
for j := 0; j < size; j++ {
|
for j := 0; j < size; j++ {
|
||||||
|
@ -38,8 +38,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tileSize = 16
|
tileSize = 16
|
||||||
tileXNum = 25
|
tileXCount = 25
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -175,8 +175,8 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Translate(float64((i%worldSizeX)*tileSize), float64((i/worldSizeX)*tileSize))
|
op.GeoM.Translate(float64((i%worldSizeX)*tileSize), float64((i/worldSizeX)*tileSize))
|
||||||
|
|
||||||
sx := (t % tileXNum) * tileSize
|
sx := (t % tileXCount) * tileSize
|
||||||
sy := (t / tileXNum) * tileSize
|
sy := (t / tileXCount) * tileSize
|
||||||
g.world.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
|
g.world.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,11 +30,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
screenWidth = 640
|
screenWidth = 640
|
||||||
screenHeight = 480
|
screenHeight = 480
|
||||||
gridSize = 10
|
gridSize = 10
|
||||||
xNumInScreen = screenWidth / gridSize
|
xGridCountInScreen = screenWidth / gridSize
|
||||||
yNumInScreen = screenHeight / gridSize
|
yGridCountInScreen = screenHeight / gridSize
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -83,8 +83,8 @@ func (g *Game) collidesWithSelf() bool {
|
|||||||
func (g *Game) collidesWithWall() bool {
|
func (g *Game) collidesWithWall() bool {
|
||||||
return g.snakeBody[0].X < 0 ||
|
return g.snakeBody[0].X < 0 ||
|
||||||
g.snakeBody[0].Y < 0 ||
|
g.snakeBody[0].Y < 0 ||
|
||||||
g.snakeBody[0].X >= xNumInScreen ||
|
g.snakeBody[0].X >= xGridCountInScreen ||
|
||||||
g.snakeBody[0].Y >= yNumInScreen
|
g.snakeBody[0].Y >= yGridCountInScreen
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) needsToMoveSnake() bool {
|
func (g *Game) needsToMoveSnake() bool {
|
||||||
@ -96,8 +96,8 @@ func (g *Game) reset() {
|
|||||||
g.apple.Y = 3 * gridSize
|
g.apple.Y = 3 * gridSize
|
||||||
g.moveTime = 4
|
g.moveTime = 4
|
||||||
g.snakeBody = g.snakeBody[:1]
|
g.snakeBody = g.snakeBody[:1]
|
||||||
g.snakeBody[0].X = xNumInScreen / 2
|
g.snakeBody[0].X = xGridCountInScreen / 2
|
||||||
g.snakeBody[0].Y = yNumInScreen / 2
|
g.snakeBody[0].Y = yGridCountInScreen / 2
|
||||||
g.score = 0
|
g.score = 0
|
||||||
g.level = 1
|
g.level = 1
|
||||||
g.moveDirection = dirNone
|
g.moveDirection = dirNone
|
||||||
@ -130,8 +130,8 @@ func (g *Game) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if g.collidesWithApple() {
|
if g.collidesWithApple() {
|
||||||
g.apple.X = rand.Intn(xNumInScreen - 1)
|
g.apple.X = rand.Intn(xGridCountInScreen - 1)
|
||||||
g.apple.Y = rand.Intn(yNumInScreen - 1)
|
g.apple.Y = rand.Intn(yGridCountInScreen - 1)
|
||||||
g.snakeBody = append(g.snakeBody, Position{
|
g.snakeBody = append(g.snakeBody, Position{
|
||||||
X: g.snakeBody[len(g.snakeBody)-1].X,
|
X: g.snakeBody[len(g.snakeBody)-1].X,
|
||||||
Y: g.snakeBody[len(g.snakeBody)-1].Y,
|
Y: g.snakeBody[len(g.snakeBody)-1].Y,
|
||||||
@ -195,8 +195,8 @@ func newGame() *Game {
|
|||||||
moveTime: 4,
|
moveTime: 4,
|
||||||
snakeBody: make([]Position, 1),
|
snakeBody: make([]Position, 1),
|
||||||
}
|
}
|
||||||
g.snakeBody[0].X = xNumInScreen / 2
|
g.snakeBody[0].X = xGridCountInScreen / 2
|
||||||
g.snakeBody[0].Y = yNumInScreen / 2
|
g.snakeBody[0].Y = yGridCountInScreen / 2
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ const (
|
|||||||
screenWidth = 640
|
screenWidth = 640
|
||||||
screenHeight = 480
|
screenHeight = 480
|
||||||
scale = 64
|
scale = 64
|
||||||
starsNum = 1024
|
starsCount = 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
type Star struct {
|
type Star struct {
|
||||||
@ -69,12 +69,12 @@ func (s *Star) Draw(screen *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
stars [starsNum]Star
|
stars [starsCount]Star
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
g := &Game{}
|
g := &Game{}
|
||||||
for i := 0; i < starsNum; i++ {
|
for i := 0; i < starsCount; i++ {
|
||||||
g.stars[i].Init()
|
g.stars[i].Init()
|
||||||
}
|
}
|
||||||
return g
|
return g
|
||||||
@ -82,14 +82,14 @@ func NewGame() *Game {
|
|||||||
|
|
||||||
func (g *Game) Update() error {
|
func (g *Game) Update() error {
|
||||||
x, y := ebiten.CursorPosition()
|
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))
|
g.stars[i].Update(float64(x*scale), float64(y*scale))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Draw(screen *ebiten.Image) {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
for i := 0; i < starsNum; i++ {
|
for i := 0; i < starsCount; i++ {
|
||||||
g.stars[i].Draw(screen)
|
g.stars[i].Draw(screen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,20 +64,20 @@ func (g *Game) Update() error {
|
|||||||
|
|
||||||
func (g *Game) Draw(screen *ebiten.Image) {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
w, _ := tilesImage.Size()
|
w, _ := tilesImage.Size()
|
||||||
tileXNum := w / tileSize
|
tileXCount := w / tileSize
|
||||||
|
|
||||||
// Draw each tile with each DrawImage call.
|
// Draw each tile with each DrawImage call.
|
||||||
// As the source images of all DrawImage calls are always same,
|
// As the source images of all DrawImage calls are always same,
|
||||||
// this rendering is done very efficiently.
|
// this rendering is done very efficiently.
|
||||||
// For more detail, see https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#Image.DrawImage
|
// 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 _, l := range g.layers {
|
||||||
for i, t := range l {
|
for i, t := range l {
|
||||||
op := &ebiten.DrawImageOptions{}
|
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
|
sx := (t % tileXCount) * tileSize
|
||||||
sy := (t / tileXNum) * tileSize
|
sy := (t / tileXCount) * tileSize
|
||||||
screen.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
|
screen.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user