pygame學習教程(六)添加鼠標點擊事件

上一篇
在上面的例子實現放置按鈕。首先我們優化一下代碼
就是這段

    def SetPo(self):  #設置位置 set position  #
        screen.blit(self.mouse_cursor,self.vertex)

screen是在if name == “main”:下定義的

screen = pygame.display.set_mode((640, 480), 0, 32)

這個壞處是很可能定義其它的screen,類似於新的screen
screen1 = pygame.display.set_mode((400, 300), 0, 32)而在我們的類裏定義一個self.screen也是不妥的,因爲很多情況都是在一個screen下畫圖,如果每實例化一次繼承一個screen是資源的的浪費,所以這裏需要加入變量。修改後的代碼

 def SetPo(self,objscreen):  #設置位置 set position  #
        objscreen.blit(self.mouse_cursor,self.vertex)```
 def DrawButton(myscreen):
    mylist=[]
    for i in ButtonDict.keys():
        mylist.append("{}.SetPo({})".format(i,myscreen))
    return mylist 

調用的時候需要修改爲 s=DrawButton(“screen”)
在這裏我們添加鼠標點擊事件,思路是獲得鼠標點擊的(x,y)值看在不在我們的按鈕範圍內,ButtonDict作爲全局變量存儲了圖標的位置。返回控件的值。代碼如下

def AtContr(x,y):
    for mykey in ButtonDict.keys():        
        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

需要在事件控制里加入判斷程序,定義自己的_click()處理程序

def butto1_click():
    print("button1鼠標單擊")
def butto2_click():
    print("button2鼠標單擊") 
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

完整代碼如下

# coding: utf8
import pygame
#導入pygame庫
from pygame.locals import *
#導入一些常用的函數和常量
from sys import exit
import  pickle
import DVerctor

ButtonDict={}
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

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(myscreen):
    mylist=[]
    for i in ButtonDict.keys():
        mylist.append("{}.SetPo({})".format(i,myscreen))
    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():       
        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鼠標單擊")
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
    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))        # 將背景圖畫上去
        # 把光標畫上去
        s=DrawButton("screen")
        for i in s:
            exec(i)
        pygame.display.update()
        # 刷新一下畫面

點擊圖標輸出:
button2鼠標單擊
button1鼠標單擊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章