一、创建窗口
import pygame #导入模块
if __name__ == '__main__':
# 1.初始化pygame
pygame.init()
# 2.创建窗口
# set_mode((宽,高)) 单位像素
screen=pygame.display.set_mode((600,400))
# 3.游戏循环
while True:
#检测事件
for event in pygame.event.get():
#检测窗口上的关闭按钮是否被点击
if event.type==pygame.QUIT:
#退出游戏
print('关闭按钮被点击')
exit()
创建的窗口如下:
image.png
窗口中显示文字
import pygame
from pygame import font
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
#设置窗口的背景颜色
screen.fill((255,255,0))
#1.创建字体对象(找一只笔)
"""
SysFont(name, size, bold=0, italic=0, constructor=None)
name->字体名
size->字体大小
bold->加粗 (有默认值,不用传参)
italic->倾斜 (有默认值,不用传参)
"""
# font=pygame.font.SysFont('Script',50)
"""
#创建自定义字体
Font(字体文件路径,字体大小)
"""
font=pygame.font.Font('./font/bbb.ttf',50)
#2.根据字体去创建显示对象(找内容)
"""
render渲染
render(self, text, antialias, color, background=None)
self
text->要显示的文字内容(str)
antialias->是否平滑(True,False)
color->计算机三原色(红R、绿G、蓝B)RGB颜色,值得范围0-255
(255,255,255)->白色
(0,0,0)->黑色
(0,255,0)->绿色
(255,0,0)->红色
(x,x,x)->灰色
"""
surface=font.render('你好,hello pygame',True,(0,255,0))
print(surface,type(surface))
#3.将内容添加到窗口上(画到纸上)
"""
blit(需要显示的对象,显示位置)
需要显示的对象->Surface类型的数据
显示位置->坐标(x,y)
"""
screen.blit(surface,(100,100))
#4.将窗口上的内容展示出来(将画有文字的纸贴出来)
pygame.display.flip()
#游戏循环
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
三、窗口中显示图片
import pygame
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
#1.获取图片对象
image=pygame.image.load('./image./1.jpg')
"""
(1)获取图片大小
get_size()
"""
image_size=image.get_size()
print(image_size)
"""
(2)图片的缩放
transform(形变):形变包含缩放、旋转和平移
scale(缩放对象,新的大小)--->返回一个缩放后的新对象(缩放到指定大小)
"""
image=pygame.transform.scale(image,(600,400))
"""
旋转
rotate(旋转对象,旋转角度)
"""
image=pygame.transform.rotate(image,180)
"""
旋转、缩放(按比例)
def rotozoom(旋转对象,旋转的角度,缩放比例)
"""
image=pygame.transform.rotozoom(image,270,0.5)
#2.将图片对象渲染到窗口上
screen.blit(image,(0,0))
#3.展示在屏幕上
pygame.display.flip()
#循环游戏:
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
运行效果:
image.png
四、显示图形
import pygame
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
"""
1.画直线
line(外观对象,线颜色,起点,终点,线条宽度)
Surface->画在哪里
color->线的颜色
start_pos->起点
end_pos->终点
width->线的宽度
lines(外观对象,线条颜色,是否闭合,点的列表,width=1)
"""
pygame.draw.line(screen,(0,255,0),(50,50),(100,100),5)
pygame.draw.lines(screen,(255,0,0),True,[(10,10),(20,50)])
"""
2.画矩形
rect(位置,颜色,(起点坐标,宽,高),线宽为零即填充)
"""
pygame.draw.rect(screen,(0,255,0),(0,0,200,200),0)
"""
3.画曲线
arc(图形对象,颜色,范围,起始角度,终止角度,线宽)
Rect->(x,y,width,height)矩形
"""
from math import pi
pygame.draw.arc(screen,(0,0,0),(200,200,100,100),pi,2*pi)
"""
4.画圆
circle(位置,颜色,圆心位置,半径,width=0)
"""
import random
pygame.draw.circle(screen,\
(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),\
(400, 200), 100)
"""
5.画椭圆
ellipse(Surface, color, Rect, width=0)
"""
pygame.draw.ellipse(screen,(0,100,0),(100,300,200,80),0)
#将内容展示在屏幕上
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
运行效果:
image.png
五、动画原理
import pygame
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
x=0
y=0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
x+=1
y+=1
screen.fill((255,255,255))
pygame.draw.circle(screen,(255,255,0),(x,y),80)
pygame.display.flip()