我正在使用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 = 4
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 = 0.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
#=========================Eggs===============================================
class Eggs(pygame.sprite.Sprite):
def __init__(self,screen):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('egg.jpg')
self.original = pygame.image.load('egg.jpg')
self.rect = self.image.get_rect()
self.area = screen.get_rect()
def add_Egg(self, space, posX, posY):
global screen
self.size= 35
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 = 0.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 from_pygame(p):
return to_pygame(p)
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
balls= []
selected = None
### 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
## for i in range(5):
## x = 200
## for j in range(5):
## # what is the mass of the box?
## mass = randint(1,3)
## # what size is the box?
## # size = randint(10, 35)
## size = 20
## # calculate its position & hold it in a Vec2d
## # comment out one of these to change the pile
## x = x + size + 10
## pos = Vec2d(x,y)
## box = add_box(space, size, pos, mass)
## boxes.append(box)
## y = y + size
snake = Snake(screen)
snake.add_snake(space, posX, posY) #add the snake after adding the boxes
allsprites.add(snake)
posX = posX + offsetX #increase x position so that the egg appears next to the snake
egg = Eggs(screen)
egg.add_Egg(space,posX,posY)#pass space ,position x and position y
allsprites.add(egg)
selected = None
posY =0
offset = 0
for i in range(5):
balls = Ball(screen)
balls.add_ball(space, offset)
offset = offset + 16
allsprites.add(balls)
print(balls)
#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)
if event.key == K_p:
ball[0].body.apply_impulse((450,0))
if event.key == K_s:
ball[0].body.apply_impulse((-450,0))
elif event.type == MOUSEBUTTONDOWN:
p = from_pygame(Vec2d(event.pos))
selected = space.point_query_first(p)
elif event.type == MOUSEBUTTONUP:
if selected != None:
selected = None
elif event.type == MOUSEMOTION:
if selected != None:
selected.body.position = from_pygame(event.pos)
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()
这是我为球添加鼠标移动事件的代码
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)
if event.type == MOUSEBUTTONDOWN:
p = from_pygame(Vec2d(event.pos))
selected = space.point_query_first(p)
elif event.type == MOUSEBUTTONUP:
if selected != None:
selected = None
elif event.type == MOUSEMOTION:
if selected != None:
selected.body.position = from_pygame(event.pos)
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()
我在 while 循环中添加了所需的事件,但是当我单击并尝试向上拖动球时,它们似乎对鼠标单击没有响应