-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.py
166 lines (123 loc) · 4.63 KB
/
Map.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
#!/usr/bin/python
import pygame
import Resources
import random
from Object import *
class Map():
def __init__(self, empty = False):
self.objectList = []
self.objectSpritesList = pygame.sprite.Group()
self.screen = pygame.display.get_surface()
if not empty:
for i in range(0, 16):
objType = random.randint(1, 2)
if objType == 1:
self.objectList.append(Object(len(self.objectList), i * 50, 550, "earth"))
elif objType == 2:
self.objectList.append(Object(len(self.objectList), i * 50, 550, "ice"))
# else:
# self.objectList.append(Object(len(self.objectList), i * 50, 550, "boing"))
# for j in range(0, 30):
# objType = random.randint(1, 2)
# while True:
# randPosX = random.randint(0, 15) * 50
# randPosY = random.randint(1, 10) * 50
# if not self.isInBlock(randPosX + 10, randPosY + 10):
# break
# if objType == 1:
# self.objectList.append(Object(len(self.objectList), randPosX, randPosY, "earth"))
# else:
# self.objectList.append(Object(len(self.objectList), randPosX, randPosY, "ice"))
for k in range(0, 5):
while True:
randPos = random.randint(0, len(self.objectList) - 1)
if not self.isInBlock(self.objectList[randPos].getX() + 10, self.objectList[randPos].getY() - 26):
break
if self.objectList[randPos].getType() != "carrot":
self.objectList.append(Object(len(self.objectList), self.objectList[randPos].getX() + 10, self.objectList[randPos].getY() - 26, "carrot"))
for obj in self.objectList:
if not self.isFloor(obj):
obj.replaceImage(obj.getType())
self.objectSpritesList.add(pygame.sprite.RenderPlain(obj))
self.save("tempServer")
def update(self):
self.objectSpritesList.update()
self.objectSpritesList.draw(self.screen)
pygame.event.pump()
def updateFloor(self):
for obj in self.objectList:
if not self.isFloor(obj):
if obj.getType() == "boing":
obj.replaceImage("earth")
else:
obj.replaceImage(obj.getType())
else:
obj.replaceImage(obj.getType(), True)
self.objectSpritesList.add(pygame.sprite.RenderPlain(obj))
def addObject(self, x, y, type):
self.objectList.append(Object(len(self.objectList), x, y, type))
self.objectSpritesList.add(pygame.sprite.RenderPlain(self.objectList[-1]))
self.updateFloor()
def removeObject(self, obj):
self.objectSpritesList.remove(obj)
self.objectList.remove(obj)
self.updateFloor()
def removeObjectFromPos(self, (x, y)):
for obj in self.objectList:
if obj.isInBlock(x, y):
self.objectSpritesList.remove(obj)
self.objectList.remove(obj)
self.updateFloor()
def getObjectFromPos(self, (x, y)):
for obj in self.objectList:
if obj.isInBlock(x, y):
return obj
return Object(type = "empty")
def isInBlock(self, x, y):
for obj in self.objectList:
if obj.isInBlock(x, y):
return True
return False
def isFloor(self, object):
if self.isInBlock(object.getX() + 5, object.getY() - 5):
return False
return True
def getObjectList(self):
return self.objectList
def setObjectList(self, objectList):
self.objectList = objectList
for obj in self.objectList:
self.objectSpritesList.add(pygame.sprite.RenderPlain(obj))
def addCarrot(self):
while True:
randPos = random.randint(0, len(self.objectList) - 1)
if not self.isInBlock(self.objectList[randPos].getX() + 10, self.objectList[randPos].getY() - 26):
break
if self.objectList[randPos].getType() != "carrot":
self.objectList.append(Object(len(self.objectList), self.objectList[randPos].getX() + 10, self.objectList[randPos].getY() - 26, "carrot"))
for obj in self.objectList:
if not self.isFloor(obj):
obj.replaceImage(obj.getType())
self.objectSpritesList.add(pygame.sprite.RenderPlain(obj))
def save(self, name):
with open("save/maps/" + name + ".mabbit", "w") as f:
for obj in self.objectList:
f.write(obj.getType() + ":" + str(obj.getX()) + ", " + str(obj.getY()) + "\n")
def saveFromStr(self, name, string):
with open("save/maps/" + name + ".mabbit", "w") as f:
f.write(string)
def load(self, name):
self.objectList = []
self.objectSpritesList = pygame.sprite.Group()
with open("save/maps/" + name + ".mabbit", "r") as f:
for line in f:
line = line.strip("\n")
self.objectList.append(Object(len(self.objectList), int(line.split(":")[1].split(",")[0]), int(line.split(":")[1].split(",")[1]), line.split(":")[0]))
for obj in self.objectList:
self.objectSpritesList.add(pygame.sprite.RenderPlain(obj))
self.updateFloor()
def getMapStr(self):
mapStr = ""
for obj in self.objectList:
mapStr = mapStr + obj.getType() + ":" + str(obj.getX()) + ", " + str(obj.getY()) + "\n"
return mapStr