python click event scope issue in pygame -


i've created gameobject class, using python pygame library, draws rectangle on screen. integrate event handler allow me draw rectangle around, click events gameobject not registering. here couple snippets of code class:

def on_event(self, event):

    if event.type == pygame.mousebuttondown:         print "i'm mousedown event!"         self.down = true         self.prev_x=pygame.mouse.get_pos(0)         self.prev_y=pygame.mouse.get_pos(1)      elif event.type == pygame.mousebuttonup , event.button == 1:         self.down = false 

def on_draw(self, surface):

    #paints rectangle particular color of square     pygame.draw.rect(surface, pygame.color(self.red, self.green, self.blue), self.geom)` 

do need make rect image mouse events register, or there other way can drag box around?

do need make rect image mouse events register, or there other way can drag box around?

no image required. solution:

note:

  1. you use event mousemotion, use event's data: .pos, , .button event.

  2. use color class store single variable:

    self.color_bg = color("blue") self.color_fg = color(20,20,20) print self.color_bg.r, self.color_bg.g, self.color_bg.b

note: kept code more verbose make easier read, ex: do:

# uses x,y = event.pos if b.rect.collidepoint( (x,y) ): # (the same thing) if b.rect.collidepoint( event.pos* ): 

solution:

import pygame pygame.locals import * random import randint  class box(object):     """simple box, draws rect , color"""     def __init__(self, rect, color):         self.rect = rect         self.color = color     def draw(self, surface):         pygame.draw.rect(surface, self.color, self.rect)      class game(object):     def __init__(self):         # random loc , size boxes         # target = box move         self.target = none          in range(5):             x,y = randint(0, self.width), randint(0, self.height)             size = randint(20, 200)              r = rect(0,0,0,0)             # used 0 because: using propery, have create first             r.size = (size, size)             r.center = (x, y)             self.boxes.append( box(r, color("red") ))      def draw(self):         b in self.boxes: b.draw()      def on_event(self, event):         # see: http://www.pygame.org/docs/ref/event.html          if event.type == mousebuttondown , event.button = 1:             # lmb down = target if collide             x,y = event.pos             b in self.boxes:                 if b.rect.collidepoint( (x,y) ): self.target = b          elif event.type == mousebuttonup , event.button = 1:             # lmb : untarget             self.target = none          elif event.type == mousemotion:             x,y = event.pos             # valid box selected?             if self.target not none:                 self.target.rect.center = (x,y) 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -