First commit

This commit is contained in:
2020-11-20 06:53:03 +01:00
commit 9b344da330
12 changed files with 509 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
*.dll
*.exe
.vscode
*.out

BIN
breakout.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 B

90
breakout.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include <SDL2/SDL.h>
SDL_Event sdlEvent;
bool should_exit = false;
Uint8 bricks[10][8];
SDL_Point ball = {236, 592};
SDL_Point ball_dir = {3, -3};
int pala = 240-48;
Uint8 colors[4][2] {{255, 0}, {255, 128}, {0, 255}, {255, 255}};
Sint8 dirs[8][2] {{-7, -7}, {-5, -10}, {-3, -13}, {-1, -16}, {1, -16}, {3, -13}, {5, -10}, {7, -7}};
SDL_AudioSpec audioSpec{11025, AUDIO_U8, 1, 0, 512, 0, 0, NULL, NULL};
Uint8 bmp[84] = {0x42, 0x4D, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x20, 0x20, 0x20, 0x00, 0xEB, 0xF3, 0xF9, 0xE4, 0xAA, 0x12, 0x69, 0xA4, 0xAB, 0xBF, 0xF9, 0xFD, 0xA8, 0x9B, 0x21, 0xB4, 0xEB, 0xFB, 0xFF, 0xFE, 0x00, 0x00};
int score = 0;
bool dead = true;
#define sound(a, b) if (!dead) { SDL_QueueAudio(sdlAudioDevice, &beeps[a], b); SDL_PauseAudioDevice(sdlAudioDevice, 0); }
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* sdlWindow = SDL_CreateWindow("BREAKOUT", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 480, 640, SDL_WINDOW_SHOWN);
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP_RW(SDL_RWFromMem(bmp, 84), 1));
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
SDL_ShowCursor(0);
SDL_CaptureMouse(SDL_TRUE);
Uint8 beeps[8000];
for (int i=0; i < 250;i++) { if ((i/2)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=250; i < 500;i++) { if ((i/3)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=500; i < 750;i++) { if ((i/4)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=750; i < 8000;i++) { beeps[i]=rand()%(255 - ((i-750)/29)); }
while(!should_exit) {
while(SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
}
if ((SDL_GetMouseState(&pala, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) && dead) {
dead = false; score = 0; SDL_memset(bricks, 0, 80);
ball = {236, 592};
ball_dir = {7, -7};
pala = 240-48;
}
pala = pala < 0 ? 0 : pala > 480-48 ? 480-48 : pala;
ball.x += ball_dir.x;
ball.y += ball_dir.y;
if (ball.x > 480-8 || ball.x < 0) { ball_dir.x = -ball_dir.x; sound(250, 250); }
if (ball.y < 0) { ball_dir.y = -ball_dir.y; sound(250, 250); }
if (ball.y > 640-8) {
if (dead) { sound(250, 250); } else { sound(750, 7250); }
ball_dir.y = -ball_dir.y;
dead = true;
}
if (!dead && ball.y > 600-8 && ball.y < 600+16 && ball.x > pala-8 && ball.x < pala+48) {
ball_dir.x = dirs[SDL_min(7, SDL_max(0, (ball.x-pala+8)/7))][0];
ball_dir.y = dirs[SDL_min(7, SDL_max(0, (ball.x-pala+8)/7))][1];
sound(0, 250);
}
if (ball.y < 128 && bricks[ball.x/48][ball.y/16] == 0) {
bricks[ball.x/48][ball.y/16] = 1; score++;
ball_dir.y = -ball_dir.y;
sound(500, 250);
}
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);
char s[5]; SDL_itoa(score, s, 10);
const int score_pos = (480 - (strlen(s) * 3 + strlen(s) - 1) * 32) / 2;
for (int i=0; i < strlen(s); i++) {
SDL_Rect src {(s[i]-48)*3, 0, 3, 5};
SDL_Rect dst {score_pos + i*(32*4), 250, 3*32, 5*32};
if (!dead) SDL_RenderCopy(sdlRenderer, sdlTexture, &src, &dst);
}
SDL_Rect rect {0, 0, 46, 14};
for (rect.y = 1; rect.y < 129; rect.y += 16) {
for (rect.x = 1; rect.x < 481; rect.x += 48) {
if (bricks[(rect.x-1)/48][(rect.y-1)/16] == 0) {
SDL_SetRenderDrawColor(sdlRenderer, colors[rect.y/32][0], colors[rect.y/32][1], 0, 255);
SDL_RenderFillRect(sdlRenderer, &rect);
}
}
}
if (!dead) {
SDL_SetRenderDrawColor(sdlRenderer, 0, 255, 255, 255);
rect = {pala, 600, 48, 16};
SDL_RenderFillRect(sdlRenderer, &rect);
}
SDL_SetRenderDrawColor(sdlRenderer, 255, 255, 255, 255);
rect = {ball.x, ball.y, 8, 8};
SDL_RenderFillRect(sdlRenderer, &rect);
SDL_RenderPresent(sdlRenderer);
}
SDL_Quit();
return 0;
}

BIN
loderunner.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 B

64
loderunner.cpp Normal file
View File

@@ -0,0 +1,64 @@
#include <SDL2/SDL.h>
SDL_Event sdlEvent;
bool should_exit = false;
static Uint8 map[16][28] { { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5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FF, 0xF0, 0x03, 0x00, 0x00, 0x0C, 0x00, 0xFF, 0xFF, 0xF0, 0x03, 0x00, 0x03, 0xCF, 0xFF, 0xFF, 0xFF, 0xF0, 0x03, 0x00, 0x03, 0xCF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00};
int hero[4] { 140, 77, 0, SDL_FLIP_NONE };
int cycle = 0;
int hanging = 0;
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* sdlWindow = SDL_CreateWindow("LODERUNNER", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 400, SDL_WINDOW_SHOWN);
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1 , SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP_RW(SDL_RWFromMem(bmp, 504), 1));
SDL_RenderSetLogicalSize(sdlRenderer, 320, 200);
SDL_SetTextureBlendMode(sdlTexture, SDL_BLENDMODE_ADD);
while (!should_exit) {
while (SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
}
if (cycle == 2) {
hanging = 0;
cycle = 0;
const Uint8* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE]) { should_exit = true; break; }
if ((map[(hero[1]+11)/11][hero[0]/10] > 2) && (map[(hero[1]+11)/11][(hero[0]+9)/10] > 2) && (map[(hero[1])/11][hero[0]/10] > 3) && (map[(hero[1])/11][(hero[0]+9)/10] > 3) ) { hero[2]=5; hero[1]+=1;} else { if (hero[2] == 5) hero[2] = 0;
if (keys[SDL_SCANCODE_RIGHT]) { hero[3] = SDL_FLIP_NONE; if ((map[hero[1]/11][(hero[0]+10)/10] > 1) && (map[(hero[1]+10)/11][(hero[0]+10)/10] > 1)) {hero[0]+=2;hero[2]=hero[2]>1?0:hero[2]+1; } }
if (keys[SDL_SCANCODE_LEFT]) { hero[3] = SDL_FLIP_HORIZONTAL; if ((map[hero[1]/11][(hero[0]-2)/10] > 1) && (map[(hero[1]+10)/11][(hero[0]-2)/10] > 1)) {hero[0]-=2;hero[2]=hero[2]>1?0:hero[2]+1; } }
if ((map[(hero[1])/11][hero[0]/10] == 3) || (map[(hero[1])/11][(hero[0]+9)/10] == 3)) { hanging = 1; }
if (keys[SDL_SCANCODE_UP]) {
if (map[(hero[1]+10)/11][hero[0]/10] == 2) { hero[0] = (hero[0] / 10) * 10; hero[1] -= 1; hero[2] = 3; hero[3] = hero[3] == SDL_FLIP_NONE ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; } else
if (map[(hero[1]+10)/11][(hero[0]+9)/10] == 2) { hero[0] = ((hero[0]+9) / 10) * 10; hero[1] -= 1; hero[2] = 3; hero[3] = hero[3] == SDL_FLIP_NONE ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; }
}
if (keys[SDL_SCANCODE_DOWN]) {
if (((map[(hero[1])/11][hero[0]/10] == 3) || (map[(hero[1])/11][(hero[0]+9)/10] == 3)) && (hero[1]/11*11 == hero[1])) { hero[1] += 1; } else
if (map[(hero[1]+11)/11][hero[0]/10] == 2) { hero[0] = (hero[0] / 10) * 10; hero[1] += 1; hero[2] = 3; hero[3] = hero[3] == SDL_FLIP_NONE ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; } else
if (map[(hero[1]+11)/11][(hero[0]+9)/10] == 2) { hero[0] = ((hero[0]+9) / 10) * 10; hero[1] += 1; hero[2] = 3; hero[3] = hero[3] == SDL_FLIP_NONE ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; }
}
}
} else cycle++;
SDL_SetRenderDrawColor(sdlRenderer, 24, 29, 25, 255);
SDL_RenderClear(sdlRenderer);
for (int y=0; y<16; y++) {
for (int x=0; x<28; x++) {
SDL_Rect src {(map[y][x])*10, 0, 10, 11};
SDL_Rect dst {20+x*10, y*11, 10, 11};
if (map[y][x] < 2) { SDL_SetTextureColorMod(sdlTexture, 149, 58, 75); } else { SDL_SetTextureColorMod(sdlTexture, 251, 249, 254); }
if (map[y][x] < 5) SDL_RenderCopyEx(sdlRenderer, sdlTexture, &src, &dst, 0, NULL, SDL_FLIP_NONE);
}
}
SDL_Rect src {hero[2]*10, (1+hanging)*11, 10, 11};
SDL_Rect dst {20+hero[0], hero[1]+hanging, 10, 11};
SDL_SetTextureColorMod(sdlTexture, 251, 249, 254); SDL_RenderCopyEx(sdlRenderer, sdlTexture, &src, &dst, 0, NULL, SDL_RendererFlip(hero[3]));
SDL_RenderPresent(sdlRenderer);
}
SDL_DestroyTexture(sdlTexture);
SDL_DestroyWindow(sdlWindow);
SDL_Quit();
return 0;
}

