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)
相关推荐
whyutianict_vv5 天前
HCIE-AI Developer 备考投入拆解:资源规划、判断矩阵与准备流程
人工智能·ai·开发
2601_967987377 天前
Next.js 文档
react·开发·全栈·文档·next.js
standxy14 天前
索引设计实战:pgvector HNSW × JSONB GIN × 复合索引
数据库·开发
七夜zippoe17 天前
Spring AI 是什么?Java 工程师的 AI 开发第一站
java·人工智能·spring·ai·开发
serene9417 天前
Modbus“出道史”:1979年诞生,后来一路火到现在
物联网·开发·modbus·通讯协议·模拟器
爱笑的源码基地18 天前
云 PACS 源码实践:HL7 对接 HIS / 电子病历
源码·开发·信创·pacs·医学影像
whyutianict_vv18 天前
AI 大模型技术架构解析:从推理链路到训练流程
人工智能·ai·架构·开发
坐吃山猪22 天前
AI复刻网站功能初体验
人工智能·开发
坐吃山猪22 天前
Codex使用说明
人工智能·开发·codex
坐吃山猪22 天前
FDE前沿部署工程师
人工智能·开发