-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.go
47 lines (38 loc) · 851 Bytes
/
paddle.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
package main
import (
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
paddleMargin float64 = 100
paddleWidth = screenWidth / 50
paddleHeight = screenHeight / 4
paddleSpeed = 15
)
type Paddle struct {
Rect
Score int
}
func (p *Paddle) Draw(screen *ebiten.Image, color color.Color) {
ebitenutil.DrawRect(screen, p.X, p.Y, p.W, p.H, color)
}
func (p *Paddle) Move(dy float64) {
if inScreenBounds(p.X, p.Y+dy, p.W, p.H) {
p.Y += dy
return
}
if dy > 0 {
p.Y = screenHeight - paddleHeight
} else {
p.Y = 0
}
}
func NewPaddle(x float64) *Paddle {
return &Paddle{Rect{x, screenHeight/2 - paddleHeight/2, paddleWidth, paddleHeight}, 0}
}
func movePaddle(key ebiten.Key, p *Paddle, dy float64) {
if ebiten.IsKeyPressed(key) {
p.Move(dy)
}
}