mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-24 01:42:05 +01:00
blocks: Show background image; Go back to title after gameover
This commit is contained in:
parent
083dafa627
commit
bb1d168154
@ -18,7 +18,7 @@ import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
const maxFlushCount = 30
|
||||
const maxFlushCount = 20
|
||||
|
||||
type Field struct {
|
||||
blocks [fieldBlockNumX][fieldBlockNumY]BlockType
|
||||
|
@ -16,14 +16,19 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"image/color"
|
||||
_ "image/jpeg"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var imageEmpty *ebiten.Image
|
||||
var imageGameover *ebiten.Image
|
||||
var (
|
||||
imageEmpty *ebiten.Image
|
||||
imageGameBG *ebiten.Image
|
||||
imageGameover *ebiten.Image
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
@ -33,6 +38,11 @@ func init() {
|
||||
}
|
||||
imageEmpty.Fill(color.White)
|
||||
|
||||
imageGameBG, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterLinear)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
imageGameover, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -50,7 +60,7 @@ func drawRect(r *ebiten.Image, x, y, width, height int) error {
|
||||
geo.Concat(ebiten.TranslateGeo(float64(x), float64(y)))
|
||||
return r.DrawImage(imageEmpty, &ebiten.DrawImageOptions{
|
||||
GeoM: geo,
|
||||
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
|
||||
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.75),
|
||||
})
|
||||
}
|
||||
|
||||
@ -91,6 +101,34 @@ func NewGameScene() *GameScene {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *GameScene) drawBackground(r *ebiten.Image) error {
|
||||
if err := r.Fill(color.White); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w, h := imageGameBG.Size()
|
||||
geo := ebiten.TranslateGeo(-float64(w)/2, -float64(h)/2)
|
||||
scaleW := ScreenWidth / float64(w)
|
||||
scaleH := ScreenHeight / float64(h)
|
||||
scale := scaleW
|
||||
if scale < scaleH {
|
||||
scale = scaleH
|
||||
}
|
||||
geo.Concat(ebiten.ScaleGeo(scale, scale))
|
||||
geo.Concat(ebiten.TranslateGeo(ScreenWidth/2, ScreenHeight/2))
|
||||
|
||||
a := 0.7
|
||||
m := ebiten.Monochrome()
|
||||
m.Concat(ebiten.ScaleColor(a, a, a, a))
|
||||
clr := ebiten.ScaleColor(1-a, 1-a, 1-a, 1-a)
|
||||
clr.Add(m)
|
||||
clr.Concat(ebiten.TranslateColor(0.3, 0.3, 0.3, 0))
|
||||
return r.DrawImage(imageGameBG, &ebiten.DrawImageOptions{
|
||||
GeoM: geo,
|
||||
ColorM: clr,
|
||||
})
|
||||
}
|
||||
|
||||
const fieldWidth = blockWidth * fieldBlockNumX
|
||||
const fieldHeight = blockHeight * fieldBlockNumY
|
||||
|
||||
@ -134,7 +172,9 @@ func (s *GameScene) Update(state *GameState) error {
|
||||
s.field.Update()
|
||||
|
||||
if s.gameover {
|
||||
// TODO: Go back to the title by pressing something
|
||||
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
|
||||
state.SceneManager.GoTo(NewTitleScene())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -227,7 +267,7 @@ func (s *GameScene) goNextPiece() {
|
||||
}
|
||||
|
||||
func (s *GameScene) Draw(r *ebiten.Image) error {
|
||||
if err := r.Fill(color.White); err != nil {
|
||||
if err := s.drawBackground(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user