mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
Spped up blocks (#59)
This commit is contained in:
parent
8145561219
commit
7b47e37d34
@ -27,9 +27,44 @@ import (
|
|||||||
var (
|
var (
|
||||||
imageEmpty *ebiten.Image
|
imageEmpty *ebiten.Image
|
||||||
imageGameBG *ebiten.Image
|
imageGameBG *ebiten.Image
|
||||||
|
imageWindows *ebiten.Image
|
||||||
imageGameover *ebiten.Image
|
imageGameover *ebiten.Image
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func fieldWindowPosition() (x, y int) {
|
||||||
|
return 20, 20
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextWindowLabelPosition() (x, y int) {
|
||||||
|
x, y = fieldWindowPosition()
|
||||||
|
return x + fieldWidth + 2*blockWidth, y
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextWindowPosition() (x, y int) {
|
||||||
|
x, y = nextWindowLabelPosition()
|
||||||
|
return x, y + blockHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func textBoxWidth() int {
|
||||||
|
x, _ := nextWindowPosition()
|
||||||
|
return ScreenWidth - 2*blockWidth - x
|
||||||
|
}
|
||||||
|
|
||||||
|
func scoreTextBoxPosition() (x, y int) {
|
||||||
|
x, y = nextWindowPosition()
|
||||||
|
return x, y + 6*blockHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func levelTextBoxPosition() (x, y int) {
|
||||||
|
x, y = scoreTextBoxPosition()
|
||||||
|
return x, y + 4*blockHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func linesTextBoxPosition() (x, y int) {
|
||||||
|
x, y = levelTextBoxPosition()
|
||||||
|
return x, y + 4*blockHeight
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
imageEmpty, err = ebiten.NewImage(16, 16, ebiten.FilterNearest)
|
imageEmpty, err = ebiten.NewImage(16, 16, ebiten.FilterNearest)
|
||||||
@ -38,23 +73,60 @@ func init() {
|
|||||||
}
|
}
|
||||||
imageEmpty.Fill(color.White)
|
imageEmpty.Fill(color.White)
|
||||||
|
|
||||||
|
// Background
|
||||||
imageGameBG, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterLinear)
|
imageGameBG, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterLinear)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Windows
|
||||||
|
imageWindows, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Windows: Field
|
||||||
|
x, y := fieldWindowPosition()
|
||||||
|
if err := drawWindow(imageWindows, x, y, fieldWidth, fieldHeight); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Windows: Next
|
||||||
|
x, y = nextWindowLabelPosition()
|
||||||
|
if err := drawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
x, y = nextWindowPosition()
|
||||||
|
if err := drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Windows: Score
|
||||||
|
x, y = scoreTextBoxPosition()
|
||||||
|
if err := drawTextBox(imageWindows, "SCORE", x, y, textBoxWidth()); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Windows: Level
|
||||||
|
x, y = levelTextBoxPosition()
|
||||||
|
if err := drawTextBox(imageWindows, "LEVEL", x, y, textBoxWidth()); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Windows: Lines
|
||||||
|
x, y = linesTextBoxPosition()
|
||||||
|
if err := drawTextBox(imageWindows, "LINES", x, y, textBoxWidth()); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gameover
|
||||||
imageGameover, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
|
imageGameover, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
||||||
y := (ScreenHeight - blockHeight) / 2
|
y = (ScreenHeight - blockHeight) / 2
|
||||||
if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil {
|
if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawRect(r *ebiten.Image, x, y, width, height int) error {
|
func drawWindow(r *ebiten.Image, x, y, width, height int) error {
|
||||||
w, h := imageEmpty.Size()
|
w, h := imageEmpty.Size()
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Scale(float64(width)/float64(w), float64(height)/float64(h))
|
op.GeoM.Scale(float64(width)/float64(w), float64(height)/float64(h))
|
||||||
@ -65,14 +137,19 @@ func drawRect(r *ebiten.Image, x, y, width, height int) error {
|
|||||||
|
|
||||||
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}
|
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}
|
||||||
|
|
||||||
func drawTextBox(r *ebiten.Image, label, content string, x, y, width int) error {
|
func drawTextBox(r *ebiten.Image, label string, x, y, width int) error {
|
||||||
if err := drawTextWithShadow(r, label, x, y, 1, fontColor); err != nil {
|
if err := drawTextWithShadow(r, label, x, y, 1, fontColor); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
y += blockWidth
|
y += blockWidth
|
||||||
if err := drawRect(r, x, y, width, 2*blockHeight); err != nil {
|
if err := drawWindow(r, x, y, width, 2*blockHeight); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func drawTextBoxContent(r *ebiten.Image, content string, x, y, width int) error {
|
||||||
|
y += blockWidth
|
||||||
if err := drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2); err != nil {
|
if err := drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -271,46 +348,30 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldX, fieldY = 20, 20
|
if err := r.DrawImage(imageWindows, nil); err != nil {
|
||||||
|
|
||||||
// Draw field
|
|
||||||
if err := drawRect(r, fieldX, fieldY, fieldWidth, fieldHeight); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw next
|
|
||||||
x := fieldX + fieldWidth + blockWidth*2
|
|
||||||
y := fieldY
|
|
||||||
if err := drawTextWithShadow(r, "NEXT", x, y, 1, fontColor); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
nextX := x
|
|
||||||
nextY := y + blockHeight
|
|
||||||
if err := drawRect(r, nextX, nextY, 5*blockWidth, 5*blockHeight); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
x = nextX
|
|
||||||
y = nextY + 5*blockHeight + blockHeight
|
|
||||||
|
|
||||||
// Draw score
|
// Draw score
|
||||||
width := ScreenWidth - 2*blockWidth - x
|
x, y := scoreTextBoxPosition()
|
||||||
if err := drawTextBox(r, "SCORE", strconv.Itoa(s.score), x, y, width); err != nil {
|
if err := drawTextBoxContent(r, strconv.Itoa(s.score), x, y, textBoxWidth()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw level
|
// Draw level
|
||||||
y += 4 * blockHeight
|
x, y = levelTextBoxPosition()
|
||||||
if err := drawTextBox(r, "LEVEL", strconv.Itoa(s.level()), x, y, width); err != nil {
|
if err := drawTextBoxContent(r, strconv.Itoa(s.level()), x, y, textBoxWidth()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw lines
|
// Draw lines
|
||||||
y += 4 * blockHeight
|
x, y = linesTextBoxPosition()
|
||||||
if err := drawTextBox(r, "LINES", strconv.Itoa(s.lines), x, y, width); err != nil {
|
if err := drawTextBoxContent(r, strconv.Itoa(s.lines), x, y, textBoxWidth()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw blocks
|
// Draw blocks
|
||||||
|
fieldX, fieldY := fieldWindowPosition()
|
||||||
if err := s.field.Draw(r, fieldX, fieldY); err != nil {
|
if err := s.field.Draw(r, fieldX, fieldY); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -322,8 +383,9 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s.nextPiece != nil {
|
if s.nextPiece != nil {
|
||||||
x := nextX
|
// TODO: Make functions to get these values.
|
||||||
y := nextY
|
x := fieldX + fieldWidth + blockWidth*2
|
||||||
|
y := fieldY + blockHeight
|
||||||
if err := s.nextPiece.DrawAtCenter(r, x, y, blockWidth*5, blockHeight*5, 0); err != nil {
|
if err := s.nextPiece.DrawAtCenter(r, x, y, blockWidth*5, blockHeight*5, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user