-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (107 loc) · 2.44 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
package main
import (
"log"
"github.com/gdamore/tcell/v2"
)
/*
Polls for user input events and blocks the main thread while waiting
*/
func getUserInput(screen tcell.Screen){
for {
event := screen.PollEvent() // waits for events for arrive and blocks the main thread
switch event := event.(type){
case *tcell.EventResize:
screen.Sync()
case *tcell.EventKey:
key := event.Key()
if key == tcell.KeyEscape || key == tcell.KeyCtrlC {
return
}else if (key == tcell.KeyLeft){
PaddleMoveMessages <- "paddle-move-left"
}else if (key == tcell.KeyRight){
PaddleMoveMessages <- "paddle-move-right"
}
}
}
}
func quit(screen tcell.Screen){
// You have to catch panics in a defer, clean up, and
// re-raise them - otherwise your application can
// die without leaving any diagnostic trace.
maybePanic := recover()
screen.Fini() //closes screen and releases resources
if maybePanic != nil {
panic(maybePanic)
}
}
func main(){
screen, err := tcell.NewScreen()
if err !=nil {
log.Fatalf("%+v",err)
}
initError:= screen.Init()
width,height := screen.Size()
if initError != nil {
log.Fatalf("%+v",initError)
}
//Set default text style
defaultStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
screen.SetStyle(defaultStyle)
defer quit(screen)
ball :=Ball{
X:width/2,
Y:height/2,
SpeedX: 1,
SpeedY: 1,
Active:true,
Screen: screen,
}
paddle:=Paddle{
X: width/2,
Y: height - 1,
SpeedX: 10,
Width: 20,
Height: 1,
Active:true,
}
var gameObjects []GameObject
gameObjects = append(gameObjects,&ball,&paddle)
blockWidth := 8
blockHeight :=1
blockOffset := 1
initYPos:=10
totalColumns:= (width / (blockWidth + blockOffset)) + blockWidth
for i := range totalColumns{
xPos :=0
if (i > 0){
xPos = ( (blockWidth + blockOffset) * i)
}
for j := range 7 {
yPos:=initYPos
if ( j > 0){
yPos = initYPos+( (blockHeight + blockOffset) * j)
}
block :=Block{
Width: blockWidth,
Height: blockHeight,
X:xPos,
Y:yPos,
Style: tcell.StyleDefault.Background(tcell.ColorOrange.TrueColor()).Foreground(tcell.ColorOrange.TrueColor()),
Active:true,
}
gameObjects = append(gameObjects,&block)
}
}
player := Player{
Life: 3,
Score: 0,
}
engine :=Engine{
Screen: screen,
Style: defaultStyle,
GameObjects: gameObjects,
Player: &player,
}
go engine.Run()
getUserInput(screen)
}