refactor: bouncing ball exercises
This commit is contained in:
@ -22,12 +22,14 @@ func main() {
|
|||||||
|
|
||||||
maxFrames = 1200
|
maxFrames = 1200
|
||||||
speed = time.Second / 20
|
speed = time.Second / 20
|
||||||
|
|
||||||
|
initVx, initVy = 5, 2
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
px, py int // ball position
|
px, py int // ball position
|
||||||
ppx, ppy int // previous ball position
|
ppx, ppy int // previous ball position
|
||||||
vx, vy = 5, 2 // velocities
|
vx, vy = initVx, initVy // velocities
|
||||||
|
|
||||||
cell rune // current cell (for caching)
|
cell rune // current cell (for caching)
|
||||||
)
|
)
|
||||||
@ -60,21 +62,18 @@ func main() {
|
|||||||
py += vy
|
py += vy
|
||||||
|
|
||||||
// when the ball hits a border reverse its direction
|
// when the ball hits a border reverse its direction
|
||||||
if px <= 0 || px >= width-1 {
|
if px <= 0 || px >= width-initVx {
|
||||||
vx *= -1
|
vx *= -1
|
||||||
}
|
}
|
||||||
if py <= 0 || py >= height-1 {
|
if py <= 0 || py >= height-initVy {
|
||||||
vy *= -1
|
vy *= -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// check whether the ball goes beyond the borders
|
|
||||||
if px < width && py < height {
|
|
||||||
// remove the previous ball and put the new ball
|
// remove the previous ball and put the new ball
|
||||||
board[px][py], board[ppx][ppy] = true, false
|
board[px][py], board[ppx][ppy] = true, false
|
||||||
|
|
||||||
// save the previous positions
|
// save the previous positions
|
||||||
ppx, ppy = px, py
|
ppx, ppy = px, py
|
||||||
}
|
|
||||||
|
|
||||||
// rewind the buffer (allow appending from the beginning)
|
// rewind the buffer (allow appending from the beginning)
|
||||||
buf = buf[:0]
|
buf = buf[:0]
|
||||||
|
@ -42,12 +42,14 @@ func main() {
|
|||||||
|
|
||||||
maxFrames = 1200
|
maxFrames = 1200
|
||||||
speed = time.Second / 20
|
speed = time.Second / 20
|
||||||
|
|
||||||
|
initVx, initVy = 5, 2
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
px, py int // ball position
|
px, py int // ball position
|
||||||
ppx, ppy int // previous ball position
|
ppx, ppy int // previous ball position
|
||||||
vx, vy = 5, 2 // velocities
|
vx, vy = initVx, initVy // velocities
|
||||||
|
|
||||||
cell rune // current cell (for caching)
|
cell rune // current cell (for caching)
|
||||||
)
|
)
|
||||||
@ -80,21 +82,18 @@ func main() {
|
|||||||
py += vy
|
py += vy
|
||||||
|
|
||||||
// when the ball hits a border reverse its direction
|
// when the ball hits a border reverse its direction
|
||||||
if px <= 0 || px >= width-1 {
|
if px <= 0 || px >= width-initVx {
|
||||||
vx *= -1
|
vx *= -1
|
||||||
}
|
}
|
||||||
if py <= 0 || py >= height-1 {
|
if py <= 0 || py >= height-initVy {
|
||||||
vy *= -1
|
vy *= -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// check whether the ball goes beyond the borders
|
|
||||||
if px < width && py < height {
|
|
||||||
// remove the previous ball and put the new ball
|
// remove the previous ball and put the new ball
|
||||||
board[px][py], board[ppx][ppy] = true, false
|
board[px][py], board[ppx][ppy] = true, false
|
||||||
|
|
||||||
// save the previous positions
|
// save the previous positions
|
||||||
ppx, ppy = px, py
|
ppx, ppy = px, py
|
||||||
}
|
|
||||||
|
|
||||||
// rewind the buffer (allow appending from the beginning)
|
// rewind the buffer (allow appending from the beginning)
|
||||||
buf = buf[:0]
|
buf = buf[:0]
|
||||||
|
@ -22,12 +22,14 @@ func main() {
|
|||||||
|
|
||||||
maxFrames = 1200
|
maxFrames = 1200
|
||||||
speed = time.Second / 20
|
speed = time.Second / 20
|
||||||
|
|
||||||
|
initVx, initVy = 5, 2
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
px, py int // ball position
|
px, py int // ball position
|
||||||
ppx, ppy int // previous ball position
|
ppx, ppy int // previous ball position
|
||||||
vx, vy = 5, 2 // velocities
|
vx, vy = initVx, initVy // velocities
|
||||||
|
|
||||||
cell rune // current cell (for caching)
|
cell rune // current cell (for caching)
|
||||||
)
|
)
|
||||||
@ -57,10 +59,10 @@ func main() {
|
|||||||
py += vy
|
py += vy
|
||||||
|
|
||||||
// when the ball hits a border reverse its direction
|
// when the ball hits a border reverse its direction
|
||||||
if px <= 0 || px >= width-1 {
|
if px <= 0 || px >= width-initVx {
|
||||||
vx *= -1
|
vx *= -1
|
||||||
}
|
}
|
||||||
if py <= 0 || py >= height-1 {
|
if py <= 0 || py >= height-initVy {
|
||||||
vy *= -1
|
vy *= -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user