-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (148 loc) · 3.99 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
package main
import (
"encoding/json"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
var (
clients = make([]*websocket.Conn, 0)
clientsMu sync.Mutex
canvasHeight float64 = 600
canvasWidth float64 = 800
paddleSpeed float64 = 10
paddleHeight float64 = 100
paddleWidth float64 = 10
gameState = map[string]float64{
"leftPaddleY": 300,
"rightPaddleY": 300,
"ballX": 300,
"ballY": 300,
"ballSpeedX": 2,
"ballSpeedY": 2,
}
playerCounter = 0
playerMap = make(map[*websocket.Conn]int)
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Allow cross-origin requests for testing
},
}
func gameLoop() {
for {
// update ball position
gameState["ballX"] += gameState["ballSpeedX"]
gameState["ballY"] += gameState["ballSpeedY"]
if gameState["ballY"] <= 0 || gameState["ballY"] >= canvasHeight {
gameState["ballSpeedY"] = -gameState["ballSpeedY"]
}
if gameState["ballX"] <= paddleWidth {
if gameState["ballY"] > gameState["leftPaddleY"] && gameState["ballY"] < gameState["leftPaddleY"]+paddleHeight {
// Ball hit the left paddle, bounce it
gameState["ballSpeedX"] = -gameState["ballSpeedX"]
} else {
// Ball missed the left paddle, player 2 wins
gameState["winner"] = 2
resetGameState()
}
} else if gameState["ballX"] >= canvasWidth-paddleWidth {
if gameState["ballY"] > gameState["rightPaddleY"] && gameState["ballY"] < gameState["rightPaddleY"]+paddleHeight {
// Ball hit the right paddle, bounce it
gameState["ballSpeedX"] = -gameState["ballSpeedX"]
} else {
// Ball missed the right paddle, player 1 wins
gameState["winner"] = 1
resetGameState()
}
}
// Broadcast updated gameState
gameStateJSON, _ := json.Marshal(gameState)
clientsMu.Lock()
for _, client := range clients {
if err := client.WriteMessage(websocket.TextMessage, gameStateJSON); err != nil {
// Handle error
}
}
clientsMu.Unlock()
time.Sleep(time.Millisecond * 20) // Roughly 60 updates per second
}
}
func resetGameState() {
gameState["leftPaddleY"] = 300
gameState["rightPaddleY"] = 300
gameState["ballX"] = 300
gameState["ballY"] = 300
gameState["ballSpeedX"] = 2
gameState["ballSpeedY"] = 2
delete(gameState, "winner")
}
func handler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
// Handle error
return
}
defer conn.Close()
clientsMu.Lock()
clients = append(clients, conn)
playerCounter++
playerMap[conn] = playerCounter
clientsMu.Unlock()
defer func() {
clientsMu.Lock()
for i, c := range clients {
if c == conn {
clients = append(clients[:i], clients[i+1:]...)
delete(playerMap, conn)
break
}
}
clientsMu.Unlock()
}()
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
// Handle error
return
}
var data map[string]string
if err := json.Unmarshal(p, &data); err != nil {
// Handle JSON decoding error
return
}
playerID := playerMap[conn]
if data["action"] == "move" {
direction := data["direction"]
if playerID == 1 {
if direction == "up" && gameState["leftPaddleY"] > 0 {
gameState["leftPaddleY"] -= paddleSpeed
} else if direction == "down" && gameState["leftPaddleY"] < canvasHeight-paddleHeight {
gameState["leftPaddleY"] += paddleSpeed
}
} else if playerID == 2 {
if direction == "up" && gameState["rightPaddleY"] > 0 {
gameState["rightPaddleY"] -= paddleSpeed
} else if direction == "down" && gameState["rightPaddleY"] < canvasHeight-paddleHeight {
gameState["rightPaddleY"] += paddleSpeed
}
}
}
gameStateJSON, _ := json.Marshal(gameState)
clientsMu.Lock()
for _, client := range clients {
if err := client.WriteMessage(messageType, gameStateJSON); err != nil {
// Handle error
}
}
clientsMu.Unlock()
}
}
func main() {
go gameLoop()
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}