上一篇
这次我们写一个画折线的程序。
原来的JCon
class JCon():
def __init__(self,vertex,mouse_image_filename):
self.vertex=vertex #设置按钮顶点 set button vertex (left,top)格式
self.mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
self.count=0 #用于计数
self.BoPo =DVerctor.Vec2d(vertex)+DVerctor.Vec2d(self.mouse_cursor.get_width(),self.mouse_cursor.get_height())
#获得范围left+width,top+height (x,y)+(x1,y1)
def SetPo(self,objscreen): #设置位置 set position #
objscreen.blit(self.mouse_cursor,self.vertex)
def SetPoMuoseup(self,newPlace:tuple):
self.vertex=newPlace
注意def SetPoMuoseup,显然放到JCon类里是不合适的鼠标按下事件是应该单独写的。所以需要删掉它。注意到主循环是
while True:
。。。。
这就是使用生成器,迭代器很好的时机。我希望实现,一个点的列表,我们的图标按照这个点的列表做折线运动。修改后的JCon()
说明:self.speedadd是控制移动速度。应该作为外部变量传入。这段代码就是展示一下生成器,迭代器的用法。
class JCon():
def __init__(self,vertex,mouse_image_filename):
self.vertex=vertex #设置按钮顶点 set button vertex (left,top)格式
self.mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
self.count=0 #用于计数
self.BoPo =DVerctor.Vec2d(vertex)+DVerctor.Vec2d(self.mouse_cursor.get_width(),self.mouse_cursor.get_height())
#获得范围left+width,top+height (x,y)+(x1,y1)
self.speedadd=DVerctor.Vec2d(0,0)
def SetPo(self,object): #设置位置 set position #
object.blit(self.mouse_cursor,self.vertex)
def SetPoMuose(self,newPlace:tuple):
self.vertex=newPlace
self.BoPo = DVerctor.Vec2d(newPlace) + DVerctor.Vec2d(self.mouse_cursor.get_width(),
self.mouse_cursor.get_height())
def JCon_Move(self,x,y=None):
if y is None:
self.vertex=(x[0],x[1])
else:
self.vertex =(x,y)
print(self.vertex)
def Draw_line(self,Mytuple,Myframe):
if self.vertex != Mytuple:
if self.speedadd[0] ==0:
self.speedadd=(DVerctor.Vec2d(Mytuple)-DVerctor.Vec2d(self.vertex))/Myframe
self.JCon_Move(DVerctor.Vec2d(self.vertex) + (DVerctor.Vec2d(self.speedadd)))
# print(abs(int(Mytuple[0])-int(self.vertex[0])))
if abs(int(Mytuple[0])-int(self.vertex[0]))<=abs(self.speedadd[0]):
self.SetPoMuose((int(Mytuple[0]),int(Mytuple[1])))
self.speedadd=[0,0]
return False
else:
return True
画折线代码,这里yield的是返回值,应该是把self.speedadd作为变量传入,判断并yield,有兴趣可以自己改一下。
def Draw_Brokenline(Myobj,linelist,PointNum=10):
for m in linelist:
Myframe=abs(m[0]-Myobj.vertex[0])/PointNum
while True:
s=Myobj.Draw_line(m,Myframe)
yield s
if s:
break
在这里我定义一个消息列表。Messagelist={}#用来处理消息,折线消息Crate_Broke。这只是为了演示在死循环中的消息处理。
完整的代码如下:
# coding: utf8
import pygame
#导入pygame库
from pygame.locals import *
#导入一些常用的函数和常量
from sys import exit
import pickle
import DVerctor
import time
#这里定义一些全局变量管理消息
ButtonDict={} #用来处理按钮的位置,捕获按钮的依据
Messagelist={}#用来处理消息,折线消息Crate_Broke
class JCon():
def __init__(self,vertex,mouse_image_filename):
self.vertex=vertex #设置按钮顶点 set button vertex (left,top)格式
self.mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
self.count=0 #用于计数
self.BoPo =DVerctor.Vec2d(vertex)+DVerctor.Vec2d(self.mouse_cursor.get_width(),self.mouse_cursor.get_height())
#获得范围left+width,top+height (x,y)+(x1,y1)
self.speedadd=DVerctor.Vec2d(0,0)
def SetPo(self,object): #设置位置 set position #
object.blit(self.mouse_cursor,self.vertex)
def SetPoMuose(self,newPlace:tuple):
self.vertex=newPlace
self.BoPo = DVerctor.Vec2d(newPlace) + DVerctor.Vec2d(self.mouse_cursor.get_width(),
self.mouse_cursor.get_height())
def JCon_Move(self,x,y=None):
if y is None:
self.vertex=(x[0],x[1])
else:
self.vertex =(x,y)
print(self.vertex)
def Draw_line(self,Mytuple,Myframe):
if self.vertex != Mytuple:
if self.speedadd[0] ==0:
self.speedadd=(DVerctor.Vec2d(Mytuple)-DVerctor.Vec2d(self.vertex))/Myframe
self.JCon_Move(DVerctor.Vec2d(self.vertex) + (DVerctor.Vec2d(self.speedadd)))
# print(abs(int(Mytuple[0])-int(self.vertex[0])))
if abs(int(Mytuple[0])-int(self.vertex[0]))<=abs(self.speedadd[0]):
self.SetPoMuose((int(Mytuple[0]),int(Mytuple[1])))
self.speedadd=[0,0]
return False
else:
return True
def Draw_Brokenline(Myobj,linelist,PointNum=10):
for m in linelist:
Myframe=abs(m[0]-Myobj.vertex[0])/PointNum
while True:
s=Myobj.Draw_line(m,Myframe)
yield s
if s:
break
class Jbutton(JCon):
pass
def TextPygame(): #测试模块是否存在和版本号
print(pygame.ver)
pgname = ['pygame.cdrom', 'pygame.cursors', 'pygame.display', 'pygame.draw',
'pygame.event', 'pygame.font', 'pygame.image', 'pygame.joystick',
'pygame.key', 'pygame.mixer', 'pygame.mouse', 'pygame.movie', 'pygame.music',
'pygame.overlay', 'pygame', 'pygame.rect', 'pygame.sndarray', 'pygame.sprite',
'pygame.surface', 'pygame.surfarray', 'pygame.time']
for i in pgname:
if i is None:
print(i+" is None")
else:
print(i + " at this computer")
def storeTree(filename,*args):
with open(filename,'wb') as fw: #打开需要用'wb'
for i in args:
pickle.dump(i, fw,-1) #为了保护数据protocol=-1,设为0可以看到数据
def grabTree(filename):
Mylist=[] #返回变量的列表
with open(filename,'rb') as fr:
while True: #这里用try最简单,不用定义循环次数
try:
Mylist.append(pickle.load(fr))
except:
break
return Mylist
def CreateButton():
# global ButtonDict
mylist=[]
for exi in ButtonDict.keys():
mylist.append("{}=Jbutton({},'{}')".format(exi,ButtonDict[exi][0],ButtonDict[exi][1]))
for exi in ButtonDict.keys():
mylist.append('ButtonDict["{0}"] = [ButtonDict["{0}"][0],({0}.BoPo[0],{0}.BoPo[1])]'.format(exi))
return mylist
def DrawButton():
mylist=[]
for i in ButtonDict.keys():
mylist.append("{}.SetPo()".format(i))
return mylist
def RunStr(s:list):
for i in s:
exec(i)
#ButtonDict = {'butto1': [(12, 13), (112, 49)], 'butto2': [(52, 73), (152, 109)]}
def AtContr(x,y):
for mykey in ButtonDict.keys():
m=(ButtonDict[mykey][0][0],ButtonDict[mykey][1][0])
n=(ButtonDict[mykey][0][1],ButtonDict[mykey][1][1])
if x in range(ButtonDict[mykey][0][0],ButtonDict[mykey][1][0]) \
and y in range(ButtonDict[mykey][0][1],ButtonDict[mykey][1][1]):
return mykey
def butto1_click():
print("button1鼠标单击")
butto2.JCon_Move(140,50)
def butto2_click():
print("button2鼠标单击")
if __name__ == "__main__":
background_image_filename = 'sushiplate.jpg'
# 指定图像文件名称
pygame.init()
# 初始化pygame,为使用硬件做准备
screen = pygame.display.set_mode((640, 480), 0, 32)
# 创建了一个窗口
pygame.display.set_caption("Hello, World!") # 设置窗口标题
background = pygame.image.load(background_image_filename).convert()
#在这里添加按钮
ButtonDict["butto1"] = [(12, 13), 'feid1.png'] #全局变量,判定
ButtonDict["butto2"] = [(52, 73), 'feid1.png']
s=CreateButton()
# RunStr(s) 这里不能调用函数
for i in s:
exec(i)
print("ButtonDict=",ButtonDict)
#修改ButtonDict
linelist = [(100, 50), (10, 10), (245, 200), (300, 100)]
MyDr = Draw_Brokenline(butto2, linelist)
Messagelist["butto21"] =1
while True:
# 游戏主循环
x, y = pygame.mouse.get_pos()
# 获得鼠标位置
# 计算光标的左上角位置
#看x,y是否在控件位置上
MyactiveC=AtContr(x, y)
for event in pygame.event.get():
if event.type == QUIT:
# 接收到退出事件后退出程序
exit()
if MyactiveC:
if event.type == KEYDOWN:
print(MyactiveC)
print("this is KEYDOWN")
elif event.type == MOUSEBUTTONDOWN:
exec("{}_click()".format(MyactiveC))
elif event.type == MOUSEMOTION:
pass
elif event.type == MOUSEBUTTONUP:
pass
screen.blit(background, (0, 0)) # 将背景图画上去
# butto2.JCon_Move(butto2.vertex[0]+0.2,butto2.vertex[1])
# butto2.JCon_Move((100,100))
# butto2.Draw_line((140,50),15)
# linelist=[(100,50),(10,10),(245,200),(300,100)]
#这里处理消息
for h in Messagelist.keys():
if h=="butto21":
if Messagelist[h] ==1:
try:
MyDr.__next__()
time.sleep(0.05)
except:
Messagelist[h] = 0
exec(('ButtonDict["{0}"] = [ButtonDict["{0}"][0],({0}.BoPo[0],{0}.BoPo[1])]'.format("butto2")))
#clock.tick(0.01)
# 把光标画上去
s=DrawButton() #我很不喜欢这末写修改这样,意思是定义字典后其它代码尽量少修改
butto1.SetPo(screen)
butto2.SetPo(screen)
pygame.display.update() # 刷新一下画面