From 7f21946126a24c27a324779823a3413679f177a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 20:19:27 +0100 Subject: add pong --- pong/pong.tdy | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pong/pong.tdy (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy new file mode 100644 index 0000000..9405a5c --- /dev/null +++ b/pong/pong.tdy @@ -0,0 +1,63 @@ +blob Paddle { + x: float + y: float +} + +blob Player { + paddle: Paddle +} + +blob State { + p1: Player + p2: Player +} + +update := fn state: State { + delta := get_delta() + + if key_down("w") { + state.p1.paddle.y = state.p1.paddle.y + delta + } + if key_down("s") { + state.p1.paddle.y = state.p1.paddle.y - delta + } + + if key_down("i") { + state.p2.paddle.y = state.p2.paddle.y + delta + } + if key_down("i") { + state.p2.paddle.y = state.p2.paddle.y - delta + } +} + +draw := fn state: State { + clear() + draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, 0.2, 1.) + draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, 0.2, 1.) + next_frame() +} + +init := fn { + running := true + + state := State() + state.p1 = Player() + state.p1.paddle = Paddle() + state.p1.paddle.x = 1. + state.p1.paddle.y = 10. + + state.p2 = Player() + state.p2.paddle = Paddle() + state.p2.paddle.x = 19. + state.p2.paddle.y = 10. + + for i := 0, i < 5, i = i + 1 { + log(state.p1.paddle.x, state.p1.paddle.y) + log(state.p2.paddle.x, state.p2.paddle.y) + + update(state) + draw(state) + } +} + +init() -- cgit v1.2.1 From 6b5e59311511b1462c7a10ba1389782f640e77a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 30 Jan 2021 23:17:17 +0100 Subject: Yield --- pong/pong.tdy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 9405a5c..d27a2f5 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -51,12 +51,13 @@ init := fn { state.p2.paddle.x = 19. state.p2.paddle.y = 10. - for i := 0, i < 5, i = i + 1 { + for i := 0, i == i, i = i + 1 { log(state.p1.paddle.x, state.p1.paddle.y) log(state.p2.paddle.x, state.p2.paddle.y) update(state) draw(state) + yield } } -- cgit v1.2.1 From 43ba4b48e4f849fd7ef288a6ca27213ed7f4904d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 23:20:25 +0100 Subject: move second paddle --- pong/pong.tdy | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index d27a2f5..7088f2e 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -16,18 +16,18 @@ update := fn state: State { delta := get_delta() if key_down("w") { - state.p1.paddle.y = state.p1.paddle.y + delta + state.p1.paddle.y = state.p1.paddle.y - delta } if key_down("s") { - state.p1.paddle.y = state.p1.paddle.y - delta + state.p1.paddle.y = state.p1.paddle.y + delta } - if key_down("i") { - state.p2.paddle.y = state.p2.paddle.y + delta - } if key_down("i") { state.p2.paddle.y = state.p2.paddle.y - delta } + if key_down("k") { + state.p2.paddle.y = state.p2.paddle.y + delta + } } draw := fn state: State { @@ -52,9 +52,6 @@ init := fn { state.p2.paddle.y = 10. for i := 0, i == i, i = i + 1 { - log(state.p1.paddle.x, state.p1.paddle.y) - log(state.p2.paddle.x, state.p2.paddle.y) - update(state) draw(state) yield -- cgit v1.2.1 From 349055ef2533bf3268f37d3c04e5adb84814f50a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 30 Jan 2021 23:24:05 +0100 Subject: remove tokio from pong --- pong/pong.tdy | 1 - 1 file changed, 1 deletion(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 7088f2e..24886c8 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -34,7 +34,6 @@ draw := fn state: State { clear() draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, 0.2, 1.) draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, 0.2, 1.) - next_frame() } init := fn { -- cgit v1.2.1 From 40b9ec3f0f1273440d4405b531cd3290dd259b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 09:55:09 +0100 Subject: Add a ball, that kinda moves --- pong/pong.tdy | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 4 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 24886c8..e1eb210 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -3,6 +3,14 @@ blob Paddle { y: float } +blob Ball { + x: float + y: float + + vx: float + vy: float +} + blob Player { paddle: Paddle } @@ -10,36 +18,89 @@ blob Player { blob State { p1: Player p2: Player + ball: Ball +} + +ball_speed := 2.0 +SPEED := 10.0 + +abs := fn a: float -> float { + if a < 0.0 { + ret -a + } + ret a +} + +and := fn a: bool, b: bool -> bool { + if a { + ret b + } + ret false +} + +or := fn a: bool, b: bool -> bool { + if a { + ret true + } + ret b +} + +rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: float, bw: float, bh: float -> bool { + dx := abs(ax - bx) + (-aw - bw) / 2. + dy := abs(ay - by) + (-ah - bh) / 2. + ret and(dx < 0., dy < 0.) +} + +ball_and_paddle_check := fn pad: Paddle, ball: Ball { + if rect_overlap(ball.x, ball.y, 0.2, 0.2, pad.x, pad.y, 0.2, 1.) { + ball.vx = -ball.vx + } } update := fn state: State { delta := get_delta() + speed := delta * SPEED if key_down("w") { - state.p1.paddle.y = state.p1.paddle.y - delta + state.p1.paddle.y = state.p1.paddle.y - speed } if key_down("s") { - state.p1.paddle.y = state.p1.paddle.y + delta + state.p1.paddle.y = state.p1.paddle.y + speed } if key_down("i") { - state.p2.paddle.y = state.p2.paddle.y - delta + state.p2.paddle.y = state.p2.paddle.y - speed } if key_down("k") { - state.p2.paddle.y = state.p2.paddle.y + delta + state.p2.paddle.y = state.p2.paddle.y + speed } + + state.ball.x = state.ball.x + delta * state.ball.vx + state.ball.y = state.ball.y + delta * state.ball.vy + + + ball_and_paddle_check(state.p1.paddle, state.ball) + ball_and_paddle_check(state.p2.paddle, state.ball) } draw := fn state: State { clear() draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, 0.2, 1.) draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, 0.2, 1.) + + draw_rectangle(state.ball.x, state.ball.y, 0.2, 0.2) } init := fn { running := true state := State() + state.ball = Ball() + state.ball.x = 10.0 + state.ball.y = 10.0 + state.ball.vx = 1.0 + state.ball.vy = 0.0 + state.p1 = Player() state.p1.paddle = Paddle() state.p1.paddle.x = 1. -- cgit v1.2.1 From a23e8c074efdde39ba0e284cc27f1aa41b4efce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:04:41 +0100 Subject: Add correct collisions --- pong/pong.tdy | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index e1eb210..9778dfc 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -46,8 +46,14 @@ or := fn a: bool, b: bool -> bool { } rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: float, bw: float, bh: float -> bool { - dx := abs(ax - bx) + (-aw - bw) / 2. - dy := abs(ay - by) + (-ah - bh) / 2. + acx := ax + aw / 2. + acy := ay + ah / 2. + + bcx := bx + bw / 2. + bcy := by + bh / 2. + + dx := abs(acx - bcx) + (-aw - bw) / 2. + dy := abs(acy - bcy) + (-ah - bh) / 2. ret and(dx < 0., dy < 0.) } @@ -85,6 +91,7 @@ update := fn state: State { draw := fn state: State { clear() + draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, 0.2, 1.) draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, 0.2, 1.) -- cgit v1.2.1 From ef62c52f4ef990c5f51dd3a3d38c90757890b898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:14:22 +0100 Subject: Fix edge case --- pong/pong.tdy | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 9778dfc..cb861f9 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -45,6 +45,13 @@ or := fn a: bool, b: bool -> bool { ret b } +sign := fn a: float -> float { + if a < 0.0 { + ret -1.0 + } + ret 1.0 +} + rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: float, bw: float, bh: float -> bool { acx := ax + aw / 2. acy := ay + ah / 2. @@ -59,7 +66,11 @@ rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: fl ball_and_paddle_check := fn pad: Paddle, ball: Ball { if rect_overlap(ball.x, ball.y, 0.2, 0.2, pad.x, pad.y, 0.2, 1.) { - ball.vx = -ball.vx + if ball.x < pad.x { + ball.vx = -abs(ball.vx) + } else { + ball.vx = abs(ball.vx) + } } } -- cgit v1.2.1 From 837492e247eb9f50ba7837ff3565157a5542e32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 31 Jan 2021 10:23:40 +0100 Subject: ball bouncy sideways --- pong/pong.tdy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index cb861f9..5d9b277 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -66,11 +66,16 @@ rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: fl ball_and_paddle_check := fn pad: Paddle, ball: Ball { if rect_overlap(ball.x, ball.y, 0.2, 0.2, pad.x, pad.y, 0.2, 1.) { + ballcy := ball.y + 0.1 + padcy := pad.y + 0.5 if ball.x < pad.x { ball.vx = -abs(ball.vx) + ball.vy = (ballcy - padcy) * 3. } else { ball.vx = abs(ball.vx) + ball.vy = (ballcy - padcy) * 3. } + log(ball.vx, ball.vy) } } @@ -116,7 +121,7 @@ init := fn { state.ball = Ball() state.ball.x = 10.0 state.ball.y = 10.0 - state.ball.vx = 1.0 + state.ball.vx = 3.0 state.ball.vy = 0.0 state.p1 = Player() -- cgit v1.2.1 From 4aa801b6b17e1c34c05d94850b9450c05f6c60d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:16:53 +0100 Subject: roof and floor --- pong/pong.tdy | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 5d9b277..fc54e36 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -100,6 +100,14 @@ update := fn state: State { state.ball.x = state.ball.x + delta * state.ball.vx state.ball.y = state.ball.y + delta * state.ball.vy + if state.ball.y < 0. { + state.ball.vy = abs(state.ball.vy) + } + + if state.ball.y > 20. - 0.2 { + state.ball.vy = -abs(state.ball.vy) + } + ball_and_paddle_check(state.p1.paddle, state.ball) ball_and_paddle_check(state.p2.paddle, state.ball) @@ -121,8 +129,8 @@ init := fn { state.ball = Ball() state.ball.x = 10.0 state.ball.y = 10.0 - state.ball.vx = 3.0 - state.ball.vy = 0.0 + state.ball.vx = 1.0 + state.ball.vy = 2.0 state.p1 = Player() state.p1.paddle = Paddle() -- cgit v1.2.1 From 62ed092e7cdb70cb97d36736605428457f948fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:17:30 +0100 Subject: remove unused --- pong/pong.tdy | 3 --- 1 file changed, 3 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index fc54e36..d79175d 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -108,7 +108,6 @@ update := fn state: State { state.ball.vy = -abs(state.ball.vy) } - ball_and_paddle_check(state.p1.paddle, state.ball) ball_and_paddle_check(state.p2.paddle, state.ball) } @@ -123,8 +122,6 @@ draw := fn state: State { } init := fn { - running := true - state := State() state.ball = Ball() state.ball.x = 10.0 -- cgit v1.2.1 From 13d2bbb024ebc52b42045b7002c396bc90ee9193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:27:07 +0100 Subject: add score and stuff --- pong/pong.tdy | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index d79175d..3d7f3f4 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -13,6 +13,7 @@ blob Ball { blob Player { paddle: Paddle + score: int } blob State { @@ -108,6 +109,22 @@ update := fn state: State { state.ball.vy = -abs(state.ball.vy) } + if state.ball.x < 0.0 { + state.ball.x = 10.0 + state.ball.y = 10.0 + state.ball.vx = -10. + state.ball.vy = 0. + state.p1.score = state.p1.score + 1 + } + + if state.ball.x > 20.0 { + state.ball.x = 10.0 + state.ball.y = 10.0 + state.ball.vx = 10. + state.ball.vy = 0. + state.p2.score = state.p2.score + 1 + } + ball_and_paddle_check(state.p1.paddle, state.ball) ball_and_paddle_check(state.p2.paddle, state.ball) } @@ -130,11 +147,13 @@ init := fn { state.ball.vy = 2.0 state.p1 = Player() + state.p1.score = 0 state.p1.paddle = Paddle() state.p1.paddle.x = 1. state.p1.paddle.y = 10. state.p2 = Player() + state.p2.score = 0 state.p2.paddle = Paddle() state.p2.paddle.x = 19. state.p2.paddle.y = 10. -- cgit v1.2.1 From 32158bc19333fbfc7affefc5ef6ff5e34c0109f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 31 Jan 2021 10:38:05 +0100 Subject: variable ball and paddle size. also add dv instead of set on paddle collision --- pong/pong.tdy | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 3d7f3f4..8727d50 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -1,8 +1,13 @@ +PADDLE_W := 0.5 +PADDLE_H := 3.0 + blob Paddle { x: float y: float } +BALL_S := 0.4 + blob Ball { x: float y: float @@ -66,17 +71,16 @@ rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: fl } ball_and_paddle_check := fn pad: Paddle, ball: Ball { - if rect_overlap(ball.x, ball.y, 0.2, 0.2, pad.x, pad.y, 0.2, 1.) { - ballcy := ball.y + 0.1 - padcy := pad.y + 0.5 + if rect_overlap(ball.x, ball.y, BALL_S, BALL_S, pad.x, pad.y, PADDLE_W, PADDLE_H) { + ballcy := ball.y + BALL_S / 2. + padcy := pad.y + PADDLE_H / 2. if ball.x < pad.x { ball.vx = -abs(ball.vx) - ball.vy = (ballcy - padcy) * 3. + ball.vy = ball.vy + (ballcy - padcy) * 3. } else { ball.vx = abs(ball.vx) - ball.vy = (ballcy - padcy) * 3. + ball.vy = ball.vy + (ballcy - padcy) * 3. } - log(ball.vx, ball.vy) } } @@ -132,10 +136,10 @@ update := fn state: State { draw := fn state: State { clear() - draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, 0.2, 1.) - draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, 0.2, 1.) + draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, PADDLE_W, PADDLE_H) + draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, PADDLE_W, PADDLE_H) - draw_rectangle(state.ball.x, state.ball.y, 0.2, 0.2) + draw_rectangle(state.ball.x, state.ball.y, BALL_S, BALL_S) } init := fn { @@ -143,8 +147,8 @@ init := fn { state.ball = Ball() state.ball.x = 10.0 state.ball.y = 10.0 - state.ball.vx = 1.0 - state.ball.vy = 2.0 + state.ball.vx = 3.0 + state.ball.vy = 0.0 state.p1 = Player() state.p1.score = 0 -- cgit v1.2.1 From 585d2e88a8066a78dcb9ebc764b3042ec970cc46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:36:56 +0100 Subject: Add paddle edges --- pong/pong.tdy | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index 8727d50..ab1459b 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -88,6 +88,8 @@ update := fn state: State { delta := get_delta() speed := delta * SPEED + paddle_height := 1.0 + if key_down("w") { state.p1.paddle.y = state.p1.paddle.y - speed } @@ -95,6 +97,14 @@ update := fn state: State { state.p1.paddle.y = state.p1.paddle.y + speed } + if state.p1.paddle.y < 0.0 { + state.p1.paddle.y = 0.0 + } + + if state.p1.paddle.y > 20.0 - paddle_height { + state.p1.paddle.y = 20.0 - paddle_height + } + if key_down("i") { state.p2.paddle.y = state.p2.paddle.y - speed } @@ -102,6 +112,15 @@ update := fn state: State { state.p2.paddle.y = state.p2.paddle.y + speed } + if state.p2.paddle.y < 0.0 { + state.p2.paddle.y = 0.0 + } + + if state.p2.paddle.y > 20.0 - paddle_height { + state.p2.paddle.y = 20.0 - paddle_height + } + + state.ball.x = state.ball.x + delta * state.ball.vx state.ball.y = state.ball.y + delta * state.ball.vy -- cgit v1.2.1 From ae2db9a668df875e5a3e76982a54e455ec080749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:48:30 +0100 Subject: SOME BLANCE --- pong/pong.tdy | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index ab1459b..e1803d6 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -7,6 +7,7 @@ blob Paddle { } BALL_S := 0.4 +BALL_STARTV := 5.0 blob Ball { x: float @@ -71,14 +72,16 @@ rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: fl } ball_and_paddle_check := fn pad: Paddle, ball: Ball { + if rect_overlap(ball.x, ball.y, BALL_S, BALL_S, pad.x, pad.y, PADDLE_W, PADDLE_H) { + SPEEDUP := 0.5 ballcy := ball.y + BALL_S / 2. padcy := pad.y + PADDLE_H / 2. if ball.x < pad.x { - ball.vx = -abs(ball.vx) + ball.vx = -abs(ball.vx) - SPEEDUP ball.vy = ball.vy + (ballcy - padcy) * 3. } else { - ball.vx = abs(ball.vx) + ball.vx = abs(ball.vx) + SPEEDUP ball.vy = ball.vy + (ballcy - padcy) * 3. } } @@ -88,8 +91,6 @@ update := fn state: State { delta := get_delta() speed := delta * SPEED - paddle_height := 1.0 - if key_down("w") { state.p1.paddle.y = state.p1.paddle.y - speed } @@ -101,8 +102,8 @@ update := fn state: State { state.p1.paddle.y = 0.0 } - if state.p1.paddle.y > 20.0 - paddle_height { - state.p1.paddle.y = 20.0 - paddle_height + if state.p1.paddle.y > 20.0 - PADDLE_H { + state.p1.paddle.y = 20.0 - PADDLE_H } if key_down("i") { @@ -116,11 +117,10 @@ update := fn state: State { state.p2.paddle.y = 0.0 } - if state.p2.paddle.y > 20.0 - paddle_height { - state.p2.paddle.y = 20.0 - paddle_height + if state.p2.paddle.y > 20.0 - PADDLE_H { + state.p2.paddle.y = 20.0 - PADDLE_H } - state.ball.x = state.ball.x + delta * state.ball.vx state.ball.y = state.ball.y + delta * state.ball.vy @@ -135,7 +135,7 @@ update := fn state: State { if state.ball.x < 0.0 { state.ball.x = 10.0 state.ball.y = 10.0 - state.ball.vx = -10. + state.ball.vx = -BALL_STARTV state.ball.vy = 0. state.p1.score = state.p1.score + 1 } @@ -143,7 +143,7 @@ update := fn state: State { if state.ball.x > 20.0 { state.ball.x = 10.0 state.ball.y = 10.0 - state.ball.vx = 10. + state.ball.vx = BALL_STARTV state.ball.vy = 0. state.p2.score = state.p2.score + 1 } @@ -166,7 +166,7 @@ init := fn { state.ball = Ball() state.ball.x = 10.0 state.ball.y = 10.0 - state.ball.vx = 3.0 + state.ball.vx = BALL_STARTV state.ball.vy = 0.0 state.p1 = Player() -- cgit v1.2.1 From 4ce6fb5d2ceead254ee7ed32d2845234a1c195fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:55:54 +0100 Subject: Add score! --- pong/pong.tdy | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'pong/pong.tdy') diff --git a/pong/pong.tdy b/pong/pong.tdy index e1803d6..15998f3 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -19,7 +19,7 @@ blob Ball { blob Player { paddle: Paddle - score: int + score: float } blob State { @@ -137,7 +137,7 @@ update := fn state: State { state.ball.y = 10.0 state.ball.vx = -BALL_STARTV state.ball.vy = 0. - state.p1.score = state.p1.score + 1 + state.p1.score = state.p1.score + 1.0 } if state.ball.x > 20.0 { @@ -145,7 +145,7 @@ update := fn state: State { state.ball.y = 10.0 state.ball.vx = BALL_STARTV state.ball.vy = 0. - state.p2.score = state.p2.score + 1 + state.p2.score = state.p2.score + 1.0 } ball_and_paddle_check(state.p1.paddle, state.ball) @@ -158,6 +158,14 @@ draw := fn state: State { draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, PADDLE_W, PADDLE_H) draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, PADDLE_W, PADDLE_H) + for y := 0.0, y < state.p1.score, y = y + 1.0 { + draw_rectangle(15.0, y * 0.5, 0.5, 0.5) + } + + for y := 0.0, y < state.p2.score, y = y + 1.0 { + draw_rectangle(5.0, y * 0.5, 0.5, 0.5) + } + draw_rectangle(state.ball.x, state.ball.y, BALL_S, BALL_S) } @@ -170,13 +178,13 @@ init := fn { state.ball.vy = 0.0 state.p1 = Player() - state.p1.score = 0 + state.p1.score = 0.0 state.p1.paddle = Paddle() state.p1.paddle.x = 1. state.p1.paddle.y = 10. state.p2 = Player() - state.p2.score = 0 + state.p2.score = 0.0 state.p2.paddle = Paddle() state.p2.paddle.x = 19. state.p2.paddle.y = 10. -- cgit v1.2.1