When I press the reset button (Power) repeatedly the program crashes. I know this code is badly written but I was hoping someone could help me. The program makes a line that bounces on the edges and leaves a trail.
Code:
Code:
#include <tice.h>
#include <keypadc.h>
#include <graphx.h>
int NewLine();
int main(void)
{
gfx_Begin();
NewLine(0, 0, 0, 0);
gfx_End();
return 0;
}
int NewLine(A, B, C, D){
srandom(rtc_Time());
gfx_ZeroScreen();
// X and Y pos for the two points of the line
A = randInt(0, 240);
B = randInt(0, 240);
C = randInt(0, 240);
D = randInt(0, 240);
//Direction of the points
bool X1Mov = randInt(-1, 1);
bool Y1Mov = randInt(-1, 1);
bool X2Mov = randInt(-1, 1);
bool Y2Mov = randInt(-1, 1);
int X1MovOffset;
int Y1MovOffset;
int X2MovOffset;
int Y2MovOffset;
//Value used to change the direction
if(X1Mov == 1){
X1MovOffset = 1;
}else{
X1MovOffset = -1;
}
if(Y1Mov == 1){
Y1MovOffset = 1;
}else{
Y1MovOffset = -1;
}
if(X2Mov == 1){
X2MovOffset = 1;
}else{
X2MovOffset = -1;
}
if(Y2Mov == 1){
Y2MovOffset = 1;
}else{
Y2MovOffset = -1;
}
gfx_SetColor(255);
gfx_FillRectangle(241, 0, 120, 240);
while (!(kb_Data[6] & kb_Clear)) {
kb_ScanGroup(6);
//Black line
gfx_SetColor(0);
gfx_Line(A, B, C, D);
//Colored line
gfx_SetColor(220);
gfx_Line(A - X1MovOffset, B - Y1MovOffset, C - X2MovOffset, D - Y2MovOffset);
//button to reset line (I think this is where the error is)
if (kb_Data[6] & kb_Power) {
msleep(200);
gfx_ZeroScreen();
NewLine();
}
//change line direction
if(A >= 240){
X1MovOffset = -1;
}else if(A <= 0){
X1MovOffset = 1;
}
if(B >= 240){
Y1MovOffset = -1;
}else if(B <= 0){
Y1MovOffset = 1;
}
if(C >= 240){
X2MovOffset = -1;
}else if(C <= 0){
X2MovOffset = 1;
}
if(D >= 240){
Y2MovOffset = -1;
}else if(D <= 0){
Y2MovOffset = 1;
}
//Move lines
A = A + X1MovOffset;
B = B + Y1MovOffset;
C = C + X2MovOffset;
D = D + Y2MovOffset;
}
}