-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (78 loc) · 1.87 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
package main
import (
"fmt"
"time"
"github.com/veandco/go-sdl2/sdl"
)
const (
screenWidth = 600
screenHeight = 800
targetTicksPerSecond = 60
)
var delta float64
func main() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
fmt.Println("initializing SDL:", err)
return
}
window, err := sdl.CreateWindow(
"Gaming in Go",
sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
screenWidth, screenHeight,
sdl.WINDOW_OPENGL)
if err != nil {
fmt.Println("initializing window:", err)
return
}
defer window.Destroy()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
fmt.Println("initializing renderer:", err)
return
}
defer renderer.Destroy()
player := newPlayer(renderer)
elements = append(elements, player)
for i := 0; i < 5; i++ {
for j := 0; j < 3; j++ {
x := (float64(i)/5)*screenWidth + (basicEnemySize / 2.0)
y := float64(j)*basicEnemySize + (basicEnemySize / 2.0)
enemy := newBasicEnemy(renderer, vector{x, y})
elements = append(elements, enemy)
}
}
initBulletPool(30, renderer)
for {
frameStartTime := time.Now()
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
return
}
}
renderer.SetDrawColor(0, 255, 0, 255)
renderer.Clear()
for _, elem := range elements {
if elem.active {
err = elem.update()
if err != nil {
fmt.Println("updating element:", err)
return
}
err = elem.draw(renderer)
if err != nil {
fmt.Println("drawing element:", err)
return
}
}
}
if err := checkCollisions(); err != nil {
fmt.Println("checking collisions:", err)
return
}
renderer.Present()
//Introduce a fake delay to test slower hardware
//time.Sleep(time.Millisecond * 50)
delta = time.Since(frameStartTime).Seconds() * targetTicksPerSecond
}
}