1. 程式人生 > >Python pygame 筆記1

Python pygame 筆記1

pygame的模組

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 管理時間和幀資訊
pygame.transform 縮放和移動影象
# -*- coding:UTF-8 -*-
#
mouse_image_filename='fugu.png'
import pygame
from pygame.locals import *
from sys import  exit
pygame.init()
background=pygame.image.load('images/sushiplate.jpg')
mouse_cursor=pygame.image.load('images/fugu.png')
#建立了一個視窗,設定解析度,第一個元祖是解析度,第二個是不具有特性就設定成0,第三個為色深
screen=pygame.display.set_mode((640,480),0,32)
#設定了一個視窗
pygame.display.set_caption('This is a game')
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))
    #把游標畫上去
    pygame.display.update()

pygame.display.set_mode((x,y),標誌位,色深)

標誌位如下:

FULLSCREEN

建立一個全屏視窗
DOUBLEBUF 建立一個“雙緩衝”視窗,建議在HWSURFACE或者OPENGL時使用
HWSURFACE 建立一個硬體加速的視窗,必須和FULLSCREEN同時使用
OPENGL 建立一個OPENGL渲染的視窗
RESIZABLE 建立一個可以改變大小的視窗
NOFRAME 建立一個沒有邊框的視窗

事件event

pygame的做法是把一系列的還是件存放一個佇列中,逐個處理

事件檢索:

pygame.event.get()

pygame.event.wait()

pygame.event.poll()一旦呼叫,他會根據現在的情形返回一個真實的事件,或者一個都沒有.

常用的事件集:

事件 產生途徑 引數
QUIT 使用者按下關閉按鈕 none
ATIVEEVENT Pygame被啟用或者隱藏 gain, state
KEYDOWN 鍵盤被按下 unicode, key, mod
KEYUP 鍵盤被放開 key, mod
MOUSEMOTION 滑鼠移動 pos, rel, buttons
MOUSEBUTTONDOWN 滑鼠按下 pos, button
MOUSEBUTTONUP 滑鼠放開 pos, button
JOYAXISMOTION 遊戲手柄(Joystick or pad)移動 joy, axis, value
JOYBALLMOTION 遊戲球(Joy ball)?移動 joy, axis, value
JOYHATMOTION 遊戲手柄(Joystick)?移動 joy, axis, value
JOYBUTTONDOWN 遊戲手柄按下 joy, button
JOYBUTTONUP 遊戲手柄放開 joy, button
VIDEORESIZE Pygame視窗縮放 size, w, h
VIDEOEXPOSE Pygame視窗部分公開(expose)? none
USEREVENT 觸發了一個使用者事件 code
import pygame
from pygame.locals import *
from sys import  exit
pygame.init()
screen_size=(640,480)
event_text=[]
front=pygame.font.SysFont('arial',16)
front_height=front.get_linesize()
screen=pygame.display.set_mode(screen_size,0,32)
pygame.display.set_caption("This is a game ")
while True:
    event=pygame.event.wait()
    event_text.append(str(event))
    #獲得實踐的名稱
    event_text=event_text[-screen_size[1]/front_height:]
    if event.type==QUIT:
        exit()
    x,y=pygame.mouse.get_pos()
    screen.fill((255,255,255))
    #y=screen_size[1]-front_height
    for text in reversed(event_text):
        screen.blit(front.render(text,True,(0,0,0)),(0,y))
        y-=front_height
    pygame.display.update()

處理滑鼠事件:

MOUSEMOTION 會在滑鼠動作的時候發生,有三個引數

buttons:一個含有三個數字的元祖,三個值分別代表左鍵,中鍵和右鍵,1代表按下了

pos:就是位置了

rel:代表現在距離上次產生滑鼠事件時的距離

MOUSEBUTTONDOWN和MOUSEBUTTONUP:

button:這個值代表哪個案件被操作

pos:位置

處理鍵盤事件:

KEYDOWN and KEYUP

import  pygame
from pygame.locals import  *
from sys import  exit
pygame.init()
screen_size=(640,480)
image=pygame.image.load('images/fugu.png')
background=pygame.image.load('images/sushiplate.jpg')
screen=pygame.display.set_mode(screen_size,0,32)
pygame.display.set_caption('This is a key event')
key_x,key_y=(0,0)
key_move_x,key_move_y=(0,0)
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()
        if event.type==KEYDOWN:
            if event.key==K_UP:
                key_move_x-=1
            elif event.key==K_DOWN:
                key_move_x+=1
            elif event.key==K_LEFT:
                key_move_y-=1
            elif event.key==K_RIGHT:
                key_move_y+=1
        else:
            key_move_x=0
            key_move_y=0
    key_y+=key_move_y
    key_x+=key_move_x
    screen.fill((0,0,0))
    screen.blit(background,(0,0))
    screen.blit(image,(key_x,key_y))
    pygame.display.update()

產生事件:

通常玩家做出什麼操作,pygame就產生對應的事件就可以了,

my_event=pygame.event.EVENT(KEYDOWN,key=K_SPACE,mod=0,unicode=u'')
my_event=pygame.event.EVENT(KEYDOWN,{key=K_SPACE,mod=0,unicode=u''})
pygame.event.post(my_event)

產生一個完全自定義的事件(這個事件是幹嘛的,這個是一個高階的處理,這裡留個坑,不是我留的,是我看的教程留的)

CATONKEYBOARD=USEREVENT+1
my_event=pygame.event.EVENT(CATONKEYBOARD,message='Bad Cat')
pygame.event.post(my_event)
#獲取這個事件
for event in pygame.even.get():
    if event.type==CATONKEYBOARD:
        print event.message