如何在鼠标拖动时让球在pygame中移动?

时间:2013-05-19 16:44:24

标签: python python-2.7 python-3.x pygame

我正在蟒蛇中制作一个小型游戏,其中球被弹射器射出,然后球破坏了盒子和蛇的结构。然而,在我制作盒子和蛇的那一刻,我无法让球从左方向来破坏盒子。我想在左侧制造一个可以拿球并开火的弹射器。我该怎么做呢? 这是我的代码

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()

如何让球与弹射器一起使用以发射球?

1 个答案:

答案 0 :(得分:0)

查看pymunk文档2分钟后:http://pymunk.googlecode.com/svn/tags/pymunk-2.0.0/docs/api/index.html

创建一个类似游戏的大炮。创建一个球对象,并调用apply_force。就这么简单。

如果你想创造一个弹射器,需要更多的物理学。您需要创建一个固定关节到地面的线(段)。然后将球连接到线段的另一端。对球段施加一些力,当球足够高时,释放关节。

编辑:

我还找到了一个非常有用的关节教程: http://code.google.com/p/pymunk/wiki/SlideAndPinJointsExample