-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmrl-paddlewar
executable file
·281 lines (216 loc) · 6.4 KB
/
mrl-paddlewar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env luajit
-- basic two player touch implementation of pong
--local S = require('syscall')
--S.setenv('MRG_RESTARTER','yes')
--S.setenv('MRG_BACKEND','mmm')
local Mrg = require('mrg')
local math = require('math')
local os = require('os')
local mrg = Mrg.new(-1,-1)
local in_menu
local game_started = false
local game_over = false
local vel_x, vel_y
local ball;
local walls;
local goals;
local top_score, bottom_score
local serve_dir = 1
function reset_game()
in_menu = true
game_started = false
game_over = false
mrg:queue_draw(nil)
vel_x = 0
vel_y = 0
serve_dir = 1
top_score = 0
bottom_score = 0
ball = {x = 0.5, y = 0.5, w = 0.02, h= 0.02}
paddles = {
{x = 0.42, y = 1.0 - 0.08, w = 0.15, h= 0.04},
{x = 0.42, y = 0.04, w = 0.15, h= 0.04},
}
goals = {
{x = 0.0, y= 0.99, w= 1.0, h = 0.1},
{x = 0.0, y= -0.095, w= 1.0, h = 0.1}
}
walls = {
{x = 0.0-0.15, y = 0.0, w = 0.15, h= 1.00},
{x = 1.0, y = 0.0, w = 0.15, h= 1.00}
}
end
local ball = {x = 0.5, y = 0.5, w = 0.02, h= 0.02};
function serve_ball()
vel_x = 0.0 + (math.random() - 0.5) / 2
vel_y = (1.0 - (math.random()/2)) * serve_dir
ball.x = 0.5
ball.y = 0.5
end
function start_game()
reset_game ()
serve_ball ()
in_menu = false
game_started = true
end
function collides(a,b)
if a.x+a.w<b.x or b.x+b.w<a.x or a.y+a.h<b.y or b.y+b.h<a.y then
return false
end
return true
end
reset_game()
function toggle_fullscreen(event)
event.mrg:set_fullscreen(not event.mrg:is_fullscreen())
end
function menu(mrg, dim)
local cr = mrg:cr()
cr:set_source_rgba(1,1,1,0.8)
cr:rectangle(-mrg:width(),-mrg:height(),mrg:width()*2,mrg:height()*2)
-- XXX: try wit Mrg.ALL here instead?
mrg:listen(Mrg.DRAG_PRESS + Mrg.MOTION + Mrg.RELEASE+ Mrg.PRESS , function(event)
event:stop_propagate()
end)
cr:fill()
mrg:set_style("background:transparent;color:black;font-size: " .. dim / 15.0 .. "px;")
mrg:set_edge_left(1 * mrg:em())
mrg:set_edge_top(1 * mrg:em())
if not game_started then
mrg:text_listen(Mrg.TAP, function()
in_menu = false
start_game()
mrg:queue_draw(nil)
end)
mrg:print ('start\n\n')
mrg:text_listen_done()
else
mrg:text_listen(Mrg.TAP, function() in_menu = false mrg:queue_draw(nil) end)
mrg:print('resume game\n\n')
mrg:text_listen_done()
mrg:text_listen(Mrg.TAP, function() start_game() end)
mrg:print('new game\n\n')
mrg:text_listen_done()
end
if mrg:is_fullscreen() == false then
mrg:text_listen(Mrg.TAP, function() mrg:set_fullscreen(true) end)
mrg:print('fullscreen\n\n')
mrg:text_listen_done()
else
mrg:text_listen(Mrg.TAP, function() mrg:set_fullscreen(false) end)
mrg:print('unfullscreen\n\n')
mrg:text_listen_done()
end
mrg:text_listen(Mrg.TAP, function() mrg:quit() end)
mrg:print('quit\n\n')
mrg:text_listen_done()
cr:set_source_rgba(1,1,0,0.2)
cr:rectangle(-mrg:width(),-mrg:height(),mrg:width()*2,mrg:height()*2)
cr:fill()
end
----------------------------------------------------------------
--
local prev_time = mrg:ms()
mrg:set_ui(function()
local cr = mrg:cr()
local dim = mrg:width()
local time = mrg:ms()
local delta_ms = time - prev_time
prev_time = time
if dim > mrg:height() then dim = mrg:height() end
cr:set_source_rgb(0,0,0)
cr:paint()
mrg:set_style("background:transparent;color:rgba(255,255,255,0.5);font-size: " .. dim / 5.0 .. "px;")
cr:translate((mrg:width()-dim)/2, (mrg:height()-dim)/2)
mrg:set_edge_left(1 * mrg:em())
mrg:set_edge_top(-4.7 * mrg:em())
cr:save()
cr:rotate(3.141512/2)
mrg:print('' .. top_score .. ' ' .. bottom_score )
cr:restore()
cr:save()
cr:scale(dim, dim)
cr:rectangle(0,0,1,1)
mrg:listen(Mrg.TAP_AND_HOLD, function()
in_menu = true
mrg:queue_draw(nil)
end)
cr:new_path()
cr:set_source_rgb(1,1,1)
cr:rectangle(ball.x,ball.y,ball.w,ball.h)
cr:fill()
for k, v in ipairs(paddles) do
cr:rectangle(v.x,v.y,v.w,v.h)
cr:fill()
end
--cr:set_source_rgba(1,1,1,0.1)
cr:rectangle(0.0, 0.7, 1.0, 0.3)
mrg:listen(Mrg.DRAG, function(event)
paddles[1].x = event.x - paddles[1].w/2;
end)
cr:new_path()
cr:rectangle(0.0, 0.0, 1.0, 0.3)
mrg:listen(Mrg.DRAG, function(event)
paddles[2].x = event.x - paddles[2].w/2;
end)
cr:new_path()
cr:restore()
mrg:set_style("background:transparent;color:yellow;font-size: " .. dim / 15.0 .. "px;")
mrg:set_edge_left(1 * mrg:em())
mrg:set_edge_top(1.5 * mrg:em())
if game_over then
cr:rectangle(0,0, mrg:width(), mrg:height())
mrg:listen(Mrg.TAP, function() reset_game() end)
cr:set_source_rgba(0,0,0,0.75)
cr:new_path()
cr:paint()
mrg:print ("game over\n\n")
mrg:set_style("background:transparent;color:white;font-size: " .. dim / 5.0 .. "px;")
mrg:print ("\n ↻")
end
if in_menu then
menu (mrg, dim)
end
-- some keybindings
mrg:add_binding("control-q", NULL, NULL, function () mrg:quit() end)
mrg:add_binding("q", NULL, NULL, function () mrg:quit() end)
mrg:add_binding("escape", NULL, NULL, function () mrg:quit() end)
mrg:add_binding("F2", NULL, NULL, function () reset_game() end)
mrg:add_binding("r", NULL, NULL, function () reset_game() end)
mrg:add_binding("f", NULL, NULL, toggle_fullscreen)
mrg:add_binding("F11", NULL, NULL, toggle_fullscreen)
ball.y = ball.y + vel_y * delta_ms / 1000.0
ball.x = ball.x + vel_x * delta_ms / 1000.0
if (collides (ball,paddles[1]) or
collides (ball,paddles[2])
) then
-- vel_x = - vel_x
vel_y = - vel_y
ball.y = ball.y + vel_y * delta_ms / 1000.0
ball.x = ball.x + vel_x * delta_ms / 1000.0
end
if (collides (ball,walls[1]) or
collides (ball,walls[2])) then
vel_x = - vel_x
ball.y = ball.y + vel_y * delta_ms / 1000.0
ball.x = ball.x + vel_x * delta_ms / 1000.0
end
if (collides (ball, goals[1])) then
top_score = top_score + 1
serve_dir = -1
serve_ball ()
end
if (collides (ball, goals[2])) then
bottom_score = bottom_score + 1
serve_dir = 1
serve_ball ()
end
-- we must queue this, since painting itself clear the needs-redraw state,
-- maybe lift this restriction since it is an unneded gotcha..
mrg:add_timeout(0, function()
mrg:queue_draw(nil)
return 0
end)
end)
math.randomseed( os.time() )
mrg:set_title('paddle war')
mrg:main()