注:
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()
运行结果:
(完。)