Skip to content
Snippets Groups Projects
Commit af14b6c4 authored by Réthelyi Bálint's avatar Réthelyi Bálint :no_mouth:
Browse files

Merge branch 'blint' into 'master'

Blint

See merge request blintmester/nhf!7
parents 632018d2 8c956dfb
No related branches found
No related tags found
1 merge request!7Blint
Showing
with 609 additions and 103 deletions
......@@ -3,7 +3,7 @@ project(nhf C)
set(CMAKE_C_STANDARD 99)
add_executable(nhf main.c view.c view.h move.c move.h files.c files.h)
add_executable(nhf main.c view.c view.h move.c move.h files.c files.h debugmalloc-impl.h debugmalloc.h)
target_link_libraries(nhf m)
......
# Super Mario szerű játék
# InfoC bagollyal
## Programozói dokumentáció
Réthelyi Bálint
IZUM0B
## _view.c_, _view.h_:
Adatszerkezet _(view.h):_
```c
typedef struct Button{
int x, y, w, h;
char title[50+1];
bool over, click;
}Button;
```
A gombokat kezelem vele.
```c
typedef struct Color{
int R, G, B;
}Color;
```
A színek kezelését oldom meg vele.
```c
typedef enum{
ground,
platform,
item
}Type;
```
A generálandó objektumok típusát kezelem ezzel.
```c
typedef struct Palyaelem{
int x, y;
int hossz, magassag;
Color color;
Type type;
struct Palyaelem *kov;
}Palyaelem;
```
Úgynevezett pályaelemek struktúráját hozom létre,
amivel láncoltlistában tárolom az objektumokat a kódomban.
```c
typedef struct Player{
char name[50+1];
int score;
struct Player *kov;
}Player;
```
A játékosokat kezelem, ezt is láncoltlistában tárolva.
```c
extern int H;
extern int W;
extern int owl;
extern SDL_Window *window;
extern SDL_Renderer *renderer;
extern TTF_Font *font;
```
Globális változókban tárolom a megjelenített ablak méreteit,
valamint a bagoly méretét és az SDL könyvtár alap változóit,
valamint a felhasznált betütípusra mutató pointert.
függvények:
```c
void sdl_init(int szeles, int magas);
```
A függvény az InfoC oldalról származó sdl_init függvény módosítása.
```c
SDL_Texture *kep_betolt(char *filename)
{
SDL_Texture *kep = IMG_LoadTexture(renderer, filename);
if (kep == NULL)
{
SDL_Log("Nem nyithato meg a kepfajl: %s", IMG_GetError());
exit(1);
}
return kep;
}
```
A függvény segítségével betöltök egy képet a későbbi megjelenítéshez.
```c
void kirajzol(SDL_Texture *kep, int x, int y) {
SDL_Rect src = {0, 0, 128, 128};
SDL_Rect dest = {x, y, owl, owl};
SDL_RenderCopy(renderer, kep, &src, &dest);
}
```
A korábban betöltött kép adott `x y` koordinátára kirajzolása.
```c
int my_rand(int min, int max)
{
return (rand() % (max - min) + min);
}
```
Mivel a kódomban sokszor kellett random generálnom értékeket,
ezért létrehoztam egy saját függvényt a könnyebb átláthatóság miatt.
```c
Palyaelem *gen_elem(Palyaelem *elem, Type type){}
```
A függvény legenerál egy adott típusú pályaelem egy elemét.
```c
Palyaelem *gen_item(Palyaelem *platforms, Type type){}
```
A függvény generál _item_-eket a korábban legenerált _platform_-okra.
```c
Palyaelem* palya_gen(Palyaelem *eleje, Type type, int hany_elem){}
```
Adott pályaelemet generál.
```c
void palyaelem_kirajzolo(Palyaelem *elem){}
```
Kirajzol egy pályaelemet.
```c
void palyakirajzol(Palyaelem *elem){}
```
Végigmegy egy listán és kirajzolja az elemeit.
```c
void draw_button(Button button, Color button_color, Color text_color){}
```
Kirajzol egy paraméterként kapott gombot, adott színekkel.
```c
void draw_score_board(Player *players, SDL_Event ev, Menu *gamestate, Button *back){}
```
Kirajzolja a dicsőség táblát, valamint figyeli a _vissza_ gombot.
```c
int last_stand(Palyaelem *elem){}
```
A függvény az adott lista legutolsó elemének 'x' koordinátájával tér vissza.
```c
bool input_text(char *dest, size_t hossz, SDL_Rect teglalap,
SDL_Color hatter, SDL_Color szoveg){}
```
Az InfoC oldalon megtalálható függvény. Forrásban megjelölve.
## _move.c, move.h:_
Adatszerkezet _(move.h):_
```c
double v = 2;
double vj = 8;
double vx = 0, vy = 0;
double g = 0.3; //saját gravitáció, mivel a 9.82 túl nehéz lenne... :'(
int jump_t = 0;
int score = 0;
int max_score = 0;
int startx = 0;
int endx = 0;
```
Olyan változók, melyek a mozgást hivatottak kezelni, ezért fontos, hogy
minden függvény elérje őket.
Függvénynek (_move.c_):
```c
static bool move(SDL_Event ev, int *x, const int *y){}
```
Belső függvény, mely az irányítást kezeli,
_előre, hátra, ugrás/esés_, visszatérési értéke a mozgás állapotától függ.
```c
void keywatcher(SDL_Event ev, int x, int y, Menu *gamestate){}
```
A függvény a kilépést figyeli (_ESC_ billentyű nyomásra kilép)
```c
Palyaelem *palyamozgat(Palyaelem *eleje, int x){}
```
Egy adott pályaelem-listát mozgat.
```c
static void ontheplatform(SDL_Event ev, Palyaelem *platf, int *x, int *y){}
```
Vizsgálom, hogy a játékos rajta van-e valamelyik pályaelemen (_platformon_)
és ha rajta van, akkor nem engedi hogy áteshessen rajta.
```c
static void ontheground(SDL_Event ev, Palyaelem *ground_d, int *x, int *y){}
```
A függvény a előzőhöz hasonlóan vizsgálja, hogy a játékos a talajon van-e.
```c
static void eattheitems(SDL_Event ev, Palyaelem **itemke, int x, int y){}
```
A függvény az előzőhöz hasonló, vizsgálja, hogy a játékos összeszedte-e
az adott _tárgyat_, és kitörli azt a láncolt listából, ha igen.
```c
static void onthecorners(int *x, int *y, Palyaelem *ground_d,
Palyaelem *platf, Palyaelem *itemke){}
```
A függvény vizsgálja hogy a karakter elérte-e a pálya szélét.
```c
static void movelogic(SDL_Event ev, int *x, int *y){}
```
Ha a karakter mozog, akkor a koordinátáit növeli a _move_ függvényben generált sebességekkel.
```c
void move_draw(SDL_Event ev, SDL_Texture *kep, int *x, int *y,
Palyaelem *ground_d,Palyaelem *platf, Palyaelem **itemke, Menu *gamestate, Player **players, char *username){}
```
A függvény meghívja a mozgás-függvényeket, és kezeli a játék végét/halált.
```c
Button clickwatcher(SDL_Event ev, Button button){}
```
A függvény figyeli, hogy egy gomb felett van-e az egér, vagy, ha rákattintanak.
A változást visszatérési értékként adja vissza.
```c
void menu(SDL_Event ev, Button *start, Button *load, Button *scoreboard, Button *close, Menu *gamestate){}
```
Kirajzolja a menüt és figyeli a menü gombjait.
## _files.c, files.h:_
Függvények (_files.c_):
```c
Player *beolvas(FILE *fp, Player *lista){}
```
Beolvassa egy fájlból az adatokat, és beírja egy listába, mellyel visszatér.
```c
Player *hozzafuz(Player *lista, Player *elem){}
```
Hozzáfűz egy elemet a listához (az elejére)
```c
Player *osszefuz(Player *egyik, Player *masik){}
```
Összefűz két listát.
```c
Player *rendez(Player *lista){}
```
A függvény rendezi a listát. A függvény nem saját, forrásban jelölöm.
```c
Player *egy_elem(char *name, int score_point){}
```
Létrehoz egy _Player_ típusú elemet, és feltölti az adatait.
```c
void lista_filebair(Player *lista, FILE *fp){}
```
Egy listát beír egy fájlba.
```c
Player *open_file_to_read(char *filename){}
```
Olvasásra nyit meg egy fájlt és visszatér egy listával, amit beolvasott és rendezett.
```c
void open_file_to_write(char *filename, Player *players){}
```
A függvény megnyit egy fájlt és beleírja a _players_ lista elemeit.
## _main.c:_
Függvények:
```c
void freeing(Palyaelem *elem)
{
Palyaelem *mozgo = elem;
while (mozgo != NULL)
{
Palyaelem *kov = mozgo->kov;
free(mozgo);
mozgo = kov;
}
}
```
```c
void freeing_player(Player *elem)
{
Player *mozgo = elem;
while (mozgo != NULL)
{
Player *kov = mozgo->kov;
free(mozgo);
mozgo = kov;
}
}
```
A függvényekkel szabadítom fel a kódban használt listákat.
```c
Uint32 idozit(Uint32 ms, void *param) {
SDL_Event ev;
ev.type = SDL_USEREVENT;
SDL_PushEvent(&ev);
return ms; /* ujabb varakozas */
}
```
Az SDL időzítéséhez használt függvény.
```c
int main(int argc, char *argv[]) {
.
.
.
}
```
A main függvényben beállítom az _SDL_-t, hogy a megfelelő megjelenítést biztosítsa
valamint a játék-változókat alaphelyzetbe állítom.
Legenerálom a pályát (pálya _alap_-ja, _pálya elemek_ és az összegyűjtendő _tárgyakat_).
Ezek után legenerálom a képernyő függvényében a képernyőn megjelenő gombokat:
_start, load, scoreboard, close, back, scoretitle_
Majd elindítom a játék ciklust (_game loop_), ahol a korábbi függvények segítségével
kezelem a játékmenetet.
\ No newline at end of file
......@@ -6,6 +6,32 @@
#IncludeRegexTransform:
/home/blint/projects/nhf/debugmalloc-impl.h
stdbool.h
-
stddef.h
-
stdlib.h
-
stdio.h
-
ctype.h
-
string.h
-
stdarg.h
-
process.h
-
unistd.h
-
/home/blint/projects/nhf/debugmalloc.h
stdbool.h
-
debugmalloc-impl.h
/home/blint/projects/nhf/debugmalloc-impl.h
/home/blint/projects/nhf/files.h
view.h
/home/blint/projects/nhf/view.h
......@@ -13,6 +39,8 @@ stdlib.h
-
stdio.h
-
debugmalloc.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/main.c
view.h
......@@ -23,10 +51,46 @@ files.h
/home/blint/projects/nhf/files.h
time.h
-
debugmalloc.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/move.c
move.h
/home/blint/projects/nhf/move.h
view.h
/home/blint/projects/nhf/view.h
files.h
/home/blint/projects/nhf/files.h
debugmalloc.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/move.h
view.h
/home/blint/projects/nhf/view.h
files.h
/home/blint/projects/nhf/files.h
debugmalloc.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/view.c
view.h
/home/blint/projects/nhf/view.h
move.h
/home/blint/projects/nhf/move.h
SDL2/SDL.h
-
SDL2/SDL_image.h
-
SDL2/SDL2_gfxPrimitives.h
-
SDL2/SDL_ttf.h
-
stdlib.h
-
stdio.h
-
debugmalloc.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/view.h
SDL2/SDL.h
......@@ -43,4 +107,6 @@ SDL2/SDL_ttf.h
-
stdbool.h
-
debugmalloc.h
/home/blint/projects/nhf/debugmalloc.h
......@@ -2,20 +2,27 @@
# Generated by "Unix Makefiles" Generator, CMake Version 3.15
CMakeFiles/nhf.dir/files.c.o
/home/blint/projects/nhf/debugmalloc-impl.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/files.c
/home/blint/projects/nhf/files.h
/home/blint/projects/nhf/move.h
/home/blint/projects/nhf/view.h
CMakeFiles/nhf.dir/main.c.o
/home/blint/projects/nhf/debugmalloc-impl.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/files.h
/home/blint/projects/nhf/main.c
/home/blint/projects/nhf/move.h
/home/blint/projects/nhf/view.h
CMakeFiles/nhf.dir/move.c.o
/home/blint/projects/nhf/debugmalloc-impl.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/files.h
/home/blint/projects/nhf/move.c
/home/blint/projects/nhf/move.h
/home/blint/projects/nhf/view.h
CMakeFiles/nhf.dir/view.c.o
/home/blint/projects/nhf/debugmalloc-impl.h
/home/blint/projects/nhf/debugmalloc.h
/home/blint/projects/nhf/files.h
/home/blint/projects/nhf/move.h
/home/blint/projects/nhf/view.c
......
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.15
CMakeFiles/nhf.dir/files.c.o: ../debugmalloc-impl.h
CMakeFiles/nhf.dir/files.c.o: ../debugmalloc.h
CMakeFiles/nhf.dir/files.c.o: ../files.c
CMakeFiles/nhf.dir/files.c.o: ../files.h
CMakeFiles/nhf.dir/files.c.o: ../move.h
CMakeFiles/nhf.dir/files.c.o: ../view.h
CMakeFiles/nhf.dir/main.c.o: ../debugmalloc-impl.h
CMakeFiles/nhf.dir/main.c.o: ../debugmalloc.h
CMakeFiles/nhf.dir/main.c.o: ../files.h
CMakeFiles/nhf.dir/main.c.o: ../main.c
CMakeFiles/nhf.dir/main.c.o: ../move.h
CMakeFiles/nhf.dir/main.c.o: ../view.h
CMakeFiles/nhf.dir/move.c.o: ../debugmalloc-impl.h
CMakeFiles/nhf.dir/move.c.o: ../debugmalloc.h
CMakeFiles/nhf.dir/move.c.o: ../files.h
CMakeFiles/nhf.dir/move.c.o: ../move.c
CMakeFiles/nhf.dir/move.c.o: ../move.h
CMakeFiles/nhf.dir/move.c.o: ../view.h
CMakeFiles/nhf.dir/view.c.o: ../debugmalloc-impl.h
CMakeFiles/nhf.dir/view.c.o: ../debugmalloc.h
CMakeFiles/nhf.dir/view.c.o: ../files.h
CMakeFiles/nhf.dir/view.c.o: ../move.h
CMakeFiles/nhf.dir/view.c.o: ../view.c
......
No preview for this file type
No preview for this file type
No preview for this file type
Blint Mester | 2
azt mondta próbáljam ki | 9140
Mike was here | 8888
ydfhfgsh| 8348
ggg| 8347
fapapucs | 8289
Jani :D | 7777
aa| 7716
unknown Player | 6666
Cirkos Cica hajj| 2924
Gajdos 4 president| 2874
random string| 2387
b| 1519
bb| 1519
vjodka| 1288
alma| 1273
Janos Pannonius Maximus Superbolicus| 1266
c| 1184
asd| 583
ddd| 579
a| 504
f*ck the world| 438
új játék| 438
még újaabbb| 438
a| 395
aeálgkmkámmklás| 395
sfgjgfkuigléoi| 262
a| 220
krumpli| 200
fuck that shit | 192
asd| 189
asd| 179
körte| 179
asdasd| 154
yssfg| 154
alma| 153
próba elemér | 141
aaa| 139
alma| 136
ődflksdfb | 130
hello world | 129
Player1 a | 12
unknown Player | 25023
Mike was here | 999999
Jani :D | 999997
egy rakás szar vagyok | 88321
\ No newline at end of file
Blint Mester | 2
Próba János 42| 1805
No preview for this file type
......@@ -80,6 +80,12 @@
</MakeCommands>
</Target>
</Build>
<Unit filename="/home/blint/projects/nhf/debugmalloc-impl.h">
<Option target="nhf"/>
</Unit>
<Unit filename="/home/blint/projects/nhf/debugmalloc.h">
<Option target="nhf"/>
</Unit>
<Unit filename="/home/blint/projects/nhf/files.c">
<Option target="nhf"/>
</Unit>
......
......@@ -2,11 +2,9 @@
// Created by blint on 2019. 11. 30..
//
#include "files.h"
#include "view.h"
#include "move.h"
#include <stdlib.h>
#include <stdio.h>
#include "debugmalloc.h"
Player *beolvas(FILE *fp, Player *lista)
{
......@@ -19,8 +17,6 @@ Player *beolvas(FILE *fp, Player *lista)
lista = uj;
}
Player *mozgo;
for (mozgo = lista; mozgo->kov != NULL; mozgo = mozgo->kov);
......@@ -94,14 +90,14 @@ Player *egy_elem(char *name, int score_point)
return elem;
}
void listakiir(Player *lista)
void lista_filebair(Player *lista, FILE *fp)
{
if (lista == NULL)
return;
Player *mozgo;
for (mozgo = lista; mozgo != NULL; mozgo = mozgo->kov)
{
printf("%s %d\n", mozgo->name, mozgo->score);
fprintf(fp,"%s| %d\n", mozgo->name, mozgo->score);
}
}
......@@ -118,3 +114,11 @@ Player *open_file_to_read(char *filename)
fclose(fp);
return elem;
}
void open_file_to_write(char *filename, Player *players)
{
FILE *fp;
fp = fopen(filename, "wt");
lista_filebair(players, fp);
fclose(fp);
}
\ No newline at end of file
......@@ -8,8 +8,13 @@
#include "view.h"
#include <stdlib.h>
#include <stdio.h>
#include "debugmalloc.h"
Player *open_file_to_read(char *filename);
void open_file_to_write(char *filename, Player *players);
Player *egy_elem(char *name, int score_point);
#endif //NHF_FILES_H
......@@ -2,6 +2,7 @@
#include "move.h"
#include "files.h"
#include <time.h>
#include "debugmalloc.h"
void freeing(Palyaelem *elem)
{
......@@ -14,6 +15,17 @@ void freeing(Palyaelem *elem)
}
}
void freeing_player(Player *elem)
{
Player *mozgo = elem;
while (mozgo != NULL)
{
Player *kov = mozgo->kov;
free(mozgo);
mozgo = kov;
}
}
Uint32 idozit(Uint32 ms, void *param) {
SDL_Event ev;
ev.type = SDL_USEREVENT;
......@@ -33,21 +45,16 @@ int main(int argc, char *argv[]) {
SDL_RenderPresent(renderer);
bool quit = false;
bool moving = false;
bool starting = false;
bool loading = false;
bool scoring = false;
int elozo_x = 0;
int elozo_y = W - 2*owl;
Palyaelem *platf = NULL, *itemke = NULL;
Palyaelem *platf = NULL, *itemke = NULL, *ground_d = NULL;
ground_d = palya_gen(ground_d, ground,50);
platf = palya_gen(platf, platform,50);
itemke = gen_item(platf, item);
endx = last_stand(platf) - 300;
endx = last_stand(platf);
Button start;
......@@ -86,6 +93,14 @@ int main(int argc, char *argv[]) {
close.over = false;
close.click = false;
Button back;
strcpy(back.title, "Visszamegyek, mert ez uncsi");
back.x = W/6;
back.w = W - (2*W/6);
back.y = H/9 * 5 + (H/18 * (5-1));
back.h = H/9;
back.click = false;
Button scoretitle;
scoretitle.x = 0;
scoretitle.y = 0;
......@@ -96,44 +111,48 @@ int main(int argc, char *argv[]) {
Color white = (Color){255, 255, 255};
char username[50+1];
Player *players = open_file_to_read("asd.txt");
while (!quit) {
Menu gamestate = on_menu;
while (gamestate != quit) {
SDL_Event ev;
SDL_WaitEvent(&ev);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
palyakirajzol(ground_d);
palyakirajzol(platf);
keywatcher(ev, bagoly, elozo_x, elozo_y, &quit, &starting);
moving = starting;
keywatcher(ev, elozo_x, elozo_y, &gamestate);
if (moving){
if (gamestate == on_game){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
palyakirajzol(ground_d);
palyakirajzol(platf);
palyakirajzol(itemke);
move_draw(ev, bagoly, &elozo_x, &elozo_y, platf, &itemke);
kirajzol(bagoly, elozo_x, elozo_y);
move_draw(ev, bagoly, &elozo_x, &elozo_y, ground_d, platf, &itemke, &gamestate, &players, username);
sprintf(scoretitle.title, "Score: %6d", max_score);
draw_button(scoretitle, green, white);
}
if (!scoring && !moving && !starting)
if (gamestate == on_menu) //draw menu
{
menu(ev, &start, &load, &scoreboard, &close, &starting, &loading, &scoring, &quit);
menu(ev, &start, &load, &scoreboard, &close, &gamestate);
}
if (!moving && !starting && scoring)
if (gamestate == on_score) //draw scoreboard
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
draw_score_board(players, ev, &scoring);
draw_score_board(players, ev, &gamestate, &back);
}
if (!moving && starting)
if (gamestate == on_start) //ask for username
{
char username[50+1];
SDL_Rect r;
r.x = W/6;
r.y = (2*H/9 + H/18);
......@@ -142,16 +161,18 @@ int main(int argc, char *argv[]) {
SDL_Color text = {255, 255, 255, 255};
SDL_Color bg = {0, 0, 0, 255};
input_text(username, 50+1, r, bg, text);
printf("%s\n", username);
moving = true;
//printf("%s\n", username);
gamestate = on_game;
}
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(bagoly);
freeing(platf);
freeing(ground_d);
freeing(itemke);
freeing_player(players);
SDL_RemoveTimer(timer_id);
SDL_Quit();
return 0;
......
......@@ -4,7 +4,8 @@
#include "move.h"
#include "view.h"
#include "files.h"
#include "debugmalloc.h"
double v = 2;
double vj = 8;
......@@ -78,26 +79,26 @@ static bool move(SDL_Event ev, int *x, const int *y)
}
void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *starting)
void keywatcher(SDL_Event ev, int x, int y, Menu *gamestate)
{
switch (ev.type) {
case SDL_KEYDOWN:
switch (ev.key.keysym.sym) {
case SDLK_ESCAPE:
*quit = true;
*gamestate = quit;
break;
}
case SDL_KEYUP:
switch (ev.key.keysym.sym) {
case SDLK_ESCAPE:
*quit = true;
*gamestate = quit;
break;
}
break;
case SDL_QUIT:
*quit = true;
*gamestate = quit;
break;
}
}
......@@ -162,7 +163,9 @@ static void eattheitems(SDL_Event ev, Palyaelem **itemke, int x, int y)
{
max_score += 50;
lemarado->kov = mozgo->kov;
lemarado = mozgo->kov;
free(mozgo);
mozgo = lemarado;
}
}
......@@ -172,22 +175,37 @@ static void eattheitems(SDL_Event ev, Palyaelem **itemke, int x, int y)
}
}
static void ontheground(int *x, int *y)
static void ontheground(SDL_Event ev, Palyaelem *ground_d, int *x, int *y)
{
Palyaelem *mozgo;
for (mozgo = ground_d; mozgo != NULL; mozgo = mozgo->kov)
{
if ((*x <= mozgo->x && (*x + owl) > (mozgo->x) ) || (*x >= mozgo->x && *x <= (mozgo->x + mozgo->hossz)))
{
if (*y >= (H - 2*owl))
if (*y >= (mozgo->y - owl))
{
*y = H - 2*owl;
*y = mozgo->y - owl;
vy = 0;
jump_t = 0;
if (ev.key.keysym.sym == SDLK_w || ev.key.keysym.sym == SDLK_UP || ev.key.keysym.sym == SDLK_SPACE)
{
jump_t++;
}
}
}
}
}
static void onthecorners(int *x, int *y, Palyaelem *platf, Palyaelem *itemke)
static void onthecorners(int *x, int *y, Palyaelem *ground_d,Palyaelem *platf, Palyaelem *itemke)
{
//pálya bal szélénél eltolja a koordinátákat
if (*x > (W - 5*owl))
{
*x = W - 5*owl;
if (endx >= 0) {
palyamozgat(ground_d, (int) v);
palyamozgat(platf, (int) v);
palyamozgat(itemke, (int) v);
startx += (int) v;
......@@ -219,22 +237,35 @@ static void movelogic(SDL_Event ev, int *x, int *y)
}
}
void move_draw(SDL_Event ev, SDL_Texture *kep, int *x, int *y, Palyaelem *platf, Palyaelem **itemke)
void move_draw(SDL_Event ev, SDL_Texture *kep, int *x, int *y, Palyaelem *ground_d,Palyaelem *platf, Palyaelem **itemke, Menu *gamestate, Player **players, char *username)
{
//call move logic
movelogic(ev, x, y);
//platformokon ott marad
ontheplatform(ev, platf, x, y);
//ontheplatform(ev, ground_d, x, y);
//megeszi az itemeket
eattheitems(ev, itemke, *x, *y);
//talajszintnél lejjebb nem ugrik
ontheground(x, y);
ontheground(ev, ground_d, x, y);
//a szeleknel eltolja a koordinatakat
onthecorners(x, y, platf, *itemke);
onthecorners(x, y, ground_d, platf, *itemke);
if ((*y >= H) || (*x >= endx + 650))
{
*gamestate = on_score;
Player *mozgo;
for (mozgo = *players; mozgo->kov != NULL; mozgo = mozgo->kov);
Player *uj = egy_elem(username, max_score);
mozgo->kov = uj;
open_file_to_write("asd.txt", *players);
*x = 0;
*y = 0;
}
kirajzol(kep, *x, *y);
}
......@@ -268,7 +299,7 @@ Button clickwatcher(SDL_Event ev, Button button)
return button;
}
void menu(SDL_Event ev, Button *start, Button *load, Button *scoreboard, Button *close, bool *moving, bool *loading, bool *scoring, bool *quit)
void menu(SDL_Event ev, Button *start, Button *load, Button *scoreboard, Button *close, Menu *gamestate)
{
Color red = (Color){255, 0, 0};
Color green = (Color){0, 255, 0};
......@@ -301,25 +332,27 @@ void menu(SDL_Event ev, Button *start, Button *load, Button *scoreboard, Button
draw_button(*close, red, white);
if (start->click)
*moving = true;
else
*moving = false;
if (start->click){
*gamestate = on_start;
start->click = false;
}
if (load->click)
*loading = true;
else
*loading = false;
{
*gamestate = on_load;
load->click = false;
}
if (scoreboard->click)
*scoring = true;
else
// *scoring = false;
{}
{
*gamestate = on_score;
scoreboard->click = false;
}
if (close->click)
*quit = true;
else
//*quit = false;
{}
{
*gamestate = quit;
close->click = false;
}
}
\ No newline at end of file
......@@ -6,6 +6,8 @@
#define NHF_MOVE_H
#include "view.h"
#include "files.h"
#include "debugmalloc.h"
extern double v;
extern double vx;
......@@ -16,14 +18,14 @@ extern int max_score;
extern int startx;
extern int endx;
void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *starting);
void keywatcher(SDL_Event ev, int x, int y, Menu *gamestate);
void move_draw(SDL_Event ev, SDL_Texture *kep, int *x, int *y, Palyaelem *platf, Palyaelem **itemke);
void move_draw(SDL_Event ev, SDL_Texture *kep, int *x, int *y, Palyaelem *ground_d,Palyaelem *platf, Palyaelem **itemke, Menu *gamestate, Player **players, char *username);
Palyaelem *palyamozgat(Palyaelem *eleje, int x);
Button clickwatcher(SDL_Event ev, Button button);
void menu(SDL_Event ev, Button *start, Button *load, Button *scoreboard, Button *close, bool *moving, bool *loading, bool *scoring, bool *quit);
void menu(SDL_Event ev, Button *start, Button *load, Button *scoreboard, Button *close, Menu *gamestate);
#endif //NHF_MOVE_H
\ No newline at end of file
......@@ -3,7 +3,6 @@
//
#include "view.h"
#include "files.h"
#include "move.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
......@@ -11,6 +10,7 @@
#include <SDL2/SDL_ttf.h>
#include <stdlib.h>
#include <stdio.h>
#include "debugmalloc.h"
int H = 600;
int W = 900;
......@@ -65,6 +65,11 @@ void kirajzol(SDL_Texture *kep, int x, int y) {
SDL_RenderCopy(renderer, kep, &src, &dest);
}
int my_rand(int min, int max)
{
return (rand() % (max - min) + min);
}
Palyaelem *gen_elem(Palyaelem *elem, Type type)
{
Palyaelem *uj = (Palyaelem *) malloc(sizeof(Palyaelem));
......@@ -75,8 +80,10 @@ Palyaelem *gen_elem(Palyaelem *elem, Type type)
switch (type){
case ground:
{
uj->x = 0;
uj->y = H - owl;
uj->hossz = W;
uj->magassag = owl;
uj->color.R = 101;
uj->color.G = 67;
uj->color.B = 33;
......@@ -114,6 +121,15 @@ Palyaelem *gen_elem(Palyaelem *elem, Type type)
switch (type){
case ground:
{
uj->hossz = my_rand((W/2), W);
int xmin = mozgo->x + mozgo->hossz + owl;
int xmax = mozgo->x + mozgo->hossz + owl*5;
uj->x = my_rand(xmin, xmax);
uj->y = H - owl;
uj->magassag = owl;
uj->color.R = 101;
uj->color.G = 67;
uj->color.B = 33;
......@@ -125,11 +141,12 @@ Palyaelem *gen_elem(Palyaelem *elem, Type type)
int xmin = mozgo->x + mozgo->hossz + owl;
int xmax = mozgo->x + mozgo->hossz + owl*3;
uj->x = rand() % (xmax - xmin) + xmin;
uj->x = my_rand(xmin, xmax);
int ymin = (mozgo->y - owl) < (H - 3*owl) ? (mozgo->y - owl) : (H - 3*owl);
int ymax = mozgo->magassag + (4*owl);
uj->y = rand() % (ymax - ymin) + ymin;
uj->y = my_rand(ymin, ymax);
if (uj->y >= (H - owl))
uj->y -= (3*owl);
uj->magassag = 20;
......@@ -169,7 +186,7 @@ Palyaelem *gen_item(Palyaelem *platforms, Type type)
mozgo_i->hossz = owl/2;
mozgo_i->magassag = owl/2;
mozgo_i->type = item;
mozgo_i->color = (Color){255, 0, 0};
mozgo_i->color = (Color){255, 255, 0};
mozgo_i->kov = (Palyaelem *) malloc(sizeof(Palyaelem));
mozgo_i = mozgo_i->kov;
......@@ -201,20 +218,9 @@ Palyaelem* palya_gen(Palyaelem *eleje, Type type, int hany_elem)
void palyakirajzol(Palyaelem *elem)
{
Palyaelem alap;
alap.x = 0;
alap.y = 550;
alap.hossz = 900;
alap.magassag = 50;
alap.color.R = 101;
alap.color.G = 67;
alap.color.B = 33;
palyaelem_kirajzolo(&alap);
Palyaelem * mozgo;
for (mozgo = elem; mozgo != NULL; mozgo = mozgo->kov)
palyaelem_kirajzolo(mozgo);
}
void draw_button(Button button, Color button_color, Color text_color)
......@@ -247,7 +253,7 @@ int last_stand(Palyaelem *elem)
return mozgo->x;
}
void draw_score_board(Player *players, SDL_Event ev, bool *scoring)
void draw_score_board(Player *players, SDL_Event ev, Menu *gamestate, Button *back)
{
int i = 1;
Player *mozgo = players;
......@@ -276,21 +282,16 @@ void draw_score_board(Player *players, SDL_Event ev, bool *scoring)
mozgo = mozgo->kov;
}
strcpy(button.title, "Visszamegyek, mert ez uncsi");
button.x = W/6;
button.w = W - (2*W/6);
button.y = last_y * i + (H/18 * (i-1));
button.h = H/9;
button.click = false;
*back = clickwatcher(ev, *back);
draw_button(button, red, white);
button = clickwatcher(ev, button);
if (button.click)
*scoring = false;
if (back->over)
draw_button(*back, green, white);
else
*scoring = true;
draw_button(*back, red, white);
if (back->click) {
*gamestate = on_menu;
}
}
bool input_text(char *dest, size_t hossz, SDL_Rect teglalap, SDL_Color hatter, SDL_Color szoveg) {
......
......@@ -12,6 +12,7 @@
#include <stdio.h>
#include <SDL2/SDL_ttf.h>
#include <stdbool.h>
#include "debugmalloc.h"
typedef enum{
ground,
......@@ -19,6 +20,15 @@ typedef enum{
item
}Type;
typedef enum {
on_menu,
on_start,
on_game,
on_load,
on_score,
quit
} Menu;
typedef struct Color{
int R, G, B;
}Color;
......@@ -71,7 +81,7 @@ Palyaelem *gen_item(Palyaelem *platforms, Type type);
int last_stand(Palyaelem *elem);
void draw_score_board(Player *players, SDL_Event ev, bool *scoring);
void draw_score_board(Player *players, SDL_Event ev, Menu *gamestate, Button *back);
bool input_text(char *dest, size_t hossz, SDL_Rect teglalap, SDL_Color hatter, SDL_Color szoveg);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment