1. 程式人生 > 實用技巧 >pygame 影象處理方面學習筆記----基礎部分

pygame 影象處理方面學習筆記----基礎部分

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

#pygame基礎筆記

首先需要理解pygame的機制: 如下圖所示(圖片來自make game with python and pygame一書) pygame_base

##看示例程式碼,學習基本知識

<pre><code> import sys, pygame pygame.init() size = width, height = 320, 240 speed = [2, 2] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("ball.bmp") ballrect = ball.get_rect() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() </code></pre>

  • pygame.display.set_mode(size) : 生成一個視窗
  • ball = pygame.image.load("ball.bmp") : 載入一個球。他是一個surface物件,和簡單的影象物件有區別
  • ballrect = ball.get_rect() : 看後面的程式碼大概知道有什麼用
  • 這個是保證可以退出程式,並且釋放資源。關於事件這個,我想後面應該會有詳細介紹。

<pre><code> while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() </code></pre>

  • ballrect = ballrect.move(speed): 移動球,注意這裡 = 和不加 =的區別
  • screen.blit(ball, ballrect): 在ballrect的位置,在螢幕上把ball的畫素值複製到螢幕上

####http://www.pygame.org/docs/tut/intro/intro.html

轉載於:https://my.oschina.net/zjuysw/blog/263439