/
main.go
133 lines (107 loc) · 3.04 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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"time"
"github.com/taylorza/go-gfx/pkg/gfx"
)
type myapp struct {
paddleTexture *gfx.Texture
ballTexture *gfx.Texture
px, py, ps float64
bx, by, dx, dy, bs float64
score int
}
func (app *myapp) Load() {
app.ballTexture, _ = gfx.LoadTexture("../assets/ballBlue.png")
app.paddleTexture, _ = gfx.LoadTexture("../assets/paddleRed.png")
app.px = (gfx.Width() - float64(app.paddleTexture.W)) / 2
app.py = gfx.Height() - float64(app.paddleTexture.H) - 80
app.ps = 500
app.bx = (gfx.Width() - float64(app.ballTexture.W)) / 2
app.by = (gfx.Height() - float64(app.ballTexture.H)) / 2
app.dx = 1
app.dy = 1
app.bs = 300
}
func (app *myapp) Update(delta float64) {
gfx.Clear(gfx.Cyan)
gfx.DrawTexture(app.bx, app.by, app.ballTexture)
gfx.DrawTexture(app.px, app.py, app.paddleTexture)
gfx.DrawRect(app.bx, app.by, float64(app.ballTexture.W), float64(app.ballTexture.H), gfx.Red)
gfx.DrawRect(app.px, app.py, float64(app.paddleTexture.W), float64(app.paddleTexture.H), gfx.Red)
app.bx += app.dx * app.bs * delta
app.by += app.dy * app.bs * delta
if app.bx <= 0 || app.bx >= gfx.Width()-float64(app.ballTexture.W) {
app.bx -= app.dx * app.bs * delta
app.dx = -app.dx
}
if gfx.KeyPressed(gfx.KeyLeft) && app.px > 0 {
app.px -= app.ps * delta
}
if gfx.KeyPressed(gfx.KeyRight) && app.px < gfx.Width()-float64(app.paddleTexture.W) {
app.px += app.ps * delta
}
if app.by <= 0 || app.by >= gfx.Height()-float64(app.ballTexture.H) {
app.by -= app.dy * app.bs * delta
app.dy = -app.dy
}
// simple collision check between the paddle and the ball
rcBall := rc(app.bx, app.by, app.ballTexture.W, app.ballTexture.H).shrink(2)
rcPaddle := rc(app.px, app.py, app.paddleTexture.W, app.paddleTexture.H).shrink(3)
if app.dy > 0 && rcBall.intersect(rcPaddle) {
app.by -= app.dy * app.bs * delta
app.bx -= app.dx * app.bs * delta
app.dy = -app.dy
app.score++
}
gfx.DrawString(gfx.Font8x16, 8, 8, fmt.Sprintf("Score %v", app.score), gfx.Transparent, gfx.Black)
}
func (app *myapp) Unload() {
}
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
memprofile = flag.String("memprofile", "", "write memory profile to file")
)
func main() {
defer pprof.StopCPUProfile()
flag.Parse()
go func() {
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
time.Sleep(20 * time.Second)
}()
if gfx.Init("GFX Paddle Game", 10, 10, 800, 600, 1, 1) {
gfx.Run(&myapp{})
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
}
type rect struct {
x1, y1, x2, y2 float64
}
func rc(x, y float64, w, h int) rect {
return rect{x1: x, y1: y, x2: x + float64(w), y2: y + float64(h)}
}
func (r rect) shrink(d float64) rect {
d /= 2
return rect{r.x1 + d, r.y1 + d, r.x2 - d, r.y2 - d}
}
func (r rect) intersect(o rect) bool {
return r.x1 <= o.x2 && r.x2 >= o.x1 &&
r.y1 <= o.y2 && r.y2 >= o.y1
}