Files
autogo/main.go
T

186 lines
4.2 KiB
Go
Raw Normal View History

2024-09-02 19:35:28 +02:00
package main
2024-09-02 20:32:40 +02:00
import (
"fmt"
"math/rand"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
screenWidth = 1300
screenHeight = 800
numLanes = 5
laneHeight = screenHeight / numLanes
garageWidth = 200
scorePosx = 10
scorePosy = 10
scoreFontSize = 32
gameOverPosx = screenWidth/2 - 150
gameOverPosy = screenHeight/2 - 20
gameOverFontSize = 32
2024-09-23 12:13:51 +02:00
)
var (
garageColors = []rl.Color{rl.Red, rl.Blue, rl.Green, rl.Yellow, rl.Purple}
autoHappy rl.Sound
autoSad rl.Sound
autoTexture rl.Texture2D
garageTexture rl.Texture2D
asphaltTexture rl.Texture2D
highScore int
2024-09-02 20:32:40 +02:00
)
type Car struct {
2024-09-23 12:13:51 +02:00
texture rl.Texture2D
lane int
color rl.Color
xPos int
speed int
2024-09-02 20:32:40 +02:00
}
2024-09-23 14:26:50 +02:00
func NewCar() Car {
return Car{
texture: autoTexture,
lane: rand.Intn(numLanes),
color: randomColor(),
xPos: 25,
speed: 1,
2024-09-23 14:26:50 +02:00
}
}
2024-09-02 20:32:40 +02:00
type Garage struct {
2024-09-23 12:13:51 +02:00
lane int
color rl.Color
2024-09-02 20:32:40 +02:00
}
2024-09-23 12:13:51 +02:00
type Game struct {
car Car
garages [numLanes]Garage
2024-09-23 12:13:51 +02:00
score int
gameOver bool
2024-09-23 14:58:40 +02:00
debugMsg string
2024-09-02 20:32:40 +02:00
}
2024-09-23 15:32:37 +02:00
func NewGame() Game {
game := Game{
2024-09-23 12:13:51 +02:00
car: NewCar(),
score: 0,
gameOver: false,
}
// Initialize garages
for i := range game.garages {
game.garages[i] = Garage{
lane: i,
color: garageColors[i],
}
}
return game
}
func randomColor() rl.Color {
return garageColors[rand.Intn(len(garageColors))]
}
2024-09-23 15:32:37 +02:00
func (g Game) draw() {
for i := 0; i <= screenHeight/laneHeight; i++ {
for j := 0; j < screenWidth/int(asphaltTexture.Width); j++ {
rl.DrawTexturePro(asphaltTexture, rl.NewRectangle(0, 0, float32(asphaltTexture.Width), float32(asphaltTexture.Height)), rl.NewRectangle(float32(j)*float32(asphaltTexture.Width), float32(i*laneHeight-laneHeight/2), float32(asphaltTexture.Width), float32(asphaltTexture.Height)), rl.Vector2{}, 0, rl.White)
}
2024-09-23 12:28:40 +02:00
}
2024-09-23 12:13:51 +02:00
// Draw garages
for _, garage := range g.garages {
rl.DrawTexture(garageTexture, int32(screenWidth-garageWidth), int32(garage.lane*laneHeight), garage.color)
2024-09-23 12:13:51 +02:00
}
// Draw car
rl.DrawTexture(g.car.texture, int32(g.car.xPos), int32(g.car.lane*laneHeight), g.car.color)
rl.DrawText(fmt.Sprintf("Score: %d\nHigh Score: %d", g.score, highScore), scorePosx, scorePosy, scoreFontSize, rl.DarkGreen)
2024-09-23 12:13:51 +02:00
if g.gameOver {
rl.DrawText("You're not an WinRAR :(\nPress Enter to Restart", gameOverPosx, gameOverPosy, gameOverFontSize, rl.Red)
2024-09-23 12:13:51 +02:00
}
}
func (g *Game) update() {
if rl.IsKeyPressed(rl.KeyF) {
rl.ToggleFullscreen()
}
2024-09-23 12:13:51 +02:00
if g.gameOver {
if rl.IsKeyPressed(rl.KeyEnter) || rl.IsKeyPressed(rl.KeyKpEnter) || rl.IsGestureDetected(rl.GestureTap) {
2024-09-23 15:32:37 +02:00
*g = NewGame()
2024-09-23 12:13:51 +02:00
}
return
}
g.car.xPos += g.car.speed
2024-09-23 12:13:51 +02:00
// Check if the car has reached the garages
if int32(g.car.xPos)+g.car.texture.Width >= screenWidth-garageWidth {
2024-09-23 12:13:51 +02:00
garage := g.garages[g.car.lane]
if g.car.color == garage.color {
rl.PlaySound(autoHappy)
g.score++
2024-09-23 12:28:40 +02:00
if g.score > highScore {
highScore = g.score
}
2024-09-23 12:13:51 +02:00
g.car = NewCar()
} else {
rl.PlaySound(autoSad)
g.gameOver = true
}
}
g.car.speed = 3 + g.score/5
2024-09-23 12:13:51 +02:00
// Move car
2024-09-23 14:58:40 +02:00
if rl.IsGestureDetected(rl.GestureTap) {
touchY := int(rl.GetTouchY())
lane := touchY / (screenHeight / numLanes)
g.debugMsg = fmt.Sprintf("Y: %d, Lane: %d", touchY, lane)
g.car.lane = lane
2024-09-23 14:26:50 +02:00
}
2024-09-23 12:13:51 +02:00
if rl.IsKeyPressed(rl.KeyUp) && g.car.lane > 0 {
g.car.lane--
} else if (rl.IsKeyPressed(rl.KeyDown) || rl.IsKeyPressed(rl.KeyLeft) || rl.IsKeyPressed(rl.KeyRight)) && g.car.lane < numLanes-1 {
g.car.lane++
}
2024-09-23 14:58:40 +02:00
rl.DrawText(g.debugMsg, 20, 100, 32, rl.Red)
2024-09-23 12:13:51 +02:00
}
2024-09-02 19:35:28 +02:00
func main() {
2024-09-02 20:32:40 +02:00
rl.InitWindow(screenWidth, screenHeight, "Dikke Vette Cargame voor Milo")
rl.InitAudioDevice()
defer rl.CloseAudioDevice()
2024-09-02 19:35:28 +02:00
2024-09-23 12:13:51 +02:00
// Load assets
2024-09-02 20:32:40 +02:00
autoHappy = rl.LoadSound("resources/auto_happy_vob.ogg")
autoSad = rl.LoadSound("resources/auto_sad_vob.ogg")
autoTexture = rl.LoadTexture("resources/car_200px.png")
2024-09-23 12:13:51 +02:00
garageTexture = rl.LoadTexture("resources/garage_200px.png")
asphaltTexture = rl.LoadTexture("resources/asphalt.png")
2024-09-23 12:13:51 +02:00
defer rl.UnloadTexture(autoTexture)
defer rl.UnloadTexture(garageTexture)
defer rl.UnloadTexture(asphaltTexture)
2024-09-02 20:32:40 +02:00
defer rl.UnloadSound(autoHappy)
defer rl.UnloadSound(autoSad)
2024-09-23 12:13:51 +02:00
game := NewGame()
2024-09-02 20:32:40 +02:00
2024-09-23 12:13:51 +02:00
rl.SetTargetFPS(60)
2024-09-02 19:35:28 +02:00
for !rl.WindowShouldClose() {
2024-09-23 12:13:51 +02:00
rl.BeginDrawing()
2024-09-23 12:28:40 +02:00
rl.ClearBackground(rl.DarkGray)
2024-09-23 12:13:51 +02:00
game.update()
game.draw()
rl.EndDrawing()
2024-09-02 19:35:28 +02:00
}
}