Python金币小游戏

游戏规则:移动挡板接住金币

游戏截图:

详细代码如下:

python 复制代码
import pygame.freetype
import sys
import random

pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('game')
p = 0
i1 = 0
s = 0
t = 0
f1 = pygame.freetype.Font('C:/windows/fonts/msyh.ttc', 100)
f2 = pygame.freetype.Font(None, 30)
img = pygame.image.load('img.png')  # 更换你的图片地址即可
img = pygame.transform.scale(img, (30, 30))
l = []

while True:
    pygame.time.delay(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if p == 0:
                t = 0
                p = 1
    screen.fill((255, 255, 255))
    if p == 0:
        f1.render_to(screen, [100, 130], '开始游戏')
    if p == 1:
        i1 += 1
        if i1 >= 50//(int(t/30)+1):
            i1 = 0
            if t < 200:
                t += 1
                l.append([random.randint(50, 520), 50])
        if t == 200 and not l:
            p = 2
        for i in l:
            screen.blit(img, [i[0], i[1]])
            i[1] += int(t/1)+1   # 金币下降速度
        x = pygame.mouse.get_pos()[0]
        if x < 40:
            x = 40
        if x > 560:
            x = 560
        pygame.draw.rect(screen, (60, 150, 250), (x-40, 320, 80, 20))
        for i in l:
            if i[1] >= 300:
                if x-70 <= i[0] <= x+40:
                    s += 1
                l.remove(i)
        f2.render_to(screen, [30, 30], 'score:%d' % s)
    if p == 2:
        f1.render_to(screen, [80, 130], 'score:%d' % s)
    pygame.display.update()
相关推荐
大霞上仙40 分钟前
nonlocal 与global关键字
开发语言·python
Mark_Aussie1 小时前
Flask-SQLAlchemy使用小结
python·flask
程序员阿龙1 小时前
【精选】计算机毕业设计Python Flask海口天气数据分析可视化系统 气象数据采集处理 天气趋势图表展示 数据可视化平台源码+论文+PPT+讲解
python·flask·课程设计·数据可视化系统·天气数据分析·海口气象数据·pandas 数据处理
ZHOU_WUYI1 小时前
Flask与Celery 项目应用(shared_task使用)
后端·python·flask
且慢.5892 小时前
Python_day47
python·深度学习·计算机视觉
佩奇的技术笔记2 小时前
Python入门手册:异常处理
python
大写-凌祁2 小时前
论文阅读:HySCDG生成式数据处理流程
论文阅读·人工智能·笔记·python·机器学习
爱喝喜茶爱吃烤冷面的小黑黑2 小时前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式
Blossom.1183 小时前
使用Python和Flask构建简单的机器学习API
人工智能·python·深度学习·目标检测·机器学习·数据挖掘·flask
Love__Tay4 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融