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()
相关推荐
正在走向自律15 分钟前
Conda 完全指南:从环境管理到工具集成
开发语言·python·conda·numpy·fastapi·pip·开发工具
lqjun082736 分钟前
PyTorch实现CrossEntropyLoss示例
人工智能·pytorch·python
DpHard1 小时前
Vscode 配置python调试环境
ide·vscode·python
小蜗笔记1 小时前
显卡、Cuda和pytorch兼容问题
人工智能·pytorch·python
高建伟-joe1 小时前
内容安全:使用开源框架Caffe实现上传图片进行敏感内容识别
人工智能·python·深度学习·flask·开源·html5·caffe
JarmanYuo2 小时前
ARM (Attention Refinement Module)
python·计算机视觉
正经教主2 小时前
【基础】Windows开发设置入门4:Windows、Python、Linux和Node.js包管理器的作用和区别(AI整理)
linux·windows·python·包管理器
Q_Q19632884752 小时前
python的漫画网站管理系统
开发语言·spring boot·python·django·flask·node.js·php
搂……住2 小时前
第一次做逆向
python
卡尔曼的BD SLAMer2 小时前
计算机视觉与深度学习 | Python实现EMD-SSA-VMD-LSTM-Attention时间序列预测(完整源码和数据)
python·深度学习·算法·cnn·lstm