examples/flappy: Accept gamepads

This commit is contained in:
Hajime Hoshi 2021-05-23 19:44:03 +09:00
parent 800a929e84
commit 7f67fb7b90

View File

@ -62,7 +62,8 @@ const (
screenWidth = 640 screenWidth = 640
screenHeight = 480 screenHeight = 480
tileSize = 32 tileSize = 32
fontSize = 32 titleFontSize = fontSize * 1.5
fontSize = 24
smallFontSize = fontSize / 2 smallFontSize = fontSize / 2
pipeWidth = tileSize * 2 pipeWidth = tileSize * 2
pipeStartOffsetX = 8 pipeStartOffsetX = 8
@ -73,6 +74,7 @@ const (
var ( var (
gopherImage *ebiten.Image gopherImage *ebiten.Image
tilesImage *ebiten.Image tilesImage *ebiten.Image
titleArcadeFont font.Face
arcadeFont font.Face arcadeFont font.Face
smallArcadeFont font.Face smallArcadeFont font.Face
) )
@ -97,6 +99,14 @@ func init() {
log.Fatal(err) log.Fatal(err)
} }
const dpi = 72 const dpi = 72
titleArcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: titleFontSize,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
arcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{ arcadeFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: fontSize, Size: fontSize,
DPI: dpi, DPI: dpi,
@ -184,8 +194,17 @@ func (g *Game) init() {
} }
} }
func isAnyKeyJustPressed() bool {
for _, k := range inpututil.PressedKeys() {
if inpututil.IsKeyJustPressed(k) {
return true
}
}
return false
}
func jump() bool { func jump() bool {
if inpututil.IsKeyJustPressed(ebiten.KeySpace) { if isAnyKeyJustPressed() {
return true return true
} }
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
@ -194,6 +213,13 @@ func jump() bool {
if len(inpututil.JustPressedTouchIDs()) > 0 { if len(inpututil.JustPressedTouchIDs()) > 0 {
return true return true
} }
for _, g := range ebiten.GamepadIDs() {
for i := 0; i < ebiten.GamepadButtonNum(g); i++ {
if inpututil.IsGamepadButtonJustPressed(g, ebiten.GamepadButton(i)) {
return true
}
}
}
return false return false
} }
@ -247,13 +273,19 @@ func (g *Game) Draw(screen *ebiten.Image) {
if g.mode != ModeTitle { if g.mode != ModeTitle {
g.drawGopher(screen) g.drawGopher(screen)
} }
var titleTexts []string
var texts []string var texts []string
switch g.mode { switch g.mode {
case ModeTitle: case ModeTitle:
texts = []string{"FLAPPY GOPHER", "", "", "", "", "PRESS SPACE KEY", "", "OR TOUCH SCREEN"} titleTexts = []string{"FLAPPY GOPHER"}
texts = []string{"", "", "", "", "", "", "", "PRESS ANY KEY OR BUTTON", "", "OR TOUCH SCREEN"}
case ModeGameOver: case ModeGameOver:
texts = []string{"", "GAME OVER!"} texts = []string{"", "GAME OVER!"}
} }
for i, l := range titleTexts {
x := (screenWidth - len(l)*titleFontSize) / 2
text.Draw(screen, l, titleArcadeFont, x, (i+4)*titleFontSize, color.White)
}
for i, l := range texts { for i, l := range texts {
x := (screenWidth - len(l)*fontSize) / 2 x := (screenWidth - len(l)*fontSize) / 2
text.Draw(screen, l, arcadeFont, x, (i+4)*fontSize, color.White) text.Draw(screen, l, arcadeFont, x, (i+4)*fontSize, color.White)