How can I make a ball move in pygame when dragged by mouse?
Question: I am making a small game in python where balls are fired by a catapult, and
then the balls destroy a structure of boxes and a snake. At the moment I have
made the boxes and the snakes however, I am unable to make the balls come from
the left direction to destroy the boxes. I want to make a catapult on the left
side which can hold a ball and fire it. How can I go about making this? Here
is my code

    
    
    import pygame
    from pygame.locals import *
    from pygame.color import *
    import pymunk as pm
    from pymunk import Vec2d
    import sys
    ## ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    class Ball(pygame.sprite.Sprite):
        def __init__(self,screen):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('ball1.jpg')
            self.original = pygame.image.load('ball1.jpg')
            self.rect = self.image.get_rect()
            self.area = screen.get_rect()
    
        def add_ball(self, space, offset):#offset
            self.mass = 3
            self.radius = 15
            self.inertia = pm.moment_for_circle(self.mass, 0, self.radius, (0,0))
    
            self.body = pm.Body(self.mass, self.inertia)
            self.body.position = offset+200, 550
            self.rect.center = to_pygame(self.body.position)
    
            self.shape = pm.Circle(self.body, self.radius, (0,0))
            space.add(self.body, self.shape)
    
        def draw(self,screen):
            pygame.draw.circle(screen, (255,255,255), self.rect.center, int(15), 3)
    
        def update(self):
            print("{0} {1}".format(self.rect.center, self.body.position))
            self.rect.center = to_pygame(self.body.position)
    
    
    ## ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    class Box(pygame.sprite.Sprite):
        def __init__(self,screen):
            pygame.sprite.Sprite.__init__(self)
    
            self.image = pygame.image.load('box1.jpg')
            self.original = pygame.image.load('box1.jpg')
            #self.image = pygame.image.load('snake.jpg')
            self.rect = self.image.get_rect()
            self.area = screen.get_rect()
    
    
        def add_box(self, space, posX, posY):
            global screen
            self.size= 30
            self.points = [(-self.size, -self.size), (-self.size, self.size), (self.size,self.size), (self.size, -self.size)]
            self.mass = 0.3
            self.moment = pm.moment_for_poly(self.mass, self.points, (0,0))
    
            self.body = pm.Body(self.mass, self.moment)
            self.body.position = Vec2d(posX, posY)
            #print("Box a {0}".format(self.body.position))
            self.rect.center = to_pygame(self.body.position)
            #print("Box b {0}".format(self.rect))
            self.shape = pm.Poly(self.body, self.points, (0,0))
            self.shape.friction = 1
            #self.shape.group = 1
    
            space.add(self.body, self.shape)
    
    
        def draw(self,screen):
            pygame.draw.rect(screen, (255,255,255), self.rect, 2)
    
        def update(self):
            self.rect.center = to_pygame(self.body.position) #pysics simulations is movin the body
    
    ## ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    class Snake(pygame.sprite.Sprite):
        def __init__(self,screen):
            pygame.sprite.Sprite.__init__(self)
    
            self.image = pygame.image.load('snake.jpg')
            self.original = pygame.image.load('snake.jpg')
            self.rect = self.image.get_rect()
            self.area = screen.get_rect()
    
        def add_snake(self, space, posX, posY):
            global screen
            self.size= 30
            self.points = [(-self.size, -self.size), (-self.size, self.size), (self.size,self.size), (self.size, -self.size)]
            self.mass = 0.1
            self.moment = pm.moment_for_poly(self.mass, self.points, (0,0))
    
            self.body = pm.Body(self.mass, self.moment)
            self.body.position = Vec2d(posX, posY)
            #print("Box a {0}".format(self.body.position))
            self.rect.center = to_pygame(self.body.position)
            #print("Box b {0}".format(self.rect))
            self.shape = pm.Poly(self.body, self.points, (0,0))
            self.shape.friction = 1
            #self.shape.group = 1
    
            space.add(self.body, self.shape)
    
    
        def draw(self,screen):
            pygame.draw.rect(screen, (5,5,255), self.rect, 2)
    
        def update(self):
            self.rect.center = to_pygame(self.body.position) #pysics simulations is movin the body
    
    
    ## ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    def to_pygame(p):
        """Small hack to convert pymunk to pygame coordinates"""
        return int(p[0]), int(-p[1]+600)
    
    
    def main():
        pygame.init()
        screen = pygame.display.set_mode((600, 600))
    
        pygame.display.set_caption("Piling boxes")
        clock = pygame.time.Clock()
    
        space = pm.Space()
        space.gravity = (0.0, -900.0)
        space.damping = 0.5
    
    
    
        ### ground
        body = pm.Body()
        shape = pm.Segment(body, (0,100), (600,100), .0)
        shape.friction = 1.0
        space.add(shape)
    
        allsprites = pygame.sprite.Group()
    
        offsetY = 62
        offsetX = 92
        posX = 180
        posY = 130
    
        for j in range(3):
            for i in range(5):
                box = Box(screen) #Add more boxes
                box.add_box(space, posX, posY)
                allsprites.add(box)
                posY = posY + offsetY
            posY = 130
            posX = posX + offsetX
    
    
    
        snake = Snake(screen)
        snake.add_snake(space, posX, posY)
        allsprites.add(snake)
    
        selected = None
    
    ##
    ##    posY = 130
    ##    offset = 92
    ##    for i in range(5):
    ##        #offset = offset - 248
    ##        box = Box(screen) #Add more boxes
    ##        #box.add_box(space)
    ##        box.add_box(space, posX, posY)
    ##        allsprites.add(box)
    ##        posX = posX + offset
    ##
    ##        #offset = offset + 62
    
        posY =0
        offset = 0
        for i in range(5):
            ball = Ball(screen)
            ball.add_ball(space, offset)
            offset = offset + 16
            allsprites.add(ball)
    
        #background = pygame.Surface(screen.get_size())
        #background = background.convert()
        #background.fill((0,0,0))
    
        #screen.blit(background, (0,0))
        test_image = pygame.image.load("AngryCloneBg.jpg")
        #screen.blit(test_image, (0,0)) #coordinates (0,500))
    
        #pygame.display.flip()
    
    
        while 1:
            clock.tick(60)
            space.step(1/90.0)
    
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit(0)
    
            allsprites.update()
            screen.blit(test_image, (0,0))
            #pygame.draw.line(screen, THECOLORS["red"], to_pygame((150,100)), to_pygame((450,100)), 3)
            allsprites.draw(screen)
            for item in allsprites:
                item.draw(screen)
            pygame.display.flip()
    
    ## ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    if __name__ == '__main__':
        main()
    

How can I make the balls to be used with a catapult to fire the balls?


Answer: After 2 minutes of looking over the pymunk docs:
<http://pymunk.googlecode.com/svn/tags/pymunk-2.0.0/docs/api/index.html>

To create a cannon like game. Create a ball object, and call apply_force. It's
as simple as that.

If you wish to create a catapult, a bit more physics are involved. You need to
create line(Segment) with a fixed joint to the ground. Then connect the ball
to the other end of the segment. Apply some force to the segment, and when the
ball will be high enough, release the joint.

EDIT:

I also found a very helpful tutorial with joints:
<http://code.google.com/p/pymunk/wiki/SlideAndPinJointsExample>

