1. 程式人生 > 其它 >Python實驗報告——第13章 Pygame遊戲程式設計

Python實驗報告——第13章 Pygame遊戲程式設計

 實驗報告

【實驗目的】 

1.掌握Pygame的基礎知識。

【實驗條件】

1.PC機或者遠端程式設計環境。

【實驗內容】

1.完成第十三章  例項01:籃球自動彈跳。

例項01:建立計算BMi指數的模組

   建立一個遊戲視窗,然後在視窗內建立一個小球,以一定的速度移動小球,當小球碰到遊戲視窗的邊緣時,小球彈回,繼續移動。

程式碼如下:

 1 import sys
 2 import  pygame
 3 pygame.init()
 4 size = width,height = 1000,1000
 5 screen = pygame.display.set_mode(size)
 6 color = (0, 0, 0)
7 ball = pygame.image.load("ball.png") 8 ballrect = ball.get_rect() 9 speed = [5,5] 10 clock = pygame.time.Clock() 11 while True: 12 clock.tick(60) 13 for event in pygame.event.get(): 14 if event.type == pygame.QUIT: 15 pygame.quit() 16 sys.exit() 17 ballrect = ballrect.move(speed)
18 if ballrect.left < 0 or ballrect.right > width: 19 speed[0] = -speed[0] 20 if ballrect.top < 0 or ballrect.bottom > height: 21 speed[1] = -speed[1] 22 screen.fill(color) 23 screen.blit(ball,ballrect) 24 pygame.display.flip()

 執行結果如下: