-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraria.py
381 lines (326 loc) · 14 KB
/
terraria.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from time import time
from enum import Enum
import os
import pygame
from pygame.locals import *
from pygame.key import *
from settings import *
from classes import *
from helpers import *
from recipes import *
###############################################################################
# MVC
def appStarted(app):
ASSETS_DIR = "assets"
app.width = 500
app.height = 500
import settings
settings.UNIT_WH = app.width / (CHUNK_SIZE * 3.5)
app.images = {}
app.background = pygame.image.load(os.path.join(ASSETS_DIR, 'background.png'))
scale = app.height / app.background.get_height()
app.background = pygame.transform.scale(app.background, (int(app.background.get_width() * scale), int(app.background.get_height() * scale)))
for f in os.listdir("assets"):
if not f.endswith(".png"):
continue
image = pygame.image.load(os.path.join(ASSETS_DIR, f))
image.set_alpha(255)
app.images[f[:-4]] = image
Game(app)
app.player = Player(app)
app.playerSpriteGroup = pygame.sprite.GroupSingle(app.player)
app.func = Functionality(app)
app.lastTime = time()
checkBackground(app)
app.paused = False
app.deathScreen = False
app.largeFont = pygame.font.Font("assets/terraria.ttf", 20)
app.font = pygame.font.Font("assets/terraria.ttf", 16)
app.smallFont = pygame.font.Font("assets/terraria.ttf", 14)
def keyPressed(app, event):
if app.deathScreen:
app.paused = False
app.deathScreen = False
app.player.die(app)
else:
app.func.handleKey(app, event)
def sizeChanged(app):
scale = app.height / app.background.get_height()
app.background = pygame.transform.scale(app.background, (int(app.background.get_width() * scale), int(app.background.get_height() * scale)))
generateChunks(app)
checkBackground(app)
def mouseMoved(app, event):
if not app.paused:
app.func.mouseX = event[0]
app.func.mouseY = event[1]
app.func.updateHovering(app)
def mousePressed(app, event):
if app.func.hovering and app.func.canInteract and not app.paused:
curInv = app.player.inventory[app.func.selectedInventory]
if app.func.hovering.breakable:
app.func.holding = time()
elif curInv and curInv.canPlace and app.func.hovering.type == Blocks.AIR:
app.game.placeBlock(app, curInv, app.func.hovering)
app.func.updateHovering(app)
elif (app.func.hovering and not app.func.canInteract) or not app.func.hovering:
curInv = app.player.inventory[app.func.selectedInventory]
if hasattr(curInv, "food") and curInv.food:
if app.player.health == 10:
return
app.player.eat(curInv)
curInv.count -= 1
if curInv.count == 0:
app.player.inventory[app.func.selectedInventory] = None
elif hasattr(curInv, "use") and curInv.use:
curInv.use(app)
curInv.count -= 1
if curInv.count == 0:
app.player.inventory[app.func.selectedInventory] = None
else:
playerX = getPixX(app, app.player.x)
right = event[0] > playerX
app.player.hit(app, right)
def mouseReleased(app):
if not app.paused: app.func.holding = None
def drawChunk(app, screen: pygame.Surface, chunk: Chunk):
for r in range(chunk.x, chunk.x + CHUNK_SIZE):
for b in range(0, BUILD_HEIGHT):
if (r, b) not in chunk.blocks:
continue
block = chunk.blocks[(r, b)]
if r == chunk.x and app.func.debug:
x, _ = getPixFromCoords(app, block.x, block.y)
pygame.draw.rect(screen, ("red" if chunk.x == app.game.getChunk(app, app.player.chunk).x else "black"),
(x, -5, (CHUNK_SIZE * UNIT_WH), app.height + 5), 1)
chunk.items.draw(screen)
chunk.mobs.draw(screen)
def drawGame(app, screen: pygame.Surface):
pygame.draw.rect(screen, getBackgroundColor(app.game.time), (0, 0, app.width, app.height), 0)
for bgX in app.game.bgX:
screen.blit(app.background, (bgX, 0))
pygame.draw.rect(screen, "#050505", (0, getPixY(app, GROUND_LEVEL - GRASS_LEVEL - TERRAIN_VARIATION),
app.width, app.height * 2), 0)
for chunk in app.game.loaded:
drawChunk(app, screen, chunk)
app.game.blocks.draw(screen)
def drawPlayer(app, screen: pygame.Surface):
x = app.width / 2
y = app.height * TERRAIN_HEIGHT
screen.blit(app.player.getSprite(), (x + 0.2, y + 4))
def drawDebug(app, screen: pygame.Surface):
gameTime = app.font.render(f"G: {app.game.time}", 1, "#2D1E1E")
pos = gameTime.get_rect()
pos.left, pos.centery = (5, 15)
screen.blit(gameTime, pos)
tps = app.font.render(f'TPS: {round(app.clock.get_fps())}', 1, "#2D1E1E")
pos = tps.get_rect()
pos.left, pos.centery = (5, 35)
screen.blit(tps, pos)
player = app.font.render(f"Player: {app.player.x}, {app.player.y}", 1, "#2D1E1E")
pos = player.get_rect()
pos.left, pos.centery = (5, 55)
screen.blit(player, pos)
mouse = app.font.render(f'M: ({app.func.mouseX}, {app.func.mouseY})', 1, "#2D1E1E")
pos = mouse.get_rect()
pos.left, pos.centery = (5, 75)
screen.blit(mouse, pos)
if app.func.hovering:
block = app.func.hovering
if block:
blockText = app.font.render(f'B: {block.type.name}', 1, "#2D1E1E")
pos = blockText.get_rect()
pos.left, pos.centery = (5, 95)
screen.blit(blockText, pos)
i = 0
for key, val in block.__dict__.items():
if (key == "type" or key.startswith("_")
or key == "rect" or "mage" in key): continue
text = app.font.render(f'{key}: {val}', 1, "#2D1E1E")
pos = text.get_rect()
pos.left, pos.centery = (5, 115 + i * 15)
i += 1
screen.blit(text, pos)
def drawHotbar(app, screen: pygame.Surface):
width = 9 * 28 + 40
itemWidth = int((width - 40) / 9)
pygame.draw.rect(screen, "#737F8F", (app.width - width - 8, 12,
width, itemWidth + 8), 0)
for i, item in enumerate(app.player.inventory):
x = 8 + (i * (itemWidth + 4))
selected = app.func.selectedInventory == i
left = app.width - width - 12 + x
slot = pygame.Surface((itemWidth, itemWidth))
slot.fill("#965816")
if selected:
pygame.draw.rect(slot, "#B4B4B4", (0, 0, itemWidth, itemWidth), 2)
if item:
image = pygame.transform.scale(getImage(app, item.name), (itemWidth - 8, itemWidth - 8))
slot.blit(image, (4, 4))
if item.count > 1:
count = app.smallFont.render(f"{item.count}", 1, "#38332F")
pos = count.get_rect()
pos.left, pos.centery = (2, 4)
slot.blit(count, pos)
if hasattr(item, "curCooldown") and item.attackCooldown != 0:
start = 0
percent = item.curCooldown / item.attackCooldown
end = percent * 2 * math.pi
cooldown = pygame.Surface((itemWidth + 8, itemWidth + 8), pygame.SRCALPHA)
pygame.draw.arc(cooldown, (0, 0, 0, 200), (0, 0, itemWidth + 8, itemWidth + 8), start, end, itemWidth)
cooldown.set_alpha(200)
slot.blit(cooldown, (-4, -4))
screen.blit(slot, (left, 16))
for h in range(0, 10):
x = app.width - (h + 1) * (18 + 4)
emptyHeart = pygame.transform.scale(getImage(app, "emptyHeart"), (16, 16))
screen.blit(emptyHeart, (x, 64))
if h == app.player.health - 0.5:
halfHeart = pygame.transform.scale(getImage(app, "halfHeart"), (16, 16))
screen.blit(halfHeart, (x, 64))
elif h < app.player.health:
heart = pygame.transform.scale(getImage(app, "heart"), (16, 16))
screen.blit(heart, (x, 64))
def drawSettings(app, screen):
left = app.width * 0.1
top = app.height * 0.05
width = app.width * 0.8
height = app.height * 0.9
pygame.draw.rect(screen, "#C79355", (left, top, width, height), 0)
for i, (action, keybind) in enumerate(KEYBINDS.items()):
cell_height = (height * 0.09) + 2
row = i * cell_height
leftCentX = app.width * 0.23
leftCentY = row + top + (cell_height) / 2
actionText = app.font.render(action, 1, "#38332F")
screen.blit(actionText, (leftCentX, leftCentY))
box = pygame.draw.rect(screen, "#9D7039", (app.width * 0.52, row + top + 25, width * 0.26, cell_height - 25), 0)
keybindText = app.font.render(keybind, 1, "#38332F")
pos = keybindText.get_rect()
pos.centerx, pos.centery = box.center
screen.blit(keybindText, pos)
def drawDeath(app, screen):
optionsBg = pygame.transform.scale(getImage(app, "options_background"), (app.width, app.height))
screen.blit(optionsBg, (0, 0))
deathText = app.font.render("You Died!", 1, "#9A9A9A")
pos = deathText.get_rect()
pos.center = (app.width / 2, app.height / 2)
screen.blit(deathText, pos)
restartText = app.font.render("Press any key to continue", 1, "#9A9A9A")
pos = restartText.get_rect()
pos.center = (app.width / 2, app.height * 0.6)
screen.blit(restartText, pos)
def drawCrafting(app, screen):
slot_wh = 24
totalWidth = app.width * 0.8
pageLength = int(totalWidth / (slot_wh + 12))
startInd = app.func.craftingPage * pageLength
numCrafts = len(app.player.canCraft)
rect = pygame.draw.rect(screen, "#767C92", (app.width * 0.09, app.height * 0.75,
app.width * 0.82, slot_wh + 48), 0)
craftHeader = app.largeFont.render(f'Crafting({numCrafts})', 1, "#545B64")
pos = craftHeader.get_rect()
pos.left, pos.centery = rect.left + 8, rect.top + 16
screen.blit(craftHeader, pos)
if len(app.player.canCraft) > app.func.craftingSelected and app.player.canCraft[app.func.craftingSelected]:
selected = app.player.canCraft[app.func.craftingSelected]
name = app.font.render(f'{selected["output"].displayName.capitalize()}(x{selected["output"].count})', 1, '#545B64')
pos = name.get_rect()
pos.right, pos.centery = rect.right - 8, rect.top + 16
screen.blit(name, pos)
for i in range(pageLength):
if (startInd + i) >= numCrafts:
break
craft = app.player.canCraft[startInd + i]["output"]
if not craft:
continue
slot_box = pygame.draw.rect(screen, "#965816", (app.width * 0.1 + 8 + (i * (slot_wh + 12)) - 4,
rect.centery + 6 - (slot_wh / 2),
slot_wh + 8, slot_wh + 8), 0)
if app.func.craftingSelected == startInd + i:
pygame.draw.rect(screen, "#B4B4B4", (app.width * 0.1 + 8 + (i * (slot_wh + 12)) - 4,
rect.centery + 6 - (slot_wh / 2),
slot_wh + 8, slot_wh + 8), 2)
image = pygame.transform.scale(getImage(app, craft.name), (slot_wh - 2, slot_wh - 2))
screen.blit(image, (app.width * 0.1 + 8 + (i * (slot_wh + 12)) + 2,
rect.centery))
canCraftNum = numCanCraft(app, app.player.canCraft[startInd + i])
if not canCraftNum:
continue
count = app.smallFont.render(f'{canCraftNum * craft.count}', 1, '#38332F')
pos = count.get_rect()
pos.left, pos.centery = slot_box.left + 2, slot_box.top + 8
screen.blit(count, pos)
def redrawAll(app, screen:pygame.Surface):
if app.deathScreen:
drawDeath(app, screen)
else:
drawGame(app, screen)
if app.func.debug:
drawDebug(app, screen)
drawHotbar(app, screen)
app.playerSpriteGroup.draw(screen)
if app.func.keybinds:
drawSettings(app, screen)
if app.func.isCrafting:
drawCrafting(app, screen)
def timerFired(app):
if not app.paused:
app.lastTime = time()
"""
PLAYER
"""
app.playerSpriteGroup.update(app)
"""
GAME
"""
app.game.time = round((app.game.time + 0.01) % 23, 2)
app.game.blocks.update(app)
for chunk in app.game.loaded:
chunk.update(app)
randomChance = random.randint(0, 50)
if randomChance == 0:
app.game.spawnRandomMob(app)
"""
FUNC
"""
app.func.updateHovering(app)
speed = app.player.getMineSpeed(app)
if app.func.holding and time() - app.func.holding > (1 / speed) / 2:
app.func.handleClick(app)
app.func.holding = None
class App:
def __init__(self):
appStarted(self)
def main():
pygame.init()
screen = pygame.display.set_mode((500, 500), pygame.RESIZABLE)
pygame.display.set_caption("Terraria")
pygame.key.set_repeat(150, 30)
app = App()
app.clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT + 1, 20)
pygame.mouse.set_cursor(pygame.cursors.tri_left)
while True: # main event loop
app.game.loadChunks(app)
for event in pygame.event.get(): # loop through event queue
if event.type == QUIT:
return
elif event.type == KEYDOWN:
keyPressed(app, event.key)
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
mousePressed(app, event.pos)
elif event.type == MOUSEMOTION:
mouseMoved(app, event.pos)
elif event.type == MOUSEBUTTONUP:
mouseReleased(app)
elif event.type == VIDEORESIZE:
app.width = screen.get_width()
app.height = screen.get_height()
sizeChanged(app)
elif event.type == pygame.USEREVENT + 1:
timerFired(app)
redrawAll(app, screen)
pygame.display.flip()
app.clock.tick(60)
if __name__ == "__main__":
main()