-
Car Application
Hi
I am attempting to make an introduction to an 80's line style game ala paperboy.
What si supposed to happen is the 'car' doesnt move but the buildings made of boxes move and constantly loop. What currently happens is the application just quits , could you help please , cheers.
Code:
#include <iostream>
#include <graphics.h>
#include <iostream>
#include <math.h>
using namespace std;
struct Vertex {
double x, y, z;
// need default values for constructor to work in Face struct...
Vertex(double _x=0, double _y=0,double _z=0) {
x = _x;
y = _y;
z = _z;
}
};
struct Face {
// start top-left and go anti-clockwise;
Vertex vertices[4];
Vertex v0, v1, v2, v3;
Face(Vertex _pts[]) {
for (int i = 0; i< 4; i++)
vertices[i] = _pts[i];
v0 = vertices[0];
v1 = vertices[1];
v2 = vertices[2];
v3 = vertices[3];
}
void drawBody() {
moveto(v0.x,v0.y);
lineto(v1.x, v1.y);
lineto(v2.x, v2.y);
lineto(v3.x, v3.y);
lineto(v0.x, v0.y);
}
};
int main() {
initwindow(640, 480, "Rectangle");
Vertex pts[] = {Vertex(100,100,0), Vertex(100, 200, 0), Vertex(200, 200, 0), Vertex(200, 100, 0)};
Face f = Face(pts);
f.drawBody();
int centreX, centreY;
centreX = 100;
float angle = -M_PI_2;
float dTheta = M_PI/10;
int myColour = 1;
centreY = 300 + 50*sin(angle);
circle(centreX, centreY, 1);
setlinestyle(0, 1, 1);
setfillstyle(1, 15);
int stangle = 0; // stangle == start angle
for (int i = 0; i < 4; i++){
pieslice(centreX, centreY,stangle + i*90, stangle+30+(i*90), 40);
}
while (!kbhit( )) {
delay(200);
cleardevice();
angle += dTheta;
stangle -= 5;
if (myColour == 15)
myColour = 4;
else myColour = 15;
setfillstyle(1, myColour);
for (int i = 0; i < 4; i++){
pieslice(centreX, centreY,stangle + i*90, stangle+30+(i*90), 40);
pieslice(centreX+105, centreY,stangle + i*90, stangle+30+(i*90), 40);
}
closegraph();
return 0;
}
}
-
It seems to me like it quits as soon as you press a key....
-
Its supposed to do this for now , right now its quitting when you press nothing
-
Yeah, that's what crappy indentation buys you - frustration and confusion.
Code:
while (!kbhit()) {
delay(200);
cleardevice();
angle += dTheta;
stangle -= 5;
if (myColour == 15)
myColour = 4;
else
myColour = 15;
setfillstyle(1, myColour);
for (int i = 0; i < 4; i++) {
pieslice(centreX, centreY, stangle + i * 90,
stangle + 30 + (i * 90), 40);
pieslice(centreX + 105, centreY, stangle + i * 90,
stangle + 30 + (i * 90), 40);
}
closegraph();
return 0;
}
-
I'm new to c++ how do i fix this?
-
Huh?
Fix what?
The indentation, so that you might have a chance of figuring out the program flow from just looking at the code?
Or do you need to know why a return statement inside a while loop causes that loop to end immediately?
Or maybe read this
http://cboard.cprogramming.com/c-pro...t-process.html
Since you're "new", you should NOT be writing so much code that you have NO idea what might happen if you try to run it.
Write, compile, test WITHIN your ability to predict the outcome. If you write too much code and you're completely lost, then you simply wrote too much code at once. Slow the pace and write smaller parts and make sure you understand what really happens.