/
main.go
119 lines (103 loc) · 1.95 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
package main
import (
"math/rand"
"time"
)
type position [2]int
type direction int
const (
stay direction = iota
up
down
right
left
)
func main() {
game := newGame()
go game.listenForKeyPress()
game.beforeGame()
game.NewGameScreen()
N, footCounter, step := 0, 0, false
for {
if game.started && game.paused {
game.pause()
continue
}
if !game.started {
continue
}
if footCounter == 8 {
footCounter = 0
step = !step
}
game.man.move(game.maxX, game.maxY, step)
for key, piu := range game.man.piu {
piu.move()
needDelete := piu.checkCactus(game.cactus)
if needDelete || piu.position[0] > game.maxX {
if len(game.man.piu) > key {
game.man.delPiu(key)
}
}
}
for _, cactus := range game.cactus {
if game.man.checkAssOnCactus(cactus) {
game.draw(true)
time.Sleep(time.Millisecond * 1000)
game.over()
}
cactus.move()
if cactus.checkHide() {
game.cactus = game.cactus[1:]
game.score++
game.checkScore()
}
}
if N > 10 && rand.Intn(10) == 9 {
height := rand.Intn(4)
switch height {
case 3:
game.newCactus(cactus3Stages, height, 3)
case 2:
game.newCactus(cactus2Stages, height, 3)
case 1:
game.newCactus(cactus1Stages, height, 2)
}
N = 0
}
game.draw(false)
N++
footCounter++
}
}
func newGame() *game {
maxX, maxY := getSize()
game := &game{
started: false,
paused: true,
score: 0,
maxX: maxX,
maxY: maxY,
speed: startSpeed,
man: newMan(position{10, maxY}),
cactus: []*cactus{},
star: newStars(maxX, int(float32(maxY)/1.3)),
}
return game
}
func newMan(pos position) *man {
return &man{
position: pos,
direction: stay,
foots: steps[1],
}
}
func newStars(maxX int, maxY int) [starsCount]star {
var stars []star
for i := 0; i <= starsCount; i++ {
x := rand.Intn(maxX)
y := rand.Intn(maxY)
stars = append(stars, star{position: position{x, y}})
}
return [starsCount]star(stars)
}