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()
相关推荐
Petrichor_H_37 分钟前
DAY 31 文件的规范拆分和写法
python
咚咚王者1 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
深蓝海拓2 小时前
使matplot显示支持中文和负号
开发语言·python
AntBlack2 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
一眼万里*e3 小时前
搭建本地deepseek大模型
python
1***Q7843 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python
二进制的Liao3 小时前
【编程】脚本编写入门:从零到一的自动化之旅
数据库·python·算法·自动化·bash
Dxy12393102164 小时前
Python为什么要使用可迭代对象
开发语言·python
Keep_Trying_Go4 小时前
论文STEERER人群计数,车辆计数以及农作物计数算法详解(pytorch)
人工智能·pytorch·python
gzu_014 小时前
基于昇腾 配置pytorch环境
人工智能·pytorch·python