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)
相关推荐
ShineWinsu1 天前
对于C++:C++11中lambda、function、bind的解析
c++·面试·笔试·开发·lambda·bind·function
rannn_1111 天前
【力扣hot100】链表专题下|138、148、23、146
java·算法·leetcode·链表·开发
rannn_1112 天前
【力扣hot100】链表专题|21、2、19、24、92、25
java·数据结构·算法·leetcode·链表·开发
rannn_1113 天前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
rannn_1116 天前
【力扣hot100】238、41、73题解
java·算法·leetcode·开发
kels889911 天前
多品种贵金属 Tick 怎样无缝合并回测?
笔记·python·信息可视化·金融·开发
人间凡尔赛11 天前
AI 编程进入 Agent 时代:2026 年 CLI 效率工具实战指南(Claude Code × Codex 横评)
开发·工具·效率
会思想的苇草i18 天前
oMLX 部署本地大模型
大模型·ai编程·开发·本地部署·omlx
深念Y20 天前
MSYS2 是什么,要不要装?
运维·环境·开发·工具·msys2
AOwhisky22 天前
云原生 DevOps 工具链从入门到实战(第一期)——DevOps概述与GitLab部署——从理念到工具落地
运维·ci/cd·云原生·gitlab·开发·devops