diff --git a/README.md b/README.md index 82267bb52b58d9b8148b4e654831d051512b5959..86bd12df76309da0927f046e68483e211e928ab3 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,14 @@ Trello : https://trello.com/b/YGhNeYX3/projet-apprentissage-par-renforcement ├── travail_individuel │ ├── Belkhiri │ │ ├── x_fenetre -│ │ └── snakes +│ │ ├── snakes +│ │ └── GameOfLife │ ├── Beret │ │ └── x_fenetre │ └── Meyer -│ └── x_fenetre +│ ├── x_fenetre +│ ├── pave_de_serpents +│ └── jeu_de_la_vie └── travail_de_groupe ├── chef_oeuvre └── jeu_appren_par_renfo diff --git a/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json b/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..17f009151d05d549c7b9e379848b7983402aca52 --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "main.h": "c" + } +} \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/Makefile b/travail_individuel/Belkhiri/GameOfLife/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..7a37dccc5fa59a8272d8e92a2494d76748d19625 --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/Makefile @@ -0,0 +1,23 @@ +CC=gcc + +LDFLAG=$(shell sdl2-config --cflags --libs) -lm -lSDL2_ttf -D_REENTRANT -lSDL2_image -pthread +CFLAG=-Wall $(shell sdl2-config --cflags --libs) + +EXEC=run +SRC=$(wildcard src/*.c) +OBJ=$(SRC:.c=.o) + +all:$(EXEC) + +$(EXEC):$(OBJ) + $(CC) -o $@ $^ $(LDFLAG) + mv $^ bin/ + +%.o:%.c + $(CC) -o $@ -c $< $(CFLAG) + +.PHONY:clean + +clean: + rm -rf bin/*.o + rm -rf $(EXEC) diff --git a/travail_individuel/Belkhiri/GameOfLife/Res/Roboto-Black.ttf b/travail_individuel/Belkhiri/GameOfLife/Res/Roboto-Black.ttf new file mode 100644 index 0000000000000000000000000000000000000000..86ec2b29ba56a3d6c45f1a8584ff3780fa70c60e Binary files /dev/null and b/travail_individuel/Belkhiri/GameOfLife/Res/Roboto-Black.ttf differ diff --git a/travail_individuel/Belkhiri/GameOfLife/Res/column.png b/travail_individuel/Belkhiri/GameOfLife/Res/column.png new file mode 100755 index 0000000000000000000000000000000000000000..0f8de7c03c16048cf145d390671b6e40d1d389c1 Binary files /dev/null and b/travail_individuel/Belkhiri/GameOfLife/Res/column.png differ diff --git a/travail_individuel/Belkhiri/GameOfLife/Res/playButton.png b/travail_individuel/Belkhiri/GameOfLife/Res/playButton.png new file mode 100755 index 0000000000000000000000000000000000000000..32aff50751457e910ce7e41eed2c1f80b9e61ad2 Binary files /dev/null and b/travail_individuel/Belkhiri/GameOfLife/Res/playButton.png differ diff --git a/travail_individuel/Belkhiri/GameOfLife/Res/title.png b/travail_individuel/Belkhiri/GameOfLife/Res/title.png new file mode 100755 index 0000000000000000000000000000000000000000..f835b524b14e0db97578de105e2d11df3a44bf95 Binary files /dev/null and b/travail_individuel/Belkhiri/GameOfLife/Res/title.png differ diff --git a/travail_individuel/Belkhiri/GameOfLife/run b/travail_individuel/Belkhiri/GameOfLife/run new file mode 100755 index 0000000000000000000000000000000000000000..cc9b9a76df8c962a5b219ba1b0f781bebca3ab35 Binary files /dev/null and b/travail_individuel/Belkhiri/GameOfLife/run differ diff --git a/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c new file mode 100644 index 0000000000000000000000000000000000000000..8b3a2d5894b9d81f214dd27bad46a1caf9cfc43b --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c @@ -0,0 +1,84 @@ +#include "gest_event.h" + + +void gestMenu(){ + SDL_Event event; + while (SDL_PollEvent(&event)){ + switch(event.type) + { + case SDL_QUIT: + running = 0; + break; + + case SDL_KEYUP: + switch (event.key.keysym.sym) + { + case SDLK_x: + running = 0; + continue; + + default: + game_state = GAME; + continue; + } + break; + + default: + continue; + } + } + SDL_Delay(5); +} + +void gestGame(){ + SDL_Event event; + while (SDL_PollEvent(&event)){ + switch(event.type) + { + case SDL_QUIT: + running = 0; + break; + + case SDL_KEYUP: + switch (event.key.keysym.sym) + { + case SDLK_x: + running = 0; + continue; + + default: + continue; + } + break; + + case SDL_MOUSEBUTTONDOWN: + if (event.button.button == SDL_BUTTON_LEFT){ + int x = event.button.x; + int y = event.button.y; + // change value of map[x][y] + if (x < ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2 + MAPSIZE*CELLSIZE && x > ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2){ + int i = (x - ScreenDimension.w/2 + MAPSIZE*CELLSIZE/2)/CELLSIZE; + int j = (y)/CELLSIZE; + map[j][i] = !map[j][i]; + } + } + break; + + default: + continue; + } + } + SDL_Delay(5); +} + + +void *EventLoop(void *arg){ + while(running){ + switch(game_state){ + case MENU : gestMenu();break; + case GAME : gestGame();break; + default:printf("game state fault");break; + } + } + return NULL; +} \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h new file mode 100644 index 0000000000000000000000000000000000000000..c5c76e37ddffc777052540d5c6668199b00b39c2 --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h @@ -0,0 +1,11 @@ +#ifndef _GEST_EVENT_H_ +#define _GEST_EVENT_H_ + + +#include "main.h" +#include "utility.h" +#include "render.h" + +void *EventLoop(void *arg); + +#endif \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/main.c b/travail_individuel/Belkhiri/GameOfLife/src/main.c new file mode 100644 index 0000000000000000000000000000000000000000..30afa9439497ca314a78ae8417ff7b7e34324ca1 --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/main.c @@ -0,0 +1,14 @@ +#include "main.h" + +int running; +int game_state; + + +int main(){ + running = 1; + game_state = MENU; + initMap(map); + + MainLoop(); + +} \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/main.h b/travail_individuel/Belkhiri/GameOfLife/src/main.h new file mode 100644 index 0000000000000000000000000000000000000000..73ac9527544c6acc5af9e8e6cca57e3edda73a1e --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/main.h @@ -0,0 +1,30 @@ +#ifndef _MAIN_HEADER_ +#define _MAIN_HEADER_ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> +#include <SDL2/SDL_ttf.h> + +#include <pthread.h> + +#include "render.h" +#include "utility.h" +#include "gest_event.h" +#include "map.h" + + +#define MENU 0 +#define GAME 1 + +extern int running; +extern int game_state; + + +#endif \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/map.c b/travail_individuel/Belkhiri/GameOfLife/src/map.c new file mode 100644 index 0000000000000000000000000000000000000000..cb922b4765c5aed5624acf3bc05775e9abe57ac7 --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.c @@ -0,0 +1,23 @@ +#include "map.h" + + +int map[MAPSIZE][MAPSIZE]; + + +void initMap(int map[MAPSIZE][MAPSIZE]) { + for(int i = 0; i < MAPSIZE; i++){ + for(int j = 0; j < MAPSIZE; j++){ + map[i][j] = rand() % 2; + } + } +} + + +void printMap(int map[MAPSIZE][MAPSIZE]){ + for(int i = 0; i < MAPSIZE; i++){ + for(int j = 0; j < MAPSIZE; j++){ + printf("%d ", map[i][j]); + } + printf("\n"); + } +} \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/map.h b/travail_individuel/Belkhiri/GameOfLife/src/map.h new file mode 100644 index 0000000000000000000000000000000000000000..111b8545fc513e390c2d75b9c9f0fe61dd046b0a --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.h @@ -0,0 +1,12 @@ +#ifndef _MAP_HEADER_ +#define _MAP_HEADER_ + +#include "main.h" + +#define MAPSIZE 20 +extern int map[MAPSIZE][MAPSIZE]; + +void printMap(int map[MAPSIZE][MAPSIZE]); +void initMap(int map[MAPSIZE][MAPSIZE]); + +#endif \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.c b/travail_individuel/Belkhiri/GameOfLife/src/render.c new file mode 100644 index 0000000000000000000000000000000000000000..9889be58826005c54def13e958b8b02843bfa837 --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.c @@ -0,0 +1,160 @@ +#include "render.h" + +SDL_Window *window = NULL; +SDL_Renderer *renderer = NULL; +SDL_DisplayMode ScreenDimension; + +TTF_Font *RobotoFont; + +SDL_Texture * titleTexture = NULL; +SDL_Surface * titleSurface = NULL; + +SDL_Texture * playButtonTexture = NULL; +SDL_Surface * playButtonSurface = NULL; + +SDL_Texture * columnTexture = NULL; +SDL_Surface * columnSurface = NULL; + +void CreateWindow(){ + + if (SDL_Init(SDL_INIT_VIDEO) != 0){ + printf("Couldn't create window."); + exit(EXIT_FAILURE); + } + + SDL_GetCurrentDisplayMode(0, &ScreenDimension); + + window = SDL_CreateWindow("Game Of Life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, ScreenDimension.w, ScreenDimension.h, SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP); + + if (window == NULL){ + printf("Couldn't create window"); + exit(EXIT_FAILURE); + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); + + if (renderer == NULL){ + printf("Couldn't create renderer."); + exit(EXIT_FAILURE); + } + + if (TTF_Init() == -1) + { + exit(EXIT_FAILURE); + } + + RobotoFont = TTF_OpenFont("Res/Roboto-Black.ttf", 50); +} + +void drawBackground(){ + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_Rect rect = {0, 0, ScreenDimension.w, ScreenDimension.h}; + SDL_RenderFillRect(renderer, &rect); +} + +void drawTitle(){ + int titleWidth, titleHeight; + SDL_QueryTexture(titleTexture, NULL, NULL, &titleWidth, &titleHeight); + SDL_Rect titleRect = {ScreenDimension.w/2 - titleWidth/2, ScreenDimension.h/2 - titleHeight, titleWidth, titleHeight}; + SDL_RenderCopy(renderer, titleTexture, NULL, &titleRect); +} + +void drawPlayButton(){ + int titleWidth, titleHeight; + SDL_QueryTexture(titleTexture, NULL, NULL, &titleWidth, &titleHeight); + int playButtonWidth, playButtonHeight; + SDL_QueryTexture(playButtonTexture, NULL, NULL, &playButtonWidth, &playButtonHeight); + playButtonHeight = playButtonHeight / 2; + playButtonWidth = playButtonWidth / 2; + SDL_Rect playButtonRect = {ScreenDimension.w/2 - playButtonWidth/2, ScreenDimension.h/2 + titleHeight, playButtonWidth, playButtonHeight}; + SDL_RenderCopy(renderer, playButtonTexture, NULL, &playButtonRect); +} + +void drawMenu(){ + SDL_RenderClear(renderer); + drawBackground(); + drawTitle(); + drawPlayButton(); + SDL_RenderPresent(renderer); +} + + +void drawMap(int map[MAPSIZE][MAPSIZE]){ + int x_offset = ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2; + for(int i = 0; i < MAPSIZE; i++){ + for(int j = 0; j < MAPSIZE; j++){ + if(map[i][j] == 1){ + SDL_Rect rect = {x_offset + j*CELLSIZE, i*CELLSIZE, CELLSIZE, CELLSIZE}; + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderFillRect(renderer, &rect); + } + } + } +} + +void drawColumns(){ + // draw 2 columnTexture, 1 left 1 right + int columnWidth, columnHeight; + SDL_QueryTexture(columnTexture, NULL, NULL, &columnWidth, &columnHeight); + int x_offset = ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2; + SDL_Rect leftRect = {0, -50, columnWidth, ScreenDimension.h + 100}; + SDL_Rect rightRect = {x_offset + MAPSIZE*CELLSIZE + columnWidth/2, -50, columnWidth, ScreenDimension.h + 100}; + SDL_RenderCopy(renderer, columnTexture, NULL, &leftRect); + SDL_RenderCopy(renderer, columnTexture, NULL, &rightRect); +} + +void drawGame(){ + SDL_RenderClear(renderer); + drawBackground(); + drawMap(map); + drawColumns(); + SDL_RenderPresent(renderer); +} + +void MainLoop(){ + CreateWindow(); + + titleSurface = IMG_Load("Res/title.png"); + playButtonSurface = IMG_Load("Res/playButton.png"); + columnSurface = IMG_Load("Res/column.png"); + + titleTexture = SDL_CreateTextureFromSurface(renderer, titleSurface); + playButtonTexture = SDL_CreateTextureFromSurface(renderer, playButtonSurface); + columnTexture = SDL_CreateTextureFromSurface(renderer, columnSurface); + + SDL_FreeSurface(titleSurface); + SDL_FreeSurface(playButtonSurface); + SDL_FreeSurface(columnSurface); + + unsigned int a = SDL_GetTicks(); + unsigned int b = SDL_GetTicks(); + double delta = 0; + + pthread_t eventThread; + if (pthread_create(&eventThread, NULL, EventLoop, NULL) != 0){ + printf("Couldn't create thread."); + exit(EXIT_FAILURE); + } + + while (running){ + a = SDL_GetTicks(); + delta = (a - b); + if (delta > 1/FPS_TO_GET){ + printf("fps : %f\n", 1000/delta); + b = a; + switch (game_state){ + case MENU: + drawMenu(); + break; + case GAME: + drawGame(); + break; + } + } + else { + // fait dormir le thread pour garder des ressources + usleep(1000 * (1/FPS_TO_GET - delta)); + } + + } +} \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.h b/travail_individuel/Belkhiri/GameOfLife/src/render.h new file mode 100644 index 0000000000000000000000000000000000000000..ead06c849701218040c65cf14d50d93be83ba1ed --- /dev/null +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.h @@ -0,0 +1,18 @@ +#ifndef _RENDER_H_ +#define _RENDER_H_ + +#include "main.h" +#include "utility.h" +#include "gest_event.h" + +#define FPS_TO_GET 1000/60.0 +#define CELLSIZE (ScreenDimension.h/MAPSIZE) + +extern SDL_Window *window; +extern SDL_Renderer *renderer; +extern SDL_DisplayMode ScreenDimension; + + +void MainLoop(); + +#endif \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/utility.c b/travail_individuel/Belkhiri/GameOfLife/src/utility.c new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/utility.h b/travail_individuel/Belkhiri/GameOfLife/src/utility.h new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/travail_individuel/Belkhiri/snakes/run b/travail_individuel/Belkhiri/snakes/run index 6338c0e03adad271a3f22d17e9e5d4567fb494bd..d3ccac3c8360b9a69698e548c7c1dc068492b904 100755 Binary files a/travail_individuel/Belkhiri/snakes/run and b/travail_individuel/Belkhiri/snakes/run differ diff --git a/travail_individuel/Belkhiri/snakes/snakes.c b/travail_individuel/Belkhiri/snakes/snakes.c index 40cbd7925ddfedbc58cfd95c0b320e495666e078..f2cd98011316231e5ba48287b2a18ac5eae3cef7 100644 --- a/travail_individuel/Belkhiri/snakes/snakes.c +++ b/travail_individuel/Belkhiri/snakes/snakes.c @@ -210,7 +210,7 @@ int main(int argc, char** argv) { for (int i = 0; i < NB_SNAKES; i++) { // random int between 0 and WIDTH - snakeList[i] = init_snake(rand()%screen.w, rand()%screen.h, rand()%200, rand()%50); + snakeList[i] = init_snake(rand()%screen.w/2, rand()%screen.h/2, rand()%200, rand()%50); } for (int i=0; i< 50; i++){ diff --git a/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json b/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000000000000000000000000000000000..4d5b55df0634dd59212f0d153967b2f7af421e22 --- /dev/null +++ b/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/SDL2" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/travail_individuel/Meyer/jeu_de_la_vie/main b/travail_individuel/Meyer/jeu_de_la_vie/main new file mode 100755 index 0000000000000000000000000000000000000000..225f7985487aff34b31046767a595cf7adcc6f40 Binary files /dev/null and b/travail_individuel/Meyer/jeu_de_la_vie/main differ diff --git a/travail_individuel/Meyer/jeu_de_la_vie/main.c b/travail_individuel/Meyer/jeu_de_la_vie/main.c new file mode 100644 index 0000000000000000000000000000000000000000..ff7b0b31d23a1f203dc3bc84dc6dc4d09e689d99 --- /dev/null +++ b/travail_individuel/Meyer/jeu_de_la_vie/main.c @@ -0,0 +1,209 @@ +#include <stdio.h> +#include <stdlib.h> +#include <SDL2/SDL.h> + +#define TAILLE_MONDE_DELIMITE 20 +#define TAILLE_TABLEAU_REGLES 9 + +int survie[TAILLE_TABLEAU_REGLES] = {0, 0, 1, 1, 0, 0, 0, 0, 0}; +int naissance[TAILLE_TABLEAU_REGLES] = {0, 0, 0, 1, 0, 0, 0, 0, 0}; + +SDL_Window *window; +int width = 700; +int height = 700; +SDL_Renderer *renderer; +SDL_Rect rect; +SDL_Event event; +int running = 1; +int tailleAffichage = 30; + +void initSDL2(){ + + SDL_Init(SDL_INIT_VIDEO); + + window = SDL_CreateWindow( + "Jeu de la vie : monde délimité", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + width, + height, + SDL_WINDOW_RESIZABLE + ); + + if(window == 0){ + fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); + } + + renderer = SDL_CreateRenderer( + window, + -1, + SDL_RENDERER_ACCELERATED + ); + + if (renderer == 0) { + fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); + SDL_DestroyRenderer(renderer); // Attention : on suppose que les NULL sont maintenus !! + renderer = NULL; + } + +} + +void destroySDL2(){ + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} + +void initMonde(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + int i = 0; + int j = 0; + + for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ + for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ + monde[i][j] = 0; + } + } +} + +void afficheMonde(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + int i = 0; + int j = 0; + + for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ + for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ + switch(monde[i][j]){ + case 0: + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0); + break; + case 1: + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + break; + default: + printf("Problème valeur tableau monde.\n"); + break; + } + rect.x = tailleAffichage*i; + rect.y = tailleAffichage*j; + rect.w = tailleAffichage-2; + rect.h = tailleAffichage-2; + SDL_RenderFillRect(renderer, &rect); + } + } +} + +void afficherEcran(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + + SDL_SetRenderDrawColor(renderer, 0, 16, 158, 0); + SDL_RenderClear(renderer); + + afficheMonde(monde); + + SDL_RenderPresent(renderer); +} + +void changeCellule(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE], int clic_x, int clic_y){ + int ligne = clic_y/tailleAffichage; + int colonne = clic_x/tailleAffichage; + if(0 == monde[colonne][ligne]){ + monde[colonne][ligne] = 1; + }else{ + monde[colonne][ligne] = 0; + } +} + +int nombreVoisinsVivants(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE], int cel_x, int cel_y){ + int nbrVoisinsVivants = 0; + if(cel_x==0 && cel_y==0){ + nbrVoisinsVivants = monde[cel_x+1][cel_y] + monde[cel_x][cel_y+1] + monde[cel_x+1][cel_y+1]; + } else if(cel_x==0 && cel_y==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x+1][cel_y] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1]; + }else if(cel_x==TAILLE_MONDE_DELIMITE-1 && cel_y==0 ){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x][cel_y+1] + monde[cel_x-1][cel_y+1]; + }else if(cel_x==TAILLE_MONDE_DELIMITE-1 && cel_y==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x][cel_y-1] + monde[cel_x-1][cel_y-1]; + }else if(cel_x==0){ + nbrVoisinsVivants = monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y] + monde[cel_x+1][cel_y+1] + monde[cel_x][cel_y+1]; + }else if(cel_x==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x][cel_y-1] + monde[cel_x-1][cel_y-1] + monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y+1] + monde[cel_x][cel_y+1]; + }else if(cel_y==0){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y+1] + monde[cel_x][cel_y+1] + monde[cel_x+1][cel_y+1] + monde[cel_x+1][cel_y]; + }else if(cel_y==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y-1] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y]; + }else{ + nbrVoisinsVivants = monde[cel_x-1][cel_y-1] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y] + + monde[cel_x+1][cel_y+1] + monde[cel_x][cel_y+1] + monde[cel_x-1][cel_y+1] + monde[cel_x-1][cel_y]; + } + return nbrVoisinsVivants; +} + +void reglesEvolutions(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + int i = 0; + int j = 0; + int nbrVoisinsVivants = 0; + for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ + for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ + nbrVoisinsVivants = nombreVoisinsVivants(monde, i, j); + if(monde[i][j]==1 && survie[nbrVoisinsVivants]==0){ + monde[i][j] = 0; + }else if(monde[i][j]==0 && naissance[nbrVoisinsVivants]==1){ + monde[i][j] = 1; + } + } + } +} + +int main() { + + int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]; + + initSDL2(); + + initMonde(monde); + + while (running) { + while (SDL_PollEvent(&event)){ + switch(event.type){ + case SDL_WINDOWEVENT: + printf("window event\n"); + switch (event.window.event){ + case SDL_WINDOWEVENT_CLOSE: + printf("appui sur la croix\n"); + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: + width = event.window.data1; + height = event.window.data2; + printf("Size : %d%d\n", width, height); + break; + } + break; + case SDL_MOUSEBUTTONDOWN: + printf("Appui :%d %d\n", event.button.x, event.button.y); + changeCellule(monde, event.button.x, event.button.y); + break; + case SDL_KEYDOWN: + switch(event.key.keysym.sym){ + case SDLK_RIGHT: + printf("goo calcul!\n"); + reglesEvolutions(monde); + break; + case SDLK_s: + printf("stop calcul...\n"); + break; + default: + printf("une touche est tapee\n"); + break; + } + break; + case SDL_QUIT : + printf("on quitte\n"); + running = 0; + break; + } + //parfois on dessine ici + afficherEcran(monde); + } + SDL_Delay(10); + } + destroySDL2(); + return 0; +} \ No newline at end of file diff --git a/travail_individuel/Meyer/jeu_de_la_vie/makefile b/travail_individuel/Meyer/jeu_de_la_vie/makefile new file mode 100644 index 0000000000000000000000000000000000000000..9e978d1309fe9b051df7c0c3bdd1e125a4aa5612 --- /dev/null +++ b/travail_individuel/Meyer/jeu_de_la_vie/makefile @@ -0,0 +1,11 @@ +CC=gcc + +main:main.o + $(CC) -o main main.o -lm -lSDL2 + @echo "=> Lancer le programme avec ./main" + +main.o:main.c + $(CC) -c main.c -g -Wall -Wextra + +clean: + rm -rf main main.o diff --git a/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json b/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000000000000000000000000000000000..4d5b55df0634dd59212f0d153967b2f7af421e22 --- /dev/null +++ b/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/SDL2" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json b/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..236a91b402122022af296ef7d154061e140897b5 --- /dev/null +++ b/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "sdl.h": "c" + } +} \ No newline at end of file diff --git a/travail_individuel/Meyer/pave_de_serpents/main b/travail_individuel/Meyer/pave_de_serpents/main index 0130edc9e30a444de3016821db2bb91eb1982ae1..5cae36a48590b4722e3e84b904b26e72c5ab66ad 100755 Binary files a/travail_individuel/Meyer/pave_de_serpents/main and b/travail_individuel/Meyer/pave_de_serpents/main differ diff --git a/travail_individuel/Meyer/pave_de_serpents/main.c b/travail_individuel/Meyer/pave_de_serpents/main.c index cbf26667f7b73b16bae5130e0243e23ecf9b2875..860bb73398edb294eee2e1acde40fd85f4bacf58 100644 --- a/travail_individuel/Meyer/pave_de_serpents/main.c +++ b/travail_individuel/Meyer/pave_de_serpents/main.c @@ -1,14 +1,14 @@ #include <SDL2/SDL.h> #include <stdio.h> #include <stdlib.h> +#include <math.h> +#define PI 3.141592654 int main(int argc, char **argv) { (void)argc; (void)argv; - int vainqueur = 0; - SDL_Window *window = NULL; @@ -25,8 +25,14 @@ int main(int argc, char **argv) SDL_Renderer *renderer; SDL_Rect rect; - int pale_1_x; - int pale_1_y; + int pale_1_x = 300; + int pale_1_y = 300; + + int pale_2_x = 700; + int pale_2_y = 300; + + int pale_3_x = 250; + int pale_3_y = 500; if (SDL_GetDesktopDisplayMode(0, &mode) != 0) { @@ -70,67 +76,86 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - // dessin - /* couleur de fond */ - SDL_SetRenderDrawColor(renderer, 157, 224, 144, 255); - SDL_RenderClear(renderer); - - //sol - SDL_SetRenderDrawColor(renderer, 49, 150, 29, 255); - rect.w = window_width; - rect.h = window_height/3; - rect.x = 0; - rect.y = window_height - window_height/3; - SDL_RenderFillRect(renderer, &rect); - - /* bloc du moulin */ - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - rect.w = 150; - rect.h = 250; - rect.x = (window_width - rect.w)/2; - rect.y = window_height - rect.h - 150; - SDL_RenderFillRect(renderer, &rect); - - /*triangle moulin*/ - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - rect.w = 150; - rect.h = 50; - rect.x = (window_width - rect.w)/2; - rect.y = window_height - rect.h - 400; - SDL_RenderFillRect(renderer, &rect); - - //pales du moulin - //pale 1 du moulin - pale_1_x = 50; - pale_1_y = 50; - SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); - SDL_RenderDrawLine(renderer, - (window_width - rect.w)/2, - window_height - rect.h - 400, - pale_1_x, - pale_1_y); - - - - /* afficher à l'ecran */ - SDL_RenderPresent(renderer); - - - - // départ de la course - /*while (vainqueur == 0) - { - - if (cercle.x > display_width) - { - vainqueur = 1; - } - }*/ - - // ecrire victoire - + int i = 20; + + while(i>0){ + + // dessin + /* couleur de fond */ + SDL_SetRenderDrawColor(renderer, 157, 224, 144, 255); + SDL_RenderClear(renderer); + + //sol + SDL_SetRenderDrawColor(renderer, 49, 150, 29, 255); + rect.w = window_width; + rect.h = window_height/3; + rect.x = 0; + rect.y = window_height - window_height/3; + SDL_RenderFillRect(renderer, &rect); + + /* bloc du moulin */ + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + rect.w = 150; + rect.h = 250; + rect.x = (window_width - rect.w)/2; + rect.y = window_height - rect.h - 150; + SDL_RenderFillRect(renderer, &rect); + + /*triangle moulin*/ + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + rect.w = 150; + rect.h = 50; + rect.x = (window_width - rect.w)/2; + rect.y = window_height - rect.h - 400; + SDL_RenderFillRect(renderer, &rect); + + //pales du moulin + //pale 1 du moulin + int theta = 40; + int px = window_width/2; + int py = window_height - rect.h - 375; + float c = cos(theta), s = sin(theta); + float dx = pale_1_x - px, dy = pale_1_y - py; + pale_1_x = pale_1_x + c * dx - s * dy; + pale_1_y = pale_1_y + s * dx + c * dy; + + SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); + SDL_RenderDrawLine(renderer, + px, + py, + pale_1_x, + pale_1_y); + + //pale 2 du moulin + dx = pale_2_x - px, dy = pale_2_y - py; + pale_2_x = pale_2_x + c * dx - s * dy; + pale_2_y = pale_2_y + s * dx + c * dy; + SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); + SDL_RenderDrawLine(renderer, + px, + py, + pale_2_x, + pale_2_y); + + //pale 3 du moulin + dx = pale_3_x - px, dy = pale_3_y - py; + pale_3_x = pale_3_x + c * dx - s * dy; + pale_3_y = pale_3_y + s * dx + c * dy; + SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); + SDL_RenderDrawLine(renderer, + px, + py, + pale_3_x, + pale_3_y); + + /* afficher à l'ecran */ + SDL_RenderPresent(renderer); + SDL_Delay(1000); + SDL_RenderClear(renderer); + i = i - 2; + } - SDL_Delay(2000); + SDL_DestroyRenderer(renderer); // fermer fenetre SDL_DestroyWindow(window); // la fenêtre diff --git a/travail_individuel/Meyer/pave_de_serpents/moulin_a_serpents.gif b/travail_individuel/Meyer/pave_de_serpents/moulin_a_serpents.gif new file mode 100644 index 0000000000000000000000000000000000000000..dfff5e4b015785619ebb0494c9039690a727023b Binary files /dev/null and b/travail_individuel/Meyer/pave_de_serpents/moulin_a_serpents.gif differ diff --git a/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json b/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000000000000000000000000000000000..4d5b55df0634dd59212f0d153967b2f7af421e22 --- /dev/null +++ b/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/SDL2" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/travail_individuel/Meyer/x_fenetre/.vscode/settings.json b/travail_individuel/Meyer/x_fenetre/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..088a016fe65a59c2cfcbb56823ba698a0d776631 --- /dev/null +++ b/travail_individuel/Meyer/x_fenetre/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "main.C": "cpp", + "cmath": "cpp" + } +} \ No newline at end of file diff --git a/travail_individuel/Meyer/x_fenetre/x_fenetre_rocket.gif b/travail_individuel/Meyer/x_fenetre/x_fenetre_rocket.gif new file mode 100644 index 0000000000000000000000000000000000000000..71985403f68d26b557688643f36554010083a4d3 Binary files /dev/null and b/travail_individuel/Meyer/x_fenetre/x_fenetre_rocket.gif differ