def handleEvents(self):
self.mouse = self.get(MOUSE)
for event, pos in self.context.events:
self.mouse = pos
handled = self.mode.handleEvent((event, pos))
if not handled:
if event.type == pygame.MOUSEBUTTONDOWN:
self.handleMouseDown(event, pos)
elif event.type == pygame.MOUSEMOTION:
self.handleMouseMotion(pos)
elif event.type == pygame.MOUSEBUTTONUP:
self.handleMouseUp(pos)
if self.mouse:
self.set(MOUSE, self.mouse)
python类MOUSEBUTTONDOWN的实例源码
def show (image):
screen = pygame.display.get_surface()
screen.fill ((255, 255, 255))
screen.blit (image, (0, 0))
pygame.display.flip ()
while 1:
event = pygame.event.wait ()
if event.type == pygame.QUIT:
raise SystemExit
if event.type == pygame.MOUSEBUTTONDOWN:
break
def process_events(self):
""" Processes mouse events and quit event """
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.stopped = True
self.render_thread.join()
self.game_controller.cleanup()
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
self.process_mouse_event(False, self.is_double_click())
elif event.type == pygame.MOUSEBUTTONDOWN:
self.process_mouse_event(True)
def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets):
"""?????????"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
stats.save_high_score()
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, stats,sb, ship, aliens, bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)
def Update(self, events):
for event in events:
if event.type == pygame.MOUSEMOTION:
if self.Rect.collidepoint(event.pos):
self.Text = (pygame.font.Font(None, 40)).render(self.DefaultText, 1, (255, 190, 0))
else:
self.Text = (pygame.font.Font(None, 40)).render(self.DefaultText, 1, (130, 130, 130))
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.Rect.collidepoint(event.pos):
return self.Action()
return self
def process_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
button.click(event.pos)
return False
def ProcessInput(self, events, pressed_keys):
"""
Process input from user
Inherits from SceneBase
Args: self
events - pygame events
pressed_keys
"""
for event in events:
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
# Move to the next scene when the user pressed Enter
pass
if event.type == pygame.MOUSEBUTTONDOWN:
for button in self.buttons:
button.isClicked(event)
if self.leveling_up:
self.continue_button.isClicked(event)
if event.type == pygame.MOUSEBUTTONUP:
for button in self.buttons:
button.isClicked(event)
if self.leveling_up:
self.continue_button.isClicked(event)
if event.type == pygame.MOUSEMOTION:
#for button in self.buttons:
# button.isHovered(event)
if self.leveling_up:
self.continue_button.isHovered(event)
def _mousedown_cb(self, widget, event):
self.__button_state[event.button-1] = 1
return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN)
def process_input(self):
# Process touchscreen input
from alarmpanel.button import STATE_PRESSED
for event in pygame.event.get():
if event.type is pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
for b in self._buttons:
if b.down(pos): break
elif event.type is pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
for b in self._buttons:
if b.up(pos): pass
# Redraw other buttons which might be stuck in the down position
elif b.state == STATE_PRESSED:
b.set_state(STATE_DEFAULT)
def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)
def notify(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == MOUSE_LEFT:
if self.rect.collidepoint(event.pos):
self.method()
def notify(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.onLeftClick(event)
elif self.selected:
self.input.update(event)
def notify(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.callback()
def handle_event(self, event):
""" Handle events of the GUI"""
if event.type == pygame.MOUSEBUTTONUP:
log.debug('Received a new event: %s', str(event))
pos = pygame.mouse.get_pos()
self.handle_mouseup(pos)
elif event.type == pygame.MOUSEBUTTONDOWN:
log.debug('Received a new event: %s', str(event))
pos = pygame.mouse.get_pos()
self.handle_mousedown(pos)
elif event.type == pygame.KEYDOWN:
log.debug('Received a new event: %s', str(event))
self.handle_key_pressed(event.key)
def big_bang(inic, tela,
quando_tick=lambda e: e, \
frequencia=28, \
desenhar=lambda e: pg.Surface((0,0)), \
quando_tecla=lambda e, k: e, \
quando_mouse=lambda e, x, y, ev: e, \
parar_quando=lambda e: False):
pg.init()
estado = inic
clock = pg.time.Clock()
while True:
pg.display.flip()
if parar_quando(estado):
print(estado)
sys.exit(0)
for event in pg.event.get():
if event.type == pg.QUIT:
print(estado)
sys.exit(0)
if event.type == pg.KEYDOWN:
estado = quando_tecla(estado, event.key)
elif event.type in [pg.MOUSEBUTTONDOWN, pg.MOUSEBUTTONUP, pg.MOUSEMOTION]:
x, y = pg.mouse.get_pos()
estado = quando_mouse(estado, x, y, event.type)
estado = quando_tick(estado)
tela.fill(COR_BRANCO)
desenhar(estado)
clock.tick(frequencia)
def on_click(self, event, function, *args):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.rect.R.collidepoint(event.pos):
function(*args) if args else function()
def on_mouse_button_click(self, event, mouse_button, function, *args):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == mouse_button:
if self.rect.R.collidepoint(event.pos):
function(*args) if args else function()
def __init__(self, text, signal, align=Align.NONE):
# make a label of the text - but we only want the image
label = GuiLabel(text, (0, 0, 0), (214, 214, 214)).image
# get the contents to render themselves
border = Resources.configs.get(self.border_config)
image = add_border(label, border, Resources.get_image(border.image))
rect = pygame.Rect(0, 0, image.get_width(), image.get_height())
super().__init__(rect, image, align, False)
self.messages = [pygame.MOUSEMOTION, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN]
self.highlight = self.get_highlight()
self.normal_image = self.image
self.signal = signal
self.changed = False
def handleEvent(self, event, clock):
handled = False
if not self.visible:
self.focussed = False
return handled
if self.groups()[0].UI_PLACEMENT_MODE:
if event.type == pygame.MOUSEBUTTONDOWN:
self.pressed_time = pygame.time.get_ticks()
self.focussed = True
if event.type == pygame.MOUSEMOTION:
if (self.focussed and pygame.time.get_ticks() - self.pressed_time > 1000):
self.long_pressed = True
self.rect.top = event.pos[1]
self.rect.left = event.pos[0]
self.dirty = 1
if event.type == pygame.MOUSEBUTTONUP:
if self.handler:
self.handler(self, event, clock)
handled = True
if self.focussed and self.long_pressed:
print event.pos[1], event.pos[0]
self.pressed_time = 0
self.long_pressed = False
self.focussed = False
return handled
def handleEvents(self, event, fpsClock):
if event.type == pygame.MOUSEBUTTONDOWN:
self.beep1.play()
if event.type == pygame.MOUSEBUTTONUP:
return False