-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
150 lines (116 loc) · 3.8 KB
/
main.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
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
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
gfx = playdate.graphics
screenWidth = playdate.display.getWidth()
screenHeight = playdate.display.getHeight()
bounceSound = playdate.sound.synth.new(playdate.sound.kWaveSine)
bounceSound:setADSR(0.1, 0.1, 0.1, 0)
pointSound = playdate.sound.synth.new(playdate.sound.kWaveSine)
pointSound:setADSR(0.25, 0.25, 0.1, 0)
leftScore = 0
rightScore = 0
kLeftWallTag = 1
kRightWallTag = 2
class("Ball").extends(gfx.sprite)
function Ball:init()
Ball.super.init(self)
self.xSpeed = 5
self.ySpeed = 6
radius = 5
local ballImage = gfx.image.new(2 * radius, 2 * radius)
gfx.pushContext(ballImage)
gfx.fillCircleAtPoint(radius, radius, radius)
gfx.popContext()
self:setImage(ballImage)
self:setCollideRect(0, 0, self:getSize())
self:moveTo(200, 120)
end
function Ball:update()
local _, _, collisions, _ = self:moveWithCollisions(self.x + self.xSpeed, self.y + self.ySpeed)
for i = 1, #collisions do
if collisions[i].other:getTag() == kLeftWallTag then
rightScore += 1
self:moveTo(screenWidth / 2, screenHeight / 2)
pointSound:playNote("C5", 1, 0.5)
return
elseif collisions[i].other:getTag() == kRightWallTag then
leftScore += 1
self:moveTo(screenWidth / 2, screenHeight / 2)
pointSound:playNote("C5", 1, 0.5)
return
end
if collisions[i].normal.x ~= 0 then
bounceSound:playNote("G4", 1, 0.2)
self.xSpeed *= -1
end
if collisions[i].normal.y ~= 0 then
bounceSound:playNote("G4", 1, 0.2)
self.ySpeed *= -1
end
end
end
class("Paddle").extends(gfx.sprite)
function Paddle:init(xPosition)
-- remember to do this so the parent sprite constructor
-- can get its bits wired up
Paddle.super.init(self)
self.ySpeed = 5
width = 8
height = 50
local paddleImage = gfx.image.new(width, height)
gfx.pushContext(paddleImage)
-- (x, y, width, height, corner rounding)
-- note that we fill at (0,0) rather than (self.x, self.y)
-- since we are in a new draw context thanks to pushContext
gfx.fillRoundRect(0, 0, width, height, 2)
gfx.popContext()
self:setImage(paddleImage)
self:setCollideRect(0, 0, self:getSize())
self:moveTo(xPosition, screenHeight / 2)
end
function Paddle:update()
if playdate.buttonIsPressed(playdate.kButtonDown) then
self:moveWithCollisions(self.x, self.y + self.ySpeed)
end
if playdate.buttonIsPressed(playdate.kButtonUp) then
self:moveWithCollisions(self.x, self.y - self.ySpeed)
end
local crankChange, _ = playdate.getCrankChange()
if crankChange ~= 0 then
self:moveWithCollisions(self.x, self.y + crankChange)
end
end
ball = Ball()
ball:add()
leftPaddle = Paddle(10)
leftPaddle:add()
rightPaddle = Paddle(screenWidth - 10)
rightPaddle:add()
leftWall = gfx.sprite.addEmptyCollisionSprite(-5, 0, 5, screenHeight)
leftWall:setTag(kLeftWallTag)
leftWall:add()
rightWall = gfx.sprite.addEmptyCollisionSprite(screenWidth, 0, 5, screenHeight)
rightWall:setTag(kRightWallTag)
rightWall:add()
topWall = gfx.sprite.addEmptyCollisionSprite(0, -5, screenWidth, 5)
topWall:add()
bottomWall = gfx.sprite.addEmptyCollisionSprite(0, screenHeight, screenWidth, 5)
bottomWall:add()
function isGameOver()
local winningScore = 5
return leftScore >= winningScore or rightScore >= winningScore
end
function playdate.update()
if isGameOver() then
gfx.drawTextAligned("Good game, pal!", screenWidth / 2, screenHeight / 2 - 25, kTextAlignment.center)
gfx.drawTextAligned("Press Ⓐ to play again", screenWidth / 2, screenHeight / 2, kTextAlignment.center)
if playdate.buttonIsPressed(playdate.kButtonA) then
leftScore = 0
rightScore = 0
end
else
gfx.sprite.update()
gfx.drawTextAligned(leftScore .. " : " .. rightScore, screenWidth / 2, 5, kTextAlignment.center)
end
end