python 包之 Pygame 游戲開發教程
一、安裝
pip install pygame
二、查看版本號
pip show pygame
三、測試內置游戲
運行 pygame 自帶的游戲
看是否運行正常
Python -m pygame.examples.aliens
四、基礎使用
初始化一個窗口,設置窗口標題
檢測用戶關閉事件,進行關閉操作
import pygame, sys from pygame.locals import * # 初始化pygame pygame.init() # 設置窗口的大小,單位為像素 screen = pygame.display.set_mode((500, 400)) # 設置窗口標題 pygame.display.set_caption('我的第一個游戲') # 程序主循環 while True: # 獲取事件 for event in pygame.event.get(): # 判斷事件是否為退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系統 sys.exit() # 繪制屏幕內容 pygame.display.update()
五、設置背景色
通過 screen.fill 方法可以給背景板填充顏色
顏色參數是一個三原色的元組
import pygame, sys from pygame.locals import * # 初始化pygame pygame.init() # 設置窗口的大小,單位為像素 screen = pygame.display.set_mode((500, 400)) # 定義顏色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 設置背景顏色 screen.fill(WHITE) # 設置窗口標題 pygame.display.set_caption('我的第一個游戲') # 程序主循環 while True: # 獲取事件 for event in pygame.event.get(): # 判斷事件是否為退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系統 sys.exit() # 繪制屏幕內容 pygame.display.update()
六、圖形繪制
繪制一條線段:pygame.draw.line(Surface, color, start_pos, end_pos, width)
繪制一條抗鋸齒的線:pygame.draw.aaline(Surface, color, start_pos, end_pos, blend)
繪制一條折線:pygame.draw.lines(Surface, color, closed, pointlist, width)
繪制一個矩形:pygame.draw.rect(Surface, color, Rect)
繪制一個矩形框:pygame.draw.rect(Surface, color, Rect, width)
繪制一個多邊形:pygame.draw.polygon(Surface, color, pointlist, width)
繪制一條弧線:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width)
繪制一個圓:pygame.draw.circle(Surface, color, Rect, radius)
繪制一個橢圓:pygame.draw.ellipse(Surface, color, Rect)
繪制一個橢圓框:pygame.draw.ellipse(Surface, color, Rect, width)
import pygame, sys from pygame.locals import * from math import pi # 初始化pygame pygame.init() # 設置窗口的大小,單位為像素 screen = pygame.display.set_mode((400,300)) # 設置窗口標題 pygame.display.set_caption('圖形繪制') # 繪制一條線 pygame.draw.line(screen, GREEN, [0, 0], [50,30], 5) # 繪制一條抗鋸齒的線 pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80],True) # 繪制一條折線 pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5) # 繪制一個空心矩形 pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2) # 繪制一個矩形 pygame.draw.rect(screen, BLACK, [150, 10, 50, 20]) # 繪制一個空心橢圓 pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) # 繪制一個橢圓 pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) # 繪制多邊形 pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5) # 繪制多條弧線 pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2) pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2) pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2) pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3*pi/2, 2*pi, 2) # 繪制一個圓 pygame.draw.circle(screen, BLUE, [60, 250], 40) # 程序主循環 while True: # 獲取事件 for event in pygame.event.get(): # 判斷事件是否為退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系統 sys.exit() # 繪制屏幕內容 pygame.display.update()
七、繪制字體
獲取字體:pygame.font.Font(filename, size)
顯示字體:pygame.font.Font.render(text, antialias, color, background=None)
獲取坐標對象:get_rect()
import pygame,sys from pygame.locals import * pygame.init() surface = pygame.display.set_mode((500, 400), 0, 32) pygame.display.set_caption("文字繪制") surface.fill((255, 255, 255)) # 獲取字體對象,可以獲取系統自帶的,也可以自定義字體 fonts = pygame.font.get_fonts() fonts = 'fonts/ARBERKLEY.ttf' basicFont = pygame.font.SysFont(fonts, 50) # surface對象 text = basicFont.render('這是一串字符', True, (255,255,255), (0,255,0)) # 設置文本位置 textRect = text.get_rect() textRectObj.center = (250, 200) # 將渲染的surface對象更新到屏幕上 surface.blit(text,textRect) # 程序主循環 while True: # 獲取事件 for event in pygame.event.get(): # 判斷事件是否為退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系統 sys.exit() # 繪制屏幕內容 pygame.display.update()
八、音頻播放
播放特效聲音:pygame.mixer.Sound(filename)
加載背景音樂:pygame.mixer.music.load(filename)
import pygame, sys from pygame.locals import * # 初始化pygame pygame.init() # 設置窗口的大小,單位為像素 screen = pygame.display.set_mode((500,400)) # 設置窗口的標題 pygame.display.set_caption('音頻播放') # 設置背景 screen.fill((255, 255, 255)) # 加載并播放一個特效音頻文件 sound = pygame.mixer.Sound('./music.mp3') sound.play() # 加載背景音樂文件 pygame.mixer.music.load('./bgmusic.mp3') # 播放背景音樂,第一個參數為播放的次數(-1表示無限循環),第二個參數是設置播放的起點(單位為秒) pygame.mixer.music.play(-1, 0.0) # 程序主循環 while True: # 獲取事件 for event in pygame.event.get(): # 判斷事件是否為退出事件 if event.type == QUIT: # 停止播放背景音樂 pygame.mixer.music.stop() # 退出pygame pygame.quit() # 退出系統 sys.exit() # 繪制屏幕內容 pygame.display.update()
九、用戶事件
用戶按下關閉按鈕:QUIT
Pygame被激活或者隱藏:ACTIVEEVENT
鍵盤被按下:KEYDOWN
鍵盤被放開:KEYUP
鼠標移動:MOUSEMOTION
鼠標按下:MOUSEBUTTONDOWN
鼠標放開:MOUSEBUTTONUP
Pygame窗口縮放:VIDEORESIZE
import pygame, sys from pygame.locals import * # 初始化pygame pygame.init() # 設置窗口的大小,單位為像素 screen = pygame.display.set_mode((500,400), 0, 32) # 設置窗口的標題 pygame.display.set_caption('用戶事件監控') # 設置背景 screen.fill((255, 255, 255)) # 程序主循環 while True: # 獲取事件 for event in pygame.event.get(): # 判斷事件是否為退出事件 if event.type == QUIT: # 退出pygame pygame.quit() # 退出系統 sys.exit() # 獲得鍵盤按下的事件 if event.type == KEYDOWN: if(event.key==K_UP or event.key==K_w): print("上") if(event.key==K_DOWN or event.key==K_s): print("下") if(event.key==K_LEFT or event.key==K_a): print("左") if(event.key==K_RIGHT or event.key==K_d): print("右") # 按下鍵盤的Esc鍵退出 if(event.key==K_ESCAPE): # 退出pygame pygame.quit() # 退出系統 sys.exit() # 獲得鼠標當前的位置 if event.type ==MOUSEMOTION: print(event.pos) # 獲得鼠標按下的位置 if event.type ==MOUSEBUTTONDOWN: print("鼠標按下:", event.pos) # 獲得鼠標抬起的位置 if event.type ==MOUSEBUTTONUP: print("鼠標抬起:", event.pos) # 繪制屏幕內容 pygame.display.update()
Python 游戲開發
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。