mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 19:58:54 +01:00
c6053bcf14
Updates #1380
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
// Copyright 2016 The Ebiten Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package twenty48
|
|
|
|
import (
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
}
|
|
|
|
const (
|
|
ScreenWidth = 420
|
|
ScreenHeight = 600
|
|
boardSize = 4
|
|
)
|
|
|
|
// Game represents a game state.
|
|
type Game struct {
|
|
input *Input
|
|
board *Board
|
|
boardImage *ebiten.Image
|
|
}
|
|
|
|
// NewGame generates a new Game object.
|
|
func NewGame() (*Game, error) {
|
|
g := &Game{
|
|
input: NewInput(),
|
|
}
|
|
var err error
|
|
g.board, err = NewBoard(boardSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// Layout implements ebiten.Game's Layout.
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
|
return ScreenWidth, ScreenHeight
|
|
}
|
|
|
|
// Update updates the current game state.
|
|
func (g *Game) Update() error {
|
|
g.input.Update()
|
|
if err := g.board.Update(g.input); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Draw draws the current game to the given screen.
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
if g.boardImage == nil {
|
|
w, h := g.board.Size()
|
|
g.boardImage = ebiten.NewImage(w, h)
|
|
}
|
|
screen.Fill(backgroundColor)
|
|
g.board.Draw(g.boardImage)
|
|
op := &ebiten.DrawImageOptions{}
|
|
sw, sh := screen.Size()
|
|
bw, bh := g.boardImage.Size()
|
|
x := (sw - bw) / 2
|
|
y := (sh - bh) / 2
|
|
op.GeoM.Translate(float64(x), float64(y))
|
|
screen.DrawImage(g.boardImage, op)
|
|
}
|