如何在 C++ 中的特定边界内移动对象
how to move a object inside a certain boundry in c++
我想做一个乒乓球游戏,我被困在球的运动上,我不希望它走出界限,以防万一是 640 x 480..... 我不希望它走出这个界限,而是像碰撞一样再次移动......以下是代码
#include <iostream>
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI");
int x = 0, y = 0, i;
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
circle(x, y, 20);
floodfill(x, y, RED);
loop1:
for (i = 0; i <= 45; i++) {
cleardevice();
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
circle(x, y, 20);
floodfill(x, y, RED);
if (y == 460) {
break;
}
else {
x += 10;
y += 10;
}
delay(10);
}
for (i = 0; i <= 46; i++) {
cleardevice();
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
circle(x, y, 20);
floodfill(x, y, RED);
if (x == 620) {
break;
}
else {
x += 10;
y -= 10;
}
delay(10);
}
for (i = 0; i <= 45; i++) {
cleardevice();
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
circle(x, y, 20);
floodfill(x, y, RED);
if (y == 20) {
break;
}
else {
x -= 10;
y -= 10;
}
delay(10);
}
for (i = 0; i <= 45; i++) {
cleardevice();
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
circle(x, y, 20);
floodfill(x, y, RED);
if (x == 20) {
goto loop1;
}
else {
x -= 10;
y += 10;
}
delay(10);
}
getch();
closegraph();
}
边界
处碰撞效果的一种简单方法是,当 Pong 运动的 y 分量"击中"上限或下边界时,取反 y-分量,当它"击中"左边界或右边界时,否定 x 分量。
简短的示例代码:
int speedvector[2];
speedvector[0] = 10;
speedvector[1] = 10;
int pongposition[2];
pongposition[0] = 100;
pongposition[1] = 100;
Main game loop:
while(gameon){
if(pongposition[0] < 0 || pongposition[0] > 640){
speedvector[0] = -speedvector[0];
}
if(pongposition[1] < 0 || pongposition[1] > 480){
speedvector[1] = -speedvector[1];
}
pongposition[0] += speedvector[0];
pongposition[1] += speedvector[1];
}
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- std::当在256字节边界上写入整数时,流的奇怪行为
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- RCPP 移动平均线 - 边界误差会导致致命误差
- Qt 移动光标不再更改选择边界
- 如何在 C++ 中的特定边界内移动对象
- 在C++窗口代码中有没有任何方法可以阻止鼠标移动到窗口边界之外
- 如何在c++中为卡片制作ASCII边界,并使其不移动