bili.png

cpp 复制代码
import pygame as pg
import sys
import time
import random

pg.init()     
screen = pg.display.set_mode((800,500))
pg.display.set_caption('runcool')
screen.fill((135, 206, 235))
bili = pg.image.load('bili.png')

#得分
coin = 0
game_font = pg.font.Font(None, 50)

#人物大小
man = pg.transform.scale(bili, (60, 85))
man_x, man_y = 200, 415
onfloor = 1                 #是否在地面上
gravity = 0.2               #重力加速度
man_vy = 0                  #Y方向速度
passed = True               #跳过加分

#初始化障碍物
piece = pg.Surface((30,200))
piece.fill((255, 255, 255))
piece_x, piece_y = 800, 410

while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_SPACE and onfloor == 1:
                man_vy = -10
                onfloor = 0
    man_vy += gravity                   # V = V0 + at
    man_y += man_vy                     # S = V0t + 1/2 a * t * t = V平 * t

    if man_y >= 415:
        man_vy = 0
        man_y = 415
        onfloor = 1

    #绘制背景及人物
    screen.fill((135,206,235))
    screen.blit(man,(man_x,man_y))
    #绘制障碍物
    screen.blit(piece,(piece_x,piece_y))
    piece_x -= 2
    if piece_x <= 0:
        passed = True
        piece_y = random.randint(350, 450)
        piece_x = 850

    #得硬币检测
    if piece_x < man_x and passed:
        coin += 1
        passed = False

    #绘制硬币得分
    screen.blit(game_font.render('coin: %d' % coin, True, [255, 0, 0]), [20, 20])
    #碰撞检测
    if man_x+60 >= piece_x and man_x <= piece_x+30 and man_y+85 >= piece_y:
        print('得分: %d' % coin) 
        pg.quit()
        sys.exit()

    pg.display.update()
    time.sleep(0.005)
相关推荐
程序员鱼皮21 小时前
前特斯拉 AI 总监:AI 编程最大的谎言,是 “提效”
前端·后端·ai·程序员·开发
sg_knight4 天前
抽象工厂模式(Abstract Factory)
java·python·设计模式·抽象工厂模式·开发
0和1的舞者7 天前
技术优化手册:从工具类到 MyBatis 配置与业务逻辑
java·后端·学习·开发·知识
0和1的舞者9 天前
公共类的注意事项详细讲解
经验分享·后端·开发·知识·总结
0和1的舞者13 天前
基于Spring的论坛系统-前置知识
java·后端·spring·系统·开发·知识
康谋自动驾驶15 天前
汽车多总线数据采集:挑战、架构与同步策略全解析
算法·自动驾驶·开发·数据处理·总线数据
智行众维1 个月前
从“测试泥潭”到“智能加速”:我们对自动驾驶仿真测试的新思考
数据库·自动驾驶·开发·技术·场景库·自动驾驶仿真测试·场景开发
Leweslyh1 个月前
制导算法开发实践指南:从入门到精通
算法·开发·武器·制导律设计
0和1的舞者1 个月前
Git 实战踩坑:如何让多个 IDE 项目共用一个远程仓库(附子模块问题解决)
git·开发·仓库·码云·子模块·操作·冲突
我有一棵树1 个月前
一、GitHub 的 WIP 功能简介
github·开发·前端、