Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct cell{
- char ch;
- u32 backColor;
- u32 chColor;
- }cell;
- int maxWidth = 60;
- int maxHeight = 34;
- cell** field;
- void setCell(int x, int y, cell c){
- field[x][y].ch = c.ch;
- field[x][y].chColor = c.chColor;
- field[x][y].backColor = c.backColor;
- }
- void fillRect(int x, int y, int width, int height, cell c){
- int _x;
- int _y;
- for(_x = x;_x < width ; _x++){
- for(_y = y; _y < height; _y++){
- setCell(_x, _y, c);
- }
- }
- return;
- }
- void update(int width, int height){
- int chWidth = 8;
- int chHeight = 8;
- int x;
- int y;
- for(x = 0; x < width; x++){
- for(y = 0; y < height; y++){
- cell c = field[x][y];
- pspDebugScreenSetBackColor(c.backColor);
- pspDebugScreenPutChar(x * chWidth, y * chHeight, c.chColor, c.ch);
- }
- }
- }
- struct{
- int up;
- int right;
- int down;
- int left;
- } keys;
- void updateKeys(){
- SceCtrlData pad;
- sceCtrlReadBufferPositive(&pad, 1);
- if (pad.Buttons){
- keys.up = keys.down = keys.left = keys.right = 0;
- if (pad.Buttons & PSP_CTRL_UP) keys.up = 1;
- if (pad.Buttons & PSP_CTRL_DOWN) keys.down = 1;
- if (pad.Buttons & PSP_CTRL_LEFT) keys.left = 1;
- if (pad.Buttons & PSP_CTRL_RIGHT) keys.right = 1;
- }
- }
- int main(int argc, char* argv[])
- {
- int bodyLength = 4;
- int* body = malloc(sizeof(int) * bodyLength * 2);
- int width = 60;
- int height = 30;
- int x;
- int y;
- int i = 0;
- for(; i < bodyLength * 2; i += 2){
- body[i] = width / 2;
- body[i + 1] = height / 2 - bodyLength - (i / 2);
- }
- cell headUpCell = (cell){(char) 0x1E, 0xFFFFFF, 0x000000};
- cell headRightCell = (cell){(char) 0x10, 0xFFFFFF, 0x000000};
- cell headDownCell = (cell){(char) 0x1F, 0xFFFFFF, 0x000000};
- cell headLeftCell = (cell){(char) 0x11, 0xFFFFFF, 0x000000};
- cell headCell = headDownCell;
- cell bodyCell = (cell){'0', 0xFFFFFF, 0x000000};
- cell foodCell = (cell){'1', 0xFFFFFF, 0x00FF00};
- field = malloc(sizeof(cell*) * width);
- for(x = 0; x < width; x++){
- field[x] = malloc(sizeof(cell) * height);
- for(y = 0; y < height; y++){
- field[x][y] = (cell){};
- }
- }
- pspDebugScreenInit();
- setupCallbacks();
- pspDebugScreenEnableBackColor(1);
- int foodx = rand() % width;
- int foody = rand() % height;
- int velx = 0;
- int vely = 1;
- while(1){
- updateKeys();
- if(velx){
- if(keys.up) vely = -1, headCell = headUpCell;
- else if(keys.down) vely = 1, headCell = headDownCell;
- if(vely) velx = 0;
- }else if(vely){
- if(keys.left) velx = -1, headCell = headLeftCell;
- else if(keys.right) velx = 1, headCell = headRightCell;
- if(velx) vely = 0;
- }
- int* headx = &body[0];
- int* heady = &body[1];
- if(velx || vely){
- for(i = bodyLength - 1; i >= 1; i--){
- body[i * 2 + 0] = body[(i - 1) * 2 + 0];
- body[i * 2 + 1] = body[(i - 1) * 2 + 1];
- }
- }
- *headx += velx;
- *heady += vely;
- if(*headx < 0) *headx = width - 1;
- if(*headx > width - 1) *headx = 0;
- if(*heady < 0) *heady = height - 1;
- if(*heady > height - 1) *heady = 0;
- for(i = 1; i < bodyLength; i++){
- if(body[i * 2] == *headx && body[i * 2 + 1] == *heady){
- bodyLength = i;
- //body = realloc(body, sizeof(int) * bodyLength * 2);
- break;
- }
- }
- if(*headx == foodx && *heady == foody){
- bodyLength++;
- body = realloc(body, sizeof(int) * bodyLength * 2);
- headx = &body[0];
- heady = &body[1];
- body[(bodyLength - 1) * 2 + 0] = *headx;
- body[(bodyLength - 1) * 2 + 1] = *heady;
- for(i = 0; i < bodyLength; i++){
- if(body[i * 2] == foodx && body[i * 2 + 1] == foody){
- foodx = rand() % width;
- foody = rand() % height;
- i = 0;
- continue;
- }
- }
- }
- fillRect(0, 0, width, height, (cell){' ', 0x000000, 0xFFFFFF});
- setCell(*headx, *heady, headCell);
- setCell(foodx, foody, foodCell);
- for(i = 2; i < bodyLength * 2; i += 2){
- setCell(body[i], body[i + 1], bodyCell);
- }
- update(width, height);
- sceDisplayWaitVblankStart(); updateKeys();
- sceDisplayWaitVblankStart(); updateKeys();
- sceDisplayWaitVblankStart(); updateKeys();
- sceDisplayWaitVblankStart(); updateKeys();
- sceDisplayWaitVblankStart(); updateKeys();
- sceDisplayWaitVblankStart();
- }
- sceKernelExitDeleteThread(0);
- sceKernelExitGame();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement