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)
相关推荐
sg_knight2 天前
设计模式与代码重构
python·设计模式·重构·开发
0和1的舞者3 天前
《MyBatis 从入门到上手:超全基础操作 + XML 配置指南》
数据库·spring boot·学习·spring·mybatis·框架·开发
ujainu6 天前
Flutter与DevEco Studio结合开发简单项目实战指南
flutter·开发·deveco studio
斯是 陋室10 天前
大模型节点中系统提示词、用户提示词、输入、输出的白话讲解
人工智能·ai·大模型·开发·工作流
ujainu11 天前
FlutterOHOS开发:从基础到跨端实战
flutter·harmonyos·开发
晚风吹长发12 天前
初步了解Linux中文件描述符-fd
linux·运维·服务器·c++·开发·文件
heartbeat..12 天前
介绍一下软件开发中常见的几种的架构模式
java·架构·开发
0和1的舞者16 天前
Postman接口测试全攻略:传参技巧与实战解析
学习·测试工具·spring·springmvc·postman·web·开发
张较瘦_17 天前
数据库|数据库设计范式:用“宠物管理系统“讲透1nf 2nf 3nf的关键逻辑
数据库·开发
做cv的小昊17 天前
在NanoPC-T6开发板上通过USB串口通信实现光源控制功能
java·后端·嵌入式硬件·边缘计算·安卓·信息与通信·开发