-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jeu 2D Pygame 2.py
299 lines (256 loc) · 8.84 KB
/
Jeu 2D Pygame 2.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
#-----------#
#Importation#
#-----------#
from Fonctions import *
import pygame
from pygame.locals import *
import pickle
#-------#
#Mémoire#
#-------#
rep="/Users/olivierpartensky/Desktop/Programme/GitHub/python_projects/Expanse/Version 1.1"
def Init():
#COLORS#
COLORS=SortirVariable(rep+"COLORS")
try:
COLORS=COLORS[0]
except:
COLORS={"made":0,
"colors":{
"BLACK": ( 0, 0, 0),
"WHITE": (255,255,255),
"RED": (255, 0, 0),
"GREEN": ( 0,255, 0),
"BLUE": ( 0, 0,255),
"CYAN": ( 0,255,255),
"PURPLE": (255, 0,255),
"YELLOW": (255,255, 0),
"BROWN": (150, 75, 0),
"ORANGE": (255,140, 0),
"CRANDOM":(random.randint(0,255),random.randint(0,255),random.randint(0,255))
}
}
CLS=[value for value in COLORS["colors"].values()]
print("COLORS initialisé")
#BLOCKS#
BLOCKS=SortirVariable(rep+"BLOCKS")
try:
BLOCKS=BLOCKS[0]
except:
BLOCKS={"made":0,
"blocks":{}
}
print("BLOCKS initialisé")
#MAPS#
MAPS=SortirVariable(rep+"MAPS")
try:
MAPS=MAPS[0]
except:
MAPS={"made":0,
"maps":{}
}
print("MAPS initialisé")
def Save():
StockerVariable(BLOCKS,rep+"BLOCKS","w")
StockerVariable(COLORS,rep+"COLORS","w")
def Quit():
Save()
quit()
Init()
#------------#
#Organisation#
#------------#
"""
Organisation={"Name":None,
"Author":"Marc Partensky",
"Windows":
{"Menu":None,
"Maps":
{"Name":None,
"Square":"Pixels"
}
"Credits":None
}
}
"""
#--------------------#
#Classes et Fonctions#
#--------------------#
#Extra#
class Editing:
def __init__(self,ci=0,mx=100,my=50,bx=5,by=5):
self.mci=ci
self.mx=mx
self.my=my
self.bci=ci
self.bx=bx
self.by=by
#Primary#
#MAP.map[y][x].blocks[y][x]
#MAP.map[y][x]
class Map:
made=0
def __init__(self,name,typ=0,value=None,tx=200,ty=100,size=5):
self.version=1
self.map=[[Square(typ,value) for x in range(tx)] for y in range(ty)]
self.id=Map.made
self.tx=tx
self.ty=ty
self.size=size
self.value=value
self.name=name
Map.made+=1
def show(self):
pygame.init()
screen_size=(self.tx*self.size,self.ty*self.size)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Showing Mode")
s=self.size
print(s)
for y in range(self.ty):
for x in range(self.tx):
if self.map[y][x].typ==0:
pygame.draw.rect(screen, self.map[y][x].color, (x*s,y*s,s,s), 0)
if self.map[y][x].typ==1:
for yi in range(self.size):
for xi in range(self.size):
pygame.draw.rect(screen, BLOCKS[str(s)][self.value].block[yi][xi], (x*s+xi,y*s+yi,1,1), 0)
pygame.display.flip()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == KEYDOWN:
done = True
pygame.quit()
def edit(self):
pygame.init()
screen_size=(self.tx*self.size,self.ty*self.size)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Editing Mode")
s=self.size
for y in range(self.ty):
for x in range(self.tx):
if self.map[y][x].typ==0:
pygame.draw.rect(screen, self.map[y][x].color, (x*s,y*s,s,s), 0)
if self.map[y][x].typ==1:
for yi in range(self.size):
for xi in range(self.size):
sb=self.map[y][x].size
pygame.draw.rect(screen, self.map[y][x].block[yi][xi], (x*s+xi,y*s+yi,sb,sb), 0)
pygame.display.flip()
e=Editing()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == MOUSEBUTTONDOWN and event.button == 1:
e.mx=event.pos[0]//s
e.my=event.pos[1]//s
if event.type == pygame.K_UP and my<s-1:
e.my+=1
if event.type == pygame.K_DOWN and my>0:
e.my-=1
if event.type == pygame.K_LEFT and mx>0:
e.mx-=1
if event.type == pygame.K_RIGHT and mx<s-1:
e.mx+=1
if event.type == K_RETURN:
e.mci=(e.mci+1)%len(CLS)
if self.map[e.my][e.mx].typ==0:
self.map[e.my][e.mx]=Square(0,e.mci)
pygame.draw.rect(screen, CLS[e.mci], (e.mx*s,e.my*s,s,s), 0)
pygame.display.flip()
pygame.quit()
def save(self):
StockerVariable(self,"Carte"+" "+self.name+" "+str(self.id),"w")
class Square:
made=0
def __init__(self,typ=0,value=0,size=5):
self.value=value
self.version=1
self.typ=typ
self.id=Square.made
Square.made+=1
if typ==0:
self.color=CLS[value]
if typ==1:
self.block=BLOCKS[str(size)][value]
class Block:
made=0
def __init__(self,name=str(BLOCKS["made"]),size=5,color=WHITE):
BLOCKS["made"]+=1
self.id=Block.made
self.size=size
self.name=name
self.block=[[color for x in range(size)] for y in range(size)]
Block.made+=1
def show(self,sx=700,sy=700):
size=self.size
tx=sx//size
ty=sy//size
pygame.init()
screen_size=(tx*size,ty*size)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Showing Mode")
for y in range(size):
for x in range(size):
pygame.draw.rect(screen, self.block[y][x], (x*tx,y*ty,tx,ty), 0)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == KEYDOWN:
done = True
pygame.quit()
def edit(self,sx=700,sy=700):
size=self.size
tx=sx//size
ty=sy//size
pygame.init()
screen_size=(tx*size,ty*size)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Editing Mode")
for y in range(size):
for x in range(size):
pygame.draw.rect(screen, self.block[y][x], (x*tx,y*ty,tx,ty), 0)
e=Editing()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == MOUSEBUTTONDOWN and event.button == 1:
e.bx=event.pos[0]//tx
e.by=event.pos[1]//ty
if event.type == KEYDOWN:
if event.key == pygame.K_DOWN and e.by<size-1:
e.by+=1
if event.key == pygame.K_UP and e.by>0:
e.by-=1
if event.key == pygame.K_LEFT and e.bx>0:
e.bx-=1
if event.key == pygame.K_RIGHT and e.bx<size-1:
e.bx+=1
if event.key == pygame.K_n:
e.bci=(e.bci+1)%len(CLS)
if event.key == K_RETURN:
self.block[e.by][e.bx]=CLS[e.bci]
for y in range(size):
for x in range(size):
pygame.draw.rect(screen, self.block[y][x], (x*tx,y*ty,tx,ty), 0)
pygame.draw.rect(screen, CLS[e.bci], (e.bx*tx,e.by*ty,tx,ty), 0)
pygame.display.flip()
pygame.quit()
def write(self,size,value):
return Blocks.BLOCKS[str(size)][value]
def save(self,size=5):
BLOCKS[str(size)][self.name]=self.__dict__
def saveappart(self):
StockerVariable(self,rep+"Block: "+self.name,"w")
#-------#
#Actions#
#-------#