bump.
So, beside the fact that my code is pretty unoptimized, could anyone try figuring out why when I get a point the coordinates for already displaying lines gets reset, and displays in another place? I'm thinking a fresh set of eyes could help.
Watch Closely:
Code:
//--------------------------------------
// Program Name:
// Author:
// License:
// Description:
//--------------------------------------
/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>
/* Standard headers - it's recommended to leave them included */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graphx.h>
#include <keypadc.h>
#include <fileioc.h>
#include <debug.h>
/* Other available headers */
// stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h, debug.h
/* Put your function prototypes here */
void drawLines();
void checkForStab();
void updateLines();
void addSixLines();
/* Put all your globals here. */
int playerX = 4, playerY = 4;
uint8_t numLines = 66, enabledLines;
bool quit;
typedef struct my_line {
int x;
int y;
bool direction;
bool displaying;
bool goingRight;
} my_line_t;
my_line_t lines[66];
void main(void) {
int key = 0, pointX = 0, pointY = 0, i = 0, keypress = 0;
bool pointGrabbed = false;
srand(rtc_Time());
gfx_Begin(gfx_8bpp);
gfx_FillScreen(gfx_white);
gfx_SetDrawBuffer();
for (i = 0; i<numLines; i++) {
my_line_t *this_line = &lines[i];
this_line->y = 30;
this_line->goingRight = true;
}
while (kb_ScanGroup(kb_group_6) != kb_Clear) {
if (keypress % 1 == 0) {
//Erase Screen
//drawLines(gfx_white);
gfx_FillScreen(gfx_white);
//Aspirin Point movement and grabbing
if (pointGrabbed == true) {
pointX = randInt(1,328);
pointY = randInt(1,208);
addSixLines();
pointGrabbed = false;
}
if (pointGrabbed == false) {
gfx_SetColor(gfx_red);
gfx_Circle(pointX, pointY, 6);
gfx_Circle(pointX, pointY, 5);
}
if (sqrt((playerX-pointX)*(playerX-pointX) + (playerY-pointY)*(playerY-pointY)) <= 10 && sqrt((playerX-pointX)*(playerX-pointX) + (playerY-pointY)*(playerY-pointY)) >= 0) {
pointGrabbed = true;
}
//Move Player and Draw
gfx_SetColor(gfx_white);
gfx_FillCircle(playerX, playerY, 4);
if (key & kb_Up && playerY >= 10)
playerY -= 3;
if (key & kb_Down && playerY < 235)
playerY += 3;
if (key & kb_Left && playerX > 4)
playerX -= 3;
if (key & kb_Right && playerX < 315)
playerX += 3;
gfx_SetColor(gfx_blue);
gfx_FillCircle(playerX, playerY, 4);
//Update Lines and Draw
updateLines();
drawLines(gfx_black);
checkForStab();
gfx_SwapDraw();
}
//Kepress stuff
keypress++;
key = kb_ScanGroup(kb_group_7);
if (quit == true)
break;
}
gfx_End();
pgrm_CleanUp();
}
void drawLines(uint8_t color) {
uint8_t i = 0;
gfx_SetColor(gfx_black);
for (i=0; i<enabledLines; i++) {
my_line_t *this_line = &lines[i];
if (this_line->direction == false && this_line->displaying == true) {
gfx_FillRectangle(this_line->x, this_line->y, 20, 3);
gfx_SetPixel(this_line->x-1, this_line->y+1);
gfx_SetPixel(this_line->x+20, this_line->y+1);
}
if (this_line->direction == true && this_line->displaying == true) {
gfx_FillRectangle(this_line->x, this_line->y, 3, 20);
gfx_SetPixel(this_line->x+1, this_line->y-1);
gfx_SetPixel(this_line->x+1, this_line->y+20);
}
}
}
void checkForStab(void) {
int i = 0;
for (i = 0; i<8; i++) {
if (gfx_GetPixel(playerX-6+i, playerY-5) == gfx_black)
quit = true;
}
for (i = 0; i<8; i++) {
if (gfx_GetPixel(playerX+5, playerY-6+i) == gfx_black)
quit = true;
}
for (i = 0; i<8; i++) {
if (gfx_GetPixel(playerX+6-i, playerY+5) == gfx_black)
quit = true;
}
for (i = 0; i<8; i++) {
if (gfx_GetPixel(playerX-5, playerY+6-i) == gfx_black)
quit = true;
}
}
void updateLines(void) {
uint8_t i;
for(i=0; i<enabledLines; i++) {
my_line_t *this_line = &lines[i];
if (this_line->x <= 0 && this_line->direction == false) {
this_line->goingRight = true;
}
if (this_line->x >= 310 && this_line->direction == false) {
this_line->goingRight = false;
}
if (this_line->y <= 0 && this_line->direction == true) {
this_line->goingRight = true;
}
if (this_line->y >= 235 && this_line->direction == true) {
this_line->goingRight = false;
}
if (this_line->direction == false && this_line->goingRight == true)
this_line->x +=3;
if (this_line->direction == true && this_line->goingRight == true)
this_line->y +=3;
if (this_line->direction == false && this_line->goingRight == false)
this_line->x -=3;
if (this_line->direction == true && this_line->goingRight == false)
this_line->y -=3;
}
}
void addSixLines() {
int i;
enabledLines += 6;
for (i=0; i<enabledLines; i++) {
my_line_t *this_line = &lines[i];
if (this_line->direction == false && this_line->displaying == false) {
this_line->direction = randInt(0,1);
this_line->y = randInt(2,235);
this_line->displaying = true;
}
if (this_line->direction == true && this_line->displaying == false); {
this_line->direction = randInt(0,1);
this_line->x = randInt(2,315);
this_line->displaying = true;
}
}
dbg_sprintf(dbgout, "Lines On Screen: %d\n", enabledLines);
}
/* Put other functions here */