-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (133 loc) · 3.01 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
package main
import (
"fmt"
"log"
"math"
"math/rand"
"os"
g "space-invaders/game"
"space-invaders/keyboard"
r "space-invaders/render"
"time"
)
func main() {
// define Size
Height, Width, err := r.GetTerminalSize()
if err != nil {
log.Fatal(err)
}
// set up randomizer
source := rand.NewSource(time.Now().UnixNano())
random := rand.New(source)
// create player
player := g.Player{X: Width / 2, Y: Height - 1}
// create enemies
enemies := make([]g.Enemy, 0)
// create bullets
bullets := make([]g.Bullet, 0)
// create tickers
enemyMoveTicker := time.NewTicker(500 * time.Millisecond)
bulletMoveTicker := time.NewTicker(60 * time.Millisecond)
gameLoopTicker := time.NewTicker(17 * time.Millisecond)
defer enemyMoveTicker.Stop()
defer bulletMoveTicker.Stop()
defer gameLoopTicker.Stop()
// initialize keyboard
keyboard.ResetRawMode()
defer keyboard.ResetRawMode()
// create channel for key event
PressedKey := make(chan string)
go func() {
var buf [3]byte
for {
n, err := os.Stdin.Read(buf[:])
if err != nil {
log.Fatal(err)
return
}
fmt.Println(buf)
if n == 1 && buf[0] == keyboard.KeyEsc[0] {
PressedKey <- keyboard.KeyEsc
} else if n == 3 && buf[0] == keyboard.KeyEsc[0] && buf[1] == '[' {
PressedKey <- string(buf[:n])
}
}
}()
// game loop
for {
for i := range bullets {
b := &bullets[i]
for j := range enemies {
e := &enemies[j]
switch {
case e.X == b.X && e.Y == b.Y || math.Abs(float64(e.X-b.X)) == 1 && math.Abs(float64(e.Y-b.Y)) == 1:
e.IsAlive = false
b.IsActive = false
case e.Y > Height-2:
e.IsAlive = false
case b.Y < 1:
b.IsActive = false
}
}
}
activeEnemies := enemies[:0]
for i := range enemies {
e := &enemies[i]
if e.IsAlive {
activeEnemies = append(activeEnemies, *e)
}
}
enemies = activeEnemies
select {
case <-gameLoopTicker.C:
// clear screen
r.ClearScreen()
// define screen size
Height, Width, err = r.GetTerminalSize()
if err != nil {
log.Panic(err)
}
// render objects
r.Render(player, enemies, bullets, Width, Height)
case <-bulletMoveTicker.C:
// shoot bullets
bullets = append(bullets, g.Bullet{X: player.X, Y: player.Y - 1, IsActive: true})
for i := range bullets {
b := &bullets[i]
b.Y -= 1
}
case <-enemyMoveTicker.C:
// add random enemies at random locations
enemyNum := random.Intn(3)
for i := 0; i < enemyNum; i++ {
enemies = append(enemies, g.Enemy{X: random.Intn(Width - 1), Y: 0, IsAlive: true})
}
for i := range enemies {
e := &enemies[i]
e.Y += 1
}
case key := <-PressedKey:
if key == keyboard.KeyEsc {
return
}
switch key {
case keyboard.KeyArrowLeft:
if player.X > 0 {
player.X -= 1
}
case keyboard.KeyArrowRight:
if player.X < Width-1 {
player.X += 1
}
case keyboard.KeyArrowUp:
if player.Y > 0 {
player.Y -= 1
}
case keyboard.KeyArrowDown:
if player.Y < Height-1 {
player.Y += 1
}
}
}
}
}