BIN
pong.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

68
pong.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include <SDL2/SDL.h>
SDL_Renderer* sdlRenderer = NULL;
SDL_Texture* sdlTexture = NULL;
bool should_exit = false;
SDL_Event sdlEvent;
SDL_AudioSpec audioSpec{11025, AUDIO_U8, 1, 0, 512, 0, 0, NULL, NULL};
Uint8 bmp[160] {0x42, 0x4D, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x15, 0x51, 0x8D, 0x8A, 0x55, 0x57, 0xAA, 0xB9, 0x51, 0x53, 0xAA, 0x98, 0x75, 0x17, 0xAA, 0xBA, 0x11, 0x51, 0x8A, 0x88, 0x3E, 0x04, 0xE4, 0x0F, 0x3E, 0x04, 0xE4, 0x0F, 0x3E, 0x64, 0xE4, 0xCF, 0x3E, 0x64, 0xC4, 0xCF, 0x02, 0x64, 0x84, 0x8F, 0x02, 0x64, 0x04, 0xFF, 0x32, 0x64, 0x24, 0xFF, 0x32, 0x64, 0x64, 0xCF, 0x02, 0x04, 0xE4, 0x0F, 0x02, 0x04, 0xE4, 0x0F, 0x14, 0x0C, 0x06, 0x1B, 0x55, 0xED, 0x96, 0x5B, 0x54, 0x40, 0x06, 0x03, 0x57, 0x64, 0xDE, 0x4B, 0x14, 0x04, 0x00, 0x03, 0x00, 0x00};
int player1_y = 260, player2_y = 260, player1_score = 0, player2_score = 0, ball_x, ball_y, ball_dx, ball_dy = 4;
bool wait = true;
#define sound(a, b) SDL_QueueAudio(sdlAudioDevice, &beeps[a], b); SDL_PauseAudioDevice(sdlAudioDevice, 0);
void Draw(int x, int y, int sx, int sy, int w, int h) {
SDL_Rect rects[2] {{sx, sy, w, h}, {x, y, w*10, h*10}};
SDL_RenderCopy(sdlRenderer, sdlTexture, &rects[0], &rects[1]);
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* sdlWindow = SDL_CreateWindow("PONG", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP_RW(SDL_RWFromMem(bmp, 160), 1));
SDL_SetTextureBlendMode(sdlTexture, SDL_BLENDMODE_ADD);
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
Uint8 beeps[8000];
for (int i=0; i < 250;i++) { if ((i/2)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=250; i < 500;i++) { if ((i/3)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=500; i < 8000;i++) { beeps[i]=rand()%(255 - ((i-500)/30)); }
while (!should_exit) {
while (SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
}
SDL_SetTextureColorMod(sdlTexture, 251, 249, 254);
SDL_SetRenderDrawColor(sdlRenderer, 24, 29, 25, 255);
SDL_RenderClear(sdlRenderer);
if (wait && player1_score == 0 && player2_score == 0) {
Draw(260, 190, 0, 5, 28, 10);
} else {
for (int i = 0; i < 600; i += 40) Draw(395, i, 0, 0, 1, 2);
}
Draw(190, 40, 3 * player1_score, 0, 3, 5);
Draw(590, 40, 3 * player2_score, 0, 3, 5);
Draw(80, player1_y, 0, 0, 1, 8);
Draw(720, player2_y, 0, 0, 1, 8);
Draw(ball_x, ball_y, 0, 0, 1, 1);
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_Q] && player1_y > 0) player1_y -= 20;
if (state[SDL_SCANCODE_A] && player1_y < 520) player1_y += 20;
if (state[SDL_SCANCODE_UP] && player2_y > 0) player2_y -= 20;
if (state[SDL_SCANCODE_DOWN] && player2_y < 520) player2_y += 20;
if (wait && (state[SDL_SCANCODE_RETURN] || state[SDL_SCANCODE_SPACE])) { ball_x = 395; ball_y = 295; ball_dx = ball_dx < 0 ? -4 : 4; wait = false; }
if (!wait) {
ball_x += ball_dx; ball_y += ball_dy;
if (ball_y < 0 || ball_y > 590) { ball_dy = -ball_dy; sound(0, 250); }
if (ball_x < 0) { player2_score++; wait = true; sound(500, 7500); }
if (ball_x > 790) { player1_score++; wait = true; sound(500, 7500); }
if (ball_dx < 0 && int(ball_x) <= 90 && int(ball_x) >= 80 && ball_y >= player1_y && ball_y <= player1_y + 80) { ball_dx = -ball_dx; if (ball_dx <= 10) ball_dx = ball_dx + 1; sound(250, 250); }
if (ball_dx > 0 && int(ball_x) >= 710 && int(ball_x) <= 720 && ball_y >= player2_y && ball_y <= player2_y + 80) { ball_dx = -ball_dx; if (ball_dx >= -10) ball_dx = ball_dx - 1; sound(250, 250); }
}
SDL_RenderPresent(sdlRenderer);
}
SDL_Quit();
return 0;
}

