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

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

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

1、運動速率

上節(jié)中,實現(xiàn)了一輛汽車在馬路上由下到上行駛,并使用了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、事件
   
我理解的就是用來解決鍵盤、鼠標、遙控器等輸入后做出什么反映的。例如上面的例子,可以通過按上方向鍵里向上來使得小車向上移動,按下向下,使得小車向下移動。當小車從下面倒出時,會從上面再出現(xiàn),當小車從上面駛出時,會從下面再出現(xiàn)。代碼如下。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),第一個參數(shù)是說明字體的,可以是"arial"等,這里None表示默認字體。第二個參數(shù)表示字的大小。如果無法知道當前系統(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)建文字。第一個參數(shù)是寫的文字;第二個參數(shù)是否開啟抗鋸齒,就是說True的話字體會比較平滑,不過相應的速度有一點點影響;第三個參數(shù)是字體的顏色;第四個是背景色,無表示透明。
下面將上面的例子添加當前汽車坐標:

復制代碼 代碼如下:

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去覆蓋前面的模塊了。

相關文章

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

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

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

    Python實現(xiàn)圖像增強

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

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

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

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

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

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

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

    Python tcp傳輸代碼實例解析

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

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

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

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

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

    Python djanjo之csrf防跨站攻擊實驗過程

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

    Python openpyxl 插入折線圖實例

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

最新評論