View difference between Paste ID: acsDLn5D and 19iHaRLn
SHOW: | | - or go back to the newest paste.
1-
-- GhastTearz' modified version of
1+
2-
-- brickbreaker by TheZipCreator
2+
local speaker = peripheral.wrap("left")
3
term.redirect(peripheral.wrap("top"))
4
local width, height = term.getSize()
5
local paddleWidth = 6
6
local keyDelay = 0.1
7
local delay = 0.15
8
local brickRows = 3
9
10
local backgroundColor = colors.black
11
local textColor       = colors.white
12
local paddleColor     = colors.white
13
local scoreLineColor  = colors.lightGray
14
local brickColor      = colors.magenta
15
local ballColor       = colors.white
16
17
-- Global variables
18
local paddle
19
local score
20
local bricks
21
local ballx
22
local bally
23
local ballvx
24
local ballvy
25
local gameOver
26
27
function restart()
28
  paddle = math.floor((width - paddleWidth) / 2)
29
  
30
  score  = 0
31
  
32
  ballx  = math.floor( width / 2)
33
  bally  = height - 8
34
  
35
  ballvx = 1
36
  ballvy = 1
37
  
38
  gameOver = false
39
  
40
  -- Fill in the brick array, which is a
41
  -- 2D array that records if the brick still
42
  -- exists at each location.
43
  bricks = {}
44
  for x = 1, width do
45
    bricks[x] = {}
46
    for y = 2, brickRows + 1 do
47
      bricks[x][y] = true
48
    end
49
  end
50
     
51
end
52
53
54
55
-- Draws the initial screen, then from there
56
-- things are changed character by character.
57
function initialDraw()
58
  term.setBackgroundColor(backgroundColor)
59
  term.clear()
60
  
61
  --Draw paddle
62
  term.setBackgroundColor(paddleColor)
63
  for i=0, paddleWidth  do
64
    term.setCursorPos(paddle+i, height)
65
    term.write(" ")
66
  end
67
  
68
  --draw score line
69
  term.setBackgroundColor(scoreLineColor)
70
  for i = 1, width do
71
    term.setCursorPos(i, 1)
72
    term.write(" ")
73
  end
74
  term.setTextColor(textColor)
75
  term.setCursorPos(1,1)
76
  term.write(score)
77
  
78
  --draw the bricks. Since it's the initial draw
79
  --we don't have to check if the bricks exist
80
  term.setBackgroundColor(brickColor)
81
  for x = 1, width do
82
    for y = 2, brickRows + 1 do
83
      term.setCursorPos(x,y)
84
      term.write(" ")
85
    end
86
  end
87
88
  -- draw the ball  
89
  term.setBackgroundColor(ballColor)
90
  term.setCursorPos(math.floor(ballx), math.floor(bally))
91
  term.write(" ")
92
93
end
94
95
96
function drawScore()
97
  term.setBackgroundColor(backgroundColor)
98
  term.setTextColor(textColor)
99
  term.clear()
100
  centertext("Final Score: "..score, height/2)
101
  centertext("Press top half to restart", (height/2)+1)
102
end
103-
  centertext("Press the top of the screen to restart", (height/2)+1)
103+
104
105
function moveBall()
106
  -- First we need to check to see if the
107
  -- ball hits a brick. We have 2 cases, either the
108
  -- brick is directly above the ball, or the brick
109
  -- is directly in the path of the ball (for
110
  -- diagonal collisions)  
111
  
112
  local breakBrickX, breakBrickY
113
  
114
  -- brick above ball
115
  if bricks[ballx][bally - 1] == true then
116
    breakBrickX = ballx
117
    breakBrickY = bally - 1
118
    bounce(1)
119
120
  -- brick in path of ball
121
  elseif bricks[ballx+ballvx][bally+ballvy] == true then
122
    breakBrickX = ballx + ballvx
123
    breakBrickY = bally + ballvy
124
    bounce(1)
125
126
    bounce(2)
127
128
  end
129
  
130
  if breakBrickX ~= nil and breakBrickY ~= nil then
131
    bricks[breakBrickX][breakBrickY] = false
132
    
133
    -- draw a blank space over the broken brick
134
    term.setBackgroundColor(backgroundColor)
135
    term.setCursorPos(breakBrickX, breakBrickY)
136
    term.write(" ")
137
    
