-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
336 lines (295 loc) · 7.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"fmt"
"github.com/anthonynsimon/bild/blur"
"github.com/veandco/go-sdl2/mix"
"github.com/veandco/go-sdl2/sdl"
"image"
_ "image/png"
"log"
"math/rand"
"os"
"sync"
"time"
"unsafe"
)
const (
winWidth = 800
winHeight = 600
)
type Direction int
const (
Up Direction = iota
Down
Left
Right
)
type Color int
const (
Red Color = iota
Brown
Blue
)
func loadAndBlurTexture(renderer *sdl.Renderer, path string) (*sdl.Texture, error) {
imgFile, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not load image: %v", err)
}
defer imgFile.Close()
img, _, err := image.Decode(imgFile)
if err != nil {
return nil, fmt.Errorf("could not decode image: %v", err)
}
// Apply Gaussian blur using bild package
blurredImg := blur.Gaussian(img, 3.0)
// Convert image.Image to SDL2 surface
surface, err := sdl.CreateRGBSurfaceFrom(unsafe.Pointer(&blurredImg.Pix[0]),
int32(blurredImg.Rect.Dx()),
int32(blurredImg.Rect.Dy()),
32,
blurredImg.Stride,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000,
)
if err != nil {
return nil, fmt.Errorf("could not create surface: %v", err)
}
defer surface.Free()
texture, err := renderer.CreateTextureFromSurface(surface)
if err != nil {
return nil, fmt.Errorf("could not create texture: %v", err)
}
return texture, nil
}
func loadMedia(renderer *sdl.Renderer) (*sdl.Texture, *mix.Music, error) {
texture, err := loadAndBlurTexture(renderer, "level1.png")
if err != nil {
log.Fatalf("Filed to load/blur image: %v", err)
}
if err := mix.Init(mix.INIT_MP3); err != nil {
log.Fatalf("Initializing SDL_mixer failed: %v", err)
}
if err := mix.OpenAudio(44100, mix.DEFAULT_FORMAT, 2, 4096); err != nil {
log.Fatalf("Failed to open audio: %v", err)
}
music, err := mix.LoadMUS("level1.mp3")
if err != nil {
return nil, nil, fmt.Errorf("could not load background music: %v", err)
}
return texture, music, nil
}
func colorToSDLColor(color Color) sdl.Color {
switch color {
case Red:
return sdl.Color{R: 255, G: 0, B: 0, A: 255}
case Brown:
return sdl.Color{R: 139, G: 69, B: 19, A: 255}
case Blue:
return sdl.Color{R: 0, G: 0, B: 255, A: 255}
default:
return sdl.Color{R: 255, G: 255, B: 255, A: 255}
}
}
type Alien struct {
Rect *sdl.Rect
FillColor Color
}
type Laser struct {
Rect *sdl.Rect
Dir Direction
}
var (
player *sdl.Rect
playerDir Direction
aliens []Alien
lasers []Laser
running bool
alienLock sync.Mutex
keys = map[sdl.Keycode]bool{}
)
func initSDL() *sdl.Window {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
log.Fatalf("Could not initialize SDL: %s", err)
}
window, err := sdl.CreateWindow("Alien Invader Z", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN)
if err != nil {
log.Fatalf("Could not create window: %s", err)
}
return window
}
func initGame() {
rand.Seed(time.Now().UnixNano())
player = &sdl.Rect{X: 390, Y: 550, W: 20, H: 20}
playerDir = Up
addRandomAlien()
running = true
}
func addRandomAlien() {
x := int32(rand.Intn(winWidth - 20))
y := int32(rand.Intn(winHeight - 20))
color := Color(rand.Intn(3))
newAlien := Alien{Rect: &sdl.Rect{X: x, Y: y, W: 20, H: 20}, FillColor: color}
aliens = append(aliens, newAlien)
}
func createAliens(ticker *time.Ticker, alienCount int) {
for range ticker.C {
for i := 0; i < alienCount; i++ {
addRandomAlien()
}
}
}
func checkCollision(a, b *sdl.Rect) bool {
return a.X < b.X+b.W && a.X+a.W > b.X && a.Y < b.Y+b.H && a.Y+a.H > b.Y
}
func moveAliens() {
for running {
alienLock.Lock()
for i, alien := range aliens {
dx := int32(rand.Intn(5) - 2) // Random between -2 and 2
dy := int32(rand.Intn(5) - 2) // Random between -2 and 2
switch alien.FillColor {
case Red:
if alien.Rect.X < player.X {
alien.Rect.X += 5
} else if alien.Rect.X > player.X {
alien.Rect.X -= 5
}
if alien.Rect.Y < player.Y {
alien.Rect.Y += 5
} else if alien.Rect.Y > player.Y {
alien.Rect.Y -= 5
}
default:
alien.Rect.X += dx
alien.Rect.Y += dy
// Make sure alien stays within window boundary
if alien.Rect.X < 0 {
alien.Rect.X = 0
}
if alien.Rect.Y < 0 {
alien.Rect.Y = 0
}
if alien.Rect.X > winWidth-20 {
alien.Rect.X = winWidth - 20
}
if alien.Rect.Y > winHeight-20 {
alien.Rect.Y = winHeight - 20
}
}
aliens[i] = alien
}
alienLock.Unlock()
time.Sleep(time.Millisecond * 50)
}
}
func main() {
window := initSDL()
defer window.Destroy()
defer sdl.Quit()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
log.Fatalf("Could not create renderer: %s", err)
}
defer renderer.Destroy()
texture, music, err := loadMedia(renderer)
if err != nil {
log.Fatalf("Could not load media: %s", err)
}
defer texture.Destroy()
defer music.Free()
music.Play(-1) // loop indefinitely
initGame()
// Create 5 aliens every 5 seconds
ticker := time.NewTicker(5 * time.Second)
go createAliens(ticker, 5)
go moveAliens() // Start moving aliens in a separate goroutine
for running {
renderer.Copy(texture, nil, nil) // Copy the background image to fill the entire window
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch e := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.KeyboardEvent:
// Handle key events here
keys[e.Keysym.Sym] = e.Type == sdl.KEYDOWN
}
}
if keys[sdl.K_LEFT] && player.X > 0 {
player.X -= 5
playerDir = Left
}
if keys[sdl.K_RIGHT] && player.X < winWidth-20 {
player.X += 5
playerDir = Right
}
if keys[sdl.K_UP] && player.Y > 0 {
player.Y -= 5
playerDir = Up
}
if keys[sdl.K_DOWN] && player.Y < winHeight-20 {
player.Y += 5
playerDir = Down
}
if keys[sdl.K_SPACE] {
laser := Laser{}
switch playerDir {
case Up:
laser.Rect = &sdl.Rect{X: player.X + 9, Y: player.Y, W: 2, H: 10}
case Down:
laser.Rect = &sdl.Rect{X: player.X + 9, Y: player.Y + 19, W: 2, H: 10}
case Left:
laser.Rect = &sdl.Rect{X: player.X, Y: player.Y + 9, W: 10, H: 2}
case Right:
laser.Rect = &sdl.Rect{X: player.X + 19, Y: player.Y + 9, W: 10, H: 2}
}
laser.Dir = playerDir
lasers = append(lasers, laser)
}
for i, laser := range lasers {
switch laser.Dir {
case Up:
laser.Rect.Y -= 5
case Down:
laser.Rect.Y += 5
case Left:
laser.Rect.X -= 5
case Right:
laser.Rect.X += 5
}
lasers[i] = laser
for j, alien := range aliens {
if checkCollision(laser.Rect, alien.Rect) {
aliens = append(aliens[:j], aliens[j+1:]...)
//addRandomAlien()
break
}
}
}
// Check if player collides with any alien
for _, alien := range aliens {
if checkCollision(player, alien.Rect) {
fmt.Println("Game Over! You were touched by an alien!")
running = false
break
}
}
renderer.SetDrawColor(255, 255, 255, 255) // White color of player
renderer.FillRect(player)
renderer.SetDrawColor(0, 255, 0, 255) // Green color of laser
for _, laser := range lasers {
renderer.FillRect(laser.Rect)
}
alienLock.Lock()
for _, alien := range aliens {
sdlColor := colorToSDLColor(alien.FillColor)
renderer.SetDrawColor(sdlColor.R, sdlColor.G, sdlColor.B, sdlColor.A)
renderer.FillRect(alien.Rect)
}
alienLock.Unlock()
renderer.Present()
time.Sleep(time.Millisecond * 16)
}
fmt.Println("Game Over!!!")
}