-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
92 lines (66 loc) · 1.95 KB
/
engine.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
package main
import (
"fmt"
"time"
"github.com/gdamore/tcell/v2"
)
/*
Represents the game engine which is repsonsible to update all the game objects
and render them to the screen.
*/
type Engine struct {
Screen tcell.Screen
Style tcell.Style
GameObjects [] GameObject
Score int
Player *Player
IsGameOver bool
}
// Updates the state of all game objects.
func update(engine *Engine, screenWidth int, screenHeight int){
// handle collisions
for i := range engine.GameObjects{
current := engine.GameObjects[i]
for j := range engine.GameObjects{
if (i==j){continue}
next := engine.GameObjects[j]
current.CheckCollision(next)
}
}
for _, gameObject := range engine.GameObjects{
gameObject.Update(*engine)
gameObject.CheckEdges(screenWidth,screenHeight)
gameObject.Display(engine)
}
engine.receiveMessage()
}
func (engine Engine) receiveMessage() {
select {
case msg := <-GameStateMessages:
switch msg {
case "game-over":
heightOffset := 4
width,height := engine.Screen.Size()
renderGameObject(engine.Screen, width/2, height/2 + heightOffset, 500, height/2 + heightOffset, engine.Style, "GAME OVER!")
renderGameObject(engine.Screen, width/2 -3, (height/2) + heightOffset*2, 1000, (height/2) + heightOffset*2, engine.Style, "PRESS ESC TO EXIT")
case "ball-droped":
engine.Player.Life-=1
renderGameObject(engine.Screen, 1, 4, 10, 4, engine.Style, fmt.Sprintf("Life: %d", engine.Player.Life))
default:
}
default:
}
}
//Go routine which runs the game
func (engine *Engine) Run(){
screen := engine.Screen
width,height := screen.Size()
for {
screen.Clear()
renderGameObject(engine.Screen, 1, 1, 10, 1, engine.Style, fmt.Sprintf("Score: %d", engine.Player.Score))
renderGameObject(engine.Screen, 1, 4, 10, 4, engine.Style, fmt.Sprintf("Life: %d", engine.Player.Life))
update(engine,width,height)
time.Sleep(20 * time.Millisecond)
screen.Show()
}
}