使用pygame添加会自动移动的球(pygame的python鼠标)

技术Pygame实现监听鼠标的示例分析Pygame实现监听鼠标的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。初始化参数import pygam

Pygame实现监听鼠标的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

初始化参数

importpygame,sys

frompygame.localsimport*

defprint_text(字体,x,y,文本,颜色=(0,0,0)):

'''打印字体函数'''

渲染(文本,真,颜色)

screen.blit(img_text,(x,y))

pygame.init()

screen=pygame。显示。set _ mode((400,400))

pygame.display.set_caption('监听鼠标活动)

而:

foreventinpygame。事件。get():

ifevent.type==QUIT:

pygame.quit()

sys.exit()

screen.fill((255,255,255))

pygame。显示。update()Pygame实现监听鼠标的示例分析

鼠标移动

事件类型事件为鼠标运动,则为鼠标移动,事件。刷卡机可以获取当前位置,事件。能量损耗率鼠标的偏移。

event.type==MOUSEMOTION:

事件。刷卡机

event.rel我们将位置输出出来,定义鼠标的位置和鼠标的偏移量

mouse_x=mouse_y=0

move_x=move_y=0

print_text(font1,0,0 '鼠标事件)

print_text(font1,0,20 '鼠标的位置:' str(mouse_x)',' str(mouse_y))

print_text(font1,0,40 '鼠标的偏移:' str(move_x)',' str(move _ y))Pygame实现监听鼠标的示例分析

鼠标点击位置

鼠标按钮向下和鼠标按钮向上记录鼠标的按下和放开动作

鼠标向下=鼠标向上=0

鼠标向下x=鼠标向下ynbsp

;= 0
mouse_up_x = mouse_up_y = 0

Pygame实现监听鼠标的示例分析

输出鼠标位置及其对用的按钮

pygame.mouse.get_pressed() 可以监听鼠标的三个按键。

x, y = pygame.mouse.get_pos()
    print_text(font1, 0, 180, "鼠标位置:" + str(x) + "," + str(y))

    b1, b2, b3 = pygame.mouse.get_pressed()
    print_text(font1, 0, 200, "按钮:" + str(b1) + "," + str(b2) + "," + str(b3))

Pygame实现监听鼠标的示例分析

完整代码 

import pygame, sys
from pygame.locals import *


def print_text(font, x, y, text, color=(0, 0, 0)):
    """打印字体函数"""
    img_text = font.render(text, True, color)
    screen.blit(img_text, (x, y))


pygame.init()
# 字体
font1 = pygame.font.SysFont("方正粗黑宋简体", 18)
# 鼠标的移动位置
mouse_x = mouse_y = 0
move_x = move_y = 0
mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("监听鼠标活动")


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x, mouse_y = event.pos
            move_x, mouse_y = event.rel
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = event.button
            mouse_down_x, mouse_down_y = event.pos
        elif event.type == MOUSEBUTTONUP:
            mouse_up = event.button
            mouse_up_x, mouse_up_y = event.pos

    screen.fill((255, 255, 255))
    print_text(font1, 0, 0, "鼠标事件")
    print_text(font1, 0, 20, "鼠标的位置:" + str(mouse_x) + "," + str(mouse_y))
    print_text(font1, 0, 40, "鼠标的偏移:" + str(move_x) + "," + str(move_y))
    print_text(font1, 0, 60, "鼠标按下:" + str(mouse_down)
               + "在" + str(mouse_down_x) + "," + str(mouse_down_y))
    print_text(font1, 0, 80, "鼠标松开:" + str(mouse_up)
               + "在" + str(mouse_up_x) + "," + str(mouse_up_y))
    x, y = pygame.mouse.get_pos()
    print_text(font1, 0, 180, "鼠标位置:" + str(x) + "," + str(y))

    b1, b2, b3 = pygame.mouse.get_pressed()
    print_text(font1, 0, 200, "按钮:" + str(b1) + "," + str(b2) + "," + str(b3))
    pygame.display.update()

关于Pygame实现监听鼠标的示例分析问题的解答就分享到这里了,希望

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/147375.html

(0)

相关推荐

  • spring事件机制实例(spring事件机制详解)

    技术Spring的事件机制知识点有哪些这篇文章主要讲解了“Spring的事件机制知识点有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring的事件机制知识点有哪

    攻略 2021年12月20日
  • Java中如何把二叉搜索树转换为累加树

    技术Java中如何把二叉搜索树转换为累加树这篇文章主要介绍了Java中如何把二叉搜索树转换为累加树,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、题目给

    攻略 2021年11月2日
  • Oracle锁的面试题有哪些

    技术Oracle锁的面试题有哪些本篇内容介绍了“Oracle锁的面试题有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成

    攻略 2021年11月5日
  • DM7如何指定外部表?

    技术DM7外部表怎么指定本篇内容主要讲解“DM7外部表怎么指定”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“DM7外部表怎么指定”吧!DM7 外部表需指定如下信息:1. 表名

    攻略 2021年12月21日
  • SQL附加数据库失败问题的解决方法

    技术SQL附加数据库失败问题的解决方法这篇文章将为大家详细讲解有关SQL附加数据库失败问题的解决方法,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。为了偷个懒,利用这个数据

    攻略 2021年11月30日
  • 1大写怎么写,英文1到10大写怎么写

    技术1大写怎么写,英文1到10大写怎么写英文1到10大写是:ONE1大写怎么写、TWO、THREE、FOUR、FIVE、SIX、SEVEN、EIGHT、NIGHT、TEN单词解析:1、ONE 读音:英 [wʌn] 美

    生活 2021年10月25日