-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (78 loc) · 1.63 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
package main
import (
"github.com/gopxl/pixel"
"github.com/gopxl/pixel/pixelgl"
"golang.org/x/image/colornames"
"math/rand"
"tetris/tetris"
"time"
)
const (
winWidth = 390
winHeight = 600
blockSize = 15
speed = 15
)
func main() {
pixelgl.Run(run)
}
func run() {
rand.Seed(time.Now().UnixNano())
cfg := pixelgl.WindowConfig{
Title: "Tetris",
Bounds: pixel.R(0, 0, winWidth, winHeight),
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
game := tetris.NewTetris(win, winWidth, winHeight)
last := time.Now()
for !win.Closed() {
dt := time.Since(last).Seconds()
last = time.Now()
win.Clear(colornames.Skyblue)
game.Archive.Draw()
if game.Current.Empty() {
game.Current.Make()
game.Current.Draw()
win.Update()
time.Sleep(time.Millisecond * 16)
continue
}
game.Current.Down(0)
// Handle user input
if win.Pressed(pixelgl.KeyLeft) {
// Move tetromino left
win.Clear(colornames.Skyblue)
game.Archive.Draw()
game.Current.Left()
}
if win.Pressed(pixelgl.KeyRight) {
// Move tetromino right
win.Clear(colornames.Skyblue)
game.Archive.Draw()
game.Current.Right()
}
if win.Pressed(pixelgl.KeyDown) {
// Move tetromino down
win.Clear(colornames.Skyblue)
game.Archive.Draw()
game.Current.Down(int(dt))
}
if win.Pressed(pixelgl.KeyUp) {
// Rotate tetromino
win.Clear(colornames.Skyblue)
game.Archive.Draw()
game.Current.Rotate()
game.Current.Draw()
}
ele := game.Archive.CanEliminates()
if len(ele) > 0 {
game.Archive.Eliminates(ele)
game.Current.Draw()
}
win.Update()
time.Sleep(time.Millisecond * 50)
}
}