1,導入pygame函數
2,設計遊戲窗口界面大小window_size=(width,heigth)=(640,480),背景顏色等ui
3,tank_image=pygame.image.load('images/tanKU.bmp')加載坦克圖片spa
4,寫函數,而後調用設計
參考代碼blog
import os,sys,pygame from pygame.locals import * def play_tank(): pygame.init() window_size=(width,heigth)=(640,480) speed=[1,1] color_white=(255,255,255) screen=pygame.display.set_mode(window_size) pygame.display.set_caption("坦克大戰") tank_image=pygame.image.load('images/tanKU.bmp') tank_rect=tank_image.get_rect() while True: for event in pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() sys.exit() tank_rect=tank_rect.move(speed) if(tank_rect.left<0) or (tank_rect.right>width): speed[0]=-speed[0] if(tank_rect.top<0) or (tank_rect.bottom>heigth): speed[1]=-speed[1] screen.fill(color_white) screen.blit(tank_image,tank_rect) pygame.display.update() play_tank()
運行結果遊戲