我正在用 python 制作一个小游戏,球由弹射器发射,然后球破坏了盒子和蛇的结构。目前我已经制作了盒子和蛇,但是我无法让球从左侧方向来破坏盒子。我想在左侧做一个弹射器,它可以握住一个球并发射它。我该怎么做呢?这是我的代码
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()
我怎样才能使球与弹射器一起使用来发射球?