[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] geometry library



Well, like I said, it doesn't work well.  Here it is. 

On 7/3/07, Michael George <mdgeorge@xxxxxxxxxxxxxx > wrote:
I'd love to take a look at it.  After the amount of time I've been
banging my head on this I find it surprising that you put something
together so quickly, but maybe it's the minkowski sum code that's been
so much effort...

--Mike

Ian Mallett wrote:
> I hardcoded one.  It took me less than an hour.  It wasn't pro, but it
> worked well enough.  I'll give it to you if you like.
>
> On 7/3/07, *Michael George * <mdgeorge@xxxxxxxxxxxxxx
> <mailto: mdgeorge@xxxxxxxxxxxxxx>> wrote:
>
>     Hi,
>
>     I was wondering if anyone can suggest a good 2D computational geometry
>     library for use with pygame.  I'm currently planning to use CGAL
>     for my
>     game, because it seems to have the features I need, but
>
>     a) it's in C++ so it will make distributing my game much more of a
>     pain
>     b) it's hugely complex
>     c) it's kind of a pain to download
>
>     Anyway, I would like to be able to at minimum compute intersections
>     between circles and polygons.  Ideally I would also be able to compute
>     minkowski sums of these objects.  Any ideas?
>
>     --Mike
>
>


#GSP 2.0.2

import pygame
from pygame.locals import *
import pygame.key
import pygame.time
import pygame.mouse
import math
import pygame.image
import sys
import pygame.mixer

pygame.display.set_mode( (640, 480), pygame.RESIZABLE )
fullscreenmode = 0
surface = pygame.display.get_surface()

clicking = 0
circles = []
linesegments = []
mousepress = []
oldmousepos = []
mousepos = []
screenshotnumber = 1
loadscreenshotnumber = 1
tool = 0 #1 = circle, 2 = line segment, 3 = line, etc.

class circle:
    def __init__(self):
        global oldmousepos, mousepos
        self.color = (0,0,0)
        self.x1 = oldmousepos[0]
        self.y1 = oldmousepos[1]
        self.x2 = mousepos[0]
        self.y2 = mousepos[1]
        self.radius = math.sqrt(  ((oldmousepos[0] - mousepos[0])**2) + ((oldmousepos[1] - mousepos[1])**2)  )
        self.width = 2

class linesegment:
    def __init__(self):
        global oldmousepos, mousepos
        self.color = (0,0,0)
        self.x1 = oldmousepos[0]
        self.y1 = oldmousepos[1]
        self.x2 = mousepos[0]
        self.y2 = mousepos[1]
        self.width = 2
        
def messagebar(write):
    if write == 0: #nothing going on
        pygame.display.set_caption("GSP Version 2.0.2")
    elif write == 1: #drawing circle
        pygame.display.set_caption("GSP Version 2.0.2: Drawing Circle...")
    elif write == 2: #drawing line segment
        pygame.display.set_caption("GSP Version 2.0.2: Drawing Line Segment...")
    elif write == 3: #drawing line
        pygame.display.set_caption("GSP Version 2.0.2: Drawing Line...")
    elif write == 10: #tool = select
        pygame.display.set_caption("GSP Version 2.0.2: Select Tool")
    elif write == 10.1: #Circle selected
        pygame.display.set_caption("GSP Version 2.0.2: Circle selected...")
    elif write == 10.2: #Line Segment selected
        pygame.display.set_caption("GSP Version 2.0.2: Line Segment selected...")
    elif write == 10.3: #Line selected
        pygame.display.set_caption("GSP Version 2.0.2: Line selected...")
    elif write == 11: #tool = circle
        pygame.display.set_caption("GSP Version 2.0.2: Circle Tool")
    elif write == 12: #tool = line segment
        pygame.display.set_caption("GSP Version 2.0.2: Line Segment Tool")
    elif write == 21: #Saving image
        pygame.display.set_caption("GSP Version 2.0.2: Saving Image...")
    elif write == 22: #Image saved
        pygame.display.set_caption("GSP Version 2.0.2: Image Saved!")
    elif write == 23: #Screen cleared
        pygame.display.set_caption("GSP Version 2.0.2: Screen Cleared!")
        
