Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
game.lua
Go to the documentation of this file.
1local function init(std, game)
2 game.highscore = game.highscore or 0
3 game.player_size = game.height/8
4 game.player_pos = game.height/2 - game.player_size/2
5 game.ball_pos_x = game.width/2
6 game.ball_pos_y = game.height/2
7 game.ball_spd_x = 0.3
8 game.ball_spd_y = 0.06
9 game.ball_size = 8
10 game.score = 0
11end
12
13local function loop(std, game)
14 -- inputs
15 game.player_dir = std.key.press.down - std.key.press.up
16 -- moves
17 game.player_pos = std.math.clamp(game.player_pos + (game.player_dir * 7), 0, game.height - game.player_size)
18 game.ball_pos_x = game.ball_pos_x + (game.ball_spd_x * game.dt)
19 game.ball_pos_y = game.ball_pos_y + (game.ball_spd_y * game.dt)
20 -- colisions
21 if game.ball_pos_x >= (game.width - game.ball_size) then
22 game.ball_spd_x = -std.math.abs(game.ball_spd_x)
23 end
24 if game.ball_pos_y >= (game.height - game.ball_size) then
25 game.ball_spd_y = -std.math.abs(game.ball_spd_y)
26 end
27 if game.ball_pos_y <= 0 then
28 game.ball_spd_y = std.math.abs(game.ball_spd_y)
29 end
30 if game.ball_pos_x <= 0 then
31 if std.math.clamp(game.ball_pos_y, game.player_pos, game.player_pos + game.player_size) == game.ball_pos_y then
32 local new_spd_y = std.math.clamp(game.ball_spd_y + (game.player_pos % 10) - 5, -10, 10)
33 game.ball_spd_y = game.ball_spd_y == 0 and new_spd_y == 0 and 20 or new_spd_y -- WOW!
34 game.ball_spd_y = game.ball_spd_y / 16
35 game.ball_spd_x = std.math.abs(game.ball_spd_x) * 1.003
36 game.score = game.score + 1
37 else
38 std.game.reset()
39 end
40 end
41end
42
43local function draw(std, game)
44 std.draw.clear(std.color.black)
45 std.draw.color(std.color.white)
46 std.draw.rect(0, 4, game.player_pos, 8, game.player_size)
47 std.draw.rect(0, game.ball_pos_x, game.ball_pos_y, game.ball_size, game.ball_size)
48 std.draw.font('Tiresias', 32)
49 std.draw.text(game.width/4, 16, game.score)
50 std.draw.text(game.width/4 * 3, 16, game.highscore)
51end
52
53local function exit(std, game)
54 game.highscore = std.math.max(game.highscore, game.score)
55end
56
57local P = {
58 meta={
59 title='Ping Pong',
60 author='RodrigoDornelles',
61 description='simple pong',
62 version='1.0.0'
63 },
64 callbacks={
65 init=init,
66 loop=loop,
67 draw=draw,
69 }
70}
71
72return P;
local game
Definition main.lua:17
local std
Definition main.lua:18
local function draw(std, game)
local function init(std, game)
local function exit(std, game)
local function loop(std, game)