好吧,我正在用python编写Conway的游戏人生,虽然我的程序可以工作,但是用户给出初始点的方式是写下每个点的坐标。在
我想要一个显示笛卡尔平面整数坐标点的界面,让我用鼠标选择一些点,最后将我选择的点作为元组(或列表,无所谓)返回。 我应该下载某种程序还是有一个模块有这样的功能?在
我还想要求一些“程序”,而不是点,图形像素,像在这个video。现在,我只是使用matplotlib来绘制图形,但是我觉得像素看起来更酷。在
提前谢谢。 编辑:我不想发布代码,因为这么长时间,但它在这里。在
import matplotlib.pyplot as plt
from string import split
reglas=raw_input("Enter rule of the game(Ej.:B3/23)")
Q=[letra for letra in reglas]
m=Q.index("/")
born, keep=[int(i) for i in Q[1:m]], [int(i) for i in Q[m+1:]]
discard=[x for x in range(1,9) if x not in keep and x not in born]
livecells=set()
a=raw_input("Enter a coordinate (a,b), which will be a living cell. This program will keep you asking coordinates until you decide to stop. To stop, instead of a coordinate, enter stop")
a=tuple([int(float(i)) for i in split(a)])
livecells.add(a)
while True:
b=raw_input("Another coordinate")
if b!="stop":
b=tuple([int(i) for i in split(b)])
livecells.add(b)
else:
break
def whoisnear(a,b):
return set([(a+1,b),(a+1,b+1),(a+1,b-1),(a,b+1),(a,b-1),(a-1,b+1),(a-1,b),(a-1,b-1)])
def firstcoordinate(x,y):
return x
def secondcoordinate(x,y):
return y
def rectanglearea(c,d,e,f):
candidates=set()
for i in range (c-3,d+3):
for j in range(e-3,f+3):
candidates.add((i,j))
return candidates
Q=[x for (x,y) in livecells]
W=[y for (x,y) in livecells]
plt.plot(Q,W,'ro')
plt.axis([min(Q)-1,max(Q)+1,min(W)-1,max(W)+1])
plt.savefig('0.png')
plt.show()
repetitions=0
minx=min([firstcoordinate(*i) for i in livecells])
maxx=max([firstcoordinate(*i) for i in livecells])
miny=min([secondcoordinate(*i) for i in livecells])
maxy=max([secondcoordinate(*i) for i in livecells])
for j in range(1,30):
try:
X={firstcoordinate(*i) for i in livecells}
Y={secondcoordinate(*i) for i in livecells}
D=rectanglearea(min(X),max(X),min(Y),max(Y))
Todiscard=set()
Toadd=set()
for i in D:
K=whoisnear(*i) & livecells
n=len(K)
if n in discard:
Todiscard.add(i)
elif n in born:
Toadd.add(i)
livecells=livecells-Todiscard
livecells=livecells|Toadd
firstcords=[x for (x,y) in livecells]
secondcords=[y for (x,y) in livecells]
plt.plot(firstcords,secondcords,'ro')
plt.axis([minx-30,maxx+30,miny-30,maxy+30])
plt.savefig(str(j)+'.png')
plt.close()
except ValueError:
print j,livecells
break
这是我为扩展
pygame
模块而开发的脚本。我不想为你写代码,但这应该让你开始!我的代码有很好的文档,您应该很容易理解。祝你好运!在屏幕模块
主代码
^{pr2}$相关问题 更多 >
编程相关推荐