2016-12-31 17 views
0

嗯,我正在編寫康威的Python生活遊戲,儘管我的程序工作正常,但用戶給出初始點的方式是編寫每個點的座標。如何顯示一個界面來選擇python中的點?

我想要一個界面,顯示笛卡爾平面的整數座標點,讓我用鼠標選擇一些,最後給出我選作元組(或列表,無所謂)的點。 我應該下載某種程序還是有一個具有此類功能的模塊?

我還想問一些「程序」,而不是點,像素圖,就像這個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 
+0

你能提供你的代碼,我或許可以提供一些基本的接口 – TheLazyScripter

+0

問題,要求我們建議還是找一本書,工具,軟件庫,教程或其他非現場資源對於堆棧溢出而言是無關緊要的,因爲它們傾向於吸引自以爲是的答案和垃圾郵件。相反,請描述問題以及到目前爲止解決問題所做的工作。請參閱[我可以在這裏詢問什麼主題?](http://stackoverflow.com/help/on-topic)。 –

+0

@Rory Daulton我不認爲我的問題是基於意見的,我只希望一個單一的程序能夠做到我想要的,我不問問哪個是最好的程序。當然,我寧願它很簡單,因爲我在編程方面很新,但這是一個意見問題。我只是想替代品。 – Diego

回答

1

這是我爲擴展pygame模塊而開發的一個腳本。我不想爲你寫代碼,但這應該讓你開始!我的代碼有很好的文檔記錄,應該很容易理解。祝你好運!

屏模塊

import pygame, sys, ctypes, os 
from pygame.locals import * 
import random 

class Screen: 
    ''' 
     --Screen module for handling the pygame display module... 
    ''' 

    def __init__(self, resolution=(640,480), caption=None, transparent=False, icon=None, flags=0): 
     ''' 
      --Grabs and sets the args 
      --Grabs the center of the screen based on the monitor resolution 
      --Creates a new instance of screen 
      ''' 

     self.resolution = resolution 
     self.flags = flags 

     if caption: 
      self.setCaption(caption) 
     if icon: 
      self.setIcon(icon) 

     self.monitorCenter = self.getCenterScreen(self.getMonitorSize()) 

     self.centerScreen() 

     self.transparent = transparent 

     self.screen = self.setScreen() 

    def update(self): 
     pygame.display.update() 


    def setIcon(self, icon): 
     ''' 
      --sets the icon at the icon position on 
       the pygame window. Maintain origonal 
       .jpg location 

      Usage: 
       --self.setIcon(self, 'coolIcon.jpg' 
       ''' 

     try: 
      self.icon = icon 
      icon = pygame.image.load(icon) 
     except: 
      self.icon = None 
      return 
     pygame.display.set_icon(icon) 

    def setScreen(self): 
     ''' 
      --creates and returns default pygame window... 

      Usage: 
       --screen.setScreen() 
      ''' 
     if self.transparent: 
      return pygame.display.set_mode(self.resolution, self.flags).convert_alpha() 
     return pygame.display.set_mode(self.resolution, self.flags) 

    def setCaption(self, caption): 
     ''' 
      --Sets window caption as (caption)... 

      Usage: 
       --screen.setCaption("My Game!") 
      ''' 

     pygame.display.set_caption(caption) 

    def didResize(self): 
     info = pygame.display.get_surface() 
     return info 

    def setMousePos(self, pos): 
     ''' 
      --Sets the mouse at [pos]... 

      Usage: 
       --screen.setMousePos((250,250)) 
      ''' 

     pygame.mouse.set_pos(pos) 


    def setScreenPos(self, position): 
     ''' 
      --Sets the windows window argument position to (position) 

      Usage: 
       --screen.setScreenPos((250,250)) 
      ''' 

     os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0])+','+str(position[1]) 

    def getMonitorSize(self): 
     ''' 
      --Returns the actual resolution of the active display 

      Usage: 
       --screen.getMonitorSize() 
      ''' 

     user32 = ctypes.windll.user32 
     screenSize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1) 
     return screenSize 

    def getCenterScreen(self, res): 
     ''' 
      --Returns the center of the screen based on the monitor 
      --resolution [res]/2 - window resolution [self.resolution]/2 

      Usage: 
       --screen.getCenterScreen((1365,782)) 
      ''' 

     center = ((res[0]/2)-self.resolution[0]/2), ((res[1]/2)-self.resolution[1]/2) 
     return center 

    def centerScreen(self): 
     ''' 
      --sets the center of the pygame window to the 
      --center of the monitor 
      --centerScreen() needs to be called before 
      --setScreen() in order for it to center... 

      Usage: 
       --screen.centerScreen() 
      ''' 

     centerScreen = self.getCenterScreen(self.getMonitorSize()) 
     self.setScreenPos(centerScreen) 

    def getFlags(self): 
     ''' 
      --Returns the flags associated the the active screen... 

      Usage: 
       --screen.getFlags() 
      ''' 

     return self.getScreenSurface().get_flags() 

    def getScreenSurface(self): 
     ''' 
      --Returns the surface object of the active screen... 

      Usage: 
       --screen.getScreenSurface() 
      ''' 

     return pygame.display.get_surface() 

    def getCaption(self): 
     ''' 
      --Returns the current caption associated with the active screen... 

      Usage: 
       --screen.getCaption() 
      ''' 

     return pygame.display.get_caption()[0] 

    def getMousePos(self): 
     ''' 
      --Returns the current mouse position [x,y] on the window... 

      Usage: 
       --screen.getMousePos() 
      ''' 
     return pygame.mouse.get_pos() 

    def closeScreen(self): 
     ''' 
      --De-initializes the screen object... 

      Usage: 
       --screen.closeScreen() 
      ''' 

     pygame.display.quit() 

    def initScreen(self): 
     ''' 
      --Re-initializes the screen object... 

      Usage: 
       --screen.initScreen() 
      ''' 

     pygame.display.init() 

    def resize(self, size): 
     ''' 
      --Creates a new screen instance with resolution of (size)... 

       --Overwrites the self.screen object with new size 
       --Grabs the New Size, current Caption, and current Flags 
       --Closes the current screen instance 
       --Creates a new screen instance 
       --Places the center of the screen at the center of the monitor 
       --Checks if flags contains FULLSCREAN if so it toggles 
       --Creates a new display instance with resolution[x,y], and flags(flags) 
       --Sets the screen caption 

       Usage: 
        --screen.resize((640,480)) 
      ''' 

     self.resolution = size 
     caption = self.getCaption() 
     self.flags = self.getFlags() 

     self.closeScreen() 
     self.initScreen() 

     self.centerScreen() 

     if self.flags == FULLSCREEN: 
      print self.flags 
      self.screen = pygame.display.set_mode((self.resolution),self.flags^FULLSCREEN) 
     else: 
      self.screen = pygame.display.set_mode((self.resolution),self.flags) 
     self.setCaption(caption) 

    def toggleFullscreen(self): 
     ''' 
      Module for toggeling between default resolution and fullscreen... 

       --Grabs the current screen CAPTION, FLAGS, and MOUSE POSITION 
       --Grabs the current surface size 
       --Checks to see what resolution the screen is currently at and toggles it 
       --Overwrites the self.screen object with new size 
       --Closes the current screen instance 
       --Creates a new screen instance 
       --Places the center of the screen at the center of the monitor 
       --Creates a new display instance with resolution[x,y], and flags^FULLSCREEN(toggle) 
       --Sets the new screens caption with old caption 

       Usage: 
        --screen.toggleFullscreen() 
      ''' 

     caption = self.getCaption() 
     self.flags = self.getFlags() 

     surface = self.getScreenSurface() 
     size = surface.get_width(), surface.get_height() 
     if size == self.resolution: 
      size = self.getMonitorSize() 
     else: 
      size = self.resolution 

     self.closeScreen() 
     self.initScreen() 

     self.centerScreen() 
     self.screen = pygame.display.set_mode((size),self.flags^FULLSCREEN) 
     self.setCaption(caption) 
     self.setIcon(self.icon) 

