目录
模块安装和导入
模块介绍
Pygame是一个开源的Python库,主要用于开发2D电子游戏和多媒体应用程序。
Pygame由Pete Shinners所创作,并建立在Simple DirectMedia Layer(SDL)库的基础上。它允许开发者使用Python语言来创建具有图形、音效和强交互性的游戏和应用程序。Pygame支持多种操作系统,包括Windows、Linux和Mac OS,具有良好的跨平台性。此外,Pygame提供了多种操作模块,如图像模块(image)、声音模块(mixer)、输入/输出(鼠标、键盘、显示屏)模块等,特别适合开发2D游戏,如飞机大战、贪吃蛇、扫雷等。
模块下载
1.按住win+R 输入cmd
2.输入 pip install pygame 然后回车可以下载
(如果觉得下载很慢可以换源下载)
换源方法:
pip install 模块名 -i 镜像源
其中模块名就是pygame ,镜像源的话国内有很多好的下载源,如豆瓣,清华,阿里云等等,此时我用我比较喜欢用的清华大学的镜像源作为示范,其他的同理
豆瓣:https://pypi.douban.com/simple
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:https://mirrors.aliyun.com/pypi/simple
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple
模块导入
import pygame
import sys
sys模块主要是对窗口关闭用的
游戏原理
游戏的原理无非是 显示画面 → 玩家操作 → 电脑运算 → 刷新画面 → 显示画面 然后继续进入玩家操作,形成一个循环,如果玩家选择退出就退出这个循环
由此就对游戏组成可以分为三部分:图像显示,接收指令,处理指令
图像显示
在 pygame模块 中可以使用 pygame.display()模块 来对图像进行显示
#导入所需的模块
import sys
import pygame
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((400,400))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
这样就可以显示出一个 “黑框”
但是这样的话会导致这个 “黑框” 一闪而过,为了维持这个窗口可以加入这段 “固定” 的代码
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
pygame.display.flip() #更新屏幕内容
这段代码几乎所有游戏都需要用到,不仅可以维持画面显示,还可以检测玩家是否关闭窗口
效果如下
这个黑框基本上是游戏的底层本体了
代码讲解
主窗口创建
#也叫screen对象,本质上是一个Surface,大小400*400
screen = pygame.display.set_mode((400,400))
screen = pygame.display.set_mode(size=(),flags=0)
size:屏幕尺寸,类型为元组
flags:功能标志位
flags可取值如下,默认为0,所以可以不填
pygame.FULLSCREEN | 创建一个全屏窗口。 |
pygame.HWSURFACE | 创建一个硬件加速窗口,必须和 FULLSCREEN 同时使用。 |
pygame.OPENGL | 创建一个 OPENGL 渲染窗口。 |
pygame.RESIZABLE | 创建一个可以改变大小的窗口。 |
pygame.DOUBLEBUF | 创建一个双缓冲区窗口,建议在 HWSURFACE 或者 OPENGL 时使用 。 |
pygame.NOFRAME | 创建一个没有边框的窗口。 |
标题修改
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
游戏循环
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
pygame.display.flip() #更新屏幕内容
这段目前只需知道显示的时候加上就行了,后面有对这段的讲解
图像创建
纯色创建
Surface=pygame.Surface(size=(width,height),flags,depth)
Surface_1.fill(color='pink')
pygame.Surface()可以创建一个“图层”
可以用fill()方法对这个图层进行纯色填充
color='pink'代表填充粉色,也可以用 rgb 构成的一个三元组来替换,如:color=(255,255,255)可以填充白色
size与flags与上面相同
depth表示颜色深度,默认为自动调节
图片创建
surface_image =pygame.image.load("图片路径")
pygame.image.load()可以创建一个图片画面
图像绘制
scrren.blit(source, dest, area=None, special_flags = 0)
source:图像资源,为字符串形式的图像地址
dest:绘制区域
area:显示区域,相当于抠图,抠出你这张图片想要显示的区域,默认为全部
图像刷新
pygame.display.flip() #更新屏幕内容
可以更新屏幕的画面
两段实例代码
#导入所需的模块
import sys
import pygame
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((400,400))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
# 创建图像
Surface_1=pygame.Surface(size=(200,200))
# 填充颜色
Surface_1.fill(color='pink')
# 图像显示
scrren.blit(Surface_1, (0,0))
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
pygame.display.flip() #更新屏幕内容
由这段不难发现,图像显示的位置为左上角为基准向,为此可以再做验证,如下
#导入所需的模块
import sys
import pygame
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((400+222,400))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
# 创建图像
surface_image =pygame.image.load("C:\code\python\shell.png")
# 图像显示
scrren.blit(surface_image, (400,0))
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
pygame.display.flip() #更新屏幕内容
图片分辨率为222*222
所以我在主窗口创建的时候 元组 第一个参数加了222
在显示的时候第一个参数加了400
然后可以发现图像与最右边对齐
因此可以得出结论,主窗口 元组 第一个参数代表宽度,第二个代表长度
图像显示的时候以图像左上角为显示基准
图像缩放旋转
图像大小不可能获得的时候就可以满足自己要求,所以可以对其缩放,当然,也可以旋转
surface_2 = pygame.transform.rotozoom(surface,angle,scale)
surface:图像
angle:旋转角度
scale:缩放倍数
实例代码如下
import pygame
#引入pygame中所有常量,比如 QUIT
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((444,444))
pygame.display.set_caption('hello')
#加载一张图片(222*222)
image_surface = pygame.image.load(".\shell.png")
# 使用rotozoom() 旋转 5 度,将图像变成原来2倍
image_2 = pygame.transform.rotozoom(image_surface,5,2)
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# 将最后生成的image_2添加到显示屏幕上
screen.blit(image_2,(0,0))
pygame.display.update()
pygame.display.flip() #更新屏幕内容
pygame.display.update()#局部刷新
.\代表这个代码运行地址,与前面C:\code\python\效果一致,甚至更好用,方便在其他设备下运行
from pygame.locals import * #引入pygame中所有常量,比如 QUIT
将pygame中所有常量“解放”
文本文字创建
字体初始化
1)在系统中加载字体
pygame.font.SysFont(name, size, bold=False, italic=False)
name:列表类型,里面可以放一些字体,系统会自动从前往后寻找,直到找到可用字体
size:字体大小
bold:是否加粗
italic:是否为斜体
2)指定字体路径
my_font = pygame.font.Font(filename, size)
filename:字体文件的位置,字符串格式
size:字体尺寸
字体对象方法
如下
最常用这段
render(text, antialias, color, background=None)
text:内容
antialias:是否抗锯齿
color:颜色
background:字体背景颜色,默认为透明
实例代码如下
#导入所需的模块
import sys
import pygame
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((400+222,400))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
# 字体文件路径 C:/Windows/Fonts/simhei.ttf
f = pygame.font.Font('C:/Windows/Fonts/simhei.ttf',100)
# f=pygame.font.SysFont(['方正粗黑宋简体','microsoftsansserif'], size=100)
# 文本
text='hello'
# 创建一个文本的surface对象
surface_text=f.render(text,True,(101,220,222),(255,255,255))
#获得显示对象的 rect区域大小
textRect =surface_text.get_rect()
#设置显示对象居中
textRect.center = ((400+222)/2,400/2)
# 显示
scrren.blit(surface_text,textRect)
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
pygame.display.flip() #更新屏幕内容
接收指令(可以实现鼠标键盘的映射)
可以通过事件来接收指令,比如键盘鼠标,事件类型如下
对于时间处理有如下方法
键盘事件处理
实例代码如下
#导入所需的模块
import sys
import pygame
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((400+222,400))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
# 创建图像
surface_image =pygame.image.load("C:\code\python\shell.png")
# 图像显示
scrren.blit(surface_image, (0,0))
# 获取图片位置
imageRect=surface_image.get_rect()
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
# 通过 key 属性对应按键
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
imageRect[1] -= 30
if event.key == pygame.K_DOWN:
imageRect[1] += 30
if event.key == pygame.K_LEFT:
imageRect[0] -= 30
if event.key == pygame.K_RIGHT:
imageRect[0] += 30
scrren.fill((255,255,255))
scrren.blit(surface_image,imageRect)
pygame.display.flip() #更新屏幕内容
for event in pygame.event.get():循环获取事件
# 判断用户是否点了"X"关闭按钮,并执行if代码段
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
这段就是之前所说的判断玩家是否退出,加入到这里可以保证这个while不会形成死循环,很重要
if event.type == pygame.KEYDOWN:
如果发现接收到的是键盘上的信息,继续进行上下左右判断
鼠标事件处理
pygame.event.MOUSEMOTION鼠标移动事件
event.pos 相对于窗口左上角,鼠标的当前坐标值(x,y)
event.rel 鼠标相对运动距离(X,Y),相对于上次事件
event.buttons 鼠标按钮初始状态(0,0,0),分别对应(左键,滑轮,右键),移动过程中点击那个键,相应位置变会为1
pygame.event.MOUSEBUTTONUP鼠标键释放事件
event.pos 相对于窗口左上角,鼠标的当前坐标值(x,y)
event.button 鼠标释放键编号(整数)左键为1,按下滚动轮2、右键为3
pygame.event.MOUSEBUTTONDOWN 鼠标键按下事件
event.pos 相对于窗口左上角,鼠标的当前坐标值(x,y)
event.button 鼠标按下键编号(整数),左键为1,按下滚动轮2、右键为3,向前滚动滑轮4、向后滚动滑轮5
实例代码
#导入所需的模块
import sys
import pygame
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((800+222,800))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
# 创建图像
surface_image =pygame.image.load("C:\code\python\shell.png")
# 图像显示
scrren.blit(surface_image, (0,0))
# 获取图片位置
imageRect=surface_image.get_rect()
# 绘制图片
scrren.fill((255,255,255))
scrren.blit(surface_image,imageRect)
pygame.display.flip() #更新屏幕内容
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
# 鼠标按下
if event.type == pygame.MOUSEBUTTONDOWN:
# pos 获取鼠标当前位置
imageRect[0]=event.pos[0]
imageRect[1]=event.pos[1]
while True:
sd=0
# 鼠标移动
if event.type == pygame.MOUSEMOTION:
scrren.fill((255,255,255))
scrren.blit(surface_image,imageRect)
pygame.display.flip() #更新屏幕内容
imageRect[0]=event.pos[0]
imageRect[1]=event.pos[1]
for event in pygame.event.get():
# 鼠标松开
if event.type == pygame.MOUSEBUTTONUP:
# 记录松开,然后退出鼠标点击的循环
sd=1
if sd==1:
break
碰撞检测
最简单的就是创建一个类,把你要判断是否碰撞的对象放进去,然后调用函数判断是否碰撞
类里面基本上可以用到这些参数
由此可以创建如下的一个类
class pengzhuang(pygame.sprite.Sprite):
#定义参数
def __init__(self,filename,location):
# 调父类来初始化子类
pygame.sprite.Sprite.__init__(self)
# 加载图片
self.image = pygame.image.load(filename)
# 获取图片rect区域
self.rect = self.image.get_rect()
# 设置位置
self.rect.topleft=location
实例演示
#导入所需的模块
import sys
import pygame
class pengzhuang(pygame.sprite.Sprite):
#定义参数
def __init__(self,filename,location):
# 调父类来初始化子类
pygame.sprite.Sprite.__init__(self)
# 加载图片
self.image = pygame.image.load(filename)
# 获取图片rect区域
self.rect = self.image.get_rect()
# 设置位置
self.rect.topleft=location
if __name__== "__main__":
# 使用pygame之前必须初始化
pygame.init()
# 设置主屏窗口
scrren = pygame.display.set_mode((800+222,800))
# 设置窗口的标题,即游戏名称
pygame.display.set_caption('hello world')
# 创建图像
#第一个参数传入图像,第二个传入位置
# 之后打点调用image就是调用图片,打点调用react就是调用位置
surface_image = pengzhuang("C:\code\python\shell.png",(0,0))
surface_image_2 = pengzhuang("C:\code\python\lovey.png",(666,333))
# 我的图像2有点大,用前面的缩放函数缩放一下
surface_image_2.image = pygame.transform.rotozoom(surface_image_2.image,0,0.3)
# 绘制图片
scrren.fill((255,255,255))
scrren.blit(surface_image.image,surface_image.rect)
scrren.blit(surface_image_2.image,surface_image_2.rect)
pygame.display.flip() #更新屏幕内容
# 固定代码段,实现点击"X"号退出界面的功能,几乎所有的pygame都会使用该段代码
while True:
# 循环获取事件,监听事件状态
for event in pygame.event.get():
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序,确保退出程序
sys.exit()
# 鼠标按下
if event.type == pygame.MOUSEBUTTONDOWN:
# pos 获取鼠标当前位置
surface_image.rect[0]=event.pos[0]
surface_image.rect[1]=event.pos[1]
while True:
sd=0
# 鼠标移动
if event.type == pygame.MOUSEMOTION:
scrren.fill((255,255,255))
scrren.blit(surface_image.image,surface_image.rect)
scrren.blit(surface_image_2.image,surface_image_2.rect)
# 然后将两个对象传入就可以进行碰撞的判断
crash_result = pygame.sprite.collide_rect(surface_image,surface_image_2)
if crash_result == True:
print('碰撞了')
else:
print('没碰撞')
pygame.display.flip() #更新屏幕内容
surface_image.rect[0]=event.pos[0]
surface_image.rect[1]=event.pos[1]
for event in pygame.event.get():
# 鼠标松开
if event.type == pygame.MOUSEBUTTONUP:
# 记录松开,然后退出鼠标点击的循环
sd=1
if sd==1:
break
谢谢观看,后面是我用以上内容做的小游戏,文本显示部分可以用一个类来写(我一开始觉得没多少文本所以草率了,然后后面懒得改了)
游戏源码
import sys
import pygame
from random import randint
#引入pygame中所有常量,比如 QUIT
from pygame.locals import *
# 比例缩放
SHUFANG_N=2
SHUFANG=1/SHUFANG_N
# 红色边框背景
# BG=(200,50,60)
# 白色边框背景
# BG=(255,255,255)
# 黑色边框背景
# BG=(0,0,0)
# 自定义边框背景
BG=(153,217,234)
# 我的电脑全屏
BACKW=1768
BACKH=1080
# 比例缩放屏幕
# BACKW=1768//SHUFANG_N
# BACKH=1080//SHUFANG_N
def liuying(wz,surface_love,bg=BG):
pygame.display.flip()
screen.fill(bg)
screen.blit(screen_2,(BACKW//20,BACKH//20))
screen_2.fill(bg_2)
screen.blit(surface_love, wz)
pygame.display.update()
class Snake_ly(pygame.sprite.Sprite):
#定义构造函数
def __init__(self,filename,location):
# 调父类来初始化子类
pygame.sprite.Sprite.__init__(self)
# 加载图片
self.image = pygame.image.load(filename)
# 缩放
self.size=0.2
self.image=pygame.transform.rotozoom(self.image,0,self.size*SHUFANG)
# 获取图片rect区域
self.rect = self.image.get_rect()
# 设置位置
self.rect.topleft=location
class Snake_xyd(pygame.sprite.Sprite):
#定义构造函数
def __init__(self,filename,location):
# 调父类来初始化子类
pygame.sprite.Sprite.__init__(self)
# 加载图片
self.image = pygame.image.load(filename)
# 缩放
self.size =0.23
self.image=pygame.transform.rotozoom(self.image,0,self.size*SHUFANG)
# 获取图片rect区域
self.rect = self.image.get_rect()
# 设置位置
self.rect.topleft=location
class Snake_sw(pygame.sprite.Sprite):
#定义构造函数
def __init__(self,filename,location):
# 调父类来初始化子类
pygame.sprite.Sprite.__init__(self)
# 加载图片
self.image = pygame.image.load(filename)
# # 缩放
# 获取图片rect区域
self.rect = self.image.get_rect()
# 设置位置
self.rect.topleft=location
if __name__== "__main__":
# 初始化
pygame.init()
chushihua_start=0
# 信用点生成基准
XYD_BACEX=BACKW-BACKW//20
XYD_BACEY=BACKH-BACKH//20
XYD_SETX=(BACKW//20,XYD_BACEX-153//SHUFANG_N)
XYD_SETY=(BACKH//20,XYD_BACEY-139//SHUFANG_N)
# 流萤生成基准
LY_SETX=BACKW//20
LY_SETY=BACKH//20
#信用点计数
num_xydnum=0
sw_nandu=1
# 胜利检测
win_ly=0
#计数
sw_move_rand=0
# 提示文本
screen = pygame.display.set_mode((BACKW,BACKH))
#填充主窗口的背景颜色
screen.fill((153,217,234))
#设置窗口标题
pygame.display.set_caption('简介')
# 字体文件路径 C:/Windows/Fonts/simhei.ttf
f = pygame.font.Font('.\STHUPO.TTF',50)
# render(text, antialias, color, background=None) -> Surface
tishi_txt_1="按 |回车| 开始游戏,本游戏作者ALX,"
tishi_txt_2="小键盘 |上下左右| 可以操作人物“流萤”移动"
tishi_txt_3="数字|1|:加速 数字|2|:减速 |ESC|:退出游戏"
tishi_txt_4=f"信用点{num_xydnum}"
tishi_txt_5=f"对...不起..."
tishi_txt_5_2=f"你赢了"
tishi_txt_6="1:再来一局 2:退出"
tishi_txt_6_2="1:再来一局 2:退出"
text_1 = f.render(tishi_txt_1,True,(0,0,0),(153,217,234))
text_2 = f.render(tishi_txt_2,True,(125,125,125),(153,217,234))
text_3 = f.render(tishi_txt_3,True,(50,20,200),(153,217,234))
text_4 = f.render(tishi_txt_4,True,(50,20,200),(153,217,234))
text_5 = f.render(tishi_txt_5,True,(200,50,50))
text_5_2 = f.render(tishi_txt_5_2,True,(101,220,222))
text_6 = f.render(tishi_txt_6,True,(200,50,50))
text_6_2 = f.render(tishi_txt_6_2,True,(101,220,222))
#获得显示对象的 rect区域大小
textRect_1 =text_1.get_rect()
textRect_2 =text_2.get_rect()
textRect_3 =text_3.get_rect()
textRect_4 =text_4.get_rect()
textRect_5 =text_5.get_rect()
textRect_5_2 =text_5_2.get_rect()
textRect_6 =text_6.get_rect()
textRect_6_2 =text_6_2.get_rect()
#设置显示对象
textRect_1.center = (BACKW//2,BACKH//2-200)
textRect_2.center = (BACKW//2,BACKH//2-100)
textRect_3.center = (BACKW//2,BACKH//2)
textRect_4.center = (150,50)
textRect_5.center = (BACKW//2,BACKH//2)
textRect_5_2.center = (BACKW//2,BACKH//2)
textRect_6.center = (BACKW//2,BACKH//2+300)
textRect_6_2.center = (BACKW//2,BACKH//2+200)
screen.blit(text_1,textRect_1)
screen.blit(text_2,textRect_2)
screen.blit(text_3,textRect_3)
# 得分
# screen.blit(text_4,textRect_4)
tishi_txt_1="吃道具“信用点”达到10个获得胜利,"
tishi_txt_2="游戏中有个怪物会抓捕你,请在这个界面选择难度"
tishi_txt_3="数字|1|:简单模式 数字|2|:困难模式 |ESC|:退出游戏"
text_1 = f.render(tishi_txt_1,True,(0,0,0),(153,217,234))
text_2 = f.render(tishi_txt_2,True,(125,125,125),(153,217,234))
text_3 = f.render(tishi_txt_3,True,(50,20,200),(153,217,234))
while True:
break_txt=1
# 循环获取事件,监听事件
for event in pygame.event.get():
# 判断用户是否点了关闭按钮
# x退出
if event.type == QUIT:
exit()
#esc退出
if event.type ==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# 回车继续
if event.key == K_RETURN:
break_txt=0
if break_txt==0:
break
pygame.display.flip() #更新屏幕内容
screen.fill((153,217,234))
tishi_txt_1="吃道具“信用点”达到10个获得胜利,"
tishi_txt_2="游戏中有个怪物会抓捕你,请在这个界面选择难度"
tishi_txt_3="数字|1|:简单模式 数字|2|:困难模式 |ESC|:退出游戏"
screen.blit(text_1,textRect_1)
screen.blit(text_2,textRect_2)
screen.blit(text_3,textRect_3)
while True:
break_txt=1
# 循环获取事件,监听事件
for event in pygame.event.get():
# 判断用户是否点了关闭按钮
# x退出
if event.type == QUIT:
exit()
#esc退出
if event.type ==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# 简单
if event.key == K_1:
break_txt=0
sw_nandu=1
# 困难
if event.key == K_2:
break_txt=0
sw_nandu=2
# # 回车继续
# if event.key == K_RETURN:
# break_txt=0
if break_txt==0:
break
pygame.display.flip() #更新屏幕内容
# 窗口名字
pygame.display.set_caption('流萤')
# 底层
size = width, height = BACKW,BACKH
bg = BG
bg_2 = (255,255,255)
# 全屏模式
screen=pygame.display.set_mode(size,pygame.FULLSCREEN)
SW_LY=(80,0)
# 正常比例
# screen=pygame.display.set_mode(size)
# SW_LY=(0,0)
screen_2=pygame.transform.rotozoom(screen,0,0.9)
# 创建图像
# 死亡
surface_lyj =pygame.image.load(".\shell.jpg")
# surface_lyj=pygame.transform.rotozoom(surface_lyj,0,0.1)
die_ly=0
# 第一版
# # 流萤
# surface_lovey =pygame.image.load(".\lovey.png")
# surface_lovey =pygame.transform.rotozoom(surface_lovey,0,0.2)
# surface_lovey=pygame.transform.rotozoom(surface_lovey,0,SHUFANG)
# surface_lovez =pygame.image.load(".\lovez.png")
# surface_lovez =pygame.transform.rotozoom(surface_lovez,0,0.2)
# surface_lovez=pygame.transform.rotozoom(surface_lovez,0,SHUFANG)
# # 信用点
# surface_xyd =pygame.image.load("xyd_2.png")
# surface_xyd =pygame.transform.rotozoom(surface_xyd,0,0.23)
# surface_xyd=pygame.transform.rotozoom(surface_xyd,0,SHUFANG)
# # 生成初始流萤
# surface_love=surface_lovey
# screen.blit(surface_love,(LY_SETX,LY_SETY))
# # screen.blit(surface_xyd,(800,500))
# 第二版
surface_lovey_tp =".\lovey.png"
surface_lovez_tp =".\lovez.png"
surface_xyd_tp="xyd_2.png"
surface_love_tp=".\lovey.png"
surface_sw_tp=".\sw.png"
surface_lovey=Snake_ly(surface_lovey_tp,(LY_SETX,LY_SETY))
surface_lovez=Snake_ly(surface_lovez_tp,(LY_SETX,LY_SETY))
surface_love=Snake_ly(surface_love_tp,(LY_SETX,LY_SETY))
surface_sw=Snake_sw(surface_sw_tp,(BACKW-400,BACKH-300))
if sw_nandu==1:
surface_sw.image=pygame.transform.rotozoom(surface_sw.image,0,0.3*SHUFANG)
if sw_nandu==2:
surface_sw.image=pygame.transform.rotozoom(surface_sw.image,0,0.5*SHUFANG)
screen.blit(surface_love.image,surface_love.rect)
# 获取图像的位置
position = surface_love.rect
position[0]=LY_SETX
position[1]=LY_SETY
# 控制上下左右的
a_sxzy=0
num_move=1
# 古希腊掌管信用点的列表
xyd_list=[]
# 速度
sudu=1
#创建时钟对象(控制游戏的FPS)
clock = pygame.time.Clock()
sizedat=0.2
# 主循环
while True:
#通过时钟对象,指定循环频率,每秒循环180次
clock.tick(180)
# 控制移动的变量
site_move = [0, 0]
for event in pygame.event.get():
# x退出
if event.type == QUIT:
exit()
#esc退出
if event.type ==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type ==KEYDOWN:
if event.key == K_UP:
# print('上')
a_sxzy=1
num_move=-sudu
if event.key == K_DOWN:
# print('下')
a_sxzy=1
num_move=sudu
if event.key == K_RIGHT:
# print('右')
surface_love.image=surface_lovey.image
a_sxzy=0
num_move=sudu
if event.key == K_LEFT:
# print('左')
surface_love.image=surface_lovez.image
num_move=-sudu
a_sxzy=0
# 按空格随机生成信用点
if event.key == K_SPACE:
xyd_x=randint(*XYD_SETX)
xyd_y=randint(*XYD_SETY)
xyd_list.append((xyd_x,xyd_y))
# 1加速
if event.key == K_1:
sudu+=1
if num_move>=0:
num_move=sudu
else :num_move=-sudu
# 2减速
if event.key == K_2:
if sudu>0:
sudu-=1
if num_move>=0:
num_move=sudu
else :num_move=-sudu
# 鼠标移动流萤
if event.type == MOUSEBUTTONDOWN:
# pos 获取鼠标当前位置
# print('鼠标按下',event.pos)
position[0]=event.pos[0]
position[1]=event.pos[1]
liuying(event.pos,surface_love.image)
while True:
sd=0
if event.type == MOUSEMOTION:
liuying(event.pos,surface_love.image)
position[0]=event.pos[0]
position[1]=event.pos[1]
for event in pygame.event.get():
if event.type == MOUSEBUTTONUP:
sd=1
if sd==1:
break
site_move[a_sxzy]+=num_move
position = position.move(site_move)
surface_love.rect=position
# 将图像放置在主屏幕上
pygame.display.flip()
screen.fill(bg)
screen.blit(screen_2,(BACKW//20,BACKH//20))
screen_2.fill(bg_2)
# 第一版
# for xydxyd in xyd_list:
# surface_xyd=Snake_ly(surface_xyd_tp,xydxyd)
# screen.blit(surface_xyd.image,surface_xyd.rect)
# 第二版
if len(xyd_list)==0:
xyd_x=randint(*XYD_SETX)
xyd_y=randint(*XYD_SETY)
xyd_list.append((xyd_x,xyd_y))
surface_xyd=Snake_ly(surface_xyd_tp,xyd_list[0])
screen.blit(surface_xyd.image,surface_xyd.rect)
# 碰撞检测
# 吃信用点
crash_result = pygame.sprite.collide_mask(surface_love,surface_xyd)
if crash_result:
xyd_list=[]
num_xydnum+=1
tishi_txt_4=f"信用点:"+'■'*num_xydnum+'□'*(10-num_xydnum)
text_4 = f.render(tishi_txt_4,True,(50,20,200),(153,217,234))
# 变大会变模糊,放弃
else:
pass
# 抓捕
crash_result_2 = pygame.sprite.collide_mask(surface_love,surface_sw)
if crash_result_2:
num_move=0
die_ly=1
else:
# 困难模式
if sw_nandu==2:
if surface_sw.rect[0]>surface_love.rect[0]:
surface_sw.rect[0]-=1
else:
surface_sw.rect[0]+=1
if surface_sw.rect[1]>surface_love.rect[1]:
surface_sw.rect[1]-=1
else:
surface_sw.rect[1]+=1
# 简单模式
if sw_nandu==1:
if sw_move_rand<=50:
if surface_sw.rect[0]>surface_love.rect[0]:
surface_sw.rect[0]-=2
sw_move_rand+=1
else:
surface_sw.rect[0]+=2
sw_move_rand+=1
else:
if surface_sw.rect[1]>surface_love.rect[1]:
surface_sw.rect[1]-=2
sw_move_rand+=1
else:
surface_sw.rect[1]+=2
sw_move_rand+=1
if sw_move_rand>=100:
sw_move_rand=0
# 撞墙判定
if (surface_love.rect[0]<=71)|(surface_love.rect[0]>=1585):
num_move=-num_move
# die_ly=1
if (surface_love.rect[1]<=37)|(surface_love.rect[1]>=929):
num_move=-num_move
# die_ly=1
# 胜利判定
if num_xydnum==10:
win_ly=1
if win_ly==1:
screen.blit(text_5_2,textRect_5_2)
screen.blit(text_6_2,textRect_6_2)
pygame.display.update()
while True:
next_ly_sw=0
for event in pygame.event.get():
# x退出
if event.type == QUIT:
exit()
#esc退出
if event.type ==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# 继续游戏判定
if event.key == K_1:
next_ly_sw=1
die_ly=0
chushihua_start=1
if event.key == K_2:
exit()
if next_ly_sw==1:
break
# 死亡检测
if die_ly==1:
screen.blit(text_5,textRect_5)
pygame.display.update()
pygame.time.wait(2000)
screen.blit(text_5,textRect_5)
screen.blit(surface_lyj, SW_LY)
pygame.display.update()
pygame.time.wait(1000)
screen.blit(text_6,textRect_6)
pygame.display.update()
while True:
next_ly_sw=0
for event in pygame.event.get():
# x退出
if event.type == QUIT:
exit()
#esc退出
if event.type ==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# 继续游戏判定
if event.key == K_1:
next_ly_sw=1
die_ly=0
chushihua_start=1
if event.key == K_2:
exit()
if next_ly_sw==1:
break
# 初始化
if chushihua_start==1:
num_xydnum=0
sudu=1
xyd_list=[]
site_move = [0, 0]
surface_lovey=Snake_ly(surface_lovey_tp,(LY_SETX,LY_SETY))
surface_lovez=Snake_ly(surface_lovez_tp,(LY_SETX,LY_SETY))
surface_love=Snake_ly(surface_love_tp,(LY_SETX,LY_SETY))
surface_sw=Snake_sw(surface_sw_tp,(BACKW-400,BACKH-300))
if sw_nandu==1:
surface_sw.image=pygame.transform.rotozoom(surface_sw.image,0,0.3*SHUFANG)
if sw_nandu==2:
surface_sw.image=pygame.transform.rotozoom(surface_sw.image,0,0.5*SHUFANG)
position = surface_love.rect
position[0]=LY_SETX
position[1]=LY_SETY
num_move=1
win_ly=0
chushihua_start=0
screen.blit(text_4,textRect_4)
screen.blit(surface_love.image, surface_love.rect)
screen.blit(surface_sw.image, surface_sw.rect)
pygame.display.update()