2017-02-09 3 views
-1

탁구 게임을 만들고 싶습니다. 공의 움직임에 집착합니다. 경계에서 벗어나는 것을 원하지 않습니다.이 경우 640x480입니다. .... 나는 그것을이 경계 나가 대신 ... 충돌의 경우처럼 다시 다음 를 이동하지 않으려는 것은 코드C++에서 특정 경계 내에서 객체를 이동하는 방법

#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(); 
} 
+1

호수는, goto''의 사용과 모든 그래픽 코드의 포함이 예 오히려 열심히 이해한다. 귀하의 예가 더 간단할수록 누군가가 그것을 이해할 시간을 가질 가능성이 높습니다. 당신은 꼭 들여 쓰기를하고 당신의 예제를 필수 구성 요소로 줄여야합니다. 또한, 로컬 파일 (''C : \\ TC \\ BGI "')을 사용하면이 예제를 다른 사람들에게 unrunnable하게 만들 수 있으므로 도움이 더욱 어려워집니다. –

+0

그건 당신이 가질 수있는'goto'의 최악의 사용법입니다. 'for' 루프에서 레이블로 위쪽으로 분기하여'for' 루프에 정수 'i'를 루프 카운터로 사용합니다. 두 루프 모두에서 다시 튀어 나오고 다시 루프백됩니다. [스파게티 코드] (https://en.wikipedia.org/wiki/Spaghetti_code) – PaulMcKenzie

답변

0

에게 충돌 효과에 대한 간단한 방법입니다 경계선은 왼쪽 또는 오른쪽 경계에 "맞을 때"상한 또는 하한 경계를 "히트"하고 x 구성 요소를 무효화 할 때 탁구 동작의 y 구성 요소를 무효화하는 것입니다.

짧은 예제 코드 : 들여 쓰기

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]; 
} 
관련 문제