BIN
snake.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 B

100
snake.cpp Normal file
View File

@@ -0,0 +1,100 @@
#include <SDL2/SDL.h>
SDL_Event sdlEvent;
bool should_exit = false;
SDL_AudioSpec audioSpec{11025, AUDIO_U8, 1, 0, 512, 0, 0, NULL, NULL};
Uint8 bmp[132] { 0x42, 0x4D, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x49, 0x24, 0x83, 0x82, 0x48, 0x24, 0x83, 0xF2, 0x48, 0x21, 0x9F, 0x82, 0x49, 0x20, 0x87, 0x9E, 0x49, 0x24, 0x9F, 0x82, 0x08, 0x24, 0x83, 0x82, 0x1C, 0x64, 0x83, 0x95, 0x51, 0xCD, 0x8A, 0x51, 0x57, 0xAA, 0xB9, 0x55, 0x53, 0xAA, 0x98, 0x75, 0x17, 0xAA, 0xBA, 0x93, 0x51, 0x9A, 0x89, 0x14, 0x0C, 0x06, 0x1B, 0x55, 0xED, 0x96, 0x5B, 0x54, 0x40, 0x06, 0x03, 0x57, 0x64, 0xDE, 0x4B, 0x14, 0x04, 0x00, 0x03, 0x00, 0x00 };
Uint8 mapa[40][30];
SDL_Point head {20, 15}, tail {20, 15};
int wait_time = 0;
Uint8 last_dir = 0;
int count = -1;
#define sound(a, b) SDL_QueueAudio(sdlAudioDevice, &beeps[a], b); SDL_PauseAudioDevice(sdlAudioDevice, 0);
void new_apple() {
int x, y;
do { x = rand()%40; y = rand()%30; } while( mapa[x][y] != 0);
mapa[x][y] = 255;
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* sdlWindow = SDL_CreateWindow("SNAKE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_RenderSetLogicalSize(sdlRenderer, 40, 30);
SDL_Texture* sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP_RW(SDL_RWFromMem(bmp, 132), 1));
SDL_SetTextureBlendMode(sdlTexture, SDL_BLENDMODE_ADD);
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
Uint8 beeps[8000];
for (int i=0; i < 250;i++) { if ((i/2)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=250; i < 7500;i++) { beeps[i]=rand()%(255 - ((i-250)/29)); }
while (!should_exit) {
while(SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
}
const Uint8* keys = SDL_GetKeyboardState(NULL);
SDL_SetRenderDrawColor(sdlRenderer, 24, 29, 25, 255);
SDL_RenderClear(sdlRenderer);
SDL_Rect rect[2] = { {0, 5, 32, 5}, {4, 10, 32, 5} };
switch (count) {
case -1:
rect[0] = {0, 10, 30, 7};
rect[1] = {5, 10, 30, 7};
case 0:
SDL_RenderCopy(sdlRenderer, sdlTexture, &rect[0], &rect[1]);
if (keys[SDL_SCANCODE_SPACE] || keys[SDL_SCANCODE_RETURN]) {
count = 599;
head = tail = {20, 15};
SDL_memset(mapa, 0, 40*30);
mapa[head.x][head.y] = SDL_SCANCODE_RIGHT + rand()%4;
new_apple();
}
break;
default:
count--;
if (++wait_time == 4) {
wait_time = 0;
last_dir = mapa[head.x][head.y];
if (mapa[head.x][head.y] == SDL_SCANCODE_UP) { head.y--; if (head.y < 0) head.y = 29; }
else if (mapa[head.x][head.y] == SDL_SCANCODE_RIGHT) { head.x++; if (head.x == 40) head.x = 0; }
else if (mapa[head.x][head.y] == SDL_SCANCODE_DOWN) { head.y++; if (head.y == 30) head.y = 0; }
else if (mapa[head.x][head.y] == SDL_SCANCODE_LEFT) { head.x--; if (head.x < 0) head.x = 39; }
if (mapa[head.x][head.y] != 255) {
if (mapa[head.x][head.y] != 0) { sound(250, 7250); count = 0; }
if (mapa[tail.x][tail.y] == SDL_SCANCODE_UP) { mapa[tail.x][tail.y] = 0; tail.y--; if (tail.y < 0) tail.y = 29; }
else if (mapa[tail.x][tail.y] == SDL_SCANCODE_RIGHT) { mapa[tail.x][tail.y] = 0; tail.x++; if (tail.x == 40) tail.x = 0; }
else if (mapa[tail.x][tail.y] == SDL_SCANCODE_DOWN) { mapa[tail.x][tail.y] = 0; tail.y++; if (tail.y == 30) tail.y = 0; }
else if (mapa[tail.x][tail.y] == SDL_SCANCODE_LEFT) { mapa[tail.x][tail.y] = 0; tail.x--; if (tail.x < 0) tail.x = 39; }
} else {
sound(0, 250); count = 599;
new_apple();
}
mapa[head.x][head.y] = last_dir;
}
if (keys[SDL_SCANCODE_UP] && last_dir != SDL_SCANCODE_DOWN) mapa[head.x][head.y] = SDL_SCANCODE_UP;
else if (keys[SDL_SCANCODE_RIGHT] && last_dir != SDL_SCANCODE_LEFT) mapa[head.x][head.y] = SDL_SCANCODE_RIGHT;
else if (keys[SDL_SCANCODE_DOWN] && last_dir != SDL_SCANCODE_UP) mapa[head.x][head.y] = SDL_SCANCODE_DOWN;
else if (keys[SDL_SCANCODE_LEFT] && last_dir != SDL_SCANCODE_RIGHT) mapa[head.x][head.y] = SDL_SCANCODE_LEFT;
if (count > 180 || ((count/8) % 2) == 0) {
SDL_Rect rect[2] = { {3*(count/60), 0, 3, 5}, {19, 2, 3, 5} };
SDL_SetTextureColorMod(sdlTexture, 130, 210, 225); SDL_RenderCopy(sdlRenderer, sdlTexture, &rect[0], &rect[1]); SDL_SetTextureColorMod(sdlTexture, 251, 249, 254);
}
SDL_Rect rect = {0, 0, 1, 1};
for (rect.y = 0; rect.y < 30; rect.y++) {
for (rect.x = 0; rect.x < 40; rect.x++) {
if (mapa[rect.x][rect.y] == 255) { SDL_SetRenderDrawColor(sdlRenderer, 149, 58, 75, 255); SDL_RenderFillRect(sdlRenderer, &rect); }
else if (mapa[rect.x][rect.y] != 0) { SDL_SetRenderDrawColor(sdlRenderer, 251, 249, 254, 255); SDL_RenderFillRect(sdlRenderer, &rect); }
}
}
}
SDL_RenderPresent(sdlRenderer);
}
SDL_Quit();
return 0;
}

BIN
tanks.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 B

95
tanks.cpp Normal file
View File

@@ -0,0 +1,95 @@
#include <SDL2/SDL.h>
struct Tank { float x, y, angle; SDL_Scancode up, down, left, right, fire; int shock, score; };
struct Bullet { float x, y, dx, dy; int ttl; };
SDL_Event sdlEvent;
bool should_exit = false;
SDL_AudioSpec audioSpec{11025, AUDIO_U8, 1, 0, 512, 0, 0, NULL, NULL};
Uint8 map[24][40] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 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x20, 0x24, 0x9C, 0x0C, 0x65, 0x24, 0x80, 0xFF, 0xFD, 0xFF, 0xFC, 0x14, 0x0C, 0x06, 0x18, 0x55, 0xED, 0x96, 0x58, 0x54, 0x40, 0x06, 0x00, 0x57, 0x64, 0xDE, 0x48, 0x14, 0x04, 0x00, 0x00, 0x00, 0x00};
Tank tank[2] {{572, 258, 270, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, 0, 0}, {40, 258, 90, SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_LCTRL, 0, 0}};
Bullet bullet[2] { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0} };
Uint8 colors[2][3] { {130, 210, 225}, {149, 58, 75} };
#define sound(a, b) SDL_QueueAudio(sdlAudioDevice, &beeps[a], b); SDL_PauseAudioDevice(sdlAudioDevice, 0);
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* sdlWindow = SDL_CreateWindow("TANKS", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, SDL_LoadBMP_RW(SDL_RWFromMem(bmp, 112), 1));
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
Uint8 beeps[8000];
for (int i=0; i < 250;i++) { beeps[i]=rand()%255; }
for (int i=250; i < 500;i++) { if ((i/3)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=500; i < 750;i++) { if ((i/4)%2==0) beeps[i]=0; else beeps[i]=128; }
for (int i=750; i < 8000;i++) { beeps[i]=rand()%(255 - ((i-750)/29)); }
while (!should_exit) {
while(SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
}
const Uint8* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE]) { should_exit = true; break; }
for (int i=0; i<2; i++) {
if (tank[i].shock == 0) {
if (keys[tank[i].right]) {sound(0, 16); tank[i].angle += 4; if (tank[i].angle > 360) tank[i].angle -= 360;}
if (keys[tank[i].left]) {sound(0, 16); tank[i].angle -= 4; if (tank[i].angle < 0) tank[i].angle += 360;}
if (keys[tank[i].up]) { sound(0, 16);
tank[i].x += SDL_sinf(tank[i].angle*M_PI/180);
if ((map[int(tank[i].y-80)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-80)/16][int(tank[i].x+27)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x+27)/16] == 1)) {
tank[i].x -= SDL_sinf(tank[i].angle*M_PI/180);
}
tank[i].y -= SDL_cosf(tank[i].angle*M_PI/180);
if ((map[int(tank[i].y-80)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-80)/16][int(tank[i].x+27)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x+27)/16] == 1)) {
tank[i].y += SDL_cosf(tank[i].angle*M_PI/180);
}
}
if (keys[tank[i].down]) {sound(0, 16);
tank[i].x -= SDL_sinf(tank[i].angle*M_PI/180);
if ((map[int(tank[i].y-80)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-80)/16][int(tank[i].x+27)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x+27)/16] == 1)) {
tank[i].x += SDL_sinf(tank[i].angle*M_PI/180);
}
tank[i].y += SDL_cosf(tank[i].angle*M_PI/180);
if ((map[int(tank[i].y-80)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-80)/16][int(tank[i].x+27)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x)/16] == 1) || (map[int(tank[i].y-52)/16][int(tank[i].x+27)/16] == 1)) {
tank[i].y -= SDL_cosf(tank[i].angle*M_PI/180);
}
}
if (keys[tank[i].fire] && bullet[i].ttl == 0) {sound(250, 250); bullet[i].ttl=5; bullet[i].x = tank[i].x+14; bullet[i].y = tank[i].y+14; bullet[i].dx=SDL_sinf(tank[i].angle*M_PI/180); bullet[i].dy=-SDL_cosf(tank[i].angle*M_PI/180);}
} else {
tank[i].shock--;
tank[i].angle += 20; if (tank[i].angle > 360) tank[i].angle -= 360;
}
if (bullet[i].ttl > 0) {
bullet[i].x += 4*bullet[i].dx;
if (map[int(bullet[i].y-80)/16][int(bullet[i].x)/16] == 1) { bullet[i].dx = -bullet[i].dx; bullet[i].x += 4*bullet[i].dx; sound(500, 250); bullet[i].ttl--;}
bullet[i].y += 4*bullet[i].dy;
if (map[int(bullet[i].y-80)/16][int(bullet[i].x)/16] == 1) { bullet[i].dy = -bullet[i].dy; bullet[i].y += 4*bullet[i].dy; sound(500, 250); bullet[i].ttl--;}
if (tank[1-i].shock == 0 && bullet[i].x > tank[1-i].x && bullet[i].x < tank[1-i].x+27 && bullet[i].y > tank[1-i].y && bullet[i].y < tank[1-i].y+27) { sound(750, 7250); bullet[i].ttl = 0; tank[1-i].shock = 50; tank[i].score++; }
}
}
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);
for (int i=0; i<2; i++) {
SDL_Rect src {11, 5, 7, 7};
SDL_Rect dst {int(tank[i].x), int(tank[i].y), 28, 28}; SDL_SetTextureColorMod(sdlTexture, colors[i][0], colors[i][1], colors[i][2]);
SDL_RenderCopyEx(sdlRenderer, sdlTexture, &src, &dst, tank[i].angle, NULL, SDL_FLIP_NONE);
SDL_SetRenderDrawColor(sdlRenderer, colors[i][0], colors[i][1], colors[i][2], 255);
if (bullet[i].ttl != 0) { dst = {int(bullet[i].x)-2, int(bullet[i].y)-2, 4, 4}; SDL_RenderFillRect(sdlRenderer, &dst); }
src = {tank[i].score*3, 0, 3, 5}; dst = {80+(1-i)*440, 16, 40, 40};
SDL_RenderCopy(sdlRenderer, sdlTexture, &src, &dst);
}
SDL_SetRenderDrawColor(sdlRenderer, 251, 249, 254, 255);
for (int y=0; y<24; y++) {
for (int x=0; x<40; x++) {
if (map[y][x] == 1) { SDL_Rect dst = {x*16, 80+y*16, 16, 16}; SDL_RenderFillRect(sdlRenderer, &dst); }
}
}
SDL_RenderPresent(sdlRenderer);
}
SDL_Quit();
return 0;
}