def handlekeys():
    global tool
    global fullscreenmode
    global circles, linesegments
    keystate = pygame.key.get_pressed()
    #(quit)
    if keystate[K_ESCAPE]:
        sys.exit()
    #Toggle fullscreen/windowed mode
    if keystate[K_F12]:
        if fullscreenmode == 0:
            pygame.display.set_mode( (1440, 900), pygame.FULLSCREEN )
            fullscreenmode = 1
        else:
            pygame.display.set_mode( (640, 480), pygame.RESIZABLE )
            fullscreenmode = 0
        pygame.time.wait(500)
    #clear screen
    if keystate[K_DELETE]:
        circles = []
        linesegments = []
        draw(1)
        messagebar(23)
        pygame.time.wait(1000)
    #take screenshot
    if keystate[K_F4]:
        global screenshotnumber
        strscreenshotnumber = str(screenshotnumber)
        messagebar(21)
        surface = pygame.display.get_surface()
        pygame.image.save(surface, "Screenshots/" + strscreenshotnumber + ".bmp")
        screenshotnumber += 1
        messagebar(22)
        pygame.time.wait(1000)
    #change tool
    if keystate[K_t]:
        toolchange = pygame.mixer.Sound('Sounds/Tool Change.wav')
        toolchange.play(0)
        tool += 1
        if tool == 3:
            tool = 0
        while 1:
            pygame.event.get()
            keystate = pygame.key.get_pressed()
            messagebar((tool+10))
            if keystate[K_t] == 0:
                break
        pygame.time.wait(250)

def drawpoint(x,y):
    pygame.draw.circle(surface, (0,0,255), (x,y), 5, 0)
    pygame.draw.circle(surface, (255,0,0), (x,y), 2, 0)

def collisiondetect(typeofcollision):
    global oldmousepos, mousepos
    if typeofcollision == 1: #x1,y1
        for i in circles:
            if (math.fabs(oldmousepos[0] - i.x1) < 5 and math.fabs(oldmousepos[1] - i.y1) < 5):
                if mousepress[0]:
                    oldmousepos = [i.x1, i.y1]
            if (math.fabs(oldmousepos[0] - i.x2) < 5and math.fabs(oldmousepos[1] - i.y2) < 5):
                if mousepress[0]:
                    oldmousepos = [i.x2, i.y2]
        for i in linesegments:
            if (math.fabs(oldmousepos[0] - i.x1) < 5 and math.fabs(oldmousepos[1] - i.y1) < 5):
                if mousepress[0]:
                    oldmousepos = [i.x1, i.y1]
            if (math.fabs(oldmousepos[0] - i.x2) < 5and math.fabs(oldmousepos[1] - i.y2) < 5):
                if mousepress[0]:
                    oldmousepos = [i.x2, i.y2]
    if typeofcollision == 2: #x2,y2
        for i in circles:
            if (math.fabs(oldmousepos[0] - i.x1) < 5 and math.fabs(oldmousepos[1] - i.y1) < 5):
                if mousepress[0]:
                    pygame.mouse.set_pos(i.x1, i.y1)
            if (math.fabs(oldmousepos[0] - i.x2) < 5and math.fabs(oldmousepos[1] - i.y2) < 5):
                if mousepress[0]:
                    pygame.mouse.set_pos(i.x2, i.y2)
        for i in linesegments:
            if (math.fabs(oldmousepos[0] - i.x1) < 5 and math.fabs(oldmousepos[1] - i.y1) < 5):
                if mousepress[0]:
                    pygame.mouse.set_pos(i.x1, i.y1)
            if (math.fabs(oldmousepos[0] - i.x2) < 5and math.fabs(oldmousepos[1] - i.y2) < 5):
                if mousepress[0]:
                    pygame.mouse.set_pos(i.x2, i.y2)

def handlemouse():
    global clicking
    global circles, linesegments
    global mousepress, oldmousepos, mousepos
    mousepress = pygame.mouse.get_pressed()
    
    if mousepress[0] == 0:
        clicking = 0
    if mousepress[0] and clicking == 0: #if click
        clicking = 1
        
        #collision detect/translate
        oldmousepos = pygame.mouse.get_pos()
        collisiondetect(1)
                
        if tool == 0: #select
            while clicking == 1:
                pygame.event.get()
                mousepos = pygame.mouse.get_pos()
                mousepress = pygame.mouse.get_pressed()
                #circle collision detect
                for i in circles:
                    if (math.fabs(mousepos[0] - i.x1) < 3 and math.fabs(mousepos[1] - i.y1) < 3) or (math.fabs(mousepos[0] - i.x2) < 3 and math.fabs(mousepos[1] - i.y2) < 3):
                        messagebar(10.1)
                #linesegment collision detect
                for i in linesegments:
                    if (math.fabs(mousepos[0] - i.x1) < 3 and math.fabs(mousepos[1] - i.y1) < 3) or (math.fabs(mousepos[0] - i.x2) < 3 and math.fabs(mousepos[1] - i.y2) < 3):
                        messagebar(10.2)
                if mousepress[0] == 0:
                    clicking = 0
        if tool == 1: #if circle
            while clicking == 1:
                messagebar(1)
                pygame.event.get()
                mousepos = pygame.mouse.get_pos()
                mousepress = pygame.mouse.get_pressed()
                radius = math.sqrt(  ((oldmousepos[0] - mousepos[0])**2) + ((oldmousepos[1] - mousepos[1])**2)  )
                if radius > 4:
                    #Draw background
                    surface.fill((255,255,255))
                    #draw circle being drawn
                    pygame.draw.circle(surface, (0,170,0), (oldmousepos[0],oldmousepos[1]), radius, 2)
                    #draw center and radial point
                    drawpoint(oldmousepos[0],oldmousepos[1])
                    drawpoint(mousepos[0],mousepos[1])
                    draw(0)
                if mousepress[0] == 0:
                    clicking = 0
                    collisiondetect(2)
            circles.append(circle())
        if tool == 2: #if line segment
            while clicking == 1:
                messagebar(2)
                pygame.event.get()
                mousepos = pygame.mouse.get_pos()
                mousepress = pygame.mouse.get_pressed()
                #Draw background
                surface.fill((255,255,255))
                #draw line being drawn
                pygame.draw.line(surface, (0,170,0), (oldmousepos[0],oldmousepos[1]), (mousepos[0],mousepos[1]), 2)
                #draw endpoints
                drawpoint(oldmousepos[0],oldmousepos[1])
                drawpoint(mousepos[0],mousepos[1])
                draw(0)
                if mousepress[0] == 0:
                    clicking = 0
                    collisiondetect(2)
            linesegments.append(linesegment())

def draw(fill):
    if fill == 1:
        #Draw background
        surface.fill((255,255,255))
    for i in circles:
        pygame.draw.circle(surface, i.color, (i.x1,i.y1), i.radius, i.width)
        drawpoint(i.x1,i.y1)
        drawpoint(i.x2,i.y2)
    for i in linesegments:
        pygame.draw.line(surface, i.color, (i.x1,i.y1), (i.x2,i.y2), i.width)
        drawpoint(i.x1,i.y1)
        drawpoint(i.x2,i.y2)
    pygame.display.flip()
    
def main():
    pygame.init()

    while 1:
        #get event list
        pygame.event.get()
        #handle keys
        handlekeys()
        #handle mouse/mouse clicks
        handlemouse()
        #update messagebar
        messagebar(0)
        #draw
        draw(1)
        
if __name__ == '__main__': main()