def ProcessInput(self, 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:
self.start_button.isClicked(event)
self.help_button.isClicked(event)
self.difficulty_button.isClicked(event)
if event.type == pygame.MOUSEBUTTONUP:
self.start_button.isClicked(event)
self.help_button.isClicked(event)
self.difficulty_button.isClicked(event)
if event.type == pygame.MOUSEMOTION:
self.start_button.isHovered(event)
self.help_button.isHovered(event)
self.difficulty_button.isHovered(event)
python类MOUSEBUTTONDOWN的实例源码
def turn(self):
global board,whos_turn,n_of_turn
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN):
mouse_pos = pygame.mouse.get_pos()
for i in range(3):
for j in range(3):
if (sqr_pos[i][j][0] < mouse_pos[0] and sqr_pos[i][j][0] + sqr_size[0] > mouse_pos[0] and
sqr_pos[i][j][1] < mouse_pos[1] and sqr_pos[i][j][1] + sqr_size[1] > mouse_pos[
1] and board[i*3+j] == NO_ONE):
board[i*3+j] = whos_turn
n_of_turn += 1
who_won(tuple(board) ,whos_turn, n_of_turn)
reward_consider(winner,tuple(board),p1,p2)
whos_turn = self.ID ^ 1
elif (event.type == pygame.K_ESCAPE):
esc_pause()
if (event.type == pygame.QUIT):
sys.exit()
# -------------------------------------------
def main():
pygame.init()
pygame.font.init()
font = pygame.font.Font(None, 24)
bg = pygame.display.set_mode((800, 600), 0, 24)
bg.fill((255,255,255))
bg.blit(font.render("Click to advance", 1, (0, 0, 0)), (0, 0))
pygame.display.update()
for cursor in [no, arrow]:
TestCursor(cursor)
going = True
while going:
pygame.event.pump()
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONDOWN:
going = False
pygame.quit()
def ProcessInput(self, 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 event.type == pygame.MOUSEBUTTONUP:
for button in self.buttons:
button.isClicked(event)
if event.type == pygame.MOUSEMOTION:
for button in self.buttons:
button.isHovered(event)
def ProcessInput(self, events, pressed_keys):
"""
Process Input from user
Inherited from SceneBase
Args: self
events - list of 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:
self.easy_button.isClicked(event)
self.adv_button.isClicked(event)
if event.type == pygame.MOUSEBUTTONUP:
self.easy_button.isClicked(event)
self.adv_button.isClicked(event)
if event.type == pygame.MOUSEMOTION:
self.easy_button.isHovered(event)
self.adv_button.isHovered(event)
def isClicked(self, event):
if self.inRange(event.pos[0], event.pos[1]):
if event.type == pygame.MOUSEBUTTONDOWN:
self.initiated = True
if self.initiated and (event.type == pygame.MOUSEBUTTONUP):
self.engaged = True
else: #when click or release is detected outside of range make sure this is still not initiated
self.initiated = False
if self.engaged:
if self.state is STATE.ACTIVE:
self.deselect()
else:
self.select()
self.initiated = False
self.engaged = False
def menu_info():
rect_pointeur = pygame.Rect(pygame.mouse.get_pos(),(1,1))
rect_retour = pygame.Rect((50,25), (180,35))
for event in pygame.event.get():
if event.type == pygame.QUIT :
abientot()
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if rect_pointeur.colliderect(rect_retour):
return 1
fenetre.fill(blanc)
fenetre.blit(calibri_font.render("< Retour", True, blue), (50, 30))
fenetre.blit(InfoMenu, (0,80))
if rect_pointeur.colliderect(rect_retour):
fenetre.blit(calibri_font.render("< Retour", True, blue2), (50, 30))
pygame.display.flip()
#Fonction lancée lorsque le joueur quitte et affichant un remerciement
def handle_events(*args):
settings_window, camera, scroll, done, dims, screen, bodies, G, COR = args
for event in pg.event.get():
if event.type == pg.VIDEORESIZE:
width, height = event.w, event.h
dims, screen = V2(width, height), pg.display.set_mode((width, height), pg.RESIZABLE)
elif event.type == pg.KEYDOWN:
scroll.key(event.key, 1)
camera.key_down(event.key)
elif event.type == pg.KEYUP:
scroll.key(event.key, 0)
camera.key_up(event.key)
elif event.type == pg.MOUSEBUTTONDOWN:
handle_mouse(settings_window, camera, event, bodies, dims, G, COR, scroll)
done |= event.type == pg.QUIT
return done, dims, screen
def manager(field):
put_tile = False
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.dict["key"] == pygame.K_ESCAPE):
sys.exit()
elif event.type == pygame.KEYDOWN and (event.dict["key"] == pygame.K_RIGHT or event.dict["key"] == pygame.K_d):
field.change_right_move(True)
elif event.type == pygame.KEYUP and (event.dict["key"] == pygame.K_RIGHT or event.dict["key"] == pygame.K_d):
field.change_right_move(False)
elif event.type == pygame.KEYDOWN and (event.dict["key"] == pygame.K_LEFT or event.dict["key"] == pygame.K_q):
field.change_left_move(True)
elif event.type == pygame.KEYUP and (event.dict["key"] == pygame.K_LEFT or event.dict["key"] == pygame.K_q):
field.change_left_move(False)
elif event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 4:
field.change_tile(1)
elif event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 5:
field.change_tile(-1)
elif event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 1:
field.put_tile()
def handle(self, message, rect):
# we need to handle a mouse move or a mousedown
# is that what we have?
if message.message_id not in self.messages:
return False
# is the mouse in the rect?
xpos = message.data.pos[0] - (rect.x + self.offset.x)
ypos = message.data.pos[1] - (rect.y + self.offset.y)
if self.rect.collidepoint(xpos, ypos):
# mouse says inside
# have we been clicked?
if message.message_id == pygame.MOUSEBUTTONDOWN:
MessageSystem.add_priority_message(self.signal)
if self.image is self.normal_image:
self.image = self.highlight
self.changed = True
else:
if self.image is self.highlight:
self.image = self.normal_image
self.changed = True
def handleEvent(self, event, clock):
if event.type == pygame.MOUSEBUTTONDOWN:
# move sprite to clicked location using interpolator
fps = clock.get_fps()
x, y = event.pos
self.line = Interpolator(
self.rect.center,
(x, y),
0.5, # duration of interpolation
fps, # current frames per second
1.0, # type of interpolation
0.5 # middle?
)
self.dirty = 1
def handleEvents(self, event, fpsClock):
if event.type == pygame.MOUSEBUTTONDOWN:
# Play sound
self.sound_beep1.play()
if event.type == pygame.MOUSEBUTTONUP:
if (not self.layer2[0].visible):
for sprite in self.layer1: sprite.visible = False
for sprite in self.layer2: sprite.visible = True
Sound("assets/audio/enter_authorization_code.wav").play()
elif (self.pin_i == len(str(self.pin))):
# Ran out of button presses
if (self.correct == 4):
self.sound_granted.play()
from screens.main import ScreenMain
self.loadScreen(ScreenMain())
else:
self.sound_deny2.play()
self.sound_denied.play()
self.reset()
return False
def button(self,msg,x,y,w,h,inactive_color,active_color, action=None):
clicked = False
for event in self.event_queue:
if event.type == pygame.MOUSEBUTTONDOWN:
clicked = True
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(self.screen, active_color,(x,y,w,h))
if clicked and action is not None:
action()
else:
pygame.draw.rect(self.screen, inactive_color,(x,y,w,h))
font = pygame.font.SysFont('monospace', 25)
textSurf, textRect = self.textObjects(msg, font, [255,255,255])
textRect.center = ( (x+(w/2)), (y+(h/2)) )
self.screen.blit(textSurf, textRect)
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if event.type == pygame.MOUSEBUTTONDOWN and self.show_msg == False and self.start_sequence == False:
if 0 <= self.board.active_ship < self.square_count:
active = self.board.ships[self.board.active_ship]
if active.unit_id in self.chosen:
active.initcolor = self.highlight_color
active.color = self.highlight_color
len1 = len(self.found)
self.found.add(active.unit_id)
len2 = len(self.found)
if len2 > len1:
self.level.game_step += 1
self.mainloop.redraw_needed[1] = True
if len(self.found) == self.current_count:
self.completed_mode = True
self.ai_enabled = True
else:
active.initcolor = (255, 0, 0)
active.color = (255, 0, 0)
self.game_over_mode = True
self.ai_enabled = True
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if event.type == pygame.MOUSEBUTTONDOWN:
if self.abc_len > self.board.active_ship > -1:
self.active_i = self.board.ships[self.board.active_ship]
if self.active_i.unit_id < self.abc_len:
self.active_item = self.active_i
if self.prev_item is not None:
self.prev_item.color = self.letter_color
self.prev_item.update_me = True
self.active_item.color = (255, 255, 255)
self.create_card(self.active_item)
self.active_item.update_me = True
self.prev_item = self.active_item
self.mainloop.redraw_needed[0] = True
else:
if self.prev_item is not None:
self.prev_item.color = (255, 255, 255)
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if event.type == pygame.MOUSEBUTTONDOWN and self.show_msg == False and self.start_sequence == False:
if 0 <= self.board.active_ship < self.square_count:
active = self.board.ships[self.board.active_ship]
if active.unit_id in self.chosen:
active.initcolor = self.highlight_color
active.color = active.initcolor
self.found.add(active.unit_id)
if len(self.found) == self.current_count:
self.completed_mode = True
self.ai_enabled = True
else:
active.initcolor = (255, 0, 0)
active.color = active.initcolor
self.game_over_mode = True # self.game_over()
self.ai_enabled = True
def handle(self, event):
# TO DO
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
self.mouse_dn = True
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1 and self.mouse_dn:
self.menu.active_cat = self.cat_id
if self.menu.active_cat_o is not None:
if self.menu.active_cat_o != self:
self.menu.active_cat_o.deactivate()
self.activate()
self.menu.mainloop.redraw_needed[1] = True
self.menu.mainloop.redraw_needed[2] = True
elif event.type == pygame.MOUSEMOTION:
self.on_mouse_over()
def handle(self, event):
# TO DO
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
self.mouse_dn = True
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1 and self.mouse_dn:
pass
"""
self.menu.active_cat = self.cat_id
#self.tab_l_scroll = (self.scroll_l // self.scroll_step)
if self.menu.mainloop.config.settings["sounds"]:
s3.play()
self.menu.mainloop.redraw_needed[1] = True
self.menu.mainloop.redraw_needed[2] = True
"""
elif event.type == pygame.MOUSEMOTION:
self.on_mouse_over()
def show(self):
# Setup
self.display.set_screen_caption('Main Menu')
last_mouse_click = (-1, -1)
for event in pygame.event.get():
if (event.type == pygame.QUIT):
return (False, '', None)
if (event.type == pygame.MOUSEBUTTONDOWN):
last_mouse_click = pygame.mouse.get_pos()
# New game button
if (self.show_button(last_mouse_click, 'new_game')):
return (True, 'choose_char', None)
# Login button
if (self.show_button(last_mouse_click, 'login')):
return (True, 'login', None)
# Logo
self.display.display_single_image(self.buttons['pydino'][2],
self.buttons['pydino'][0])
# Default choice
return (True, 'main_menu', None)
def handle_event(self, event):
""" Slider event handler
:param event: event to handle
"""
if not self.visible:
return
mouse_events = [pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN, pygame.MOUSEMOTION]
if getattr(event, "source", None):
self.event_source_local = False
else:
self.event_source_local = True
if event.type in mouse_events:
pos = event.pos
if not self.bounding_box.collidepoint(pos):
return
self.mouse_action(event)
elif event.type == USER_EVENT_TYPE:
self.user_event_action(event)
def mouse_action(self, event):
""" Mouse event handler
:param event: event to handle
"""
pos = event.pos
if self.selected and not(self.last_knob_position < pos[0] < self.last_knob_position + self.knob_width and pos[1] > self.bounding_box.y) and event.type != pygame.KEYUP:
return
button_press_simulation = getattr(event, "p", None)
if event.type == pygame.MOUSEBUTTONUP and self.clicked:
self.release_action(pos)
elif event.type == pygame.MOUSEBUTTONDOWN and self.bounding_box.collidepoint(pos):
self.press_action()
elif event.type == pygame.MOUSEMOTION and (pygame.mouse.get_pressed()[0] or button_press_simulation) and self.bounding_box.collidepoint(pos) and self.clicked:
self.motion_action(pos)
def game_over(self, p1_won):
font = config.get_default_font(config.game_over_label_font_size)
if p1_won:
text = "PLAYER 1 WON!"
else:
text = "PLAYER 2 WON!"
rendered_text = font.render(text, False, (255, 255, 255))
self.canvas.surface.blit(rendered_text, (config.resolution - font.size(text)) / 2)
pygame.display.flip()
pygame.event.clear()
paused = True
while paused:
event = pygame.event.wait()
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
paused = False
self.is_game_over = True
def main():
pygame.init()
pygame.font.init()
font = pygame.font.Font(None, 24)
bg = pygame.display.set_mode((800, 600), 0, 24)
bg.fill((255,255,255))
bg.blit(font.render("Click to advance", 1, (0, 0, 0)), (0, 0))
pygame.display.update()
for cursor in [no, arrow]:
TestCursor(cursor)
going = True
while going:
pygame.event.pump()
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONDOWN:
going = False
pygame.quit()
def update(self, event):
try:
if not self.contains(*event.pos):
if not self.focussed:
return
except AttributeError:
return
if event.type == pygame.MOUSEMOTION:
if self.contains(*event.pos):
self.focussed = True
self.mouse_entered()
else:
self.focussed = False
self.mouse_left()
self.mouse_moved(event)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.contains(*event.pos):
self.clicked(event)
def Refresh(self):
self.ResetKeys()
events = pygame.event.get()
for event in events:
if(event.type == pygame.KEYDOWN):
self.CheckInputCache(event.key)
self.pressed[event.key] = True
self.keyDown[event.key] = True
self.instantKey = event.key
elif(event.type == pygame.KEYUP):
self.pressed[event.key] = False
self.keyUp[event.key] = True
elif(event.type == pygame.MOUSEBUTTONDOWN):
self.pressed[self.mouseButtons[event.button-1]] = True
self.btnDown[self.mouseButtons[event.button-1]] = True
elif(event.type == pygame.MOUSEBUTTONUP):
self.pressed[self.mouseButtons[event.button-1]] = False
self.btnUp[self.mouseButtons[event.button-1]] = True
def update(self, *args):
"""
Perform any updates on the Widget if needed;
basic implementation of focus, active-state and border-rendering;
used for interaction in more advanced, derivated Widget-classes
parameters: tuple arguments for the update (first argument should be an instance pygame.event.Event)
return values: -
"""
if self.isActive() and len(args) > 0:
event = args[0]
if event.type == pygame.MOUSEBUTTONDOWN:
self.setFocused(self.rect.collidepoint(event.pos))
if self.isDirty():
self.rect = self._border.getBounds(self._bounds)
self.image = self._border.getBorderedImage(self._getAppearance(*args))
if not self.isActive():
inactive = self.image.copy()
inactive.fill(disabeledOverlay)
self.image.blit(inactive, (0, 0))
def __eventProcessing(self):
'''
fuehrt bei einem event die entsprechende Aktion aus
bewegen oder schiessen
Parameter: -
return values: -
'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
elif event.type == pygame.KEYDOWN:
self.player1.update(self.mymap)
self.shaderUpdate = True
self.sendPos((self.player1.rect.centerx, self.player1.rect.centery))
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet = self.player1.shot(event.pos)
if not bullet == None:
self.sendShot(event.pos)
def process_input(self, events):
if not self.end_state[0]:
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_position = pygame.mouse.get_pos()
x = int(mouse_position[0] / self.square_width)
y = int(mouse_position[1] / self.square_width)
if (x, y) not in self.players[1].positions:
if self.players[0].add_position(x, y, self.minimax_board):
computer_position = pruned_minimax(self.minimax_board)
if not end_state(self.minimax_board)[0]:
self.players[1].add_position(computer_position[0][0], computer_position[0][1],
self.minimax_board, 1)
else:
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
self.clear()
elif event.key == pygame.K_SPACE:
self.clear(True)
# the update method checks if the board is in an end state:
def check(self, event=None): # checks for inputs
if self.off:
return False
if event is None:
return False
else:
if event.type == pygame.MOUSEBUTTONDOWN: # if the mousewheel is being used then offset the text so it looks like it is scrolling up and down
if event.button == 5:
if self.offSet + 4 > self.height - self.rect[3]:
pass
else:
self.offSet += 4
elif event.button == 4:
if self.offSet - 4 < 0:
pass
else:
self.offSet -= 4
return False
def mainViewerLoop(self):
while not self._receivedUpdate: # Wait for first update
time.sleep(0.5)
self._initViewier()
while self._runPygame:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # User clicked quit
self._runPygame = False # Flag done
elif event.type == pygame.MOUSEBUTTONDOWN: # Mouse Click
self._handleClick(pygame.mouse.get_pos())
elif event.type == pygame.KEYDOWN: # Key Press Down
self._handleKeypress(event.key)
if self._receivedUpdate:
self._drawViewer()
self._receivedUpdate = False
time.sleep(0.2)
pygame.quit() # Done. Quit pygame.