pygame學(xué)習(xí)筆記(5):游戲精靈
據(jù)說(shuō)在任天堂FC時(shí)代,精靈的作用相當(dāng)巨大,可是那時(shí)候只知道怎么玩超級(jí)瑪麗、魂斗羅,卻對(duì)精靈一點(diǎn)也不知。pygame.sprite.Sprite就是Pygame里面用來(lái)實(shí)現(xiàn)精靈的一個(gè)類(lèi),使用時(shí),并不需要對(duì)它實(shí)例化,只需要繼承他,然后按需寫(xiě)出自己的類(lèi)就好了,因此非常簡(jiǎn)單實(shí)用。
一、什么是精靈
精靈可以認(rèn)為成是一個(gè)個(gè)小圖片,一種可以在屏幕上移動(dòng)的圖形對(duì)象,并且可以與其他圖形對(duì)象交互。精靈圖像可以是使用pygame繪制函數(shù)繪制的圖像,也可以是原來(lái)就有的圖像文件。
二、sprite中主要且常用的變量有以下幾個(gè):更多詳細(xì)的見(jiàn)http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
self.image這個(gè)負(fù)責(zé)顯示什么。如self.image=pygame.Surface([x,y])說(shuō)明該精靈是一個(gè)x,y大小的距形,self.image=pygame.image.load(filename)說(shuō)明該精靈調(diào)用顯示filename這個(gè)圖片文件。
self.image.fill([color]),負(fù)責(zé)對(duì)self.image著色,如self.image=pygame.Surface([x,y])
self.image.fill([255,0,0])
對(duì)x,y距形填充紅色。
self.rect負(fù)責(zé)在哪里顯示。一般來(lái)說(shuō),先用self.rect=self.image.get_rect()獲得image距形大小,然后給self.rect設(shè)定顯示的位置,一般用self.rect.topleft(topright、bottomleft、bottomright)來(lái)設(shè)定某一個(gè)角的顯示位置。另外,self.rect.top、self.rect.bottom、self.rect.right、self.rect.left分別表示上下左右。
self.update 負(fù)責(zé)使精靈行為生效。
Sprite.add 添加精靈到group中去。
Sprite.remove 從group中刪除
Sprite.kill 從groups中全部刪除精靈
Sprite.alive 判斷精靈是否屬于groups
三、建立一個(gè)簡(jiǎn)單的精靈
所有精靈在建立時(shí)都是從pygame.sprite.Sprite中繼承的。
(1)做一個(gè)精靈,繪制一個(gè)寬30、高30的距形,具體代碼如下:
class Temp(pygame.sprite.Sprite):
def __init__(self,color,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([30,30])
self.image.fill(color)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
這里逐句進(jìn)行一下分析,pygame.sprite.Sprite.__init__(self)完成初始化。self.image = pygame.Surface([30,30])定義顯示30*30的一個(gè)距形surface。self.image.fill(color)用color來(lái)填充顏色。self.rect=self.image.get_rect()獲取self.image大小。self.rect.topleft=initial_position確定左上角顯示位置,當(dāng)然也可以用topright、bottomrigh、bottomleft來(lái)分別確定其他幾個(gè)角的位置。精靈的顯示,在一個(gè)640*480大小的白色窗體[50,100]的位置繪制一個(gè)30*30大小的紅色距形,完整代碼如下:
import pygame,sys
pygame.init()
class Temp(pygame.sprite.Sprite):
def __init__(self,color,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([30,30])
self.image.fill(color)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
b=Temp([255,0,0],[50,100])
screen.blit(b.image,b.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
(2)做一個(gè)精靈,顯示內(nèi)容為某一圖片,這里以前面用過(guò)的小車(chē)圖片為例,代碼如下:
import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
#self.rect.topleft=initial_position
self.rect.bottomright=initial_position
print self.rect.right
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
b=Car(fi,[150,100])
screen.blit(b.image,b.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
這段代碼與(1)的不同之處在于self.image定義為pygame.image.load(filename),用來(lái)顯示filename文件,本代碼使用了ok1.jpg文件,并定義了小車(chē)右底角的顯示位置是[150,100]。
三、學(xué)習(xí)精靈組
(1)使用精靈在屏幕上放多個(gè)圖像,這種方法沒(méi)用利用精靈組的概念,而是利用了list來(lái)生成每一個(gè)精靈。Cargroup用來(lái)存儲(chǔ)不同位置的Car,screen.blit(carlist.image,carlist.rect)逐個(gè)顯示每一個(gè)精靈。具體見(jiàn)代碼:
import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.bottomright=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,360],[250,280])
Cargroup=[]
for lo in locationgroup:
Cargroup.append(Car(fi,lo))
for carlist in Cargroup:
screen.blit(carlist.image,carlist.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
具體效果見(jiàn)圖:
(2)使用精靈組來(lái)實(shí)現(xiàn)多個(gè)圖像。上面精靈是存在一個(gè)列表中,很方便,就是有點(diǎn)不太好用。除了精靈,pygame還提供了精靈組,它很適合處理精靈列表,有添加,移除,繪制,更新等方法。具體如下:http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
Group.sprites 精靈組
Group.copy 復(fù)制
Group.add 添加
Group.remove 移除
Group.has 判斷精靈組成員
Group.update 更新
Group.draw 位塊顯示
Group.clear - 繪制背景
Group.empty 清空
同樣還是上面的這個(gè)例子,這里用精靈組來(lái)實(shí)現(xiàn)。
import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.bottomright=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,360],[250,280])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
Cargroup.add(Car(fi,lo))
for carlist in Cargroup.sprites():
screen.blit(carlist.image,carlist.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
兩個(gè)例子都是在[150,200],[350,360],[250,280]三個(gè)位置顯示三輛小車(chē),不同之處第一個(gè)用的是list,第二個(gè)用的是精靈組。差別就在幾句話(huà)上,一是Cargroup=pygame.sprite.Group()定義Cargroup為精靈組,二是Cargroup.add(Car(fi,lo))用add代替了append,三是for carlist in Cargroup.sprites()這句中逐個(gè)顯示精靈,這里試了一下,直接用for carlist in Cargroup也是可以的。精靈組的代碼是高度優(yōu)化過(guò)了,常常比列表還快。插入和刪除都是常見(jiàn)的操作,代碼還可以避免內(nèi)存在循環(huán)中反復(fù)消耗。
四、動(dòng)畫(huà)
利用精靈組做動(dòng)畫(huà)會(huì)顯得比較方便,這里我們首先讓上面的三輛小車(chē)運(yùn)動(dòng)起來(lái)。
(1)三輛小車(chē)以不同的速度前行,利用random.choice隨機(jī)生成[-10,-1]之間的值作為速度讓小車(chē)從下向上運(yùn)動(dòng),并且當(dāng)?shù)竭_(dá)頂部時(shí),再?gòu)牡撞砍霈F(xiàn)。代碼如下:
import pygame,sys
from random import *
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position,speed):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
self.speed=speed
def move(self):
self.rect=self.rect.move(self.speed)
if self.rect.bottom < 0: #當(dāng)小車(chē)底部到達(dá)窗口頂部時(shí),讓小車(chē)從下面出來(lái)
self.rect.top=480
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
speed=[0,choice([-10,-1])]
Cargroup.add(Car(fi,lo,speed))
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
pygame.time.delay(20)
screen.fill([255,255,255])
for carlist in Cargroup.sprites():
carlist.move()
screen.blit(carlist.image,carlist.rect)
pygame.display.update()
(2)可以通過(guò)左右鍵控制三輛小車(chē)的左右移動(dòng),按左鍵向左移動(dòng),當(dāng)?shù)竭_(dá)最左邊時(shí),不再移動(dòng),按右鍵向右移動(dòng),當(dāng)?shù)竭_(dá)最右邊時(shí),不再移動(dòng)。具體代碼如下:
import pygame,sys
from random import *
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position,speed):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
self.speed=speed
def move(self):
self.rect=self.rect.move(self.speed)
if self.rect.bottom < 0:
self.rect.top=480
def moveleft(self):
self.rect.left=self.rect.left-10
if self.rect.left<0:
self.rect.left=0
def moveright(self):
self.rect.right=self.rect.right+10
if self.rect.right>640:
self.rect.right=640
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
speed=[0,choice([-10,-1])]
Cargroup.add(Car(fi,lo,speed))
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
for carlist in Cargroup.sprites():
carlist.moveleft()
screen.blit(carlist.image,carlist.rect)
if event.key==pygame.K_RIGHT:
for carlist in Cargroup.sprites():
carlist.moveright()
screen.blit(carlist.image,carlist.rect)
pygame.time.delay(20)
screen.fill([255,255,255])
for carlist in Cargroup.sprites():
carlist.move()
screen.blit(carlist.image,carlist.rect)
pygame.display.update()
相關(guān)文章
windows 下python+numpy安裝實(shí)用教程
這篇文章主要介紹了windows 下python+numpy安裝實(shí)用教程,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12python基礎(chǔ)教程之popen函數(shù)操作其它程序的輸入和輸出示例
popen函數(shù)允許一個(gè)程序?qū)⒘硪粋€(gè)程序作為新進(jìn)程啟動(dòng),并可以傳遞數(shù)據(jù)給它或者通過(guò)它接收數(shù)據(jù),下面使用示例學(xué)習(xí)一下他的使用方法2014-02-02Python實(shí)現(xiàn)EXCEL表格的排序功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)EXCEL表格的排序功能示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法
這篇文章主要介紹了pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

python實(shí)現(xiàn)從本地?cái)z像頭和網(wǎng)絡(luò)攝像頭截取圖片功能

python基礎(chǔ)詳解之if循環(huán)語(yǔ)句