class Grid(object): 
    def __init__(self, screen, size=(16,16)): 
     self.screen = screen 
     self.surface = self.screen.screen 
     self.size = size 
     self.grid = tuple(((x,y) for y in range(0, self.screen.resolution[1], size[1]) for x in range(0, self.screen.resolution[0], size[0]))) 
     self.elements = self.create_elements() 
     self.active_elements = [] 

    def create_elements(self): 
     l = {} 
     for x,y in self.grid: 
      if x in l: 
       l[x].update({y:None}) 
      else: 
       l[x] = {0:None} 
     return l 
    def set_grid(self, new_size): 
     self.grid = ((x,y) for y in range(0, self.screen.resolution[1], new_size[1])for x in range(0, self.screen.resolution[0], new_size[0])) 

    def check_active(self): 
     self.active_elements = [i for i in [self.elements[x][y] for x,y in self.grid] if i != None] 

    def add_element(self, (x,y), color): 
     self.elements[x][y] = (color, (x*self.size[0],y*self.size[1], self.size[0], self.size[1]), 0) 
     self.check_active() 

    def remove_element(self, (x,y)): 
     self.elements[x][y] = None 
     self.check_active() 

    def draw_grid(self): 
     for x, y in self.grid: 
      pygame.draw.line(self.surface, (255,255,255), (x, 0), (x, self.screen.resolution[0]), 1) 
      pygame.draw.line(self.surface, (255,255,255), (0, y), (self.screen.resolution[0], y), 1) 

     for elem in self.active_elements: 
      pygame.draw.rect(self.surface, *elem) 

主代碼

import pygame, sys 
from pygame.constants import * 
from PyScreen.screen import Screen, Grid 
pygame.init() 

if __name__ == '__main__': 
    my_display = Screen(caption='Conways Game Of Life', resolution=(1280, 720)) 
    grid = Grid(my_display, (16,16)) #second parameter is the grid size 
    grid.add_element((0, 0), (255,255,255)) #anytime you want to add a cell 
    grid.add_element((16,16), (255,255,255)) 
    grid.remove_element((0, 0)) #when you want to remove a cell 
    while True: 
     for e in pygame.event.get(): 
      if e.type == pygame.QUIT: 
       pygame.quit() 
       sys.exit() 
     my_display.screen.fill((0,0,0)) 
     grid.draw_grid() 
     my_display.update() 
相關問題