-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
146 lines (120 loc) · 3.9 KB
/
main.lua
File metadata and controls
146 lines (120 loc) · 3.9 KB
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
VIRTUAL_WIDTH = 384
VIRTUAL_HEIGHT = 216
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
PADDLE_WIDTH = 8
PADDLE_HEIGHT = 32
PADDLE_SPEED = 140
BALL_SIZE = 4
BALL_SPEED = 60
FONT_NAME = 'bit5x3.ttf'
LARGE_FONT = love.graphics.newFont(FONT_NAME, 32)
SMALL_FONT = love.graphics.newFont(FONT_NAME, 16)
push = require 'push'
player1 = {
x = 10,
y = 10,
score = 0
}
player2 = {
x = VIRTUAL_WIDTH - 10 - PADDLE_WIDTH,
y = VIRTUAL_HEIGHT - 10 - PADDLE_HEIGHT,
score = 0
}
ball = {
x = VIRTUAL_WIDTH / 2 - BALL_SIZE / 2,
y = VIRTUAL_HEIGHT / 2 - BALL_SIZE / 2,
dx = 0,
dy = 0
}
gameState = 'title'
function love.load()
math.randomseed(os.time())
love.graphics.setDefaultFilter('nearest', 'nearest')
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT)
love.window.setTitle('Pong')
resetBall()
end
function love.update(dt)
if love.keyboard.isDown('w') then
player1.y = player1.y - PADDLE_SPEED * dt
elseif love.keyboard.isDown('s') then
player1.y = player1.y + PADDLE_SPEED * dt
end
if love.keyboard.isDown('up') then
player2.y = player2.y - PADDLE_SPEED * dt
elseif love.keyboard.isDown('down') then
player2.y = player2.y + PADDLE_SPEED * dt
end
if gameState == 'play' then
ball.x = ball.x + ball.dx * dt
ball.y = ball.y + ball.dy * dt
if ball.x <= 0 then
player2.score = player2.score + 1
resetBall()
gameState = 'serve'
elseif ball.x >= VIRTUAL_WIDTH - BALL_SIZE then
player1.score = player1.score + 1
resetBall()
gameState = 'serve'
end
if ball.y <= 0 or ball.y >= VIRTUAL_HEIGHT - BALL_SIZE then
ball.dy = -ball.dy
end
if collides(player1, ball) then
ball.dx = -ball.dx * 1.02
ball.x = player1.x + PADDLE_WIDTH
elseif collides(player2, ball) then
ball.dx = -ball.dx * 1.02
ball.x = player2.x - BALL_SIZE
end
end
end
function love.keypressed(key)
if key == 'escape' or key == 'q' then
love.event.quit()
end
if (key == 'enter' or key == 'return') then
if gameState == 'title' then
gameState = 'serve'
elseif gameState == 'serve' then
gameState = 'play'
end
end
end
function love.draw()
push:start()
love.graphics.clear(40 / 255, 45 / 255, 52 / 255, 255 / 255)
if gameState == 'title' then
love.graphics.setFont(LARGE_FONT)
love.graphics.printf('Pong', 0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.setFont(SMALL_FONT)
love.graphics.printf('Press Enter', 0, VIRTUAL_HEIGHT - 32, VIRTUAL_WIDTH, 'center')
elseif gameState == 'serve' then
love.graphics.setFont(SMALL_FONT)
love.graphics.printf('Press Enter to Serve!', 0, 10, VIRTUAL_WIDTH, 'center')
end
love.graphics.setFont(LARGE_FONT)
love.graphics.print(player1.score, VIRTUAL_WIDTH / 2 - 36, VIRTUAL_HEIGHT / 2 - 16)
love.graphics.print(player2.score, VIRTUAL_WIDTH / 2 + 16, VIRTUAL_HEIGHT / 2 - 16)
love.graphics.setFont(SMALL_FONT)
love.graphics.rectangle('fill', player1.x, player1.y, PADDLE_WIDTH, PADDLE_HEIGHT)
love.graphics.rectangle('fill', player2.x, player2.y, PADDLE_WIDTH, PADDLE_HEIGHT)
love.graphics.rectangle('fill', ball.x, ball.y, BALL_SIZE, BALL_SIZE)
push:finish()
end
function collides(p, b)
return not (p.x > b.x + BALL_SIZE or p.y > b.y + BALL_SIZE or b.x > p.x + PADDLE_WIDTH or b.y > p.y + PADDLE_HEIGHT)
end
function resetBall()
ball.x = VIRTUAL_WIDTH / 2 - BALL_SIZE / 2
ball.y = VIRTUAL_HEIGHT / 2 - BALL_SIZE / 2
ball.dx = BALL_SPEED + math.random(BALL_SPEED)
if math.random(2) == 1 then
ball.dx = -ball.dx
end
ball.dy = BALL_SPEED / 2 + math.random(BALL_SPEED)
if math.random(2) == 1 then
ball.dy = -ball.dy
end
end