一.最小游戲系統(tǒng)
import pygame
# 1.初始化游戲相關(guān)的硬件
pygame.init()
# 2.創(chuàng)建游戲窗口
"""
set_mode(窗口大小) - 窗口大小: (width, height)
"""
window = pygame.display.set_mode((600, 400))
# 3.游戲循環(huán)(保證游戲能夠一直運(yùn)行)
while True:
# 4.檢測(cè)事件
for event in pygame.event.get():
# 判斷事件類(lèi)型,根據(jù)不同的事件做不同的事情
# 5.判斷關(guān)閉按鈕是否點(diǎn)擊
if event.type == pygame.QUIT:
exit() # 關(guān)閉程序
二.顯示圖片
import pygame
1.初始化游戲相關(guān)的硬件
pygame.init()2.創(chuàng)建游戲窗口
"""
set_mode(窗口大小) - 窗口大小: (width, height)
"""
window = pygame.display.set_mode((600, 400))3.設(shè)置窗口標(biāo)題
pygame.display.set_caption('圖片')4.設(shè)置窗口背景顏色
"""
計(jì)算機(jī)三原色(rgb): 紅(red)、綠(green)、藍(lán)(blue)
r、g、b的范圍是0-255
(255,0,0) - 紅色
(0,255,0) - 綠色
(0,0,255) - 藍(lán)色
(255,255,255) - 白色
(0, 0, 0) - 黑色
(255, 255, 0) - 黃色
"""
window.fill((0, 100, 0))
===================顯示圖片====================
1.1加載圖片
"""
load(圖片路徑) - 將指定路徑下的圖片轉(zhuǎn)換成圖片對(duì)象,并且返回
相對(duì)路徑:./ - 代表當(dāng)前文件所在的目錄
../ - 代表當(dāng)前文件所在的目錄的上層目錄
.../ - 代表當(dāng)前文件所在的目錄的上層目錄的上層目錄
"""
image1 = pygame.image.load('./files/luffy4.jpg')
image2 = pygame.image.load('./files/luffy1.png')
1.2 獲取圖片大小
img_w1, img_h1 = image1.get_size()
print(img_w1, img_h1)
1.3 圖片的縮放
"""
scale(圖片對(duì)象, 大小) - 將指定圖片縮放到指定的大小,返回縮放后的新圖片
"""
image1 = pygame.transform.scale(image1, (200, 200))
"""
rotozoom(圖片對(duì)象, 旋轉(zhuǎn)角度, 縮放比例)
旋轉(zhuǎn)角度 - 角度(0~360)
縮放比例 - 縮放比
"""
image1 = pygame.transform.rotozoom(image1, 45, 0.5)
2.顯示圖片
"""
- blit(顯示對(duì)象, 位置) - 在窗口的指定位置顯示指定對(duì)象
位置 - 是一個(gè)元祖, (x, y)
"""
window.blit(image2, (0, 0))
window.blit(image1, (0, 0))
- 刷新屏幕(只要是想要更新屏幕的顯示,都必須刷新屏幕)
pygame.display.flip()
3.游戲循環(huán)(保證游戲能夠一直運(yùn)行)
while True:
# 4.檢測(cè)事件
for event in pygame.event.get():
# 判斷事件類(lèi)型,根據(jù)不同的事件做不同的事情
# 5.判斷關(guān)閉按鈕是否點(diǎn)擊
if event.type == pygame.QUIT:
exit() # 關(guān)閉程序
三.顯示文字
import pygame
1.初始化游戲相關(guān)的硬件
pygame.init()
2.創(chuàng)建游戲窗口
"""
set_mode(窗口大小) - 窗口大小: (width, height)
"""
window = pygame.display.set_mode((600, 400))
pygame.display.set_caption('文字')
window.fill((255, 255, 255))
=================顯示文字=================
1. 創(chuàng)建字體對(duì)象(選筆)
"""
1)系統(tǒng)字體
SysFont(字體名字,字體大小,是否加粗=False,是否傾斜=False) - 返回字體對(duì)象
2)自定義字體
Font(字體文件路徑, 字體大小)
"""
font1 = pygame.font.SysFont('宋體', 50)
font1 = pygame.font.Font('./files/aa.ttf', 50)
2.創(chuàng)建文字對(duì)象
"""
render(文字,是否平滑,文字顏色, 背景顏色=None)
"""
text = font1.render('hello pygame 你好', True, (0, 0, 0), (255, 255, 0))
-
獲取文字對(duì)象大小
t_w, t_h = text.get_size()
print(text.get_size())
-
旋轉(zhuǎn)文字
text = pygame.transform.rotozoom(text, 45, 1)
3.顯示文字對(duì)象
window.blit(text, (600-t_w, 400-t_h))
window.blit(text, (0, 0))
pygame.display.flip()
3.游戲循環(huán)(保證游戲能夠一直運(yùn)行)
while True:
# 4.檢測(cè)事件
for event in pygame.event.get():
# 判斷事件類(lèi)型,根據(jù)不同的事件做不同的事情
# 5.判斷關(guān)閉按鈕是否點(diǎn)擊
if event.type == pygame.QUIT:
exit() # 關(guān)閉程序
四.顯示圖形(繪畫(huà))
===============顯示圖形==============
1.直線(xiàn)
"""
line(畫(huà)在哪兒, 顏色,起點(diǎn),終點(diǎn),線(xiàn)寬=1)
"""
pygame.draw.line(window, (255, 0, 0), (10, 10), (10, 100), 5)
"""
def lines(Surface, color, closed, pointlist, width=1)
"""
points = [(20, 20), (300, 200), (200, 400), (120, 100), (500, 50)]
pygame.draw.lines(window, (0, 255, 0), True, points)
2.畫(huà)圓
"""
def circle(Surface, color, pos, radius, width=0)
"""
圓環(huán)
pygame.draw.circle(window, (0, 0, 255), (100, 100), 50, 2)
實(shí)心圓
pygame.draw.circle(window, (255, 255, 0), (100, 200), 50, 0)
3.畫(huà)矩形
"""
def rect(Surface, color, Rect, width=0)
Rect - (x, y, w, h)
"""
pygame.draw.rect(window, (255, 20, 110), (290, 50, 120, 80))
pygame.draw.rect(window, (100, 20, 110), (300, 60, 100, 60))
4.畫(huà)橢圓
"""
def ellipse(Surface, color, Rect, width=0)
"""
pygame.draw.ellipse(window, (0, 23, 200), (300, 200, 200, 100))
5.畫(huà)弧線(xiàn)
"""
def arc(Surface, color, Rect, start_angle, stop_angle, width=1)
start_angle,stop_angle - 弧度(0-2pi)
"""
pi = 3.1415926
pygame.draw.arc(window, (0, 0, 0), (20, 250, 100, 100), pi/4, 3*pi/4, 5)
6.畫(huà)多邊形
"""
def polygon(Surface, color, pointlist, width=0)
"""
points2 = [(100, 100), (120, 200), (80, 200)]
pygame.draw.polygon(window, (20, 200, 100), points2)
pygame.display.flip()
3.游戲循環(huán)(保證游戲能夠一直運(yùn)行)
while True:
# 4.檢測(cè)事件
for event in pygame.event.get():
# 判斷事件類(lèi)型,根據(jù)不同的事件做不同的事情
# 5.判斷關(guān)閉按鈕是否點(diǎn)擊
if event.type == pygame.QUIT:
exit() # 關(guān)閉程序
五.做動(dòng)畫(huà)
from random import randint
import pygame
1.初始化游戲相關(guān)的硬件
pygame.init()
2.創(chuàng)建游戲窗口
"""
set_mode(窗口大小) - 窗口大小: (width, height)
"""
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
text = pygame.font.SysFont('Times', 50).render('hello', True, (255, 0, 255))
window.blit(text, (100, 200))
================動(dòng)畫(huà)效果==================
pygame.draw.circle(window, (255, 0, 0), (100, 100), 50)
c_x = 100
c_y = 100
# 第一次刷新
pygame.display.flip()
# 3.游戲循環(huán)(保證游戲能夠一直運(yùn)行)
num = 0
speed = 3
r = 50
speed2 = 1
while True:
pygame.time.delay(10)
=========移動(dòng)============
pygame.draw.circle(window, (255, 255, 255), (c_x, c_y), r)
# 變大小
r += speed2
if r > 100:
r = 100
speed2 *= -1
elif r < 10:
r = 10
speed2 *= -1
# 移動(dòng)
c_y += speed
if c_y >= 550:
c_y = 550
speed *= -1
elif c_y < 50:
c_y = 50
speed *= -1
# window.fill((255, 255, 255))
pygame.draw.circle(window, (randint(0, 255), randint(0, 255), randint(0, 255)), (c_x, c_y), r)
pygame.display.flip()
window.blit(text, (100, 200))
##### =========閃爍===========
# if num % 10000 == 0:
# num = 0
# pygame.draw.circle(window, (randint(0, 255), randint(0, 255), randint(0, 255)), (c_x, c_y), 50)
# pygame.display.update()
##### 延時(shí)指定時(shí)間,時(shí)間單位是毫秒
# pygame.time.delay(100)
# pygame.draw.circle(window, (randint(0, 255), randint(0, 255), randint(0, 255)), (c_x, c_y), 50)
# pygame.display.update()
# 4.檢測(cè)事件
for event in pygame.event.get():
# 判斷事件類(lèi)型,根據(jù)不同的事件做不同的事情
# 5.判斷關(guān)閉按鈕是否點(diǎn)擊
if event.type == pygame.QUIT:
exit() # 關(guān)閉程序
---
---
###六.事件
from random import randint
import pygame
# 1.初始化游戲相關(guān)的硬件
pygame.init()
# 2.創(chuàng)建游戲窗口
"""
set_mode(窗口大小) - 窗口大小: (width, height)
"""
window = pygame.display.set_mode((600, 400))
window.fill((255, 255, 255))
pygame.draw.circle(window, (255, 255, 0), (100, 100), 50)
pygame.display.flip()
# 3.游戲循環(huán)(保證游戲能夠一直運(yùn)行)
is_blink = False
while True:
if is_blink:
window.fill((255, 255, 255))
pygame.draw.circle(window, (randint(0, 255), randint(0, 255), randint(0, 255)), (100, 100), 50)
pygame.display.update()
# 4.檢測(cè)事件
"""
1.鼠標(biāo)事件 - event.pos
MOUSEBUTTONUP
MOUSEBUTTONDOWN
MOUSEMOTION
2.鍵盤(pán)事件 - event.key
KEYDOWN
KEYUP
"""
for event in pygame.event.get():
# 判斷事件類(lèi)型,根據(jù)不同的事件做不同的事情
# 5.判斷關(guān)閉按鈕是否點(diǎn)擊
if event.type == pygame.QUIT:
exit() # 關(guān)閉程序
elif event.type == pygame.MOUSEBUTTONDOWN:
print('鼠標(biāo)按下', event.pos)
is_blink = not is_blink
elif event.type == pygame.MOUSEBUTTONUP:
print('鼠標(biāo)彈起', event.pos)
elif event.type == pygame.MOUSEMOTION:
print('動(dòng)', event.pos)
pygame.draw.circle(window, (randint(0, 255), randint(0, 255), randint(0, 255)), event.pos, 100)
pygame.display.update()
elif event.type == pygame.KEYDOWN:
print('鍵盤(pán)按下', event.key, chr(event.key))
elif event.type == pygame.KEYUP:
print('鍵盤(pán)彈起', event.key, chr(event.key))