1. 程式人生 > >pygame學習教程(三)

pygame學習教程(三)

在這裡我會通過一個例子介紹pygame以及讓大家學習python的類和麵向物件的思想。物件的本質就是將很多共有的資料和方法歸納起來繼承。抽象:提取現實世界中某事物的關鍵特性。編寫一個具體的類再去理解是非常好的辦法。
我們的目標是完成一個可以移動的影象,在這個過程裡。我會把我建立類,事件,響應,訊息的實現思路和大家共享。首先考慮建立基類。我們需要獲得滑鼠點選事件,可能 需要移動,所以獲得影象位置,顯示影象顯然是必須的。
第一步,讓影象顯示出來。
類的定義

import pygame
#匯入pygame庫
from pygame.locals import *
#匯入一些常用的函式和常量
from sys import exit
import  pickle
from PIL import Image

class Jbutton():
    def __init__(self,vertex,mouse_image_filename):
        self.vertex=vertex                         #設定按鈕頂點 set button vertex
        img = Image.open(mouse_image_filename)     #獲得寬度,高度
        self.width,self.height=img.size
        self.mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

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

在這個類裡我們定義了類的變數self.vertex,self.width,self.height, self.mouse_cursor 。和兩個方法__init__ SetPo
類的方法與普通的函式只有一個特別的區別——它們必須有一個額外的第一個引數名稱, 按照慣例它的名稱是 self。
第一種方法__init__()方法是一種特殊的方法,被稱為類的建構函式或初始化方法,當建立了這個類的例項時就會呼叫該方法。
screen.blit(self.mouse_cursor,self.vertex)
Screen沒有定義,引入外部的變數,實際上這種方法是不推薦的。後面會改掉。這裡只是展示一下
完整程式碼
在這裡插入圖片描述

# coding: utf8
import pygame
#匯入pygame庫
from pygame.locals import *
#匯入一些常用的函式和常量
from sys import exit
import  pickle
from PIL import Image

class Jbutton():
    def __init__(self,vertex,mouse_image_filename):
        self.vertex=vertex                         #設定按鈕頂點 set button vertex
        img = Image.open(mouse_image_filename)     #獲得寬度,高度
        self.width,self.height=img.size
        self.mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

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

if __name__ == "__main__":
    background_image_filename = 'sushiplate.jpg'
    mouse_image_filename = 'feid1.png'
    # 指定影象檔名稱
    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()
    mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
    butto1 = Jbutton((12,13),mouse_image_filename)#tleft,top
    #mouse_cursor.get_height(),self.mouse_cursor.get_width()
    butto2 = Jbutton((12+mouse_cursor.get_width(),13+mouse_cursor.get_height()), mouse_image_filename)#測試範圍
    print(butto1.__dict__)
    # 載入並轉換影象
    while True:
        # 遊戲主迴圈
        for event in pygame.event.get():
            if event.type == QUIT:
                # 接收到退出事件後退出程式
                exit()

        screen.blit(background, (0, 0))
        # 將背景圖畫上去

        x, y = pygame.mouse.get_pos()
        # 獲得滑鼠位置
        x -= mouse_cursor.get_width()+2
        y -= mouse_cursor.get_height()+2
        # 計算游標的左上角位置

        #screen.blit(mouse_cursor, (x, y))
        # 把游標畫上去
        butto1.SetPo()
        butto2.SetPo()
        #screen.blit(mouse_cursor, butto1.vertex)
        pygame.display.update()
        # 重新整理一下畫面