Python基于pygame实现图片代替鼠标移动效果

yipeiwu_com6年前Python基础

本文实例讲述了Python基于pygame实现图片代替鼠标移动效果。分享给大家供大家参考,具体如下:

想想现在学校pygame有几个钟了,就写了一个小程序:图片代替鼠标移动

程序的运行效果:

当鼠标移动到窗口内,鼠标不见了,取而代之的是图片.....

代码部分如下:

#pygame first program
import pygame
from pygame.locals import *
from sys import exit
__author__ = {'name' : 'Hongten',
       'mail' : 'hongtenzone@foxmail.com',
       'QQ'  : '648719819',
       'Version' : '1.0'}
BG_IMAGE = 'c:\\test\\1.gif'
MOUSE_IMAGE = 'c:\\test\\mouse.gif'
pygame.init()
#设置窗口的大小
screen = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Hongten\'s First Pygame Program')
bg = pygame.image.load(BG_IMAGE).convert()
mouse_cursor = pygame.image.load(MOUSE_IMAGE).convert_alpha()
while True:
  for event in pygame.event.get():
    if event.type == QUIT:
      exit()
  screen.blit(bg, (0, 0))
  #鼠标的x,y坐标
  x, y = pygame.mouse.get_pos()
  #隐藏鼠标
  pygame.mouse.set_visible(False)
  x -= mouse_cursor.get_width() / 2
  y -= mouse_cursor.get_height() / 2
  #用其他图形代替鼠标
  screen.blit(mouse_cursor, (x, y))
  pygame.display.update()

完整实例代码代码点击此处本站下载

希望本文所述对大家Python程序设计有所帮助。

相关文章

python操作mysql数据库

一、数据库基本操作 1. 想允许在数据库写中文,可在创建数据库时用下面命令 create database zcl charset utf8; 2. 查看students表结构 desc...

Python生成器的使用方法和示例代码

本文是《Effect Python 编写高质量Python代码的59个有效方法》的学习笔记。主要记录生成器的使用方法和示例代码。 返回队列的函数 如果函数要产生一系列结果,那么最简单...

python读取excel指定列数据并写入到新的excel方法

如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据----------------...

python实现实时视频流播放代码实例

这篇文章主要介绍了python实现实时视频流播放代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @action(metho...

Python使用sorted对字典的key或value排序

sorted函数 sorted(iterable,key,reverse) iterable 待排序的可迭代对象 key 对应的是个函数, 该函数用来决定选取用哪些值来进行排...