def print_text(font,x,y,text,color=(255,255,255)):
imgText=font.render(text,True,color)
screen.blit(imgText,(x,y))
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Mouse Demo")
font1=pygame.font.Font(None,24)
white=255,255,255
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
while True:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
if event.type==MOUSEMOTION:
mouse_x,mouse_y=event.pos
move_x,move_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
keys=pygame.mouse.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0,100,0))
print_text(font,0,0,"Mouse Events")
print_text(font1,0,20,"Mouse position:"+str(mouse_x)+","+str(mouse_y))
print_text(font,0,40,"Mouse relative:"+str(move_x)+","+str(move_y))
print_text(font1,0,60,"Mouse button down:"+","+str(mouse_down)+"at"+str(mouse_down_x)+","+str(mouse_down_y))
print_text(font1,0,80,"Mouse button up:"+","+str(mouse_up)+"at"+str(mouse_up_x)+","+str(mouse_up_y))
print_text(font1,0,160,"Mouse Polling")
x,y=pygame.mouse.get_pos()
print_text(fon1,0,180,"Mouse position:"+str(x)+","+str(y))
b1,b2,b3=pygame.mouse.get_pressed()
print_text(font1,0,200,"Mouse buttons:"+str(b1)+","+str(b2)+","+str(b3))
pygame.display.update()
为什么会出现“此代码无法访问”,求解
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- P2441M 2022-11-04 23:52关注
是按照原代码输的吗?如果是,那keys=...那里开始缩进错了,screen.fill也是,接在exit后面
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥66 关于川崎机器人调速问题
- ¥15 winFrom界面无法打开
- ¥30 crossover21 ARM64版本安装软件问题
- ¥15 mymetaobjecthandler没有进入
- ¥15 mmo能不能做客户端怪物
- ¥15 osm下载到arcgis出错
- ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
- ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
- ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
- ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?