python - Mouse Movement in Pygame when selecting objects does not seem to work -
i making small game using pygame, , @ moment have 5 balls falling top of screen , landing on pile of boxes destroy knock them down. @ moment seems work fine however, trying select balls mouse when balls fall down able select them again , return them original position.
here code program
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 = 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 {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 = 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 movin 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 {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 movin body ## ~~~~~~~~~~~~~~~~~~~~~~~~~ def to_pygame(p): """small hack convert pymunk 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 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 ## in range(5): ## x = 200 ## j in range(5): ## # mass of box? ## mass = randint(1,3) ## # size box? ## # size = randint(10, 35) ## size = 20 ## # calculate position & hold in vec2d ## # comment out 1 of these change 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 snake after adding boxes allsprites.add(snake) posx = posx + offsetx #increase x position egg appears next snake egg = eggs(screen) egg.add_egg(space,posx,posy)#pass space ,position x , position y allsprites.add(egg) selected = none posy =0 offset = 0 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) 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) item in allsprites: item.draw(screen) pygame.display.flip() ## ~~~~~~~~~~~~~~~~~~~~~~~~~ if __name__ == '__main__': main()
here bit of code add mouse movement events ball
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) 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) item in allsprites: item.draw(screen) pygame.display.flip()
i have added needed events in while loop when click , try drag balls upwards seem not respond mouse click
the problem seem indenting code
elif event.type == mousebuttondown: p = from_pygame(vec2d(event.pos)) selected = space.point_query_first(p)
one level much. change if
instead of elif
. tried changing , move balls using mouse.
hope working now. :)
Comments
Post a Comment