当前位置: 首页>>代码示例>>Python>>正文


Python pygame.event方法代码示例

本文整理汇总了Python中pygame.event方法的典型用法代码示例。如果您正苦于以下问题:Python pygame.event方法的具体用法?Python pygame.event怎么用?Python pygame.event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pygame的用法示例。


在下文中一共展示了pygame.event方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: check_events

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def check_events(ai_settings: Settings, stats: GameStats, game_items: GameItems):
    """Respond to keypresses and mouse events."""

    # Watch for keyboard and mouse events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit_game(stats)

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, stats, game_items)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event, game_items.ship)

        elif event.type == pygame.MOUSEBUTTONDOWN:
            check_mousedown_events(ai_settings, stats, game_items) 
开发者ID:goswami-rahul,项目名称:alien-invasion-game,代码行数:18,代码来源:game_functions.py

示例2: check_keydown_events

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def check_keydown_events(event: EventType, ai_settings: Settings
                         , stats: GameStats, game_items: GameItems):
    """Respond when key is being pressed."""
    if event.key == pygame.K_RIGHT:
        # Move ship to the right.
        game_items.ship.moving_right = True

    elif event.key == pygame.K_LEFT:
        # Move ship to the left.
        game_items.ship.moving_left = True

    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings, game_items)

    elif event.key == pygame.K_q:
        quit_game(stats)

    elif event.key == pygame.K_RETURN:  # ENTER key
        start_new_game(ai_settings, stats, game_items) 
开发者ID:goswami-rahul,项目名称:alien-invasion-game,代码行数:21,代码来源:game_functions.py

示例3: update

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def update(self, event: pg.event) -> None:
        """Update game logic with each game tick."""
        if game_vars.is_started:
            # Scroll the earth into view when the game starts
            if self.entry_y_offset > 0:
                self.entry_y_offset = max(self.entry_y_offset - self.entry_speed, 0)

            # If we are not doing a task - we can move the background
            if not game_vars.open_task:
                key_pressed = pg.key.get_pressed()

                if key_pressed[pg.K_a] or key_pressed[pg.K_LEFT]:
                    self.__scroll_left()
                if key_pressed[pg.K_d] or key_pressed[pg.K_RIGHT]:
                    self.__scroll_right()

                self.__update_tiles(event)
            else:
                game_vars.open_task.update(event)

        self.current_cloud_bg_pos += BG_CLOUDS_SCROLL_SPEED
        self.current_cloud_fg_pos += FG_CLOUDS_SCROLL_SPEED

        self.__update_positions()
        self.__update_indicators() 
开发者ID:python-discord,项目名称:code-jam-5,代码行数:27,代码来源:earth.py

