Bullet安装 与 Visual Studio开发环境配置

环境:Bullet3 Viusal Studio 2017 Windows10
工具:Cmake-3.16.3

0 准备

下载Bullet:https://github.com/bulletphysics/bullet3
下载Cmake:https://cmake.org/download/ (选择Windows win64-x64 ZIP)

1 编译Bullet库

打开cmake-3.16.3-win64-x64\bin\cmake-gui.exe
在这里插入图片描述

Where is the source code:bullet源码目录 Where to build the
binaries:编译出的东西放在哪,可以新建一个/build目录来存放

点Configure,点确定,选择Visual Studio版本,确定
点Generate

进入build目录

用Visual Studio打开ALL_BUILD.vcxproj
选择debug
在这里插入图片描述
右键ALL_BUILD项目,点生成

在这里插入图片描述
等待编译结束,进入/build/lib/Debug,可看到已经生成的.lib库文件
在这里插入图片描述
Debug版的Bullet库文件已经成功生成,Release版操作类似

2 搭建Hello World环境

Visual Studio新建空白控制台程序,例如叫testproj,打开自动生成的testproj.cpp
进入…\bullet3-master\examples\HelloWorld

HelloWorld.cpp中代码全部复制到testproj.cpp

打开项目 > 属性
打开VC++目录

包含目录中加入bullet源码位置,如D:\bullet\bullet3-master\src;
库目录中加入上步编译出的lib文件位置,如D:\bullet\bullet3-master\build\lib\Debug

在这里插入图片描述
打开C/C++ > 代码生成,把运行库改为多线程调试 (/MTd)
打开链接器 > 输入,在附加依赖项加入lib文件的名字:

Bullet2FileLoader_Debug.lib
Bullet3Collision_Debug.lib
Bullet3Common_Debug.lib
Bullet3Dynamics_Debug.lib
Bullet3Geometry_Debug.lib
Bullet3OpenCL_clew_Debug.lib
BulletCollision_Debug.lib
BulletDynamics_Debug.lib
BulletExampleBrowserLib_Debug.lib
BulletFileLoader_Debug.lib
BulletInverseDynamicsUtils_Debug.lib
BulletInverseDynamics_Debug.lib
BulletRobotics_Debug.lib
BulletSoftBody_Debug.lib
BulletWorldImporter_Debug.lib
BulletXmlWorldImporter_Debug.lib
BussIK_Debug.lib
clsocket_Debug.lib
ConvexDecomposition_Debug.lib
GIMPACTUtils_Debug.lib
gtest_Debug.lib
gwen_Debug.lib
HACD_Debug.lib
LinearMath_Debug.lib
OpenGLWindow_Debug.lib

在这里插入图片描述
点击Apply,环境配置完毕

运行HelloWorld程序,效果如下图
在这里插入图片描述

好的,以下是一个简单的飞机大战游戏示例代码,可以在 Visual Studio Code 中运行: ```python import os import turtle import math import random # 设置窗口 win = turtle.Screen() win.bgcolor("black") win.title("飞机大战") win.bgpic("background.gif") # 注册形状 turtle.register_shape("player.gif") turtle.register_shape("enemy.gif") turtle.register_shape("bullet.gif") # 添加玩家飞机 player = turtle.Turtle() player.speed(0) player.penup() player.shape("player.gif") player.color("blue") player.goto(0, -250) player.setheading(90) # 玩家飞机移动速度 player_speed = 15 # 添加敌机列表 enemies = [] # 创建敌机 for i in range(10): enemies.append(turtle.Turtle()) for enemy in enemies: enemy.speed(0) enemy.penup() enemy.shape("enemy.gif") enemy.color("red") enemy.goto(random.randint(-200, 200), random.randint(100, 250)) # 敌机移动速度 enemy_speed = 2 # 添加子弹 bullet = turtle.Turtle() bullet.speed(0) bullet.penup() bullet.shape("bullet.gif") bullet.color("yellow") bullet.shapesize(0.5, 0.5) bullet.hideturtle() bullet.goto(0, -400) bullet.setheading(90) bullet_speed = 20 bullet_state = "ready" # 玩家飞机移动 def move_left(): x = player.xcor() x -= player_speed if x < -280: x = -280 player.setx(x) def move_right(): x = player.xcor() x += player_speed if x > 280: x = 280 player.setx(x) def move_up(): y = player.ycor() y += player_speed if y > 250: y = 250 player.sety(y) def move_down(): y = player.ycor() y -= player_speed if y < -250: y = -250 player.sety(y) # 发射子弹 def fire_bullet(): global bullet_state if bullet_state == "ready": bullet_state = "fire" x = player.xcor() y = player.ycor() + 10 bullet.showturtle() bullet.goto(x, y) # 检测碰撞 def is_collision(t1, t2): distance = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor() - t2.ycor(), 2)) if distance < 15: return True else: return False # 注册键盘事件 turtle.listen() turtle.onkey(move_left, "Left") turtle.onkey(move_right, "Right") turtle.onkey(move_up, "Up") turtle.onkey(move_down, "Down") turtle.onkey(fire_bullet, "space") # 运行主循环 while True: # 移动敌机 for enemy in enemies: y = enemy.ycor() y -= enemy_speed enemy.sety(y) # 检测敌机是否撞墙 if y < -300: enemy.goto(random.randint(-200, 200), random.randint(100, 250)) # 检测敌机是否与玩家飞机相撞 if is_collision(enemy, player): player.hideturtle() enemy.hideturtle() print("Game Over") break # 检测敌机是否与子弹相撞 if is_collision(enemy, bullet): bullet.hideturtle() bullet_state = "ready" enemy.goto(random.randint(-200, 200), random.randint(100, 250)) # 移动子弹 if bullet_state == "fire": y = bullet.ycor() y += bullet_speed bullet.sety(y) # 检测子弹是否撞墙 if y > 300: bullet.hideturtle() bullet_state = "ready" # 检测是否所有敌机被消灭 dead_enemies = [enemy for enemy in enemies if not enemy.isvisible()] if len(dead_enemies) == len(enemies): print("You Win!") break turtle.mainloop() ``` 注意:在运行时需要将三个 .gif 图片放在与代码相同的目录下。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值