-
Notifications
You must be signed in to change notification settings - Fork 3
/
PB.py
532 lines (513 loc) · 26.6 KB
/
PB.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import random
import os, pygame, sys
from pygame.locals import *
import spritesheet
import sound
import math
from monster import Monster
from map import Level
import sys
import os
#Utility functions
def find(f, seq):
i=0
for item in seq:
if item==f:
return i
i+=1
def ispressing(buttontopleft, buttonbottomright, mousex, mousey):
if mousex>buttontopleft[0] and mousex<buttonbottomright[0]:
if mousey>buttontopleft[1] and mousey<buttonbottomright[1]:
return True
return False
def trunc(f, n):
slen = len('%.*f' % (n, f))
try:
return int(str(f)[:slen])
except:
return int(str(f)[:slen-1])
#----End of Utility Functions----#
class PixelBuild:
def __init__(self):
#initialize timing variable
self.timing=0
# initialize sounds
pygame.mixer.init()
#looping
self.looping=True
#set variable shop for shop image
self.shop = pygame.image.load("sprites/shop.bmp")
self.shop.set_colorkey((255,255,255))
#initialize if running or not
self.running = True
#variable with players coins
self.coins=0
#cheating!!!!
self.godmode=False
#sounds...
self.placesound=pygame.mixer.Sound("sounds/place.wav")
self.deletesound=pygame.mixer.Sound("sounds/delete.wav")
#game icon
self.icon = pygame.image.load("sprites/game.bmp")
#set alpha color
self.icon.set_colorkey((0,0,0))
#set icon
pygame.display.set_icon(self.icon)
#initialize level
self.level = Level()
try:
#try to load file
self.level.loadFile("level0000.map")
except:
# if i cant load file create it from default.map
os.system ("cp %s %s" % ("default.map", "level0000.map"))
#load the file i created
self.level.loadFile("level0000.map")
#Spawn either a forest or clearing
if random.randint(1,8)==1:
for i in range(2):
self.level.spawntree()
del(i)
else:
for i in range(5):
self.level.spawntree()
del(i)
# Initialize pygame
pygame.init()
# Initialize pygame window size 800 x 600
self.screen = pygame.display.set_mode((800, 600))
# Set window title
pygame.display.set_caption('PixelBuild')
# Run titlescreen scene
self.titlescreen()
def titlescreen(self):
#get the rectangle from the icom
iconRect = self.icon.get_rect()
#set icon into the center
iconRect.centerx = self.screen.get_rect().centerx
#set the y value of icon
iconRect.y=0
#blit the icon onto the screen
self.screen.blit(self.icon, iconRect)
#set title
font1 = pygame.font.Font(None, 72)
text1 = font1.render('PixelBuild', True, (255, 255, 255))
textRect1 = text1.get_rect()
textRect1.centerx = self.screen.get_rect().centerx
textRect1.y = 100
self.screen.blit(text1, textRect1)
#set instructions
font2 = pygame.font.Font(None, 17)
text2 = font2.render('Press <Enter> To Play', True, (255, 255, 255))
textRect2 = text2.get_rect()
textRect2.centerx = self.screen.get_rect().centerx
textRect2.y = 150
self.screen.blit(text2, textRect2)
# Update the screen
pygame.display.update()
# Wait for enter to be pressed
# The user can also quit
waiting=True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
waiting=False
running=False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
waiting = False
self.loading()
def loading(self):
#make spritesheet
self.ss=spritesheet.spritesheet("sprites/spritesheet.bmp")
#render the background
self.background = self.level.render(self.ss)
self.background.set_colorkey((255, 255, 255))
pygame.display.set_caption('PixelBuild')
self.character=self.ss.image_at(pygame.rect.Rect(96,64, 32, 32), colorkey = (255, 255, 255))
self.carrying = self.ss.image_at(pygame.rect.Rect(0,512, 32, 32), colorkey = (255, 255, 255))
#Create The Backgound
self.background.blit(self.character, (400, 300))
self.screen.blit(self.background, (-100, -100))
self.clock = pygame.time.Clock()
pygame.display.flip()
#ss.image_at(pygame.Rect(32, 96, 32, 32))
self.monsters=[]
monsternum=25
if self.level.monsters=={}:
for mon in range(monsternum):
self.monsters.append(Monster(ss.image_at(pygame.rect.Rect(96, 288, 32, 32), colorkey = (255, 255, 255)), 5, [random.randint(10, 1280), random.randint(10, 1280)], level))
del(mon)
else:
for mon in self.level.monsters:
self.monsters.append(Monster(self.ss.image_at(pygame.rect.Rect(96, 288, 32, 32), colorkey = (255, 255, 255)), 5, [int(self.level.monsters[mon]["x"]), int(self.level.monsters[mon]["y"])], self.level))
del(mon)
self.defaultmap=open("default.map", "r").read()
self.xoffset=0
self.yoffset=0
self.selected=""
self.monsterspeed=7
self.monsterdirection=random.randint(0,4)
self.inv=['1', '2', '3', '4', '5', '6','7', '8', '9','0', 'q', 'b', 't', 'i', 'd', 'g', 'tr', 'w', "wf"]
self.invcount=[100, 100,100, 100,100, 100, 100,100, 100,100, 100,0, 0, 0, 1, 0, 5, 0, 0]
self.currentselected=1
self.currentselectednum=self.invcount[self.currentselected-1]
self.playerx=400
self.playery=300
self.chunk=(0,0,0,0)
pygame.key.set_repeat(1,25)
self.background = self.level.render(self.ss)
self.background.set_colorkey((255, 255, 255))
self.currentbox=pygame.Surface((48, 64))
self.currentbox.fill((255,255,255))
self.running=True
self.dead=False
self.update()
def update(self):
#update shop item
shopitem=self.ss.image_at(pygame.rect.Rect(((int(self.level.key[str(self.inv[self.currentselected-1])]['tile'].split(", ")[0])-1)*32, (int(self.level.key[str(self.inv[self.currentselected-1])]['tile'].split(", ")[1])-1)*32, 32, 32)))
# client.Loop()
#if len(self.blockdata)>1:
# print block
self.currentbox.fill((255,255,255))
self.currentselectednum=self.invcount[self.currentselected-1]
if self.dead:
self.looping=False
self.time = pygame.time.get_ticks()
self.screen.fill(0)
self.screen.blit(self.background, (0+self.xoffset, 0+self.yoffset))
selected = shopitem
self.currentbox.blit(selected, (8,24))
font = pygame.font.Font(None, 13)
# Render the text
text = font.render('Currently', True, (0,0, 0), (255, 255, 255))
text1 = font.render('Selected', True, (0,0, 0), (255, 255, 255))
text2 = font.render(str(self.currentselectednum), True, (0,0, 0), (255, 255, 255))
self.currentbox.blit(text, (4,4))
self.currentbox.blit(text1, (8,14))
text2.set_colorkey((255, 255, 255))
self.currentbox.blit(text2, (20 ,56))
self.screen.blit(self.currentbox, (0, 536))
self.screen.blit(self.character, (400, 300))
self.screen.blit(self.carrying, (400, 300))
self.screen.blit(self.shop, (117, 10))
font2 = pygame.font.Font("fonts/Minecraftia.ttf", 14)
cointext = font2.render(str(self.coins), True, (0, 0, 0), (127, 127, 127))
self.screen.blit(cointext, (271, 35))
self.screen.blit(selected, (132, 18))
pygame.display.flip()
if not self.dead:
self.background.fill(0)
self.chunk1=[str(self.chunk[0]), str(self.chunk[1]), str(self.chunk[2]), str(self.chunk[3])]
self.timing+=1
print(self.monsters)
for mon in self.monsters:
self.monsterstat=mon.update(self.time, 150, self.playerx, self.playery, self.timing)
if self.monsterstat==0 and self.godmode==False:
self.dead=True
elif self.monsterstat>0:
self.coins+=1
self.background = self.level.render(self.ss)
self.background.set_colorkey((255, 255, 255))
for mon in self.monsters:
self.background.blit(mon.image, (mon.rect[0],mon.rect[1]))
for event in pygame.event.get():
if event.type==QUIT:
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.writeConfig("level"+"".join(self.chunk1)+".map", self.monsters,True, invcount, player=(self.playerx, self.playery))
pygame.quit()
running=False
sys.exit()
break
elif event.type == KEYDOWN:
if event.key == K_w:
if self.playery>0:
if self.level.getTile(trunc((self.playerx+16)/32,0), trunc((self.playery+4)/32,0))["name"]=="floor":
self.yoffset+=5
self.playery-=5
elif self.level.getTile(trunc((self.playerx+16)/32,0), trunc((self.playery+4)/32,0))["name"]=="pool":
self.yoffset+=3
self.playery-=3
else:
self.level.writeConfig("level"+"".join(self.chunk1)+".map", self.monsters)
#1280, -980, 2
screen.fill(0)
self.playery=1280
self.yoffset=-980
chunkb=chunk
if chunk[2]==0:
chunk=[chunk[0]+1, chunk[1], chunk[2], chunk[3]]
else:
chunk=[chunk[0], chunk[1], chunk[2]-1, chunk[3]]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
if os.path.exists("level"+"".join(self.chunk1)+".map")!=True:
open("level"+"".join(self.chunk1)+".map","w").write(defaultmap)
self.level.loadFile("level"+"".join(self.chunk1)+".map")
if random.randint(1,2)==1:
for i in range(5):
level.spawntree()
else:
for i in range(2):
self.level.spawntree()
del(i)
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
else:
chunk=chunkb
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.writeConfig("level"+"".join(self.chunk1)+".map", [])
if chunk[2]==0:
chunk=[chunk[0]+1, chunk[1], chunk[2], chunk[3]]
else:
chunk=[chunk[0], chunk[1], chunk[2]-1, chunk[3]]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.loadFile("level"+"".join(self.chunk1)+".map")
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
if event.key == K_s:
try:
if self.level.getTile(trunc((self.playerx+16)/32,0), trunc((self.playery+25)/32,0))["name"]=="floor":
self.yoffset-=5
self.playery+=5
elif self.level.getTile(trunc((self.playerx+16)/32,0), trunc((self.playery+25)/32,0))["name"]=="pool":
self.yoffset-=3
self.playery+=3
except KeyError:
self.level.writeConfig("level"+"".join(self.chunk1)+".map", self.monsters)
screen.fill(0)
self.playery=0
self.yoffset=300
chunkb=chunk
if chunk[0]==0:
chunk=[chunk[0], chunk[1], chunk[2]+1, chunk[3]]
else:
chunk=[chunk[0]-1, chunk[1], chunk[2], chunk[3]]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
if os.path.exists("level"+"".join(self.chunk1)+".map")!=True:
open("level"+"".join(self.chunk1)+".map","w").write(defaultmap)
self.level.loadFile("level"+"".join(self.chunk1)+".map")
if random.randint(1,2)==1:
for i in range(5):
self.level.spawntree()
else:
for i in range(2):
self.level.spawntree()
del(i)
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
else:
chunk=chunkb
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.writeConfig("level"+"".join(self.chunk1)+".map", [])
if chunk[0]==0:
chunk=[chunk[0], chunk[1], chunk[2]+1, chunk[3]]
else:
chunk=[chunk[0]-1, chunk[1], chunk[2], chunk[3]]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.loadFile("level"+"".join(self.chunk1)+".map")
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
if event.key == K_a:
if self.level.getTile(trunc((self.playerx+12)/32,0), trunc((self.playery+16)/32,0))["name"]=="floor" and self.playerx>-5:
self.playerx-=5
self.xoffset+=5
elif self.level.getTile(trunc((self.playerx+12)/32,0), trunc((self.playery+16)/32,0))["name"]=="pool":
self.xoffset+=3
self.playerx-=3
if self.playerx<=-5:
self.level.writeConfig("level"+"".join(self.chunk1)+".map", self.monsters)
screen.fill(0)
#Figure out xoffset
self.playerx=900
self.xoffset=-496
chunkb=chunk
if chunk[3]==0:
chunk=[chunk[0], chunk[1]+1, chunk[2], chunk[3]]
else:
chunk=[chunk[0], chunk[1], chunk[2], chunk[3]-1]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
if os.path.exists("level"+"".join(self.chunk1)+".map")!=True:
open("level"+"".join(self.chunk1)+".map","w").write(defaultmap)
self.level.loadFile("level"+"".join(self.chunk1)+".map")
if random.randint(1,2)==1:
for i in range(5):
self.level.spawntree()
else:
for i in range(2):
self.level.spawntree()
del(i)
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
else:
chunk=chunkb
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.writeConfig("level"+"".join(self.chunk1)+".map", [])
if chunk[3]==0:
chunk=[chunk[0], chunk[1]+1, chunk[2], chunk[3]]
else:
chunk=[chunk[0], chunk[1], chunk[2], chunk[3]-1]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.loadFile("level"+"".join(self.chunk1)+".map")
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
if event.key == K_d:
try:
if self.level.getTile(trunc((self.playerx+25)/32,0), trunc((self.playery+16)/32,0))["name"]=="floor" and self.playerx<40*32:
self.xoffset-=5
self.playerx+=5
elif self.level.getTile(trunc((self.playerx+25)/32,0), trunc((self.playery+16)/32,0))["name"]=="pool":
self.xoffset-=3
self.playerx+=3
except KeyError:
self.level.writeConfig("level"+"".join(self.chunk1)+".map", self.monsters)
screen.fill(0)
#Figure out xoffset
self.playerx=0
self.xoffset=400
chunkb=chunk
if chunk[1]==0:
chunk=[chunk[0], chunk[1], chunk[2], chunk[3]+1]
else:
chunk=[chunk[0], chunk[1]-1, chunk[2], chunk[3]]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
if os.path.exists("level"+"".join(self.chunk1)+".map")!=True:
open("level"+"".join(self.chunk1)+".map","w").write(defaultmap)
self.level.loadFile("level"+"".join(self.chunk1)+".map")
if random.randint(1,2)==1:
for i in range(5):
self.level.spawntree()
else:
for i in range(2):
self.level.spawntree()
del(i)
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
else:
chunk=chunkb
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.writeConfig("level"+"".join(self.chunk1)+".map", [])
if chunk[1]==0:
chunk=[chunk[0], chunk[1], chunk[2], chunk[3]+1]
else:
chunk=[chunk[0], chunk[1]-1, chunk[2], chunk[3]]
self.chunk1=[str(chunk[0]), str(chunk[1]), str(chunk[2]), str(chunk[3])]
self.level.loadFile("level"+"".join(self.chunk1)+".map")
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(character, (400, 300))
pygame.display.flip()
if event.key == K_1:
currentselected=1
if event.key == K_2:
currentselected=2
if event.key == K_3:
currentselected=3
if event.key == K_4:
currentselected=4
if event.key == K_5:
currentselected=5
if event.key == K_6:
currentselected=6
if event.key == K_7:
currentselected=7
if event.key == K_8:
currentselected=8
if event.key == K_9:
currentselected=9
if event.key == K_0:
currentselected=0
#4 is down and 5 is up
if event.type == MOUSEBUTTONDOWN and event.button==4:
if currentselected==0:
currentselected=len(inv)
else:
currentselected-=1
elif event.type == MOUSEBUTTONDOWN and event.button==5:
if currentselected==len(inv):
currentselected=0
else:
currentselected+=1
elif event.type==MOUSEBUTTONDOWN and event.button==1:
if ispressing((122,22), (129, 46), pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
if currentselected==0:
currentselected=len(inv)
else:
currentselected-=1
elif ispressing((167,22), (174, 42), pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
if currentselected==len(inv):
currentselected=0
else:
currentselected+=1
elif ispressing((358,16), (501, 52), pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
if coins>0:
coins-=1
self.invcount[currentselected-1]+=5
else:
if self.invcount[currentselected-1]>0:
placesound.play()
mouse=[pygame.mouse.get_pos()[0]-400, pygame.mouse.get_pos()[1]-300]
try:
if self.level.getTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0))["name"]=="floor":
if self.level.getTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), False)!=inv[currentselected-1]:
self.level.setTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), str(inv[currentselected-1]))
# client.Client.newdata=["placeblock", []]
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(self.currentbox, (0, 536))
screen.blit(character, (400, 300))
screen.blit(shop, (117, 10))
screen.blit(cointext, (550, 25))
invcount[currentselected-1]-=1
for mon in self.monsters:
background.blit(mon.image, (mon.rect[0],mon.rect[1]))
pygame.display.flip()
except:
pass
elif event.type==MOUSEBUTTONDOWN and event.button==3:
try:
deletesound.play()
mouse=[pygame.mouse.get_pos()[0]-400, pygame.mouse.get_pos()[1]-300]
invcount[find(self.level.getTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), False), inv)]+=1
if self.level.getTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0))['name'] in ['trap', 'wall']:
self.level.setTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), "g")
else:
self.level.setTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), "7")
background = self.level.render(ss)
background.set_colorkey((255, 255, 255))
screen.blit(background, (0+self.xoffset, 0+self.yoffset))
screen.blit(self.currentbox, (0, 536))
screen.blit(character, (400, 300))
for mon in self.monsters:
background.blit(mon.image, (mon.rect[0],mon.rect[1]))
pygame.display.flip()
except TypeError:
if self.level.getTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), False)=="le":
invcount[find("4",inv)]+=1
self.level.setTile(trunc(((mouse[0]*1.0/32))+self.playerx*1.0/32, 0), trunc(((mouse[1]*1.0/32))+self.playery*1.0/32,0), "g")
if self.looping:
self.update()
p=PixelBuild()