- Unificat sistema canvi colors
- Pinta de un color segons vaja rapid o lento - [Fix] Ja no se'n eix i peta
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.vscode/*
|
||||
jix
|
||||
*.exe
|
||||
|
||||
5
Makefile
5
Makefile
@@ -1,2 +1,5 @@
|
||||
all:
|
||||
windows:
|
||||
gcc main.c -lmingw32 -lSDL2main -lSDL2 -o jix.exe
|
||||
|
||||
linux:
|
||||
gcc main.c -lSDL2 -o jix
|
||||
|
||||
106
main.c
106
main.c
@@ -12,6 +12,14 @@
|
||||
#define MODE_NORMAL 0
|
||||
#define MODE_CREANT 1
|
||||
|
||||
#define COLOR_BLACK 0
|
||||
#define COLOR_WHITE 1
|
||||
#define COLOR_GREY 2
|
||||
#define COLOR_RED 3
|
||||
#define COLOR_YELLOW 4
|
||||
#define COLOR_DARKRED 5
|
||||
#define COLOR_DARKTEAL 6
|
||||
|
||||
SDL_Window *win;
|
||||
SDL_Renderer *ren;
|
||||
SDL_Texture * tex;
|
||||
@@ -20,12 +28,26 @@ Uint8 pixels[200*200];
|
||||
int player_x = 100;
|
||||
int player_y = 199;
|
||||
int mode = MODE_NORMAL;
|
||||
int lento = 0;
|
||||
|
||||
void set_color(int color)
|
||||
{
|
||||
switch (color) {
|
||||
case COLOR_BLACK: SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); break;
|
||||
case COLOR_WHITE: SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); break;
|
||||
case COLOR_GREY: SDL_SetRenderDrawColor(ren, 128, 128, 128, 255); break;
|
||||
case COLOR_RED: SDL_SetRenderDrawColor(ren, 217, 87, 99, 255); break;
|
||||
case COLOR_YELLOW: SDL_SetRenderDrawColor(ren, 251, 242, 54, 255); break;
|
||||
case COLOR_DARKRED: SDL_SetRenderDrawColor(ren, 172, 50, 50, 255); break;
|
||||
case COLOR_DARKTEAL: SDL_SetRenderDrawColor(ren, 48, 128, 130, 255); break;
|
||||
}
|
||||
}
|
||||
|
||||
void draw_player()
|
||||
{
|
||||
const int x = player_x+OFFSET_X;
|
||||
const int y = player_y+OFFSET_Y;
|
||||
SDL_SetRenderDrawColor(ren, 217, 87, 99, 255);
|
||||
set_color(COLOR_RED);
|
||||
SDL_RenderDrawLine(ren, x, y-3, x+3, y);
|
||||
SDL_RenderDrawLine(ren, x+3, y, x, y+3);
|
||||
SDL_RenderDrawLine(ren, x, y+3, x-3, y);
|
||||
@@ -34,22 +56,44 @@ void draw_player()
|
||||
|
||||
void put_pix()
|
||||
{
|
||||
pixels[player_x+player_y*200] = 2;
|
||||
pixels[player_x+player_y*200] = COLOR_DARKRED;
|
||||
SDL_SetRenderTarget(ren, tex);
|
||||
SDL_SetRenderDrawColor(ren, 128, 128, 128, 255);
|
||||
if (lento)
|
||||
set_color(COLOR_DARKRED);
|
||||
else
|
||||
set_color(COLOR_DARKTEAL);
|
||||
SDL_RenderDrawPoint(ren, player_x, player_y);
|
||||
SDL_SetRenderTarget(ren, NULL);
|
||||
}
|
||||
|
||||
int any_black(int x, int y)
|
||||
{
|
||||
return (pixels[x+1+y*200]==0 || pixels[x-1+y*200]==0 || pixels[x+(y+1)*200]==0 || pixels[x+(y-1)*200]==0);
|
||||
}
|
||||
|
||||
void close_sector()
|
||||
{
|
||||
SDL_SetRenderTarget(ren, tex);
|
||||
SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
|
||||
set_color(COLOR_WHITE);
|
||||
for (int y=0;y<200;++y)
|
||||
for (int x=0;x<200;++x)
|
||||
if (pixels[x+y*200]==2)
|
||||
if ((pixels[x+y*200]==COLOR_DARKRED || pixels[x+y*200]==COLOR_DARKTEAL) && any_black(x,y))
|
||||
{
|
||||
pixels[x+y*200]=1;
|
||||
pixels[x+y*200]=COLOR_WHITE;
|
||||
SDL_RenderDrawPoint(ren, x, y);
|
||||
}
|
||||
SDL_SetRenderTarget(ren, NULL);
|
||||
}
|
||||
|
||||
void make_rapid()
|
||||
{
|
||||
SDL_SetRenderTarget(ren, tex);
|
||||
set_color(COLOR_DARKTEAL);
|
||||
for (int y=0;y<200;++y)
|
||||
for (int x=0;x<200;++x)
|
||||
if (pixels[x+y*200]==COLOR_DARKRED && any_black(x,y))
|
||||
{
|
||||
pixels[x+y*200]=COLOR_DARKTEAL;
|
||||
SDL_RenderDrawPoint(ren, x, y);
|
||||
}
|
||||
SDL_SetRenderTarget(ren, NULL);
|
||||
@@ -68,9 +112,9 @@ int main(int argc, char *argv[])
|
||||
for (int i=0; i<200; ++i) pixels[i]=pixels[i+199*200]=pixels[i*200]=pixels[199+i*200]=1;
|
||||
|
||||
SDL_SetRenderTarget(ren, tex);
|
||||
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
|
||||
set_color(COLOR_BLACK);
|
||||
SDL_RenderClear(ren);
|
||||
SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
|
||||
set_color(COLOR_WHITE);
|
||||
SDL_RenderDrawLine(ren, 0, 0, 199, 0);
|
||||
SDL_RenderDrawLine(ren, 0, 0, 0, 199);
|
||||
SDL_RenderDrawLine(ren, 199, 199, 199, 0);
|
||||
@@ -94,20 +138,50 @@ int main(int argc, char *argv[])
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_GetTicks()-t>=15)
|
||||
int speed = 15;
|
||||
if (lento) speed = 30;
|
||||
if (SDL_GetTicks()-t>=speed)
|
||||
{
|
||||
if (keys[SDL_SCANCODE_UP] && player_y>0 && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || pixels[player_x+(player_y-1)*200]==1)) {
|
||||
if (mode==MODE_CREANT)
|
||||
put_pix();
|
||||
else if (pixels[player_x+(player_y-1)*200]==0)
|
||||
lento=1;
|
||||
mode = MODE_CREANT; player_y--;
|
||||
} else if (keys[SDL_SCANCODE_RIGHT] && player_x<199 && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || pixels[player_x+1+player_y*200]==1)) {
|
||||
if (mode==MODE_CREANT)
|
||||
put_pix();
|
||||
else if (pixels[player_x+1+player_y*200]==0)
|
||||
lento=1;
|
||||
mode = MODE_CREANT; player_x++;
|
||||
} else if (keys[SDL_SCANCODE_DOWN] && player_y<199 && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || pixels[player_x+(player_y+1)*200]==1)) {
|
||||
if (mode==MODE_CREANT)
|
||||
put_pix();
|
||||
else if (pixels[player_x+(player_y+1)*200]==0)
|
||||
lento=1;
|
||||
mode = MODE_CREANT; player_y++;
|
||||
} else if (keys[SDL_SCANCODE_LEFT] && player_x>0 && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || pixels[player_x-1+player_y*200]==1)) {
|
||||
if (mode==MODE_CREANT)
|
||||
put_pix();
|
||||
else if (pixels[player_x-1+player_y*200]==0)
|
||||
lento=1;
|
||||
mode = MODE_CREANT; player_x--;
|
||||
}
|
||||
|
||||
if (mode==MODE_CREANT && !keys[SDL_SCANCODE_SPACE])
|
||||
{
|
||||
make_rapid();
|
||||
lento=0;
|
||||
}
|
||||
|
||||
if (pixels[player_x+player_y*200]==1)
|
||||
{
|
||||
if (keys[SDL_SCANCODE_UP] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_y>0 && pixels[player_x+(player_y-1)*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_y--; }
|
||||
else if (keys[SDL_SCANCODE_RIGHT] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_x<199 && pixels[player_x+1+player_y*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_x++; }
|
||||
else if (keys[SDL_SCANCODE_DOWN] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_y<199 && pixels[player_x+(player_y+1)*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_y++; }
|
||||
else if (keys[SDL_SCANCODE_LEFT] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_x>0 && pixels[player_x-1+player_y*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_x--; }
|
||||
if (pixels[player_x+player_y*200]==1) {
|
||||
close_sector();
|
||||
mode = MODE_NORMAL;
|
||||
}
|
||||
t = SDL_GetTicks();
|
||||
}
|
||||
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
|
||||
set_color(COLOR_BLACK);
|
||||
SDL_RenderClear(ren);
|
||||
SDL_Rect rect = {OFFSET_X,OFFSET_Y,200,200};
|
||||
SDL_RenderCopy(ren, tex, NULL, &rect);
|
||||
|
||||
Reference in New Issue
Block a user