需要用到pygame模塊
常用語句
1.pygame.init()
初始化,使用前都加一句
2.pygame.display.set_mode(size)
設置游戲屏幕寬高
3.pygame.display.set_caption('game')
設置屏幕標題
4.pygame.image.load('a.jpg')
加載圖片,加載后如果要控制其,需要先用get_rect()獲取其位置,然后用move(x,y)移動,舉例:
picture = pygame.image.load('a.jpg') #添加個圖片在里面
position = picture.get_rect() #獲取圖像的位置矩形
position = position.move(1,1) #移動圖像,這里是同時向右下移動1px
5.pygame.transform.flip(picture, True, True)
設置圖像翻轉,第一個代表翻轉的圖像,第二個代表是否水平翻轉,第三個代表是否垂直翻轉
6.screen.fill((255,255,255))
設置填充背景,如果不設置,原來的內容就會一直在,其實就相當于每次更新界面時把界面clear了一下,并且clear成我們想要的背景,不過這句運行后不會直接填充,其只是寫入到內存,真正填充需要在pygame.display.flip()這句運行后才會顯示
7.screen.blit(picture, position)
更新圖像,跟那面那句一樣只是寫入到內存,真正要看到更新需要運行下面那句才能夠顯示
8.pygame.display.flip()
更新整個界面,也就是將所有內容一起更新顯示,如果沒有這句,整個屏幕一片黑,什么都沒有
9.pygame.time.delay(10)
設置時間延遲(ms)
10.設置頻率
需要先實例化一個Clock()對象,然后再設置頻率,舉例:
clock = pygame.time.Clock()
clock.tick(200) #頻率為1秒更新200次,假如設置一次移動2px,那么就會一秒移動200px
10.pygame.QUIT
退出游戲
常用方法
對圖像
1.pygame.transform.flip(picture, True, False)
將圖像翻轉更新,第一個參數(shù)是要更新的東西,第二個代表是否水平翻轉,第三個代表是否垂直翻轉
2.pygame.transform.rotate(picture, angle)
將圖像旋轉(逆時針),第一個參數(shù)是圖像,第二個是度數(shù),比如逆時針轉90度:
pygame.transform.rotate(picture, 90)
3.pygame.transform.smoothscale(picture, (x,y))
縮放圖像,第一個參數(shù)是圖像,后一個參數(shù)為元組,里面有兩個元素,分別是其x、y方向大小,舉例:
picture = pygame.transform.smoothscale(picture, (position.width*2,position.height*2)) #這里把長寬變?yōu)樵瓉淼膬杀?
4.surface.set_colorkey((r,g,b))
把想要變色的顏色弄透明,舉例:
picture = pygame.imgae.load('a.jpg').convert()
#要弄透明,最好給圖片弄個透明設置通道,也就是加個.convert()/.convert_alpha(),因為jpg不支持后者,所以這里用第一個
picture.set_colorkey((255,255,255)) #把白色弄透明
注:
對jpg圖片來說,透明度效果可能不太好,旁邊偏白的陰影去不掉,而png圖片一般就能處理的很好
5.surface.set_alpha(num)
也是設置透明度,將整個圖片設置透明度,范圍:0-255,舉例:
picture = pygame.imgae.load('a.png').convert_alpha() #加個.convert_alpha,給圖片弄個透明設置通道
picture.set_alpha(80) #將透明度設置為80
注:
4、5兩種透明度處理方法可以聯(lián)合起來使用,比如把白色部分透明了,再把整體透明了
6.surface.get_at((x,y))
獲得surface對象上某個坐標的顏色像素,舉例:
picture = pygame.image.load('a.png')
position = picture.get_rect()
print(picture.get_at(position.center)) #輸出圖片最中間點的顏色
結果為:(136, 0, 21, 255)
說明紅綠藍對應配色,以及透明度(255代表不透明)
7.surface.set_at((x,y),num)
設置surface對象上某個坐標的透明度,舉例:
picture = pygame.image.load('a.png')
for each in range(200,500):
picture.set_at((300,each),0) #將圖片坐標(300,200-500)之間的點都弄空白
繪制圖形
1.pygame.draw.rect(surface,(r,g,b),rect,width)
繪制一個矩形,第一個參數(shù)說明畫在哪個surface上,比如可以是屏幕,也可以是圖像上,第二個參數(shù)是一個顏色參數(shù)的元組,第三個矩形的區(qū)域,需要傳入二層元組((x,y),(width,height)),x、y表示其左上角坐標,width、height表示其長寬,第四個是線條寬度(當為0的時候代表填充內部),舉例:
screen = pygame.display.set_mode((200,200))
pygame.draw.rect(screen,(255,0,0),((100,100),(5,5)),1) #在屏幕上畫一個紅色矩形
2.pygame.draw.circle(Surface, color, pos, radius, width)
繪制一個圓形,第三個參數(shù)是圓心坐標,第四個是半徑,舉例:
pygame.draw.circle(picture, (255,0,0), (10,10), 10, 1)
3.pygame.draw.line(Surface, color, start_pos, end_pos, width)
繪制一條直線,第三、四個參數(shù)代表起點和終點,舉例:
pygame.draw.line(screen, (255,0,0), (1,1), (50,50), 1)
注:
還有個aaline,和line差不多,但是抗鋸齒,也就是看起來比line柔順些,舉例:
pygame.draw.aaline(screen, (255,0,0), (1,1), (50,50), 1)
4.pygame.draw.lines(Surface, color, closed, pointlist, width)
繪制一堆線段,第三個參數(shù)代表是否首尾兩個點相連(0就不相連,1相連),第四個參數(shù)是一堆點坐標的列表,舉例:
list_lines = ((1,1),(10,0),(200,3),(2,50),(3,6),(5,1))
pygame.draw.lines(screen, (255,0,0), 1, list_lines, 1)
注:
像線段這種東西是沒有包著東西的,所以說width不能設置為0(否則沒東西顯示),因為填充不了(aaline除外,而且aaline如果設置為0,會比原來更深、粗一點)
5.pygame.draw.polygon(Surface, color, pointlist, width)
繪制一個多邊形,第三個參數(shù)是坐標列表,代表多邊形各個頂點,其實和上面那個一堆線段效果挺像的,但是這個是一個圖形,有東西包著,所以可以width=0來填充,舉例:
list_poly = ((1,1),(10,0),(200,3),(2,50),(3,6),(5,1))
pygame.draw.polygon(screen, (255,0,0), list_poly, 0)
6.pygame.draw.ellipse(Surface, color, rect, width)
繪制一個橢圓,其實質就是照著一個矩形里繪制一個最大的圓,所以第三個參數(shù)就是那個外面框這的矩形大小(假裝有),舉例:
pygame.draw.ellipse(screen, (255,0,0), (220,50,200,200), 0)
#這個比較圓
pygame.draw.ellipse(screen, (0,0,0), (100,100,400,100), 1)
#這個比較扁,這兩個看起來是不是很像太陽系
7.pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width)
繪制一個弧線,最后兩個參數(shù)是起始/結束角度(逆時針),其實質就像是描繪橢圓的外邊多少度到多少度的那一段線,用這個前最好先:import math,這樣就可以使用pi(3.1415926...)了,舉例:
import math
pygame.draw.arc(screen, (0,0,0), (220,50,200,200), 0, math.pi, 1) #從0度到180的圓弧
更多pygame.draw參考
https://www.cnblogs.com/leonyoung/archive/2012/07/01/2572205.html
對其他
1.pygame.mouse.get_pos()
獲得當前鼠標位置
常見事件
(在pygame下,比如:pygame.QUIT)
QUIT:按下右上角
KEYDOWN/KEYUP:按下/松開某個鍵
MOUSEMOTION:移動鼠標
MOUSEBUTTONDOWN/MOUSEBUTTONUP:按下/松開鼠標(其對應的button中1為左鍵,2為中間的,3為右鍵,4為向上滾輪,5為向下滾輪)
注:
上面內容對應event.type,比如要判斷為按下鼠標左鍵鍵時:
if event.type == MOUSEBUTTONDOWN: #判斷為鼠標按下
if event.button == 1 #1為左鍵
pass
鍵盤按鍵代碼
(在pygame下,比如:pygame.K_0)
K_0 0,對應的還有其他數(shù)字
K_a a,對應的還有其他字母
K_KP 數(shù)字鍵盤上的0-9
K_UP ↑,對應的還有DOWN/RIGHT/LEFT
K_F1 F1,對應的還有其他
K_BACKSPACE 回退鍵
K_TAB TAB鍵
K_SPACE 空格鍵
K_RETURN 回車
K_RSHIFT 右邊的shift,對應的還有LSHIFT
K_RALT 右邊的alt,對應的還有LALT
K_RCTRL 右邊的ctrl,對應的還有LCTRL
注:
上面的屬性對應的是event.key,比如要判斷是否為上鍵時:
if event.type == pygame.KEYDOWN: #判斷為鍵盤按下
if event.key == pygame.K_UP: #判斷哪個key
pass
動畫精靈
其實就是指一個圖片類,比如飛機游戲中的飛機,然后新建一個類,里面就存這個飛機的圖片
類繼承于pygame.sprite.Sprite
然后pygame使用精靈組來管理精靈的繪制和更新,精靈組是一個簡單的容器。一般使用pygame.sprite.Group()函數(shù)來創(chuàng)建一個精靈組,當要添加時用group.add(sprite_one)
舉例
class Ball(pygame.sprite.Sprite):
def __init__(self, image, position, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position #確定左上角位置
self.speed = speed
init()
screen = display.set_mode((600,400))
image = "a.jpg"
balls = []
for i in range(5):
position = [random.randint(0,600), random.randint(0,400)]
speed = [random.randint(0,10), random.randint(0,10)]
ball = Ball(image, position, speed)
balls.append(ball)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
for each in balls:
screen.blit(each.image, each.rect)
display.flip()
參考
https://www.cnblogs.com/msxh/p/5013555.html
碰撞檢測
pygame.sprite.spritecollide(sprite, group, dokill, collied=None):
用于檢測某個精靈是否和組中其他精靈相撞,第一個參數(shù)用于指定某個精靈,第二參數(shù)用于指定一個組(由sprite.Group()來生成),第三個用于設置是否從組中刪除檢測到碰撞的精靈,第四個參數(shù)指定一個回調函數(shù),用于定制一個特殊的檢測方法(默認情況下檢測精靈之間的rect屬性),舉例:
pygame.sprite.spritecollide(picture, group, False, pygame.sprite.collide_circle)
實例1.移動圖像小游戲,會自動移動,還可以通過鍵盤方向鍵移動
(自動移動、鍵盤事件)
import pygame
import sys
pygame.init() #使用pygame前需要初始化一下
size = width, height = 600, 400
speed = [-2, 1] #第一個參數(shù)代表x軸方向移動像素,這里就是向左移動2px,第二個就是向下移動1px
bg = (255, 255, 255)
fullscreen = False #先不全屏
screen = pygame.display.set_mode(size) #設置屏幕大小
pygame.display.set_caption('game') #設置標題
picture = pygame.image.load('a.jpg') #添加個圖片在里面
position = picture.get_rect() #獲取圖像的位置矩形
l_head = picture
r_head = pygame.transform.flip(picture, True, False)
while True:
for event in pygame.event.get(): #獲取事件
if event.type == pygame.QUIT: #當點擊右上角×時,關閉
sys.exit()
if event.type == pygame.KEYDOWN: #當點擊鍵盤上的按鍵時
if event.key == pygame.K_UP: #當按上鍵時,移動方向變成正上方
speed = [0, -1]
if event.key == pygame.K_DOWN:
speed = [0, 1]
if event.key == pygame.K_LEFT:
speed = [-1, 0]
picture = l_head
if event.key == pygame.K_RIGHT:
speed = [1, 0]
picture = r_head
if event.key == pygame.K_F11: #設置F11為全屏
fullscreen = not fullscreen #點擊后原來False和True就對調
if fullscreen:
size = width, heigth = 1920, 1080 #屏幕大小改變了,邊界也記得改變
screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)
#設置屏幕,第一個參數(shù)就是尺寸,第二個代表全屏,第三個是只有全屏才支持的硬件加速
else:
size = width, heigth = 600, 400 #變回原來尺寸后邊界尺寸也得變回來
screen = pygame.display.set_mode(size)
position = position.move(speed) #圖像位置移動
if position.left < 0 or position.right > width: #當要超出左/右邊界時
picture = pygame.transform.flip(picture, True, False)
#將圖像翻轉更新,第一個參數(shù)是要更新的東西,第二個代表是否水平翻轉,第三個代表是否垂直翻轉
speed[0] = -speed[0] #速度反過來,原來向左就變成向右
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
screen.fill(bg) #設置填充背景
screen.blit(picture, position) #更新圖像
pygame.display.flip() #更新界面
pygame.time.delay(10) #設置10毫秒延遲
實例2.設置圖片,并可以用鼠標拖動(鼠標事件)
import pygame
import sys
pygame.init() # 使用pygame前需要初始化一下
size = width, height = 600, 400
bg = (255, 255, 255)
screen = pygame.display.set_mode(size) # 設置屏幕大小
pygame.display.set_caption('game') # 設置標題
picture = pygame.image.load('a.jpg') # 添加個圖片在里面
position = picture.get_rect() # 獲取圖像的位置矩形
moving = False #后面鼠標事件用來判斷使用
while True:
for event in pygame.event.get(): # 獲取事件
if event.type == pygame.QUIT: # 當點擊右上角×時,關閉
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: #當按下鼠標時
if event.button == 1: #按下的是鼠標左鍵
moving = True
if event.type == pygame.MOUSEBUTTONUP: #當松開鼠標時
if event.button == 1:
moving = False
if moving:
position = pygame.mouse.get_pos() #當moving為True時設置位置為鼠標當前的位置
screen.fill(bg) # 設置填充背景
screen.blit(picture, position) # 更新圖像
pygame.display.flip() # 更新界面
pygame.time.delay(10) # 設置10毫秒延遲
實例3.將事件顯示在屏幕
import pygame
import sys
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
pygame.display.set_caption('game')
bg = (0,0,0) #背景設置為黑色
font = pygame.font.Font(None, 20) #實例一個font對象,第一個參數(shù)是字體,這里選擇默認,第二個是字體大小
line_height = font.get_linesize() #將get_linesize()獲取的行高賦值給行高
position = 0
screen.fill(bg)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(font.render(str(event), True, (0,255,0)), (0, position))
#font.render()用于渲染文字,第一個參數(shù)是要渲染的文本,這里將事件獲取的內容渲染
#第二個參數(shù)代表是否刪除鋸齒(去除模糊的馬賽克)
#第三個參數(shù)是渲染顏色
position += line_height #渲染一次后,位置移動到下一行
if position > height: #當內容寫滿(大于屏幕高度),清屏
position = 0
screen.fill(bg)
pygame.display.flip()
實例4.自動貪食蛇
from pygame import *
import pygame
import sys
import random
init()
screen = display.set_mode((600,400))
cat = image.load("a.jpg")
position = cat.get_rect()
a = random.randint(100,600),random.randint(100,400)
speed = [0,0]
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if position.center[0] < a[0]: #通過判斷圖片和隨機生成點的位置來進行移動
speed[0] = 1
if position.center[1] < a[1]:
speed[1] = 1
if position.center[0] > a[0]:
speed[0] = -1
if position.center[1] > a[1]:
speed[1] = -1
if position.center[0] == a[0]:
speed[0] = 0
if position.center[1] == a[1]:
speed[1] = 0
if (position.center[0] == a[0]) & (position.center[1] == a[1]):
a = random.randint(100, 600), random.randint(100, 400)
position = position.move(speed)
screen.fill((255,255,255))
draw.rect(screen, (255,0,0), ((a),(10,10)),0)
screen.blit(cat, position)
display.flip()
time.delay(3)