如何在python中显示选择点的接口?

2024-09-30 10:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我正在用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

Tags: inaddforreturndefrangepltmin
1条回答
网友
1楼 · 发布于 2024-09-30 10:27:16

这是我为扩展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)

主代码

^{pr2}$

相关问题 更多 >

    热门问题