138
    -- print new score. Since score only gets
139
    -- bigger, we don't need to worry about
140
    -- redrawing the whole score line
141
    score = score + 1
142
    term.setBackgroundColor(scoreLineColor)
143
    term.setTextColor(textColor)
144
    term.setCursorPos(1,1)
145
    term.write(score)
146
  end
147
  
148
  local oldx = ballx
149
  local oldy = bally
150
  ballx = ballx + ballvx
151
  bally = bally + ballvy
152
  
153
  
154
  -- ball hits the paddle
155
  if bally == height - 1
156
      and ballx >= paddle
157-
    bounce(1) 
157+
158
    bounce(1)
159
        
160
  end
161
  
162
  -- ball hits top wall
163
  if bally == 2 then
164
    bounce(1)
165
        
166
  end
167
  
168
  -- ball hits right wall
169
  if ballx >= width then
170
    bounce(2)
171
172
  end
173
  
174
  -- ball hits left wall
175
  if ballx <= 1 then
176
    bounce(2)
177
        
178
  end
179
  
180
  --Now draw the new position of the ball
181
  term.setBackgroundColor(backgroundColor)
182
  term.setCursorPos(oldx, oldy)
183
  term.write(" ")
184
  term.setBackgroundColor(ballColor)
185
  term.setCursorPos(math.floor(ballx),math.floor(bally))
186
  term.write(" ")
187
end
188
189
190
191
function bounce(dir)
192
  if dir == 1 then
193
speaker.playSound("create:fwoomp",.5,2)
194
    ballvy = -ballvy
195
  end
196
  if dir == 2 then
197
speaker.playSound("create:fwoomp",.5,2)
198
    ballvx = -ballvx
199
  end
200
end
201
202
203
-- direction of -1 is left and 1 is right
204
-- drawing is done by removing a character from
205
-- one end of the paddle and drawing a new
206
-- character at the other end.
207
function movePaddle( direction )
208
209
  local removeX, addX
210
211
  -- calculate the new paddle position and also
212
  -- find the positions of the chars that need to
213
  -- be added and removed  
214
  if direction == 1
215
      and paddle + paddleWidth <  width then
216
    removeX = paddle
217
    addX    = paddle + paddleWidth + 1 
218
    paddle  = paddle + 1
219
       
220
  elseif direction == -1
221
      and paddle > 1 then
222
    removeX = paddle + paddleWidth
223
    addX    = paddle - 1
224
    paddle  = paddle - 1 
225
  else
226
    return
227
  end
228
229
  -- Draw the new paddle
230
  term.setBackgroundColor(backgroundColor)
231
  term.setCursorPos(removeX, height)
232
  term.write(" ")
233
  
234
  term.setBackgroundColor(paddleColor)
235
  term.setCursorPos(addX, height)
236
  term.write(" ")
237
end
238
239
240
function centertext(text, height)
241
  term.setCursorPos((width/2)-(string.len(text)/2), height)
242
  term.write(text)  
243
end
244
245
246
247
-- Main Game Loop
248
restart()
249
initialDraw()
250
251
-- every delay seconds a timer goes off which
252
-- moves the ball
253
local timerID = os.startTimer(delay)
254-
    if y >= 50 then
254+
255
while true do
256
  
257
  event, value, x, y = os.pullEvent()
258
  
259-
    if y >= 100 then
259+
260
   
261
    if y <= 10 then
262
      restart()
263
      initialDraw()
264
    end
265
    
266
    if x == 0 and y == 0 then
267-
    if x >= 10 
267+
268-
        or value == keys.d then
268+
269
      term.clear()
270
      term.setCursorPos(1,1)
271
      return
272-
    if x <= 10
272+
273-
        or value == keys.a then
273+
274
    if x >= 20 
275
            
276
        or value == keys.d then 
277
            speaker.playSound("quark:ambient.clock",.3)
278
      movePaddle(1)
279
    end
280
    
281
    if x <= 20
282
        or value == keys.a then 
283
            speaker.playSound("quark:ambient.clock",.3)
284
      movePaddle(-1)
285
    end
286
  end
287
  
288
  if event == "timer" and timerID == value then
289
    moveBall()
290
    timerID = os.startTimer(delay)
291
  end
292
  
293
  if bally > height then
294
    drawScore()
295
  end
296
  
297
end