欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pygame學(xué)習(xí)筆記(3):運(yùn)動(dòng)速率、時(shí)間、事件、文字

 更新時(shí)間:2015年04月15日 09:21:01   投稿:junjie  
這篇文章主要介紹了pygame學(xué)習(xí)筆記(3):運(yùn)動(dòng)速率、時(shí)間、事件、文字,本文講解了運(yùn)動(dòng)速率、事件、字體及字符顯示等內(nèi)容,需要的朋友可以參考下

1、運(yùn)動(dòng)速率

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

復(fù)制代碼 代碼如下:

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、事件
   
我理解的就是用來解決鍵盤、鼠標(biāo)、遙控器等輸入后做出什么反映的。例如上面的例子,可以通過按上方向鍵里向上來使得小車向上移動(dòng),按下向下,使得小車向下移動(dòng)。當(dāng)小車從下面倒出時(shí),會(huì)從上面再出現(xiàn),當(dāng)小車從上面駛出時(shí),會(huì)從下面再出現(xiàn)。代碼如下。event.type == pygame.KEYDOWN用來定義事件類型,if event.key==pygame.K_UP這里是指當(dāng)按下向上箭頭時(shí),車前進(jìn)。if event.key==pygame.K_DOWN則相反,指按下向下箭頭,車后退。

復(fù)制代碼 代碼如下:

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、字體及字符顯示

使用字體模塊用來做游戲的文字顯示,大部分游戲都會(huì)有諸如比分、時(shí)間、生命值等的文字信息。pygame主要是使用pygame.font模塊來完成,常用到的一些方法是:
pygame.font.SysFont(None, 16),第一個(gè)參數(shù)是說明字體的,可以是"arial"等,這里None表示默認(rèn)字體。第二個(gè)參數(shù)表示字的大小。如果無法知道當(dāng)前系統(tǒng)中裝了哪些字體,可以使用pygame.font.get_fonts()來獲得所有可用字體。
pygame.font.Font("AAA.ttf", 16),用來使用TTF字體文件。
render("hello world!", True, (0,0,0), (255, 255, 255)),render方法用來創(chuàng)建文字。第一個(gè)參數(shù)是寫的文字;第二個(gè)參數(shù)是否開啟抗鋸齒,就是說True的話字體會(huì)比較平滑,不過相應(yīng)的速度有一點(diǎn)點(diǎn)影響;第三個(gè)參數(shù)是字體的顏色;第四個(gè)是背景色,無表示透明。
下面將上面的例子添加當(dāng)前汽車坐標(biāo):

復(fù)制代碼 代碼如下:

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)

這個(gè)例子里直接讓背景重繪一下,就不會(huì)再像1、2里面那樣用空白的rect去覆蓋前面的模塊了。

相關(guān)文章

  • pycharm創(chuàng)建一個(gè)python包方法圖解

    pycharm創(chuàng)建一個(gè)python包方法圖解

    在本篇文章中小編給大家分享了關(guān)于pycharm怎么創(chuàng)建一個(gè)python包的相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-04-04
  • Python實(shí)現(xiàn)圖像增強(qiáng)

    Python實(shí)現(xiàn)圖像增強(qiáng)

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)圖像增強(qiáng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • python實(shí)現(xiàn)股票歷史數(shù)據(jù)可視化分析案例

    python實(shí)現(xiàn)股票歷史數(shù)據(jù)可視化分析案例

    股票交易數(shù)據(jù)分析可直觀股市走向,對(duì)于如何把握股票行情,快速解讀股票交易數(shù)據(jù)有不可替代的作用,感興趣的可以了解一下
    2021-06-06
  • Django配置Mysql數(shù)據(jù)庫連接的實(shí)現(xiàn)

    Django配置Mysql數(shù)據(jù)庫連接的實(shí)現(xiàn)

    本文主要介紹了Django配置Mysql數(shù)據(jù)庫連接的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python在圖片中添加文字的兩種方法

    Python在圖片中添加文字的兩種方法

    這篇文章主要給大家介紹了在Python在圖片中添加文字的兩種方法,分別是使用OpenCV和PIL這兩個(gè)方法實(shí)現(xiàn),在實(shí)際應(yīng)用中要在這兩種方法中擇優(yōu)使用。兩種方法都給出了詳細(xì)示例代碼,需要的朋友可以參考下。
    2017-04-04
  • Python tcp傳輸代碼實(shí)例解析

    Python tcp傳輸代碼實(shí)例解析

    這篇文章主要介紹了Python tcp傳輸代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Python實(shí)現(xiàn)12306火車票搶票系統(tǒng)

    Python實(shí)現(xiàn)12306火車票搶票系統(tǒng)

    這篇文章主要介紹了Python實(shí)現(xiàn)12306火車票搶票系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • Pytorch中view函數(shù)實(shí)例講解

    Pytorch中view函數(shù)實(shí)例講解

    這篇文章主要給大家介紹了關(guān)于Pytorch中view函數(shù)的相關(guān)資料,PyTorch中的.view()函數(shù)是一個(gè)用于改變張量形狀的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Python djanjo之csrf防跨站攻擊實(shí)驗(yàn)過程

    Python djanjo之csrf防跨站攻擊實(shí)驗(yàn)過程

    csrf攻擊,即cross site request forgery跨站(域名)請(qǐng)求偽造,這里的forgery就是偽造的意思。這篇文章主要給大家介紹了關(guān)于Python djanjo之csrf防跨站攻擊的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • Python openpyxl 插入折線圖實(shí)例

    Python openpyxl 插入折線圖實(shí)例

    這篇文章主要介紹了Python openpyxl 插入折線圖實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評(píng)論