发布时间:2019-08-27 08:02:16编辑:auto阅读(1509)
鼠标的位置和其他PyGame的程序一样使用坐标来表示。坐标的值经常使用x和y变量来表示。左上角的坐标值是0,0,x和y的值随着鼠标的向右和向下的移动而增加。
打印鼠标左键点击位置
import pygame pygame.init() windowSize=[400,300] screen=pygame.display.set_mode(windowSize) clock=pygame.time.Clock() done=False while not done: for event in pygame.event.get(): if event.type==pygame.MOUSEBUTTONDOWN: pos=pygame.mouse.get_pos() print pos if event.type==pygame.QUIT: done=True pygame.quit()
通过点击矩形内改变背景颜色
import pygame pygame.init() windowSize=[400,300] screen=pygame.display.set_mode(windowSize) clock=pygame.time.Clock() black=pygame.color.Color("#000000") white=pygame.color.Color("#FFFFFF") btnColour=pygame.color.Color("#A45C8F") btnWidth=50 btnLength=20 btnX=(windowSize[0]-btnWidth)/2 btnY=(windowSize[1]-btnLength)/2 toggled=False pos=(0,0) done=False while not done: if toggled: screen.fill(black) else: screen.fill(white) pygame.draw.rect(screen,btnColour,[btnX,btnY,btnWidth,btnLength]) if btnX<=pos[0]<=btnX+btnWidth and btnY<=pos[1]<=btnY+btnLength: toggled=not toggled pos=[0,0] for event in pygame.event.get(): if event.type==pygame.MOUSEBUTTONDOWN: pos=pygame.mouse.get_pos() if event.type==pygame.QUIT: done=True pygame.display.flip() clock.tick(10) pygame.quit()
上一篇: python 一些有趣的模块
下一篇: anaconda更换python版本
48841
47900
38680
35838
30264
27015
26049
20885
20676
19042
473°
558°
578°
586°
565°
545°
617°
686°
803°
882°