轉載請注明:@小五義 http://www.cnblogs.com/xiaowuyi
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時間乘以速度得到移動距離,這樣就能保證更加流暢。
#@小五義 http://www.cnblogs.com/xiaowuyi 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則相反,指按下向下箭頭,車后退。
#@小五義 http://www.cnblogs.com/xiaowuyi 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的話字體會比較平滑,不過相應的速度有一點點影響;第三個參數是字體的顏色;第四個是背景色,無表示透明。
下面將上面的例子添加當前汽車坐標:
#@小五義 http://www.cnblogs.com/xiaowuyi 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去覆蓋前面的模塊了。