Python游戏开发1—到五秒就停下


一、Pygame最小开发框架

import sys,pygame
 
pygame.init()                               # 初始化
screen=pygame.display.set_mode((600,400))   # 设置屏幕宽高
pygame.display.set_caption('游戏之旅')       # 设置标题名称
 
while True:
    # 事件队列
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            sys.exit()
    pygame.display.update()  # 刷新屏幕

二、绘制文字

先绘制“开始游戏”在屏幕上吧。

import sys,pygame
import pygame.freetype

# 定义颜色
black = 0, 0, 0
white = 255,255,255
red = 255, 45, 0
 
pygame.init()     # 初始化
screen=pygame.display.set_mode((600,400))   # 设置屏幕宽高
pygame.display.set_caption('游戏之旅')       # 设置标题名称

f1 = pygame.freetype.Font(r"C:\Windows\Fonts\msyh.ttc",100)  # 设置文字对象

while True:
    # 事件队列
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            sys.exit()

    screen.fill(white)       #整个屏幕填充白色
    f1rect = f1.render_to(screen,(100,300),"开始计时/停止计时",fgcolor=black,size=50)
    pygame.display.update()  # 刷新屏幕

三、显示计时

import sys,pygame,time
import pygame.freetype

# 定义颜色
black = 0, 0, 0
white = 255,255,255
red = 255, 45, 0
 
pygame.init()     # 初始化
screen=pygame.display.set_mode((600,400))   # 设置屏幕宽高
pygame.display.set_caption('游戏之旅')       # 设置标题名称

f1 = pygame.freetype.Font(r"C:\Windows\Fonts\msyh.ttc",100)  # 设置文字对象
count = 0  #计时的时间(s)

while True:
    # 事件队列
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            sys.exit()
    # 计时
    count += 0.1
    time.sleep(0.1)

    screen.fill(white)       #整个屏幕填充白色
    f1rect = f1.render_to(screen,(100,300),"开始计时/停止计时",fgcolor=black,size=50)
    #round()以四舍五入的方式保留几位小数
    f1rect = f1.render_to(screen,(100,200),str(round(count,1)),fgcolor=black,size=50) 
    pygame.display.update()  # 刷新屏幕

四、控制计时

import sys,pygame,time
import pygame.freetype

# 定义颜色
black = 0, 0, 0
white = 255,255,255
red = 255, 45, 0
 
pygame.init()     # 初始化
screen=pygame.display.set_mode((600,400))   # 设置屏幕宽高
pygame.display.set_caption('游戏之旅')       # 设置标题名称
f1 = pygame.freetype.Font(r"C:\Windows\Fonts\msyh.ttc",100)  # 设置文字对象

count = 0          #计时的时间(s)
start = False      # 开始计时控制

while True:
    # 事件队列
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if f1rect.left<event.pos[0]<f1rect.right and f1rect.top<event.pos[1]<f1rect.bottom:
                if start == False: #如果此时没有开始计时,那么就开始计时
                    start = True
                    count = 0      #重新开始计时的时候把计时清零
                else:              #如果已经开始计时了,再按就停止
                    start = False

    #如果开始计时,那么每隔0.1s,计时+0.1
    if start == True:
        count += 0.1
        time.sleep(0.1)

    screen.fill(white)       #整个屏幕填充白色
    f1rect = f1.render_to(screen,(100,300),"开始计时/停止计时",fgcolor=black,size=50)
    #round()以四舍五入的方式保留几位小数
    CountRect=f1.render_to(screen,(200,100),"计时:"+str(round(count,1))+"s",fgcolor=black,size=50)
    pygame.display.update()  # 刷新屏幕

五、判断胜负

import sys,pygame,time
import pygame.freetype

# 定义颜色
black = 0, 0, 0
white = 255,255,255
red = 255, 45, 0
 
pygame.init()     # 初始化
screen=pygame.display.set_mode((600,400))   # 设置屏幕宽高
pygame.display.set_caption('游戏之旅')       # 设置标题名称
f1 = pygame.freetype.Font(r"C:\Windows\Fonts\msyh.ttc",100)  # 设置文字对象

count = 0          #计时的时间(s)
start = False      # 开始计时控制

while True:
    # 事件队列
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if f1rect.left<event.pos[0]<f1rect.right and f1rect.top<event.pos[1]<f1rect.bottom:
                if start == False: #如果此时没有开始计时,那么就开始计时
                    start = True
                    count = 0      #重新开始计时的时候把计时清零
                else:              #如果已经开始计时了,再按就停止
                    start = False
    screen.fill(white)       #整个屏幕填充白色

    #如果开始计时,那么每隔0.1s,计时+0.1
    if start == True:
        count += 0.1
        time.sleep(0.1)

    #判断胜负,如果刚好5s的时候按下停止,游戏成功,否则失败
    if round(count,1) == 5.0 and start == False:  #由于是浮点数,判断时精确到一位小数
        f1win=f1.render_to(screen,(100,200),"恭喜你,游戏成功了!",fgcolor=red,size=50)  #输出提示的字样
    if round(count,1) != 5.0 and start == False and round(count,1) != 0.0:
        f1win=f1.render_to(screen,(100,200),"失败了,请继续努力",fgcolor=red,size=50)  #输出提示的字样

    # 绘制文字
    f1rect = f1.render_to(screen,(100,300),"开始计时/停止计时",fgcolor=black,size=50)
    #round()以四舍五入的方式保留几位小数
    CountRect=f1.render_to(screen,(200,100),"计时:"+str(round(count,1))+"s",fgcolor=black,size=50)
    pygame.display.update()  # 刷新屏幕

六、游戏作弊

思考,如何让玩家一定失败,且不易被察觉。😈😈😈😈😈😈😈😈😈😈

七、打包程序

  1. 按win+R打开运行(只针对windows环境)

  2. 输入cmd打开命令行

  3. 输入pip install pyinstaller安装PyInstaller 模块(需联外网)

  4. 输入 Pyinstaller -F 你要打包的程序的绝对路径

    注意中间有空格,-F就是只生产单个文件,别的指令就自己查文档吧


文章作者: 彭韦浩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 彭韦浩 !
  目录