https://study.163.com/course/courseMain.htm?courseId=1006188025&share=2&shareId=400000000398149python
實現Python遊戲編程第一步要安裝Python,Python官網對菜鳥來講是個不錯選擇。但博主推薦Anaconda,它是一個更強大的Python框架,簡單容易操做,性價比很高。編程
訪問anaconda下載地址微信
https://www.anaconda.com/download/框架
選擇本身電腦的操做系統,分別下載64位和32位的anaconda。ide
pygame不少腳本是Python2版本寫的,不少腳本是Python3寫的,所以兩個版本下載最好。oop
打開下載後的anaconda的prompt,輸入pip install pygameui
而後機器會自動安裝全部pygame依賴的包spa
最後打開Spyder,輸入import pygame,若是沒有報錯,則搞定了。操作系統
咱們來生成第一個pygame程序,即產生一個遊戲界面3d
import pygame,sys #導入pygame和sys模塊 from pygame.locals import* #導入pygame 局部變量 pygame.init() #pygame全部模塊初始化 screen=pygame.display.set_mode((400,300))#設置屏幕長和寬值 while True: #main game loop遊戲主循環 for event in pygame.event.get(): #遍歷pygame事件列表 if event.type==QUIT: #若是點擊關閉按鈕(window右上) pygame.quit() #關閉pygame庫 sys.exit() #系統退出 pygame.display.update() #把screen繪製到屏幕上
下面咱們來運行一個pygame繪圖,讓你們熟悉顏色參數,屏幕等等
# -*- coding: utf-8 -*- """ Created on Sun Oct 7 10:16:24 2018 做者郵件:231469242@qq.com 做者微信公衆號:PythonEducation """ import pygame, sys from pygame.locals import * pygame.init() # set up the window DISPLAYSURF = pygame.display.set_mode((800, 800), 0, 32) pygame.display.set_caption('Drawing') # set up the colors BLACK = ( 0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = ( 0, 255, 0) BLUE = ( 0, 0, 255) # draw on the surface object DISPLAYSURF.fill(WHITE) pygame.draw.polygon(DISPLAYSURF, GREEN, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106))) pygame.draw.line(DISPLAYSURF, BLUE, (60, 60), (120, 60), 4) pygame.draw.line(DISPLAYSURF, BLUE, (120, 60), (60, 120)) pygame.draw.line(DISPLAYSURF, BLUE, (60, 120), (120, 120), 4) pygame.draw.circle(DISPLAYSURF, BLUE, (300, 50), 20, 0) pygame.draw.ellipse(DISPLAYSURF, RED, (300, 200, 40, 80), 1) pygame.draw.rect(DISPLAYSURF, RED, (200, 150, 100, 50)) pixObj = pygame.PixelArray(DISPLAYSURF) pixObj[380][280] = BLACK pixObj[382][282] = BLACK pixObj[384][284] = BLACK pixObj[386][286] = BLACK pixObj[388][288] = BLACK del pixObj # run the game loop while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()
見下圖,咱們繪製了多個圖形
https://study.163.com/course/courseMain.htm?courseId=1006183019&share=2&shareId=400000000398149