Pygame实现鼠标到达的地方出现错误图标

注:
python版本:python3

前言

今天突发奇想,做了一个Python程序,实现了鼠标到达的地方出现错误图标。
为了让其他人少走弯路,我才写了这个博客。

凭借"老夫"多月的"经验",我就知道一定难!
本想就这么算了,可是实在是太无聊了,想写代码。

安装Pygame

1.打开cmd

2.输入 :
python版本 > 3.7:pip3 install pygame
python版本 = 3.7pip3.7 install pygame
python版本 < 3.7pip install pygame

3.等待。
等的时候可以接着打会儿游戏,一会儿就下好了。
*诶呀我靠,安装完给我重启了,还好写博客可以自动保存,但我游戏进度没了……(´;︵;`)

代码

picture.png
链接: https://pan.baidu.com/s/1N1URzRl5OO4GWzoniTzy_A 提取码: y6g9

import pygame
import sys

# 初始化
pygame.init()
size = width,height = 1000,600 # 设置屏幕尺寸
screen = pygame.display.set_mode(size) # 创建surface对象
pygame.display.set_caption('Pygame实现鼠标到达的地方出现错误图标') # 创建标题
picture = pygame.image.load('picture.png')
screen.fill((255,255,255)) # 填充屏幕

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYUP  and event.key == pygame.K_F5:
            screen.fill((255,255,255)) # 填充屏幕
    
    x,y = pygame.mouse.get_pos()
    x -= picture.get_width()/2
    y -= picture.get_height()/2
    screen.blit(picture,(x,y))
    # 刷新屏幕
    pygame.display.flip()

运行结果:

还可以这样:

import pygame
import sys

# 初始化
pygame.init()
size = width,height = 1000,600 # 设置屏幕尺寸
screen = pygame.display.set_mode(size) # 创建surface对象
pygame.display.set_caption("Pygame实现鼠标到达的地方出现错误图标") # 创建标题
picture = pygame.image.load("picture.png")
screen.fill((255,255,255)) # 填充屏幕

moving = False
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN: # 获取点击鼠标事件
            if event.button == 1: # 点击鼠标左键
               moving = True
        if event.type == pygame.MOUSEBUTTONUP: # 获取松开鼠标事件
            if event.button == 1: # 松开鼠标左键
                moving = False
        if event.type == pygame.KEYUP  and event.key == pygame.K_F5:# 获取松开键盘事件
            screen.fill((255,255,255)) # 填充屏幕

    screen.fill(WHITE) # 填充屏幕
	if moving:
   		x,y = pygame.mouse.get_pos()
    	x -= picture.get_width()/2
   		y -= picture.get_height()/2
    	screen.blit(picture,(x,y))
    # 刷新屏幕
    pygame.display.flip()

运行结果:

(完。)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值