pygame學習筆記(3):運動速率、時間、事件、文字

來源:互聯網
上載者:User
1、運動速率

上節中,實現了一輛汽車在馬路上由下到上行駛,並使用了pygame.time.delay(200)來進行時間延遲。看了很多參考材料,基本每個材料都會談到不同配置機器下運動速率的問題,有的是通過設定頻率解決,有的是通過設定速度解決,自己本身水平有限,看了幾篇,覺得還是《Beginning Game Development with Python and Pygame》這裡面提到一個方法比較好。代碼如下,代碼裡更改的地方主要是main裡的代碼,其中利用clock=pygame.time.Clock()來定義時鐘,speed=250.0定義了速度,每秒250像素,time_passed=clock.tick()為上次已耗用時間單位是毫秒,time_passed_seconds=time_passed/1000.0將單位改為秒,distance_moved=time_passed_seconds*speed時間乘以速度得到移動距離,這樣就能保證更加流暢。

複製代碼 代碼如下:


import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(yloc):
my_car=pygame.image.load('ok1.jpg')
locationxy=[310,yloc]
screen.blit(my_car,locationxy)
pygame.display.flip()


if __name__=='__main__':
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()

clock=pygame.time.Clock()
looper=480
speed=250.0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()

pygame.draw.rect(screen,[255,255,255],[310,(looper+132),83,132],0)
time_passed=clock.tick()
time_passed_seconds=time_passed/1000.0
distance_moved=time_passed_seconds*speed
looper-=distance_moved

if looper<-480:
looper=480
loadcar(looper)

2、事件

我理解的就是用來解決鍵盤、滑鼠、遙控器等輸入後做出什麼反映的。例如上面的例子,可以通過按上方向鍵裡向上來使得小車向上移動,按下向下,使得小車向下移動。當小車從下面倒出時,會從上面再出現,當小車從上面駛出時,會從下面再出現。代碼如下。event.type == pygame.KEYDOWN用來定義事件類型,if event.key==pygame.K_UP這裡是指當按下向上箭頭時,車前進。if event.key==pygame.K_DOWN則相反,指按下向下箭頭,車後退。

複製代碼 代碼如下:


import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(yloc):
my_car=pygame.image.load('ok1.jpg')
locationxy=[310,yloc]
screen.blit(my_car,locationxy)
pygame.display.flip()


if __name__=='__main__':
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()

looper=480

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_UP:
looper=looper-50
if looper<-480:
looper=480

pygame.draw.rect(screen,[255,255,255],[310,(looper+132),83,132],0)
loadcar(looper)
if event.key==pygame.K_DOWN:
looper=looper+50
if looper>480:
looper=-480
pygame.draw.rect(screen,[255,255,255],[310,(looper-132),83,132],0)
loadcar(looper)

3、字型及字元顯示

使用字型模組用來做遊戲的文字顯示,大部分遊戲都會有諸如比分、時間、生命值等的文字資訊。pygame主要是使用pygame.font模組來完成,常用到的一些方法是:
pygame.font.SysFont(None, 16),第一個參數是說明字型的,可以是"arial"等,這裡None表示預設字型。第二個參數表示字的大小。如果無法知道當前系統中裝了哪些字型,可以使用pygame.font.get_fonts()來獲得所有可用字型。
pygame.font.Font("AAA.ttf", 16),用來使用TTF字型檔。
render("hello world!", True, (0,0,0), (255, 255, 255)),render方法用來建立文字。第一個參數是寫的文字;第二個參數是否開啟消除鋸齒,就是說True的話字型會比較平滑,不過相應的速度有一點點影響;第三個參數是字型的顏色;第四個是背景色,無表示透明。
下面將上面的例子添加當前汽車座標:
複製代碼 代碼如下:


import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(yloc):
my_car=pygame.image.load('ok1.jpg')
locationxy=[310,yloc]
screen.blit(my_car,locationxy)
pygame.display.flip()
def loadtext(xloc,yloc):
textstr='location:'+str(xloc)+','+str(yloc)
text_screen=my_font.render(textstr, True, (255, 0, 0))
screen.blit(text_screen, (50,50))

if __name__=='__main__':
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])

my_font=pygame.font.SysFont(None,22)
screen.fill([255,255,255])
loadtext(310,0)
lineleft()
lineright()
linemiddle()

looper=480

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_UP:
looper=looper-50
if looper<-132:
looper=480
if event.key==pygame.K_DOWN:
looper=looper+50
if looper>480:
looper=-132
loadtext(310,looper)
screen.fill([255,255,255])
loadtext(310,looper)
lineleft()
lineright()
linemiddle()
loadcar(looper)

這個例子裡直接讓背景重繪一下,就不會再像1、2裡面那樣用空白的rect去覆蓋前面的模組了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.