-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEV_S_1_F.py
More file actions
218 lines (184 loc) · 5.13 KB
/
EV_S_1_F.py
File metadata and controls
218 lines (184 loc) · 5.13 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
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
# Made by: obayed elsayed
# Version 1.3, first phase: map generation
# Finished save option
# need to make save option more usable, (add save to txt option and retrieve info from txt)
# need to transfer code over to java script for better animations tkinter is too annoying to deal with
# Start Phase / version two and implement the creature class
import random
from map import *
from tkinter import *
grid=[]
seed_save='' #gets save string (seed to be added)
length=[100,200] ##### Y,X CAREFUL Nmap_characterT X,Y!!!
rand_val_init=0 # fil value used for seed
#literally map character!
map_character='8'
def init():
global length
global grid
temp=[]
copy=[]
for i in range(length[1]):
temp.append(map_character)
# I had to make a copy of temp and append that instead, cuz it makes a reference to every single list in grid, as the same list
# basically changing one list, changes the rest so leave the copy stuff
for j in range(length[0]):
copy=temp.copy()
grid.append(copy)
def display():
global grid
for i in range(len(grid)):
print(''.join(grid[i]))
def seed():
#fill value is the chance of the grid either beeing an map_character or a blank
global grid
global seed_save
for i in range(1,length[0]-1):
for j in range(1,length[1]-1):
fill_value=random.random()
if (fill_value>0.55):
grid[i][j]=' '
# save section
for i in range(length[0]):
seed_save+= ''.join(grid[i])+'\n'
def is_empty(y,x):
if (grid[y][x]==" "):
return True
else:
return False
# cell life rules, 3 neightbours, lives
# if empty gets filled, if 3 neighbours
def count_neighbours(y,x):
neighbours=0
if (y==0 or y== length[0]-1 or x==0 or x== length[1]-1):
return(False)
if grid[y-1][x]==map_character:
neighbours+=1
if grid[y+1][x]==map_character:
neighbours+=1
if grid[y][x-1]==map_character:
neighbours+=1
if grid[y][x+1]==map_character:
neighbours+=1
return neighbours
def count_UP_neighbours(y,x): # not used yet, use to smooth out map more
neighbours=0
if (y==0 or y== length[0]-1 or x==0 or x== length[1]-1):
return(False)
if grid[y-1][x]==map_character:
neighbours+=1
if grid[y+1][x]==map_character:
neighbours+=1
if grid[y][x-1]==map_character:
neighbours+=1
if grid[y][x+1]==map_character:
neighbours+=1
return neighbours
def count_side_neighbours(y,x):
neighbours=0
if (y==0 or y== length[0]-1 or x==0 or x== length[1]-1):
return(False)
if grid[y-1][x-1]==map_character:
neighbours+=1
if grid[y-1][x+1]==map_character:
neighbours+=1
if grid[y+1][x-1]==map_character:
neighbours+=1
if grid[y+1][x+1]==map_character:
neighbours+=1
return neighbours
#for i in range(1,length[0]-1): # y row
# for j in range(1,length[1]-1): # x row
def generate():
global grid
for i in range(1,length[0]-1): # y row
for j in range(1,length[1]-1): # x row
if(count_neighbours(i,j)<1):
if(is_empty(i,j)):
grid[i][j]=map_character
else:
grid[i][j]=" "
def clean_fill():
for i in range(1,length[0]-1): # y row
for j in range(1,length[1]-1): # x row
if(count_neighbours(i,j)>=3):
if(is_empty(i,j)==True):
grid[i][j]=map_character
def clean_space():
for i in range(1,length[0]-1): # y row
for j in range(1,length[1]-1): # x row
if(count_neighbours(i,j)<2):
if(is_empty(i,j)==False):
grid[i][j]=" "
init()
seed()
# main body
display()
for i in range(200):
#display()
#print("\n")
generate()
display()
for i in range(5):
clean_fill()
clean_space()
display()
# end of main body
# UI, and save options functions
def save():
create_save=open("test.txt","w")
create_save.write(seed_save)
create_save.close()
def open_save():
global save
open_save=open('test.txt','r')
save=open_save.readlines()
open_save.close()
print("Got save successfully")
def present():
global save
print(save)
def get_save():
global grid
grid[:] = []
for i in range(len(save)):
grid.append(list(save[i].strip("\n")))
for i in range(length[0]):
print(''.join(grid[i]))
print("\n")
def test():
print("test")
def gen():
for i in range(400):
#display()
#print("\n")
generate()
display()
for i in range(5):
clean_fill()
clean_space()
display()
window = Tk()
# create a menu
menu = Menu(window)
window.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Save", command=save)
filemenu.add_command(label="open...", command=open_save)
filemenu.add_command(label="Show", command=present)
filemenu.add_command(label="Get", command=get_save)
filemenu.add_command(label="Gen", command=gen)
points=[]
canvas=Canvas(window,width = 1300, height=750)
canvas.pack()
#points.append(canvas.create_rectangle(10,5,10,10,fill="black"))
for i in range(length[0]):
for j in range(length[1]):
if grid[i][j]== map_character:
points.append(canvas.create_rectangle(j+j*5,i+i*5,j+j*5+5,i+5+i*5,fill="black"))
else:
#print(True)
points.append(canvas.create_rectangle(j+j*5,i+i*5,j+j*5+5,i+5+i*5,fill="green"))
print(len(points))
mainloop()