mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
Add examples/2048 (#181)
This commit is contained in:
parent
8855df40e4
commit
8daa246da4
93
examples/2048/2048/board.go
Normal file
93
examples/2048/2048/board.go
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
// 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 (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Board struct {
|
||||||
|
size int
|
||||||
|
tiles map[*Tile]struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBoard(size int) *Board {
|
||||||
|
b := &Board{
|
||||||
|
size: size,
|
||||||
|
tiles: map[*Tile]struct{}{},
|
||||||
|
}
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
b.addRandomTile()
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Board) addRandomTile() bool {
|
||||||
|
cells := make([]bool, b.size*b.size)
|
||||||
|
for t := range b.tiles {
|
||||||
|
i := t.x + t.y*b.size
|
||||||
|
cells[i] = true
|
||||||
|
}
|
||||||
|
availableCells := []int{}
|
||||||
|
for i, b := range cells {
|
||||||
|
if b {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
availableCells = append(availableCells, i)
|
||||||
|
}
|
||||||
|
if len(availableCells) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
c := availableCells[rand.Intn(len(availableCells))]
|
||||||
|
v := 2
|
||||||
|
if rand.Intn(10) == 0 {
|
||||||
|
v = 4
|
||||||
|
}
|
||||||
|
x := c % b.size
|
||||||
|
y := c / b.size
|
||||||
|
t := NewTile(v, x, y)
|
||||||
|
b.tiles[t] = struct{}{}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Board) Move(dir Dir) {
|
||||||
|
b.addRandomTile()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Board) Draw(screen *ebiten.Image) error {
|
||||||
|
posToTile := map[int]*Tile{}
|
||||||
|
for t := range b.tiles {
|
||||||
|
i := t.x + t.y*b.size
|
||||||
|
posToTile[i] = t
|
||||||
|
}
|
||||||
|
str := ""
|
||||||
|
for j := 0; j < b.size; j++ {
|
||||||
|
for i := 0; i < b.size; i++ {
|
||||||
|
t := posToTile[i+j*b.size]
|
||||||
|
if t != nil {
|
||||||
|
str += fmt.Sprintf("[%4d]", t.value)
|
||||||
|
} else {
|
||||||
|
str += "[ ]"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
str += "\n"
|
||||||
|
}
|
||||||
|
ebitenutil.DebugPrint(screen, str)
|
||||||
|
return nil
|
||||||
|
}
|
61
examples/2048/2048/game.go
Normal file
61
examples/2048/2048/game.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
ScreenWidth = 420
|
||||||
|
ScreenHeight = 600
|
||||||
|
boardSize = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
input *Input
|
||||||
|
board *Board
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGame() *Game {
|
||||||
|
return &Game{
|
||||||
|
input: NewInput(),
|
||||||
|
board: NewBoard(boardSize),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Update() error {
|
||||||
|
if err := g.input.Update(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dir, ok := g.input.Dir(); ok {
|
||||||
|
g.board.Move(dir)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Draw(screen *ebiten.Image) error {
|
||||||
|
if err := g.board.Draw(screen); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
81
examples/2048/2048/input.go
Normal file
81
examples/2048/2048/input.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
// 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 (
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Dir int
|
||||||
|
|
||||||
|
const (
|
||||||
|
DirUp Dir = iota
|
||||||
|
DirRight
|
||||||
|
DirDown
|
||||||
|
DirLeft
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d Dir) Vector() (x, y int) {
|
||||||
|
switch d {
|
||||||
|
case DirUp:
|
||||||
|
return 0, -1
|
||||||
|
case DirRight:
|
||||||
|
return 1, 0
|
||||||
|
case DirDown:
|
||||||
|
return 0, 1
|
||||||
|
case DirLeft:
|
||||||
|
return -1, 0
|
||||||
|
}
|
||||||
|
panic("not reach")
|
||||||
|
}
|
||||||
|
|
||||||
|
type Input struct {
|
||||||
|
keyState map[ebiten.Key]int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInput() *Input {
|
||||||
|
return &Input{
|
||||||
|
keyState: map[ebiten.Key]int{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
dirKeys = map[ebiten.Key]Dir{
|
||||||
|
ebiten.KeyUp: DirUp,
|
||||||
|
ebiten.KeyRight: DirRight,
|
||||||
|
ebiten.KeyDown: DirDown,
|
||||||
|
ebiten.KeyLeft: DirLeft,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i *Input) Update() error {
|
||||||
|
for k := range dirKeys {
|
||||||
|
if ebiten.IsKeyPressed(k) {
|
||||||
|
i.keyState[k]++
|
||||||
|
} else {
|
||||||
|
i.keyState[k] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) Dir() (Dir, bool) {
|
||||||
|
for k, d := range dirKeys {
|
||||||
|
if i.keyState[k] == 1 {
|
||||||
|
return d, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, false
|
||||||
|
}
|
29
examples/2048/2048/tile.go
Normal file
29
examples/2048/2048/tile.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
type Tile struct {
|
||||||
|
value int
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTile(value int, x, y int) *Tile {
|
||||||
|
return &Tile{
|
||||||
|
value: value,
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
}
|
||||||
|
}
|
45
examples/2048/main.go
Normal file
45
examples/2048/main.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// 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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/examples/2048/2048"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
game = twenty48.NewGame()
|
||||||
|
)
|
||||||
|
|
||||||
|
func update(screen *ebiten.Image) error {
|
||||||
|
if err := game.Update(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if ebiten.IsRunningSlowly() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := game.Draw(screen); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := ebiten.Run(update, twenty48.ScreenWidth, twenty48.ScreenHeight, 1, "2048 (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user