refactor: bouncing ball exercises

This commit is contained in:
Inanc Gumus
2019-03-19 23:27:26 +03:00
parent d2648d4622
commit 33cfb40bf8
3 changed files with 29 additions and 29 deletions

View File

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

View File

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

View File

@ -22,12 +22,14 @@ func main() {
maxFrames = 1200
speed = time.Second / 20
initVx, initVy = 5, 2
)
var (
px, py int // ball position
ppx, ppy int // previous ball position
vx, vy = 5, 2 // velocities
px, py int // ball position
ppx, ppy int // previous ball position
vx, vy = initVx, initVy // velocities
cell rune // current cell (for caching)
)
@ -57,10 +59,10 @@ func main() {
py += vy
// when the ball hits a border reverse its direction
if px <= 0 || px >= width-1 {
if px <= 0 || px >= width-initVx {
vx *= -1
}
if py <= 0 || py >= height-1 {
if py <= 0 || py >= height-initVy {
vy *= -1
}