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

some f*cking awsome idea for object generating and drawing, but still nothing...

some f*cking awsome idea for object generating and drawing, but still nothing on the screen. move work, without jump :(
parent 1fee21d1
Branches
No related tags found
1 merge request!4Blint
...@@ -6,16 +6,18 @@ ...@@ -6,16 +6,18 @@
#IncludeRegexTransform: #IncludeRegexTransform:
/home/blint/projects/nhf/move.c /home/blint/projects/nhf/main.c
move.h
/home/blint/projects/nhf/move.h
view.h view.h
/home/blint/projects/nhf/view.h /home/blint/projects/nhf/view.h
SDL.h move.h
/home/blint/projects/nhf/move.h
SDL2/SDL_image.h
-
SDL2/SDL2_gfxPrimitives.h
- -
stdbool.h stdbool.h
- -
math.h time.h
- -
/home/blint/projects/nhf/move.h /home/blint/projects/nhf/move.h
......
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -17,17 +17,44 @@ int W = 900; ...@@ -17,17 +17,44 @@ int W = 900;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
Palyaelem *gen_elem() Palyaelem *gen_elem(Palyaelem *elem)
{ {
Palyaelem *elem = (Palyaelem *) malloc(sizeof(Palyaelem)); Palyaelem *uj = (Palyaelem *) malloc(sizeof(Palyaelem));
elem->hossz = rand() % (W * 2); uj->kov = NULL;
elem->x = rand() % W;
elem->y = rand() % (H - (owl * 2)); if (elem == NULL)
elem->magassag = 20; {
elem->kov = NULL; uj->hossz = rand() % (W / 2);
uj->x = rand() % W;
uj->y = rand() % (H / 2) + (550 - owl);
uj->magassag = 20;
elem = uj;
return elem; return elem;
} }
Palyaelem *mozgo;
for (mozgo = elem; mozgo->kov != NULL; mozgo = mozgo->kov);
mozgo->kov = uj;
uj->hossz = rand() % (W / 2);
uj->x = rand() % W + (mozgo->x + mozgo->hossz);
uj->y = rand() % (H / 2) + mozgo->y;
uj->magassag = 20;
return elem;
}
Palyaelem *palyamozgat(Palyaelem *eleje, int x)
{
Palyaelem *mozgo;
for (mozgo = eleje; mozgo != NULL; mozgo = mozgo->kov)
{
mozgo->x -= x;
}
return eleje;
}
void palyaelem_kirajzolo(Palyaelem *elem, SDL_Renderer *prenderer) void palyaelem_kirajzolo(Palyaelem *elem, SDL_Renderer *prenderer)
{ {
SDL_Rect r; SDL_Rect r;
...@@ -51,6 +78,7 @@ void palyakirajzol(SDL_Renderer *prenderer) ...@@ -51,6 +78,7 @@ void palyakirajzol(SDL_Renderer *prenderer)
palyaelem_kirajzolo(&alap, prenderer); palyaelem_kirajzolo(&alap, prenderer);
} }
Uint32 idozit(Uint32 ms, void *param) { Uint32 idozit(Uint32 ms, void *param) {
SDL_Event ev; SDL_Event ev;
ev.type = SDL_USEREVENT; ev.type = SDL_USEREVENT;
...@@ -66,18 +94,19 @@ int main(int argc, char *argv[]) { ...@@ -66,18 +94,19 @@ int main(int argc, char *argv[]) {
SDL_Texture *bagoly = kep_betolt(renderer, "infoc.png"); SDL_Texture *bagoly = kep_betolt(renderer, "infoc.png");
SDL_TimerID timer_id = SDL_AddTimer(20, idozit, NULL); SDL_TimerID timer_id = SDL_AddTimer(1000, idozit, NULL);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
bool quit = false; bool quit = false;
bool moving = false; bool moving = false;
bool reset = false;
int elozo_x = 0; int elozo_x = 0;
int elozo_y = 550 - owl; int elozo_y = 550 - owl;
Palyaelem *elem = gen_elem(); Palyaelem *elem = NULL;
Palyaelem *mozgo = elem;
while (!quit) { while (!quit) {
...@@ -86,14 +115,17 @@ int main(int argc, char *argv[]) { ...@@ -86,14 +115,17 @@ int main(int argc, char *argv[]) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
elem = gen_elem(elem);
Palyaelem *mozgo = elem;
palyaelem_kirajzolo(mozgo, renderer); palyaelem_kirajzolo(mozgo, renderer);
palyakirajzol(renderer); palyakirajzol(renderer);
keywatcher(ev, bagoly, elozo_x, elozo_y, &quit, &moving, renderer); keywatcher(ev, bagoly, elozo_x, elozo_y, &quit, &moving, renderer);
if (moving){ if (moving){
move_draw(ev, bagoly, renderer, &elozo_x, &elozo_y, timer_id); move_draw(ev, bagoly, renderer, &elozo_x, &elozo_y);
} }
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
......
...@@ -9,85 +9,60 @@ ...@@ -9,85 +9,60 @@
#include <math.h> #include <math.h>
double v = 5.0; int v = 5;
double vx = 0; int vx = 0;
double vy = 0; int vy = 0;
double g = 9.82; double g = 9.82;
double pi = 3.14159; double pi = 3.14159;
int t = 0; int t = 0;
bool jump = false; bool jump = false;
bool jumping = false; bool jumping = false;
static void move(SDL_Event ev, SDL_TimerID timer) static void move(SDL_Event ev)
{ {
double y_tmp = 0;
switch (ev.type) { switch (ev.type) {
case SDL_MOUSEMOTION:
return;
case SDL_KEYDOWN: case SDL_KEYDOWN:
switch (ev.key.keysym.sym) { if (ev.key.keysym.sym == SDLK_a) {
case SDLK_LEFT:
vx = -1; vx = -1;
vx *= v; vx *= v;
break; break;
}
case SDLK_RIGHT: if (ev.key.keysym.sym == SDLK_d) {
vx = 1; vx = 1;
vx *= v; vx *= v;
break; break;
case SDLK_SPACE:
t = 0;
if (vx == 0) {
y_tmp = v - (g * t);
if (y_tmp >= 0)
vy = -1;
else
vy = 1;
} }
else
{
y_tmp = v * sin(pi/4) - g*t;
if (y_tmp >= 0)
vy = -1;
else
vy = 1;
}
vy *= v;
jump = true;
jumping = true;
break;
/*case SDLK_w: if (ev.key.keysym.sym == SDLK_w) {
vy = -1; vy = -1;
vy *= v; vy *= v;
break; break;
}
case SDLK_s: if (ev.key.keysym.sym == SDLK_s) {
vy = 1; vy = 1;
vy *= v; vy *= v;
break; break;
*/
} }
case SDL_KEYUP: case SDL_KEYUP:
switch (ev.key.keysym.sym) { switch (ev.key.keysym.sym) {
case SDLK_LEFT: case SDLK_a:
if (vx < 0) if (vx < 0)
vx = 0; vx = 0;
break; break;
case SDLK_RIGHT: case SDLK_d:
if (vx > 0) if (vx > 0)
vx = 0; vx = 0;
break; break;
/*case SDLK_SPACE: case SDLK_w:
jump = false;
break;
/*case SDLK_w:
if (vy < 0) if (vy < 0)
vy = 0; vy = 0;
break; break;
...@@ -95,16 +70,19 @@ static void move(SDL_Event ev, SDL_TimerID timer) ...@@ -95,16 +70,19 @@ static void move(SDL_Event ev, SDL_TimerID timer)
case SDLK_s: case SDLK_s:
if (vy > 0) if (vy > 0)
vy = 0; vy = 0;
break;*/ break;
default: default:
break; break;
} }
break; break;
case SDL_USEREVENT: case SDL_USEREVENT:
if (jumping)
t++;
break; break;
} }
} }
void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *moving, SDL_Renderer *renderer) void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *moving, SDL_Renderer *renderer)
...@@ -122,6 +100,7 @@ void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool * ...@@ -122,6 +100,7 @@ void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *
case SDLK_r: case SDLK_r:
kirajzol(renderer, kep, 0, 550 - owl); kirajzol(renderer, kep, 0, 550 - owl);
*moving = true;
break; break;
} }
break; break;
...@@ -132,14 +111,19 @@ void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool * ...@@ -132,14 +111,19 @@ void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *
} }
} }
void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y, SDL_TimerID timer) void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y)
{ {
move(ev, timer); move(ev);
*x += (int) vx; *x += vx;
*y += (int) vy; *y += vy;
if (*y >= (550 -owl))
{
*y = 550 - owl;
}
if (*y == (550 - owl) && !jump) /*if (*y == (550 - owl) && !jump)
jumping = false; jumping = false;*/
kirajzol(renderer, kep, *x, *y); kirajzol(renderer, kep, *x, *y);
} }
\ No newline at end of file
...@@ -9,19 +9,19 @@ ...@@ -9,19 +9,19 @@
#include <stdbool.h> #include <stdbool.h>
#include <math.h> #include <math.h>
extern double v; extern int v;
extern double vx; extern int vx;
extern double vy; extern int vy;
extern double g; extern double g;
extern double pi; extern double pi;
extern int t; extern int t;
extern bool jump; extern bool jump;
extern bool jumping; extern bool jumping;
static void move(SDL_Event ev, SDL_TimerID timer); static void move(SDL_Event ev);
void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *moving, SDL_Renderer *renderer); void keywatcher(SDL_Event ev, SDL_Texture *kep, int x, int y, bool *quit, bool *moving, SDL_Renderer *renderer);
void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y, SDL_TimerID timer); void move_draw(SDL_Event ev, SDL_Texture *kep, SDL_Renderer *renderer, int *x, int *y);
#endif //NHF_MOVE_H #endif //NHF_MOVE_H
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment