python - How can I make a ball move in pygame when dragged by mouse? -


i making small game in python balls fired catapult, , balls destroy structure of boxes , snake. @ moment have made boxes , snakes however, unable make balls come left direction destroy boxes. want make catapult on left side can hold ball , fire it. how can go making this? here code

import pygame pygame.locals import * pygame.color import * import pymunk pm 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 {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 movin 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 {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 movin body   ## ~~~~~~~~~~~~~~~~~~~~~~~~~  def to_pygame(p):     """small hack convert pymunk 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      j in range(3):         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 ##    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     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)          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)         item in allsprites:             item.draw(screen)         pygame.display.flip()  ## ~~~~~~~~~~~~~~~~~~~~~~~~~  if __name__ == '__main__':     main() 

how can make balls used catapult fire balls?

after 2 minutes of looking on pymunk docs: http://pymunk.googlecode.com/svn/tags/pymunk-2.0.0/docs/api/index.html

to create cannon game. create ball object, , call apply_force. it's simple that.

if wish create catapult, bit more physics involved. need create line(segment) fixed joint ground. connect ball other end of segment. apply force segment, , when ball high enough, release joint.

edit:

i found helpful tutorial joints: http://code.google.com/p/pymunk/wiki/slideandpinjointsexample


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -