pygame
1.pygame基本操作
import pygame
#1初始化游戲模塊
pygame.init()
#創(chuàng)建游戲窗口
#display.set_mode(窗口大小):創(chuàng)建一個(gè)窗口并且返回
窗口大小:是一個(gè)元組;并且元組中需要兩個(gè)值分別表示寬度和高度
window = pygame.display.set_mode((400,500))
#讓游戲一直運(yùn)行,直到點(diǎn)擊關(guān)閉按鈕才結(jié)束
while True:
#獲取游戲中產(chǎn)生的所有事件
for event in pygame.event.get():
#type判斷事件的類型
if event.type == pygame.QUIT:
exit() #退出程序
2.顯示圖片
fill(顏色)
顏色;計(jì)算機(jī)三原色(紅,綠,藍(lán)),每個(gè)顏色對(duì)應(yīng)的值得范圍是0-255,
可以通過改變?nèi)闹悼梢哉{(diào)配出不同的顏色
顏色值:是一個(gè)元組,元組中三個(gè)元素,分別代表紅綠藍(lán)(rgb)
(255,0,0)---red
(0,255,0)---green
(0,0,255)---blue
import pygame
#1.初始化游戲模塊
pygame.init()
#2.創(chuàng)建窗口
window = pygame.display.set_mode((600,600))
#給窗口填充顏色
window.fill((100,100,100))
# pygame.load():獲取本地圖片,返回圖片對(duì)象
image = pygame.image.load("./files/timg2.jpg")
# getsize()獲取圖片的大小,返回值為一個(gè)元組,有兩個(gè)元素,分別是寬和高
image_width,image_height = image.get_size()
#渲染圖片
#blit(渲染對(duì)象,位置)
#位置:坐標(biāo)(x,y),值的類型是元組,元組有兩個(gè)元素分別對(duì)應(yīng)x,y坐標(biāo)
window.blit(image,(600-image_width,600-image_height))
#展示內(nèi)容
pygame.display.flip()
#3.游戲循環(huán)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
結(jié)果:
3.形變
形變:
a.縮放
transform.scale(縮放對(duì)象,目標(biāo)大小):將指定的對(duì)象縮放到指定的大小,會(huì)返回縮放后的對(duì)象
b.旋轉(zhuǎn)縮放(指定縮放比例)
rotozoom(Surface,angle,scale)
Surface:旋轉(zhuǎn)縮放對(duì)象
angle:旋轉(zhuǎn)的角度(0-360)
scale:縮放比例
c.旋轉(zhuǎn)
rotate(Surface,angle)
Surface:旋轉(zhuǎn)縮放對(duì)象
angle:旋轉(zhuǎn)的角度(0-360)
import pygame
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
#圖片相關(guān)
#1.加載圖片
image = pygame.image.load("./files/timg2.jpg")
#縮放
new_image = pygame.transform.scale(image,(100,100))
#旋轉(zhuǎn)縮放(指定縮放比例)
new_image = pygame.transform.rotozoom(image,0,1)
# new_image = pygame.transform.rotate(image,45)
#2.渲染圖片
window.blit(new_image,(100,100))
#3.展示內(nèi)容
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
4.顯示文字
a.創(chuàng)建系統(tǒng)的字體對(duì)象
SysFont(name, size, bold=False, italic=False)
name : 字體名(系統(tǒng)支持的字體名)
size :字體大小
bold:是否加粗
italic:是否傾斜
b.創(chuàng)建自定義的字體對(duì)象
Font(字體文件路徑,字體大小)
字體文件路徑:ttf文件
render( text, antialias, color,background=None)
text:需要顯示的文字(字符串)
antialias:是否平滑(布爾)
color:顏色
background:背景顏色
import pygame
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
#顯示文字
#創(chuàng)建字體對(duì)象
#a創(chuàng)建系統(tǒng)的字體對(duì)象
font = pygame.font.SysFont("Times",30)
#b創(chuàng)建自定義字體對(duì)象
font = pygame.font.Font("./files/aa.ttf",30)
#根據(jù)字體創(chuàng)建文字對(duì)象
ext = font.render("winner winner chicken dinner",True,(0,0,255),(255,255,0))
print(text.get_size())
#3.渲染文字
window.blit(text,(50,50))
#4.展示內(nèi)容
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
結(jié)果:
5.顯示圖形
1.畫直線
def line(Surface, color, start_pos, end_pos, width=1)
Surface:畫在哪
color:線的顏色
start_pos:起點(diǎn)
end_pos:終點(diǎn)
width=1:線的寬度
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.line(window,(255,0,0),(50,50),(400,50),8)
pygame.draw.line(window,(255,0,0),(50,50),(50,400),8)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
2.畫線段(折線)
def lines(Surface, color, closed, pointlist, width=1)
Surface:畫在哪
color:線的顏色
closed:是否閉合(是否連接起點(diǎn)和終點(diǎn))
pointlist:點(diǎn)對(duì)應(yīng)的列表
width:線寬
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.lines(window,(255,0,0),True,[(100,200),(250,120),(20,450)],6)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
3.畫圓
def circle(Surface, color, pos, radius, width=0)
Surface:畫哪
color:顏色
pos:圓心坐標(biāo)
radius:半徑
width:線寬 0-->填充
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.circle(window,(255,255,0),(250,250),100,2)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
4畫矩形
def rect(Surface, color, Rect, width=0)
Surface:畫哪
color:顏色
Rect:范圍(元組,元組中的四個(gè)元素,分別是x,y,width,height)
width:線寬
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.rect(window,(220,255,100),(0,0,50,100))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
5.畫多邊形
def polygon(Surface, color, pointlist, width=0)
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.polygon(window,(0,255,200),[(100,200),(50,450),(150,300)])
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
6.畫橢圓
def ellipse(Surface, color, Rect, width=0)
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.ellipse(window,(200,200,250),(100,100,300,200))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
7.畫弧線
def arc(Surface, color, Rect, start_angle, stop_angle, width=1)
start_angle:0-2pi
stop_angle:
import pygame
from math import pi
pygame.init()
window = pygame.display.set_mode((500,500))
window.fill((255,255,255))
pygame.draw.arc(window,(200,100,120),(200,200,150,150),0,pi/2)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
6.事件
事件的type:
QUIT:關(guān)閉按鈕被點(diǎn)擊事件
MOUSEBUTTONDOWN:鼠標(biāo)按下事件
MOUSEBUTTONUP:鼠標(biāo)彈起
MOUSEMOTION:鼠標(biāo)移動(dòng)
KEYDOWN:鍵盤按下
KEYUP:鍵盤彈起
b.事件的pos ---鼠標(biāo)事件發(fā)生的位置(坐標(biāo))
c.事件的key----鍵盤事件被按的鍵對(duì)應(yīng)的編碼值
import pygame
from random import randint
pygame.init()
window = pygame.display.set_mode((600, 600))
window.fill((0, 255, 255))
#游戲循環(huán)
while True:
#所有的事件處理的入口就是for循環(huán)
#for循環(huán)中的代碼只有游戲事件發(fā)生后才會(huì)執(zhí)行
for event in pygame.event.get():
#不同的事件發(fā)生后,對(duì)用type值不一樣
if event.type == pygame.QUIT:
print("點(diǎn)擊關(guān)閉按鈕")
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
print(event.pos)
print("按下")
pygame.draw.circle(window,(randint(0,255),randint(0,255),\
randint(0,255)),event.pos,randint(50,100))
pygame.display.flip()
elif event.type == pygame.MOUSEBUTTONUP:
print("彈起")
elif event.type == pygame.MOUSEMOTION:
print("在動(dòng)")
elif event.type == pygame.KEYDOWN:
print("按下去",chr(event.key))
elif event.type == pygame.KEYUP:
print("彈起來")
7.動(dòng)畫原理
import pygame
pygame.init()
window = pygame.display.set_mode((600, 600))
window.fill((255, 255, 255))
pygame.display.flip()
#球的圓心坐標(biāo)
x = 100
y = 100
r = 20
add1 = 4
add2 = 3
#游戲循環(huán)
while True:
#將之前的球覆蓋
window.fill((255,255,255))
#延遲
pygame.time.delay(10)
#不斷的畫圓
pygame.draw.circle(window,(255,0,0),(x,y),r)
pygame.display.update()
#改變y值讓圓在垂直方向移動(dòng)
y += add1
x += add2
# r += add
# if r>=120 or r<=20:
# add *= -1
#邊界檢測(cè)
if y >= 580 or y<=20:
add1 *= -1
if x>= 580 or x<=20:
add2 *= -1
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
8.按住不放原理
import pygame
pygame.init()
window = pygame.display.set_mode((600, 600))
window.fill((255, 255, 255))
# pygame.display.flip()
image = pygame.image.load("./files/timg2.jpg")
#縮放
image = pygame.transform.rotozoom(image,0,0.5)
window.blit(image,(100,100))
#獲取圖片的寬度和高度
image_w,image_h = image.get_size()
pygame.display.flip()
#用來存儲(chǔ)圖片是否移動(dòng)
flag = False
# 保存圖片的坐標(biāo)
image_x = 100
image_y = 100
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
#判斷鼠標(biāo)按下的位置是否在圖片上
m_x,m_y = event.pos
if image_x<=m_x<=image_x+image_w and image_y<=m_y<=image_y+image_h:
flag = True
if event.type == pygame.MOUSEBUTTONUP:
flag = False
#鼠標(biāo)移動(dòng)事件
#鼠標(biāo)在移動(dòng)并且flag是True
if event.type == pygame.MOUSEMOTION and flag:
#填充背景色,覆蓋原來的內(nèi)容
window.fill((255,255,255))
#鼠標(biāo)移動(dòng)的位置渲染圖片
center_x,center_y = event.pos
image_x,image_y = center_x-image_w/2,center_y-image_h/2
window.blit(image,(image_x,image_y))
# 更新屏幕顯示
pygame.display.update()