-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.lua
75 lines (55 loc) · 1.87 KB
/
Pong.lua
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
-- This code is under the authority of the GNU 3.0 Public License
-- Created by SpechtD, modified by Gabriel
-- Modified 2024 with a higher gameplay speed
local xVel = 0
local yVel = 0
local xPos = 0
local yPos = 0
local ballSize = LCD_W / 50
local scoreA = 0
local scoreB = 0
local aPadPos = 0
local bPadPos = 0
local function startBall()
xPos = LCD_W/2 - ballSize/2
yPos = LCD_H/2 - ballSize/2
yVel = math.random(0, 30) - 15
xVel = (math.random() > 0.5) and 10 or -10
end
local function init()
startBall()
end
local function run()
lcd.clear()
aPadPos = (getValue("thr") - 1024) / 2048 * -(LCD_H - ballSize*4)
bPadPos = (getValue("ele") - 1024) / 2048 * -(LCD_H - ballSize*4)
if xPos < ballSize and (yPos + ballSize) >= aPadPos and yPos <= (aPadPos + ballSize * 4) then
xVel = -xVel
xPos = ballSize + 1
yVel = math.random(0, 6) - 3
elseif xPos > (LCD_W - ballSize*2) and (yPos + ballSize) >= bPadPos and yPos <= (bPadPos + ballSize * 4) then
xVel = -xVel
xPos = LCD_W - ballSize*2 - 1
yVel = math.random(0, 6) - 3
end
if xPos >= LCD_W - ballSize then
scoreA = scoreA + 1
startBall()
elseif xPos <= 0 then
scoreB = scoreB + 1
startBall()
elseif yPos >= LCD_H - ballSize or yPos <= 0 then
yVel = -yVel
end
xPos = xPos + xVel
yPos = yPos + yVel
lcd.drawFilledRectangle(xPos,yPos,ballSize,ballSize)
lcd.drawFilledRectangle(0, aPadPos, ballSize, ballSize*4)
lcd.drawFilledRectangle(LCD_W - ballSize, bPadPos, ballSize, ballSize*4)
lcd.drawLine(LCD_W/2, 0, LCD_W/2, LCD_H, DOTTED, 0)
lcd.drawNumber(LCD_W * 0.25, ballSize, scoreA, 0)
lcd.drawNumber(LCD_W * 0.75, ballSize, scoreB, 0)
lcd.refresh()
return 0
end
return {init=init, run=run}