|
@@ -3,29 +3,13 @@ import random
|
|
|
import sdl2
|
|
import sdl2
|
|
|
import sdl2/ttf
|
|
import sdl2/ttf
|
|
|
|
|
|
|
|
-const
|
|
|
|
|
- WindowWidth = 640
|
|
|
|
|
- WindowHeight = 480
|
|
|
|
|
-
|
|
|
|
|
- PaddleWidth = 16
|
|
|
|
|
- PaddleHeight = 64
|
|
|
|
|
- PaddleSpeed = 400.0
|
|
|
|
|
-
|
|
|
|
|
- BallRadius = 8
|
|
|
|
|
-
|
|
|
|
|
- MaxBallComponentSpeed = 1000'f32
|
|
|
|
|
-
|
|
|
|
|
- TextWidth = 128
|
|
|
|
|
- TextHeight = 64
|
|
|
|
|
|
|
+import ./cons
|
|
|
|
|
+import ./ball/ball
|
|
|
|
|
|
|
|
type
|
|
type
|
|
|
Paddle = ref object
|
|
Paddle = ref object
|
|
|
x, y: float32
|
|
x, y: float32
|
|
|
|
|
|
|
|
- Ball = ref object
|
|
|
|
|
- x, y: float32
|
|
|
|
|
- vx, vy: float32
|
|
|
|
|
-
|
|
|
|
|
Input {.pure.} = enum
|
|
Input {.pure.} = enum
|
|
|
Up,
|
|
Up,
|
|
|
Down,
|
|
Down,
|
|
@@ -46,13 +30,7 @@ proc newPaddle(x: float32): Paddle =
|
|
|
y: (WindowHeight + PaddleHeight) / 2,
|
|
y: (WindowHeight + PaddleHeight) / 2,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-proc newBall(): Ball =
|
|
|
|
|
- Ball(
|
|
|
|
|
- x: WindowWidth / 2 - BallRadius,
|
|
|
|
|
- y: WindowHeight / 2 - BallRadius,
|
|
|
|
|
- vx: (rand(2.0) - 1) * 100,
|
|
|
|
|
- vy: (rand(2.0) - 1) * 300
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
func collision(ball: Ball, paddle: Paddle): bool =
|
|
func collision(ball: Ball, paddle: Paddle): bool =
|
|
|
return not (
|
|
return not (
|
|
@@ -99,12 +77,12 @@ proc speedup(v: var float32) =
|
|
|
v = min(v, MaxBallComponentSpeed)
|
|
v = min(v, MaxBallComponentSpeed)
|
|
|
|
|
|
|
|
proc updateBall(g: Game, dt: float32) =
|
|
proc updateBall(g: Game, dt: float32) =
|
|
|
- g.ball.x += g.ball.vx * dt
|
|
|
|
|
- g.ball.y += g.ball.vy * dt
|
|
|
|
|
|
|
+ g.ball.dtx(dt)
|
|
|
|
|
+ g.ball.dty(dt)
|
|
|
|
|
|
|
|
# bounce on upper and lower borders, add speedup on bounce
|
|
# bounce on upper and lower borders, add speedup on bounce
|
|
|
if g.ball.y < 0:
|
|
if g.ball.y < 0:
|
|
|
- g.ball.y = 0
|
|
|
|
|
|
|
+ g.ball.reset_y()
|
|
|
bounce(g.ball.vy)
|
|
bounce(g.ball.vy)
|
|
|
speedup(g.ball.vx)
|
|
speedup(g.ball.vx)
|
|
|
elif g.ball.y + 2 * BallRadius > WindowHeight:
|
|
elif g.ball.y + 2 * BallRadius > WindowHeight:
|
|
@@ -125,12 +103,12 @@ proc updateBall(g: Game, dt: float32) =
|
|
|
# opponent scored
|
|
# opponent scored
|
|
|
if g.ball.x + 2 * BallRadius < 0:
|
|
if g.ball.x + 2 * BallRadius < 0:
|
|
|
inc g.scores.opponent
|
|
inc g.scores.opponent
|
|
|
- g.ball = newBall()
|
|
|
|
|
|
|
+ g.ball = NewBall()
|
|
|
|
|
|
|
|
# player scored
|
|
# player scored
|
|
|
elif g.ball.x > WindowWidth:
|
|
elif g.ball.x > WindowWidth:
|
|
|
inc g.scores.player
|
|
inc g.scores.player
|
|
|
- g.ball = newBall()
|
|
|
|
|
|
|
+ g.ball = NewBall()
|
|
|
|
|
|
|
|
|
|
|
|
|
proc draw(renderer: RendererPtr, paddle: Paddle) =
|
|
proc draw(renderer: RendererPtr, paddle: Paddle) =
|
|
@@ -174,7 +152,7 @@ proc newGame(): Game =
|
|
|
running: true,
|
|
running: true,
|
|
|
player: newPaddle(PaddleWidth),
|
|
player: newPaddle(PaddleWidth),
|
|
|
opponent: newPaddle(WindowWidth - 2 * PaddleWidth),
|
|
opponent: newPaddle(WindowWidth - 2 * PaddleWidth),
|
|
|
- ball: newBall(),
|
|
|
|
|
|
|
+ ball: NewBall(),
|
|
|
scores: (0'u, 0'u)
|
|
scores: (0'u, 0'u)
|
|
|
)
|
|
)
|
|
|
|
|
|