-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a20f571
Showing
7 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Prepare dependencies | ||
run: sudo apt-get install libx11-dev libglx-dev libxi-dev libxext-dev libxrandr-dev libgl-dev libxcursor-dev libxinerama-dev libxxf86vm-dev | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.22.2' | ||
- name: Install dependencies | ||
run: go get . | ||
|
||
- name: Build | ||
run: go build -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
_build/ | ||
_backup/ | ||
*.bak | ||
*.obj | ||
*.tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Try to nudge the logo into the corner |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"image/color" | ||
"log" | ||
"math" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/hajimehoshi/ebiten/v2" | ||
"github.com/hajimehoshi/ebiten/v2/ebitenutil" | ||
"github.com/hajimehoshi/ebiten/v2/text" | ||
"golang.org/x/image/font/basicfont" | ||
) | ||
|
||
//go:embed dvd-logo.png | ||
var logoImageData []byte // Embedded the logo image | ||
|
||
const ( | ||
screenWidth = 800 | ||
screenHeight = 600 | ||
logoWidth = 120 | ||
logoStartVelocity = 2 | ||
logoMaxVelocity = 3 | ||
cornerTolerance = 5 | ||
nudgeAmount = 0.5 | ||
) | ||
|
||
type Game struct { | ||
logoX float64 | ||
logoY float64 | ||
velocityX float64 | ||
velocityY float64 | ||
cornerHits int | ||
startTime time.Time | ||
logoImage *ebiten.Image | ||
logoHeight float64 | ||
hitCorner bool | ||
paused bool | ||
terminated bool | ||
keyState map[ebiten.Key]bool | ||
} | ||
|
||
func (g *Game) Update() error { | ||
// Handle key press events | ||
g.handleKeyPresses() | ||
|
||
if g.paused { | ||
if g.terminated { | ||
return ebiten.Termination | ||
} | ||
return nil | ||
} | ||
|
||
g.logoX += g.velocityX | ||
g.logoY += g.velocityY | ||
g.hitCorner = false | ||
|
||
// Check for collision with window borders | ||
if g.logoX < 0 { | ||
g.logoX = 0 | ||
g.velocityX = -g.velocityX | ||
} | ||
if g.logoX+logoWidth > screenWidth { | ||
g.logoX = screenWidth - logoWidth | ||
g.velocityX = -g.velocityX | ||
} | ||
if g.logoY < 0 { | ||
g.logoY = 0 | ||
g.velocityY = -g.velocityY | ||
} | ||
if g.logoY+g.logoHeight > screenHeight { | ||
g.logoY = screenHeight - g.logoHeight | ||
g.velocityY = -g.velocityY | ||
} | ||
|
||
// Check if the logo touches the corner | ||
if g.logoX < cornerTolerance || g.logoX > screenWidth-logoWidth-cornerTolerance { | ||
if g.logoY < cornerTolerance || g.logoY > screenHeight-g.logoHeight-cornerTolerance { | ||
g.cornerHits++ | ||
g.hitCorner = true | ||
} | ||
} | ||
|
||
// Adjust velocity based on mouse input | ||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { | ||
x, y := ebiten.CursorPosition() | ||
dx := float64(x) - (g.logoX + logoWidth/2) | ||
dy := float64(y) - (g.logoY + g.logoHeight/2) | ||
g.velocityX += dx * nudgeAmount / 1000 | ||
g.velocityY += dy * nudgeAmount / 1000 | ||
|
||
// Clamp velocity to logoMaxVelocity | ||
if math.Abs(g.velocityX) > logoMaxVelocity { | ||
g.velocityX = math.Copysign(logoMaxVelocity, g.velocityX) | ||
} | ||
if math.Abs(g.velocityY) > logoMaxVelocity { | ||
g.velocityY = math.Copysign(logoMaxVelocity, g.velocityY) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *Game) handleKeyPresses() { | ||
// Check for escape key press to toggle pause state | ||
if ebiten.IsKeyPressed(ebiten.KeyEscape) { | ||
if !g.keyState[ebiten.KeyEscape] { | ||
g.paused = !g.paused | ||
} | ||
g.keyState[ebiten.KeyEscape] = true | ||
} else { | ||
g.keyState[ebiten.KeyEscape] = false | ||
} | ||
|
||
if g.paused { | ||
// Check for 'C' to continue | ||
if ebiten.IsKeyPressed(ebiten.KeyC) { | ||
if !g.keyState[ebiten.KeyC] { | ||
g.paused = false | ||
} | ||
g.keyState[ebiten.KeyC] = true | ||
} else { | ||
g.keyState[ebiten.KeyC] = false | ||
} | ||
|
||
// Check for 'Q' to quit | ||
if ebiten.IsKeyPressed(ebiten.KeyQ) { | ||
g.terminated = true | ||
g.keyState[ebiten.KeyQ] = true | ||
} else { | ||
g.keyState[ebiten.KeyQ] = false | ||
} | ||
} | ||
} | ||
|
||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { | ||
return screenWidth, screenHeight | ||
} | ||
|
||
func (g *Game) Draw(screen *ebiten.Image) { | ||
// Set the background color | ||
if g.hitCorner { | ||
screen.Fill(color.RGBA{0, 255, 0, 255}) // Flash green if hit a corner | ||
} else { | ||
screen.Fill(color.RGBA{0, 0, 255, 255}) // Default blue background | ||
} | ||
|
||
// Draw the logo | ||
op := &ebiten.DrawImageOptions{} | ||
scale := logoWidth / float64(g.logoImage.Bounds().Dx()) | ||
op.GeoM.Scale(scale, scale) | ||
op.GeoM.Translate(g.logoX, g.logoY) | ||
screen.DrawImage(g.logoImage, op) | ||
|
||
// Update window title with corner hits and elapsed time | ||
g.updateWindowTitle() | ||
|
||
if g.paused { | ||
g.drawPauseMenu(screen) | ||
} | ||
} | ||
|
||
func (g *Game) updateWindowTitle() { | ||
elapsedTime := time.Since(g.startTime) | ||
hours := int(elapsedTime.Hours()) | ||
minutes := int(elapsedTime.Minutes()) % 60 | ||
seconds := int(elapsedTime.Seconds()) % 60 | ||
milliseconds := int(elapsedTime.Milliseconds()) % 1000 | ||
title := fmt.Sprintf("Hits: %d | Time: %02d:%02d:%02d.%02d", g.cornerHits, hours, minutes, seconds, milliseconds/10) | ||
ebiten.SetWindowTitle(title) | ||
} | ||
|
||
func (g *Game) drawPauseMenu(screen *ebiten.Image) { | ||
// Draw the pause menu background | ||
pauseMenuWidth := 300 | ||
pauseMenuHeight := 200 | ||
pauseMenuX := (screenWidth - pauseMenuWidth) / 2 | ||
pauseMenuY := (screenHeight - pauseMenuHeight) / 2 | ||
ebitenutil.DrawRect(screen, float64(pauseMenuX), float64(pauseMenuY), float64(pauseMenuWidth), float64(pauseMenuHeight), color.RGBA{0, 0, 128, 255}) // Dark blue background | ||
|
||
// Draw the pause menu border | ||
borderThickness := 2.0 | ||
ebitenutil.DrawRect(screen, float64(pauseMenuX), float64(pauseMenuY), float64(pauseMenuWidth), borderThickness, color.White) | ||
ebitenutil.DrawRect(screen, float64(pauseMenuX), float64(pauseMenuY), borderThickness, float64(pauseMenuHeight), color.White) | ||
ebitenutil.DrawRect(screen, float64(pauseMenuX), float64(pauseMenuY+pauseMenuHeight)-borderThickness, float64(pauseMenuWidth), borderThickness, color.White) | ||
ebitenutil.DrawRect(screen, float64(pauseMenuX+pauseMenuWidth)-borderThickness, float64(pauseMenuY), borderThickness, float64(pauseMenuHeight), color.White) | ||
|
||
// Draw the pause menu text | ||
pauseText := "PAUSED" | ||
continueText := "[C]ontinue" | ||
quitText := "[Q]uit" | ||
text.Draw(screen, pauseText, basicfont.Face7x13, pauseMenuX+pauseMenuWidth/2-len(pauseText)*7/2, pauseMenuY+50, color.White) | ||
text.Draw(screen, continueText, basicfont.Face7x13, pauseMenuX+pauseMenuWidth/2-len(continueText)*7/2, pauseMenuY+100, color.White) | ||
text.Draw(screen, quitText, basicfont.Face7x13, pauseMenuX+pauseMenuWidth/2-len(quitText)*7/2, pauseMenuY+150, color.White) | ||
} | ||
|
||
func main() { | ||
ebiten.SetWindowSize(screenWidth, screenHeight) | ||
ebiten.SetWindowTitle("DVD Logo Bouncer") | ||
|
||
logoImage, _, err := ebitenutil.NewImageFromReader(bytes.NewReader(logoImageData)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
scale := logoWidth / float64(logoImage.Bounds().Dx()) | ||
logoHeight := scale * float64(logoImage.Bounds().Dy()) | ||
|
||
game := &Game{ | ||
logoX: float64(rand.Intn(screenWidth - int(logoWidth))), | ||
logoY: float64(rand.Intn(screenHeight - int(logoHeight))), | ||
velocityX: logoStartVelocity, | ||
velocityY: logoStartVelocity, | ||
startTime: time.Now(), | ||
logoImage: logoImage, | ||
logoHeight: logoHeight, | ||
keyState: make(map[ebiten.Key]bool), | ||
} | ||
|
||
if err := ebiten.RunGame(game); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module example.com/dvdlogo | ||
|
||
go 1.22.2 | ||
|
||
require ( | ||
github.com/hajimehoshi/ebiten/v2 v2.7.6 | ||
golang.org/x/image v0.18.0 | ||
) | ||
|
||
require ( | ||
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect | ||
github.com/ebitengine/hideconsole v1.0.0 // indirect | ||
github.com/ebitengine/purego v0.7.0 // indirect | ||
github.com/jezek/xgb v1.1.1 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.20.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 h1:48bCqKTuD7Z0UovDfvpCn7wZ0GUZ+yosIteNDthn3FU= | ||
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895/go.mod h1:XZdLv05c5hOZm3fM2NlJ92FyEZjnslcMcNRrhxs8+8M= | ||
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE= | ||
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A= | ||
github.com/ebitengine/purego v0.7.0 h1:HPZpl61edMGCEW6XK2nsR6+7AnJ3unUxpTZBkkIXnMc= | ||
github.com/ebitengine/purego v0.7.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= | ||
github.com/hajimehoshi/bitmapfont/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4= | ||
github.com/hajimehoshi/bitmapfont/v3 v3.0.0/go.mod h1:+CxxG+uMmgU4mI2poq944i3uZ6UYFfAkj9V6WqmuvZA= | ||
github.com/hajimehoshi/ebiten/v2 v2.7.6 h1:dKM/BdPZP+I/I0ElcqfQ1d06W+kA0nwhUOWzEdEBIbY= | ||
github.com/hajimehoshi/ebiten/v2 v2.7.6/go.mod h1:Ulbq5xDmdx47P24EJ+Mb31Zps7vQq+guieG9mghQUaA= | ||
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4= | ||
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= | ||
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ= | ||
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E= | ||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= | ||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= | ||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= | ||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= |