-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (161 loc) · 4.35 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
package main
import (
"fmt"
"os"
"time"
"github.com/gdamore/tcell/v2"
)
const paddleHeight int = 6
const paddleAsset rune = 0x2588
const ballAsset rune = 0x25CF
var screenH, screenW int
type Paddle struct {
x, y int
}
type Ball struct {
x, y, velX, velY int
}
func renderChar(screen tcell.Screen, x, y, w, h int, ch rune, style tcell.Style) {
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
screen.SetContent(x+i, y+j, ch, nil, style)
}
}
}
func initScreen() tcell.Screen {
screen, err := tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to initialize screen: %v\n", err)
}
err = screen.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to initialize screen: %v\n", err)
}
return screen
}
func main() {
// x-axis right, y-axis down
screen := initScreen()
screenW, screenH = screen.Size()
var paddleLeft, paddleRight *Paddle = &Paddle{}, &Paddle{}
var ball *Ball = &Ball{}
initState(screen, paddleLeft, paddleRight, ball)
// start the background process for getting the user input
// if user presses any key, its detail is sent through a channel to the
// foreground process
keyPressedChan := initPlayerInputProcess(screen)
for {
// drawing frames
renderState(screen, paddleLeft, paddleRight, ball)
time.Sleep(50 * time.Millisecond)
// game update logic - user input process and ball move logic
keyPressed := getKeyPressed(keyPressedChan)
handleKeyPress(screen, keyPressed, paddleLeft, paddleRight)
// ball movements logic
updateBallState(ball, paddleLeft, paddleRight)
if isGameOver(ball, paddleLeft, paddleRight) {
screen.Fini()
os.Exit(0)
break
}
}
}
func isGameOver(ball *Ball, paddleLeft, paddleRight *Paddle) bool {
if ball.x < paddleLeft.x || ball.x > paddleRight.x {
return true
}
return false
}
func getKeyPressed(keyPressedChan chan string) string {
keyPressed := ""
select {
case keyPressed = <-keyPressedChan:
default:
keyPressed = ""
}
return keyPressed
}
func handleKeyPress(screen tcell.Screen, keyPressed string, paddleLeft, paddleRight *Paddle) {
if keyPressed == "q" {
screen.Fini()
os.Exit(0)
return
} else if keyPressed == "up" {
if paddleRight.y > 0 {
paddleRight.y--
}
} else if keyPressed == "down" {
if paddleRight.y < screenH-paddleHeight {
paddleRight.y++
}
} else if keyPressed == "w" {
if paddleLeft.y > 0 {
paddleLeft.y--
}
} else if keyPressed == "s" {
if paddleLeft.y < screenH-paddleHeight {
paddleLeft.y++
}
}
}
func updateBallState(ball *Ball, paddleLeft, paddleRight *Paddle) {
ball.x += ball.velX
ball.y += ball.velY
if ball.y <= 0 || ball.y >= screenH-1 {
ball.velY = -ball.velY
}
if ball.x == paddleLeft.x+1 {
if ball.y >= paddleLeft.y-1 && ball.y <= paddleLeft.y+paddleHeight {
ball.velX = -ball.velX
}
} else if ball.x == paddleRight.x-1 {
if ball.y >= paddleRight.y-1 && ball.y <= paddleRight.y+paddleHeight {
ball.velX = -ball.velX
}
}
}
func initState(screen tcell.Screen, paddleLeft, paddleRight *Paddle, ball *Ball) {
paddleLeft.y = screenH/2 - paddleHeight/2
paddleRight.y = screenH/2 - paddleHeight/2
paddleLeft.x = 0
paddleRight.x = screenW - 1
ball.x = screenW / 2
ball.y = screenH / 2
ball.velX = -1
ball.velY = -1
renderState(screen, paddleLeft, paddleRight, ball)
}
func renderState(screen tcell.Screen, paddleLeft, paddleRight *Paddle, ball *Ball) {
screen.Clear()
renderChar(screen, paddleLeft.x, paddleLeft.y, 1, paddleHeight, paddleAsset, tcell.StyleDefault)
renderChar(screen, paddleRight.x, paddleRight.y, 1, paddleHeight, paddleAsset, tcell.StyleDefault)
renderChar(screen, ball.x, ball.y, 1, 1, ballAsset, tcell.StyleDefault)
screen.Show()
}
// starts the process for taking user inputs in the background
// and returns the channel
func initPlayerInputProcess(screen tcell.Screen) chan string {
input := make(chan string)
keyPressed := ""
go func() {
for {
eventPoll := screen.PollEvent()
switch event := eventPoll.(type) {
case *tcell.EventKey:
if event.Key() == tcell.KeyUp {
keyPressed = "up"
} else if event.Key() == tcell.KeyDown {
keyPressed = "down"
} else if event.Rune() == 'w' {
keyPressed = "w"
} else if event.Rune() == 's' {
keyPressed = "s"
} else if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC || event.Rune() == 'q' {
keyPressed = "q"
}
input <- keyPressed
}
}
}()
return input
}