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()
相关推荐
天天爱吃肉821812 分钟前
【工程师硬件学习笔记:串口‑总线体系与三电试验室异构系统集成深度实战】
大数据·笔记·python·嵌入式硬件·学习·汽车
AC赳赳老秦14 分钟前
官方技术文档聚合实践:用 OpenClaw 批量抓取开源项目文档,构建离线可检索技术知识库
java·运维·服务器·python·信息可视化·deepseek·openclaw
Zenova EdgeOS16 分钟前
工业边缘 SDK 设计实战:从 API 到 Python/Go 多语言工程落地
python·golang·php
XLYcmy28 分钟前
pdf论文处理:CSV输出模式
数据库·python·pycharm·pdf·论文·csv·dify
苏灿烤鱼1 小时前
14MB 模型,凭什么跟 270M 对打?
javascript·python·agent
小田学Python10 小时前
100行Python代码,搭一个能干活的AI Agent
python·langchain·大模型·ai agent
Csvn10 小时前
🐍 Day3 : Python 容器精讲 — list、dict、set、tuple 底层实现与高级操作
后端·python
weixin-a1530030831611 小时前
python-装饰器
开发语言·python
我的xiaodoujiao11 小时前
快速学习Python基础知识详细图文教程17--类型注解和断点调试
开发语言·python·学习·测试工具
每天吃饭的羊12 小时前
Chrome DevTools MCP
python