示例4: update

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def update(self, event: pg.event) -> None:
        """Update tile size, tint; check if we clicked on task."""
        # Check if this task was completed
        if self.task is None:
            self.scale_n_current = 1

        # Get tile size to check for collision with mouse
        image_size = self._image_cache[self.scale_n_current].get_size()
        tile_rect = pg.Rect(
            (self.pos_x, self.pos_y), (image_size[0], image_size[1] // 2)
        )
        if not game_vars.open_task and tile_rect.collidepoint(pg.mouse.get_pos()):
            # We clicked on tile - start the task
            if event.type == pg.MOUSEBUTTONDOWN and self.task is not None:
                self.task.start()
            self.is_hovering = True
        else:
            self.is_hovering = False

        # Animation
        self._breathe() 
开发者ID:python-discord,项目名称:code-jam-5,代码行数:23,代码来源:tile.py

示例5: _draw_pause_window

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def _draw_pause_window(self, event: pg.event) -> None:
        """Draw the pause window."""
        # Background
        self.screen.blit(self.window_image, self.window_rect)

        font = pg.font.Font(None, 60)
        font.set_bold(True)
        pause_text = font.render("PAUSED", True, Color.white)
        text_x = (
            self.window_rect.x
            + (self.window_rect.width // 2)
            - (pause_text.get_width() // 2)
        )
        text_y = self.window_rect.y + 25
        self.screen.blit(pause_text, (text_x, text_y))

        self._draw_buttons(event) 
开发者ID:python-discord,项目名称:code-jam-5,代码行数:19,代码来源:game_view.py

示例6: _draw_buttons

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def _draw_buttons(self, event: pg.event) -> None:
        """Draw buttons for the pause window."""
        mouse_x, mouse_y = pg.mouse.get_pos()
        if self.resume_btn.rect.collidepoint(mouse_x, mouse_y):
            self.resume_btn.draw(hover=True)

            # Click resume button
            if event.type == pg.MOUSEBUTTONDOWN:
                Sound.click.play()
                game_vars.is_paused = False
        else:
            self.resume_btn.draw()

        if self.exit_btn.rect.collidepoint(mouse_x, mouse_y):
            self.exit_btn.draw(hover=True)

            # Click exit button; reset the game
            if event.type == pg.MOUSEBUTTONDOWN:
                Sound.click.play()
                game_vars.reset_game = True
        else:
            self.exit_btn.draw() 
开发者ID:python-discord,项目名称:code-jam-5,代码行数:24,代码来源:game_view.py

示例7: update

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def update(self, event: pg.event) -> None:
        """Check mouse collisions if player is in maze."""
        super().update()

        for cell in self.maze:
            mouse_hover = cell.rect.collidepoint(pg.mouse.get_pos())
            if (
                not self.started
                and cell.cell_type == self.CellType.START
                and mouse_hover
            ):
                self.started = True
            elif self.started and mouse_hover:
                if cell.cell_type == self.CellType.END:
                    self._complete(True)
                elif cell.cell_type == self.CellType.WALL:
                    self._complete(False) 
开发者ID:python-discord,项目名称:code-jam-5,代码行数:19,代码来源:task.py

示例8: draw

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def draw(self, mouse_x: int, mouse_y: int, event: pg.event) -> None:
        """Hadle all options events and draw elements."""
        # draw the infinity background and credits layout
        draw_infinity_bg(self.screen, self.background, self.bg_rect_1, self.bg_rect_2)
        self.screen.blit(self.credits, (0, 0, WIDTH, HEIGHT))

        # check if back buttn is hovered
        if self.back_btn.rect.collidepoint(mouse_x, mouse_y):
            # draw its hover state
            self.back_btn.draw(hover=True)

            # if the back button is clicked play click sound and
            # return to the main menu
            if event.type == pg.MOUSEBUTTONDOWN:
                Sound.click.play()
                return WindowState.main_menu
        else:
            # if it is not hover draw its normal state
            self.back_btn.draw()
        return WindowState.credit 
开发者ID:python-discord,项目名称:code-jam-5,代码行数:22,代码来源:credits.py

示例9: run

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def run(self):
        self.done = []
        self.stop = []
        for x in range(NUM_EVENTS_TO_POST):
            ee = event.Event(USEREVENT)
            try_post = 1

            # the pygame.event.post raises an exception if the event
            #   queue is full.  so wait a little bit, and try again.
            while try_post:
                try:
                    event_module.post(ee)
                    try_post = 0
                except:
                    pytime.sleep(0.001)
                    try_post = 1

            if self.stop:
                return
        self.done.append(1) 
开发者ID:wistbean,项目名称:fxxkpython,代码行数:22,代码来源:fastevents.py

示例10: add_modifiers

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def add_modifiers(event):
    d = event.dict
    d.update(modifiers)
    d['cmd'] = event.ctrl or event.meta 
开发者ID:mcgreentn,项目名称:GDMC,代码行数:6,代码来源:root.py

示例11: capture_mouse

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def capture_mouse(self, widget):
        # put the mouse in "virtual mode" and pass mouse moved events to the
        # specified widget
        if widget:
            pygame.mouse.set_visible(False)
            pygame.event.set_grab(True)
            self.captured_widget = widget
        else:
            pygame.mouse.set_visible(True)
            pygame.event.set_grab(False)
            self.captured_widget = None 
开发者ID:mcgreentn,项目名称:GDMC,代码行数:13,代码来源:root.py

示例12: call_idle_handlers

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def call_idle_handlers(self, event):
        def call(ref):
            widget = ref()
            if widget:
                widget.idleevent(event)
            else:
                print "Idle ref died!"
            return bool(widget)

        self.idle_handlers = filter(call, self.idle_handlers) 
开发者ID:mcgreentn,项目名称:GDMC,代码行数:12,代码来源:root.py

示例13: send_key

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def send_key(widget, name, event):
        widget.dispatch_key(name, event) 
开发者ID:mcgreentn,项目名称:GDMC,代码行数:4,代码来源:root.py

示例14: get_mouse_for

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def get_mouse_for(widget):
        last = last_mouse_event
        event = Event(0, last.dict)
        event.dict['local'] = widget.global_to_local(event.pos)
        add_modifiers(event)
        return event 
开发者ID:mcgreentn,项目名称:GDMC,代码行数:8,代码来源:root.py

示例15: check_keyup_events

# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import event [as 别名]
def check_keyup_events(event: EventType, ship: Ship):
    """Respond when key is stopped being pressed."""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False 
开发者ID:goswami-rahul,项目名称:alien-invasion-game,代码行数:8,代码来源:game_functions.py


注:本文中的pygame.event方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。