2018-04-07 14:05:01 +02:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2021-06-24 14:49:37 +02:00
|
|
|
//go:build example
|
2020-10-06 17:45:54 +02:00
|
|
|
// +build example
|
2018-04-07 14:05:01 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
_ "image/png"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
2020-10-03 08:11:51 +02:00
|
|
|
"golang.org/x/image/font/opentype"
|
2018-04-07 14:05:01 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/wav"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
|
|
|
resources "github.com/hajimehoshi/ebiten/v2/examples/resources/images/flappy"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
2018-04-07 14:05:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
func floorDiv(x, y int) int {
|
|
|
|
d := x / y
|
|
|
|
if d*y == x || x >= 0 {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
return d - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func floorMod(x, y int) int {
|
|
|
|
return x - floorDiv(x, y)*y
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
tileSize = 32
|
2021-05-23 12:44:03 +02:00
|
|
|
titleFontSize = fontSize * 1.5
|
|
|
|
fontSize = 24
|
2018-05-27 19:38:55 +02:00
|
|
|
smallFontSize = fontSize / 2
|
2018-04-07 14:05:01 +02:00
|
|
|
pipeWidth = tileSize * 2
|
|
|
|
pipeStartOffsetX = 8
|
|
|
|
pipeIntervalX = 8
|
|
|
|
pipeGapY = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-05-27 19:38:55 +02:00
|
|
|
gopherImage *ebiten.Image
|
|
|
|
tilesImage *ebiten.Image
|
2021-05-23 12:44:03 +02:00
|
|
|
titleArcadeFont font.Face
|
2018-05-27 19:38:55 +02:00
|
|
|
arcadeFont font.Face
|
|
|
|
smallArcadeFont font.Face
|
2018-04-07 14:05:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
img, _, err := image.Decode(bytes.NewReader(resources.Gopher_png))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
gopherImage = ebiten.NewImageFromImage(img)
|
2018-04-07 14:05:01 +02:00
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(resources.Tiles_png))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
tilesImage = ebiten.NewImageFromImage(img)
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-10-03 08:11:51 +02:00
|
|
|
tt, err := opentype.Parse(fonts.PressStart2P_ttf)
|
2018-04-07 14:05:01 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
const dpi = 72
|
2021-05-23 12:44:03 +02:00
|
|
|
titleArcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
|
|
|
Size: titleFontSize,
|
|
|
|
DPI: dpi,
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-03 08:11:51 +02:00
|
|
|
arcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
2018-04-07 14:05:01 +02:00
|
|
|
Size: fontSize,
|
|
|
|
DPI: dpi,
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
})
|
2020-10-03 08:11:51 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
smallArcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
2018-05-27 19:38:55 +02:00
|
|
|
Size: smallFontSize,
|
|
|
|
DPI: dpi,
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
})
|
2020-10-03 08:11:51 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ModeTitle Mode = iota
|
|
|
|
ModeGame
|
|
|
|
ModeGameOver
|
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
mode Mode
|
|
|
|
|
|
|
|
// The gopher's position
|
|
|
|
x16 int
|
|
|
|
y16 int
|
|
|
|
vy16 int
|
|
|
|
|
|
|
|
// Camera
|
|
|
|
cameraX int
|
|
|
|
cameraY int
|
|
|
|
|
|
|
|
// Pipes
|
|
|
|
pipeTileYs []int
|
|
|
|
|
|
|
|
gameoverCount int
|
2021-07-09 19:03:48 +02:00
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
touchIDs []ebiten.TouchID
|
2021-07-09 19:03:48 +02:00
|
|
|
gamepadIDs []ebiten.GamepadID
|
2021-07-21 17:15:30 +02:00
|
|
|
|
|
|
|
audioContext *audio.Context
|
|
|
|
jumpPlayer *audio.Player
|
|
|
|
hitPlayer *audio.Player
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGame() *Game {
|
|
|
|
g := &Game{}
|
|
|
|
g.init()
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) init() {
|
|
|
|
g.x16 = 0
|
|
|
|
g.y16 = 100 * 16
|
|
|
|
g.cameraX = -240
|
|
|
|
g.cameraY = 0
|
|
|
|
g.pipeTileYs = make([]int, 256)
|
|
|
|
for i := range g.pipeTileYs {
|
|
|
|
g.pipeTileYs[i] = rand.Intn(6) + 2
|
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
|
2021-08-04 07:54:04 +02:00
|
|
|
if g.audioContext == nil {
|
|
|
|
g.audioContext = audio.NewContext(48000)
|
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
|
2022-06-30 18:37:07 +02:00
|
|
|
jumpD, err := vorbis.DecodeWithoutResampling(bytes.NewReader(raudio.Jump_ogg))
|
2021-07-21 17:15:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-07-22 09:39:02 +02:00
|
|
|
g.jumpPlayer, err = g.audioContext.NewPlayer(jumpD)
|
2021-07-21 17:15:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-06-30 18:37:07 +02:00
|
|
|
jabD, err := wav.DecodeWithoutResampling(bytes.NewReader(raudio.Jab_wav))
|
2021-07-21 17:15:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-07-22 09:39:02 +02:00
|
|
|
g.hitPlayer, err = g.audioContext.NewPlayer(jabD)
|
2021-07-21 17:15:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 12:49:43 +02:00
|
|
|
func (g *Game) isKeyJustPressed() bool {
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
2018-04-07 14:05:01 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
|
|
|
return true
|
|
|
|
}
|
2022-04-11 03:43:53 +02:00
|
|
|
g.touchIDs = inpututil.AppendJustPressedTouchIDs(g.touchIDs[:0])
|
2021-07-10 16:28:32 +02:00
|
|
|
if len(g.touchIDs) > 0 {
|
2018-04-07 21:59:13 +02:00
|
|
|
return true
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
2021-07-09 19:03:48 +02:00
|
|
|
g.gamepadIDs = ebiten.AppendGamepadIDs(g.gamepadIDs[:0])
|
|
|
|
for _, g := range g.gamepadIDs {
|
2021-07-20 12:49:43 +02:00
|
|
|
if ebiten.IsStandardGamepadLayoutAvailable(g) {
|
|
|
|
if inpututil.IsStandardGamepadButtonJustPressed(g, ebiten.StandardGamepadButtonRightBottom) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if inpututil.IsStandardGamepadButtonJustPressed(g, ebiten.StandardGamepadButtonRightRight) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The button 0/1 might not be A/B buttons.
|
|
|
|
if inpututil.IsGamepadButtonJustPressed(g, ebiten.GamepadButton0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if inpututil.IsGamepadButtonJustPressed(g, ebiten.GamepadButton1) {
|
2021-05-23 12:44:03 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 14:05:01 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-01 08:59:45 +02:00
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2018-04-07 14:05:01 +02:00
|
|
|
switch g.mode {
|
|
|
|
case ModeTitle:
|
2021-07-20 12:49:43 +02:00
|
|
|
if g.isKeyJustPressed() {
|
2018-04-07 14:05:01 +02:00
|
|
|
g.mode = ModeGame
|
|
|
|
}
|
|
|
|
case ModeGame:
|
|
|
|
g.x16 += 32
|
|
|
|
g.cameraX += 2
|
2021-07-20 12:49:43 +02:00
|
|
|
if g.isKeyJustPressed() {
|
2018-04-07 14:05:01 +02:00
|
|
|
g.vy16 = -96
|
2021-07-21 17:15:30 +02:00
|
|
|
g.jumpPlayer.Rewind()
|
|
|
|
g.jumpPlayer.Play()
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
g.y16 += g.vy16
|
|
|
|
|
|
|
|
// Gravity
|
|
|
|
g.vy16 += 4
|
|
|
|
if g.vy16 > 96 {
|
|
|
|
g.vy16 = 96
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.hit() {
|
2021-07-21 17:15:30 +02:00
|
|
|
g.hitPlayer.Rewind()
|
|
|
|
g.hitPlayer.Play()
|
2018-04-07 14:05:01 +02:00
|
|
|
g.mode = ModeGameOver
|
|
|
|
g.gameoverCount = 30
|
|
|
|
}
|
|
|
|
case ModeGameOver:
|
|
|
|
if g.gameoverCount > 0 {
|
|
|
|
g.gameoverCount--
|
|
|
|
}
|
2021-07-20 12:49:43 +02:00
|
|
|
if g.gameoverCount == 0 && g.isKeyJustPressed() {
|
2018-04-07 14:05:01 +02:00
|
|
|
g.init()
|
|
|
|
g.mode = ModeTitle
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 08:59:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-07 14:05:01 +02:00
|
|
|
|
2020-04-01 08:59:45 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2018-04-07 14:05:01 +02:00
|
|
|
screen.Fill(color.RGBA{0x80, 0xa0, 0xc0, 0xff})
|
|
|
|
g.drawTiles(screen)
|
|
|
|
if g.mode != ModeTitle {
|
|
|
|
g.drawGopher(screen)
|
|
|
|
}
|
2021-05-23 12:44:03 +02:00
|
|
|
var titleTexts []string
|
2018-04-07 14:05:01 +02:00
|
|
|
var texts []string
|
|
|
|
switch g.mode {
|
|
|
|
case ModeTitle:
|
2021-05-23 12:44:03 +02:00
|
|
|
titleTexts = []string{"FLAPPY GOPHER"}
|
2021-07-20 12:49:43 +02:00
|
|
|
texts = []string{"", "", "", "", "", "", "", "PRESS SPACE KEY", "", "OR A/B BUTTON", "", "OR TOUCH SCREEN"}
|
2018-04-07 14:05:01 +02:00
|
|
|
case ModeGameOver:
|
2019-10-17 19:15:33 +02:00
|
|
|
texts = []string{"", "GAME OVER!"}
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
2021-05-23 12:44:03 +02:00
|
|
|
for i, l := range titleTexts {
|
|
|
|
x := (screenWidth - len(l)*titleFontSize) / 2
|
|
|
|
text.Draw(screen, l, titleArcadeFont, x, (i+4)*titleFontSize, color.White)
|
|
|
|
}
|
2018-04-07 14:05:01 +02:00
|
|
|
for i, l := range texts {
|
|
|
|
x := (screenWidth - len(l)*fontSize) / 2
|
|
|
|
text.Draw(screen, l, arcadeFont, x, (i+4)*fontSize, color.White)
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:38:55 +02:00
|
|
|
if g.mode == ModeTitle {
|
|
|
|
msg := []string{
|
|
|
|
"Go Gopher by Renee French is",
|
|
|
|
"licenced under CC BY 3.0.",
|
|
|
|
}
|
|
|
|
for i, l := range msg {
|
|
|
|
x := (screenWidth - len(l)*smallFontSize) / 2
|
|
|
|
text.Draw(screen, l, smallArcadeFont, x, screenHeight-4+(i-1)*smallFontSize, color.White)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 14:05:01 +02:00
|
|
|
scoreStr := fmt.Sprintf("%04d", g.score())
|
|
|
|
text.Draw(screen, scoreStr, arcadeFont, screenWidth-len(scoreStr)*fontSize, fontSize, color.White)
|
2018-09-29 19:27:33 +02:00
|
|
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) pipeAt(tileX int) (tileY int, ok bool) {
|
|
|
|
if (tileX - pipeStartOffsetX) <= 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
if floorMod(tileX-pipeStartOffsetX, pipeIntervalX) != 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
idx := floorDiv(tileX-pipeStartOffsetX, pipeIntervalX)
|
|
|
|
return g.pipeTileYs[idx%len(g.pipeTileYs)], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) score() int {
|
|
|
|
x := floorDiv(g.x16, 16) / tileSize
|
|
|
|
if (x - pipeStartOffsetX) <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return floorDiv(x-pipeStartOffsetX, pipeIntervalX)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) hit() bool {
|
|
|
|
if g.mode != ModeGame {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const (
|
|
|
|
gopherWidth = 30
|
|
|
|
gopherHeight = 60
|
|
|
|
)
|
|
|
|
w, h := gopherImage.Size()
|
|
|
|
x0 := floorDiv(g.x16, 16) + (w-gopherWidth)/2
|
|
|
|
y0 := floorDiv(g.y16, 16) + (h-gopherHeight)/2
|
|
|
|
x1 := x0 + gopherWidth
|
|
|
|
y1 := y0 + gopherHeight
|
|
|
|
if y0 < -tileSize*4 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if y1 >= screenHeight-tileSize {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
xMin := floorDiv(x0-pipeWidth, tileSize)
|
|
|
|
xMax := floorDiv(x0+gopherWidth, tileSize)
|
|
|
|
for x := xMin; x <= xMax; x++ {
|
|
|
|
y, ok := g.pipeAt(x)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if x0 >= x*tileSize+pipeWidth {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if x1 < x*tileSize {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if y0 < y*tileSize {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if y1 >= (y+pipeGapY)*tileSize {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) drawTiles(screen *ebiten.Image) {
|
|
|
|
const (
|
|
|
|
nx = screenWidth / tileSize
|
|
|
|
ny = screenHeight / tileSize
|
|
|
|
pipeTileSrcX = 128
|
|
|
|
pipeTileSrcY = 192
|
|
|
|
)
|
|
|
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
for i := -2; i < nx+1; i++ {
|
|
|
|
// ground
|
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
|
|
|
|
float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
|
2018-10-23 16:27:27 +02:00
|
|
|
screen.DrawImage(tilesImage.SubImage(image.Rect(0, 0, tileSize, tileSize)).(*ebiten.Image), op)
|
2018-04-07 14:05:01 +02:00
|
|
|
|
|
|
|
// pipe
|
|
|
|
if tileY, ok := g.pipeAt(floorDiv(g.cameraX, tileSize) + i); ok {
|
|
|
|
for j := 0; j < tileY; j++ {
|
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Scale(1, -1)
|
|
|
|
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
|
|
|
|
float64(j*tileSize-floorMod(g.cameraY, tileSize)))
|
|
|
|
op.GeoM.Translate(0, tileSize)
|
2018-10-23 16:27:27 +02:00
|
|
|
var r image.Rectangle
|
2018-04-07 14:05:01 +02:00
|
|
|
if j == tileY-1 {
|
2018-10-23 16:27:27 +02:00
|
|
|
r = image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize)
|
2018-04-07 14:05:01 +02:00
|
|
|
} else {
|
2018-10-23 16:27:27 +02:00
|
|
|
r = image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize*2)
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
2018-10-23 16:27:27 +02:00
|
|
|
screen.DrawImage(tilesImage.SubImage(r).(*ebiten.Image), op)
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
for j := tileY + pipeGapY; j < screenHeight/tileSize-1; j++ {
|
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
|
|
|
|
float64(j*tileSize-floorMod(g.cameraY, tileSize)))
|
2018-10-23 16:27:27 +02:00
|
|
|
var r image.Rectangle
|
2018-04-07 14:05:01 +02:00
|
|
|
if j == tileY+pipeGapY {
|
2018-10-23 16:27:27 +02:00
|
|
|
r = image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize)
|
2018-04-07 14:05:01 +02:00
|
|
|
} else {
|
2018-10-23 16:27:27 +02:00
|
|
|
r = image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize+tileSize)
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
2018-10-23 16:27:27 +02:00
|
|
|
screen.DrawImage(tilesImage.SubImage(r).(*ebiten.Image), op)
|
2018-04-07 14:05:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) drawGopher(screen *ebiten.Image) {
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
2018-04-07 17:38:52 +02:00
|
|
|
w, h := gopherImage.Size()
|
|
|
|
op.GeoM.Translate(-float64(w)/2.0, -float64(h)/2.0)
|
2018-04-07 14:05:01 +02:00
|
|
|
op.GeoM.Rotate(float64(g.vy16) / 96.0 * math.Pi / 6)
|
2018-04-07 17:38:52 +02:00
|
|
|
op.GeoM.Translate(float64(w)/2.0, float64(h)/2.0)
|
2018-04-07 14:05:01 +02:00
|
|
|
op.GeoM.Translate(float64(g.x16/16.0)-float64(g.cameraX), float64(g.y16/16.0)-float64(g.cameraY))
|
|
|
|
op.Filter = ebiten.FilterLinear
|
|
|
|
screen.DrawImage(gopherImage, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-04-01 08:59:45 +02:00
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
|
|
|
ebiten.SetWindowTitle("Flappy Gopher (Ebiten Demo)")
|
|
|
|
if err := ebiten.RunGame(NewGame()); err != nil {
|
2018-04-07 14:05:01 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|