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()
相关推荐
大模型码小白24 分钟前
企业级检索增强后端集成:Java 服务如何管理知识库版本
java·服务器·开发语言·人工智能·python·microsoft
农村小镇哥33 分钟前
Python3开发快速获取图片的文字
开发语言·python
FriendshipT1 小时前
Ultralytics:解读Proto模块
人工智能·pytorch·python·深度学习·目标检测
zhiSiBuYu05171 小时前
Python3 循环语句新手实战指南
python
ShirleyWang0121 小时前
Day02 K3s NGF(Nginx Gateway Fabric)单 Worker 环境网关更新与故障处置 SOP
linux·服务器·python·k8s·k3s
ZDQNFU1 小时前
ORM之SQLAlchemy教程
后端·python
jeffzhengye1 小时前
Claude-Real-Video:让 Claude 真正看懂视频,0 Token 成本
python·微信
IT知识分享1 小时前
WebP转JPG开发经验:从格式解码到本地批量转换的实践
javascript·python·图片转换
小柯南敲键盘1 小时前
Temu多语言商品图翻译实现方案
人工智能·python
威联通安全存储2 小时前
TS-h1677AXU-RP在工程机械机器人弧焊中的部署
大数据·人工智能·python·机器人