我正在开发一个项目,我需要创建一个Node Graph。
这就是我到目前为止所做的事情(一个屏幕和一个在鼠标点击时绘制圆圈的功能),现在我被卡住了。
如何在创建后选择一个圆来移动它?
import pygame
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
TEXTCOLOR = ( 0, 0, 0)
(width, height) = (800, 600)
BASICFONTSIZE = 10
count = 0
running = True
def getPos():
pos = pygame.mouse.get_pos()
return (pos)
def drawCircle():
global count
pos = getPos()
radius = 20
pygame.draw.circle(screen, BLUE, pos, radius,7)
textSurf = BASICFONT.render(str(count), True, TEXTCOLOR)
textRect = textSurf.get_rect()
textRect.center = int(pos[0]),int(pos[1])
screen.blit(textSurf, textRect)
count += 1
def main():
global running, screen, BASICFONT
pygame.init()
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("TUFF")
screen.fill(WHITE)
pygame.display.update()
while running:
ev = pygame.event.get()
for event in ev:
if event.type == pygame.MOUSEBUTTONUP:
drawCircle()
pygame.display.update()
if event.type == pygame.QUIT:
running = False
if __name__ == '__main__':
main()
答案 0 :(得分:0)
from random import randrange, choice
import pygame
import sys
from pygame.locals import *
snode = int(input("How manny nodes do you need? = "))
FR = 30
SIZE = 640, 480
BGCOLOR = (255,255,255)
NODECOLOR = (255,0,170)
NODESIZE = 25,25
GRIDSPACING = 50
MAXTRIES = 1000
STARTINGNODES = snode
TEXTCOLOR = (0,255,0)
BASICFONTSIZE = 20
class Graph(object):
def __init__(self):
self.nodes = set()
# record positions of each node so that we can check for overlaps
self.positions = dict()
def add(self, node):
global count
count = 0
added = False
# add node at random location
while not added:
x, y = (randrange(0, SIZE[0], GRIDSPACING),
randrange(0, SIZE[1], GRIDSPACING))
if not (x,y) in self.positions:
added = True
self.nodes.add(node)
node.setpos((x,y), self)
count += 1
if count >= MAXTRIES:
raise ValueError("Could not alocate space for node representation")
def update(self):
SCREEN.fill(BGCOLOR)
for node in self.nodes:
pygame.draw.rect(SCREEN, node.color, node.rect, 5)
textSurf = BASICFONT.render(str(node.id), True, TEXTCOLOR)
textRect = textSurf.get_rect()
textRect.center = int(node.rect[0] + 13),int(node.rect[1] + 13)
SCREEN.blit(textSurf, textRect)
for neighbor in node.neighbors:
pygame.draw.line(SCREEN, NODECOLOR,node.rect.center, neighbor.rect.center)
class Node(object):
# incremented with each
# each node has a unique ID that
creation_counter = 0
def __init__(self):
self.id = self.__class__.creation_counter
self.__class__.creation_counter += 1
self.rect = None
self.color = NODECOLOR
self.neighbors = set()
def setpos(self, pos, graph = None):
if self.rect and graph:
# remove self from previous position in the graph:
graph.positions.pop(self.rect.topleft, None)
self.rect = pygame.Rect(pos[0], pos[1],NODESIZE[0], NODESIZE[1])
if graph:
graph.positions[pos] = self
def __hash__(self):
return self.id
def create_graph():
# create new graph:
graph = Graph()
nodes = []
for i in range(STARTINGNODES):
node = Node()
graph.add(node)
return graph
def init():
global SCREEN
pygame.init()
SCREEN = pygame.display.set_mode(SIZE)
def quit():
pygame.quit()
def main():
global BASICFONT, screen
graph = create_graph()
selected = None
try:
init()
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
while True:
graph.update()
pygame.event.pump()
# Exit "ESC" key is pressed
if pygame.key.get_pressed()[pygame.K_ESCAPE]:
break
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
# round down x,y to multiples of NODESIZE
x -= x % NODESIZE[0]
y -= y % NODESIZE[1]
pygame.draw.rect(SCREEN, (0,0,255), (x,y) + NODESIZE)
if (x,y) in graph.positions:
node = graph.positions[x,y]
if selected:
print selected.id, node.id
if selected is node:
selected = None
node.color = NODECOLOR
elif selected not in node.neighbors:
selected.neighbors.add(node)
node.neighbors.add(selected)
else:
selected.neighbors.remove(node)
node.neighbors.remove(selected)
else:
node.color = (0,0,0)
selected = node
elif selected:
selected.setpos((x,y), graph)
elif event.type == pygame.MOUSEMOTION and event.buttons[0] and selected:
x, y = event.pos
# round down x,y to multiples of NODESIZE
x -= x % NODESIZE[0]
y -= y % NODESIZE[1]
selected.setpos((x,y), graph)
pygame.display.flip()
pygame.time.delay(FR)
finally:
quit()
if __name__ == "__main__":
main()