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)
相关推荐
寒水馨6 天前
聊聊DevOps,开发与运维如何分工协作?
运维·ci/cd·开发·devops
程序员鱼皮11 天前
会Vibe Coding的同事:我一个人干掉整个技术部!
ai·程序员·互联网·编程·开发·代码
程序员鱼皮20 天前
没听说过设计模式?保姆级教程来了!
计算机·程序员·开发·学习路线·自学
程序员鱼皮24 天前
Cursor 网页版来了,这下拉屎时也能工作了
计算机·ai·程序员·开发·项目·编程经验
暮乘白帝过重山25 天前
为什么要写RedisUtil这个类
redis·开发·暮乘白帝过重山
风舞雪凌月1 个月前
【教程】Android(AOSP)Framework开发/ROM定制快速教程
android·framework·开发·aosp·rom
程序员鱼皮2 个月前
学 Java 还是 Go 语言?这事儿很简单!
java·后端·计算机·程序员·开发·编程经验·自学编程
Python智慧行囊2 个月前
Python 中 Django 中间件:原理、方法与实战应用
python·中间件·架构·django·开发