asphalt texture and speed up when getting more points

This commit is contained in:
bdnugget 2024-09-23 19:58:14 +02:00
parent d1fc577bdb
commit d55585f77b
3 changed files with 39 additions and 22 deletions

61
main.go
View File

@ -8,21 +8,30 @@ import (
) )
const ( const (
screenWidth = 1300 screenWidth = 1300
screenHeight = 800 screenHeight = 800
numLanes = 5 numLanes = 5
numGarages = 5 laneHeight = screenHeight / numLanes
carSpeed = 5 carTextureWidth = 200
laneHeight = screenHeight / numLanes carTextureHeight = 200
garageWidth = 200
garageHeight = 200
scorePosx = 10
scorePosy = 10
scoreFontSize = 32
gameOverPosx = screenWidth/2 - 150
gameOverPosy = screenHeight/2 - 20
gameOverFontSize = 32
) )
var ( var (
garageColors = [numGarages]rl.Color{rl.Red, rl.Blue, rl.Green, rl.Yellow, rl.Purple} garageColors = []rl.Color{rl.Red, rl.Blue, rl.Green, rl.Yellow, rl.Purple}
autoHappy rl.Sound autoHappy rl.Sound
autoSad rl.Sound autoSad rl.Sound
autoTexture rl.Texture2D autoTexture rl.Texture2D
garageTexture rl.Texture2D garageTexture rl.Texture2D
highScore int asphaltTexture rl.Texture2D
highScore int
) )
type Car struct { type Car struct {
@ -30,6 +39,7 @@ type Car struct {
lane int lane int
color rl.Color color rl.Color
xPos int xPos int
speed int
} }
func NewCar() Car { func NewCar() Car {
@ -38,6 +48,7 @@ func NewCar() Car {
lane: rand.Intn(numLanes), lane: rand.Intn(numLanes),
color: randomColor(), color: randomColor(),
xPos: 25, xPos: 25,
speed: 1,
} }
} }
@ -48,7 +59,7 @@ type Garage struct {
type Game struct { type Game struct {
car Car car Car
garages [numGarages]Garage garages [numLanes]Garage
score int score int
gameOver bool gameOver bool
debugMsg string debugMsg string
@ -76,22 +87,23 @@ func randomColor() rl.Color {
} }
func (g Game) draw() { func (g Game) draw() {
// Draw stripes on the road for i := 0; i <= screenHeight/laneHeight; i++ {
for i := 0; i < numLanes; i++ { for j := 0; j < screenWidth/int(asphaltTexture.Width); j++ {
rl.DrawRectangle(0, int32(i*laneHeight), screenWidth, 2, rl.White) 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)
}
} }
// Draw garages // Draw garages
for _, garage := range g.garages { for _, garage := range g.garages {
rl.DrawTexture(garageTexture, int32(screenWidth-150), int32(garage.lane*laneHeight), garage.color) rl.DrawTexture(garageTexture, int32(screenWidth-garageWidth), int32(garage.lane*laneHeight), garage.color)
} }
// Draw car // Draw car
rl.DrawTexture(g.car.texture, int32(g.car.xPos), int32(g.car.lane*laneHeight), g.car.color) 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), 10, 10, 32, rl.DarkGreen) rl.DrawText(fmt.Sprintf("Score: %d\nHigh Score: %d", g.score, highScore), scorePosx, scorePosy, scoreFontSize, rl.DarkGreen)
if g.gameOver { if g.gameOver {
rl.DrawText("You're not an WinRAR :(\nPress Enter to Restart", int32(screenWidth/2-150), int32(screenHeight/2-20), 32, rl.Red) rl.DrawText("You're not an WinRAR :(\nPress Enter to Restart", gameOverPosx, gameOverPosy, gameOverFontSize, rl.Red)
} }
} }
@ -101,16 +113,16 @@ func (g *Game) update() {
} }
if g.gameOver { if g.gameOver {
if rl.IsKeyPressed(rl.KeyEnter) { if rl.IsKeyPressed(rl.KeyEnter) || rl.IsKeyPressed(rl.KeyKpEnter) || rl.IsGestureDetected(rl.GestureTap) {
*g = NewGame() *g = NewGame()
} }
return return
} }
g.car.xPos += carSpeed g.car.xPos += g.car.speed
// Check if the car has reached the garages // Check if the car has reached the garages
if int32(g.car.xPos)+g.car.texture.Width >= screenWidth-100 { if int32(g.car.xPos)+g.car.texture.Width >= screenWidth-garageWidth {
garage := g.garages[g.car.lane] garage := g.garages[g.car.lane]
if g.car.color == garage.color { if g.car.color == garage.color {
rl.PlaySound(autoHappy) rl.PlaySound(autoHappy)
@ -125,6 +137,8 @@ func (g *Game) update() {
} }
} }
g.car.speed = 3 + g.score/5
// Move car // Move car
if rl.IsGestureDetected(rl.GestureTap) { if rl.IsGestureDetected(rl.GestureTap) {
touchY := int(rl.GetTouchY()) touchY := int(rl.GetTouchY())
@ -151,7 +165,10 @@ func main() {
autoSad = rl.LoadSound("resources/auto_sad_vob.ogg") autoSad = rl.LoadSound("resources/auto_sad_vob.ogg")
autoTexture = rl.LoadTexture("resources/car_200px.png") autoTexture = rl.LoadTexture("resources/car_200px.png")
garageTexture = rl.LoadTexture("resources/garage_200px.png") garageTexture = rl.LoadTexture("resources/garage_200px.png")
asphaltTexture = rl.LoadTexture("resources/asphalt.png")
defer rl.UnloadTexture(autoTexture) defer rl.UnloadTexture(autoTexture)
defer rl.UnloadTexture(garageTexture)
defer rl.UnloadTexture(asphaltTexture)
defer rl.UnloadSound(autoHappy) defer rl.UnloadSound(autoHappy)
defer rl.UnloadSound(autoSad) defer rl.UnloadSound(autoSad)

BIN
resources/asphalt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 633 KiB