88
tetris.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include <SDL2/SDL.h>
SDL_Event sdlEvent;
bool should_exit = false;
struct Tetromino { Uint16 figure; Uint8 orig, prev, next; };
Tetromino tetromino[19] { {0x0660, 0, 0, 0}, {0x4444, 1, 2, 2}, {0x0F00, 1, 1, 1}, {0x0C60, 2, 4, 4}, {0x2640, 2, 3, 3}, {0x06C0, 3, 6, 6}, {0x4620, 3, 5, 5}, {0x4460, 4, 8, 10}, {0x2E00, 4, 9, 7}, {0xC440, 4, 10, 8}, {0x0E80, 4, 7, 9}, {0x44C0, 5, 12, 14}, {0x0E20, 5, 13, 11}, {0x6440, 5, 14, 12}, {0x8E00, 5, 11, 13}, {0x4640, 6, 16, 18}, {0x4E00, 6, 17, 15}, {0x4C40, 6, 18, 16}, {0x0E40, 6, 15, 17} };
Uint8 starting[7] { 0, 1, 3, 5, 7, 11, 15 };
Uint8 colors[7][3] { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 0}, {0, 255, 255}, {255, 0, 255}, {255, 128, 0} };
Uint8 board[18][10];
SDL_Point piece_pos {3, 0};
Uint8 current_piece = 0;
Uint8 next_piece = 0;
Uint8 level = 0;
Uint8 speed = (20-level)*3;
bool is_valid_move() {
int x = piece_pos.x+3;
int y = piece_pos.y+3;
Uint16 piece = tetromino[current_piece].figure;
for (int i=0; i<16; i++) {
if ((piece & 1) && ( (x >= 10) || (x < 0) || (y >= 18) || (board[y][x] != 0) ) ) { return false; }
piece = piece >> 1;
x--; if (x < piece_pos.x) { x = piece_pos.x+3; y--; }
}
return true;
}
void fix_piece() {
int x = piece_pos.x+3;
int y = piece_pos.y+3;
Uint16 piece = tetromino[current_piece].figure;
for (int i=0; i<16; i++) {
if (piece & 1) { board[y][x] = tetromino[current_piece].orig+1; }
piece = piece >> 1;
x--; if (x < piece_pos.x) { x = piece_pos.x+3; y--; }
}
piece_pos = {3, 0};
current_piece = next_piece;
next_piece = starting[rand()%7];
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* sdlWindow = SDL_CreateWindow("TETRIS", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 576, SDL_WINDOW_SHOWN);
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
while(!should_exit) {
while(SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { should_exit = true; break; }
if (sdlEvent.type == SDL_KEYDOWN) {
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_RIGHT) { piece_pos.x++; if (!is_valid_move()) piece_pos.x--; }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_LEFT) { piece_pos.x--; if (!is_valid_move()) piece_pos.x++; }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_DOWN) { piece_pos.y++; if (!is_valid_move()) { piece_pos.y--; fix_piece(); } }
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_UP) { current_piece = tetromino[current_piece].next; if (!is_valid_move()) current_piece = tetromino[current_piece].prev; }
}
}
SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
SDL_RenderClear(sdlRenderer);
for (int y=0; y<18; y++) {
for (int x=0; x<10; x++) {
if (board[y][x] == 0) continue;
SDL_Rect rect { 32*x, 32*y, 32, 32};
SDL_SetRenderDrawColor(sdlRenderer, colors[board[y][x]-1][0], colors[board[y][x]-1][1], colors[board[y][x]-1][2], 255); SDL_RenderFillRect(sdlRenderer, &rect);
SDL_SetRenderDrawColor(sdlRenderer, colors[board[y][x]-1][0]/2, colors[board[y][x]-1][1]/2, colors[board[y][x]-1][2]/2, 255); SDL_RenderDrawRect(sdlRenderer, &rect);
}
}
int x = piece_pos.x+3;
int y = piece_pos.y+3;
Uint16 piece = tetromino[current_piece].figure;
for (int i=0; i<16; i++) {
if (piece & 1) {
SDL_Rect rect { 32*x, 32*y, 32, 32};
SDL_SetRenderDrawColor(sdlRenderer, colors[tetromino[current_piece].orig][0], colors[tetromino[current_piece].orig][1], colors[tetromino[current_piece].orig][2], 255); SDL_RenderFillRect(sdlRenderer, &rect);
SDL_SetRenderDrawColor(sdlRenderer, colors[tetromino[current_piece].orig][0]/2, colors[tetromino[current_piece].orig][1]/2, colors[tetromino[current_piece].orig][2]/2, 255); SDL_RenderDrawRect(sdlRenderer, &rect);
}
piece = piece >> 1;
x--; if (x < piece_pos.x) { x = piece_pos.x+3; y--; }
}
speed--;
if (speed == 0) {
speed = (20-level) * 3;
piece_pos.y++; if (!is_valid_move()) { piece_pos.y--; fix_piece(); }
}
SDL_RenderPresent(sdlRenderer);
}
SDL_Quit();
return 0;
}