commit 04b70906ce60ebe8823ead7f3eff1da2d2359f09
parent 75162ff667d611a757773ce1288c78e93bace235
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 25 Dec 2022 17:43:53 +0900
add acceleration
Diffstat:
M | ex6/ex6.c | | | 106 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
1 file changed, 60 insertions(+), 46 deletions(-)
diff --git a/ex6/ex6.c b/ex6/ex6.c
@@ -12,7 +12,8 @@
#define INIT_HEIGHT (600)
#define FPS (60)
#define SUB_TICK (4)
-#define NUM_RECT (100)
+#define NUM_RECT (20)
+#define GRAVITY (1000)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
@@ -42,6 +43,7 @@ struct rect {
float ppx, ppy; // previous position
float px, py; // top left corner
float vx, vy;
+ float ax, ay;
int w, h;
int m;
};
@@ -50,6 +52,7 @@ struct circle {
float ppx, ppy;
float px, py;
float vx, vy;
+ float ax, ay;
int r;
int m;
};
@@ -60,7 +63,8 @@ Window window;
unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT;
GC gc, sgc[NUM_RECT];
Atom wm_delete_window;
-struct circle circle[NUM_RECT];
+struct rect block[NUM_RECT];
+struct rect player;
int next_menu = START_MENU;
@@ -200,6 +204,9 @@ receive_events(int key_state[])
case 's':
key_state[KEY_S] = KEY_DOWN;
break;
+ case ' ':
+ key_state[KEY_SPACE] = KEY_DOWN;
+ break;
default:
break;
}
@@ -221,6 +228,9 @@ receive_events(int key_state[])
case 's':
key_state[KEY_S] = KEY_UP;
break;
+ case ' ':
+ key_state[KEY_SPACE] = KEY_UP;
+ break;
default:
break;
}
@@ -243,17 +253,19 @@ handle_inputs(int key_state[])
next_menu = GAME_OVER;
return;
}
- /*
- circle[0].vx = circle[0].vy = 0;
+ player.vx = 0;
if (key_state[KEY_D] == KEY_DOWN)
- circle[0].vx += 300;
+ player.vx += 300;
if (key_state[KEY_A] == KEY_DOWN)
- circle[0].vx += -300;
+ player.vx += -300;
+ /*
if (key_state[KEY_S] == KEY_DOWN)
- circle[0].vy += 300;
+ player.vy += 300;
if (key_state[KEY_W] == KEY_DOWN)
- circle[0].vy += -300;
+ player.vy += -300;
*/
+ if (key_state[KEY_SPACE] == KEY_DOWN)
+ player.vy = -300;
}
void
@@ -261,8 +273,10 @@ rect_next_tick(struct rect *s, long ndt) // nano second
{
s->ppx = s->px;
s->ppy = s->py;
- s->px = s->px + s->vx * ndt / 1000 / 1000 / 1000;
- s->py = s->py + s->vy * ndt / 1000 / 1000 / 1000;
+ s->vx += s->ax * ndt / 1000 / 1000 / 1000;
+ s->vy += s->ay * ndt / 1000 / 1000 / 1000;
+ s->px += s->vx * ndt / 1000 / 1000 / 1000;
+ s->py += s->vy * ndt / 1000 / 1000 / 1000;
// bind within the window
if (s->px < 0) {
@@ -288,8 +302,10 @@ circle_next_tick(struct circle *c, long ndt)
{
c->ppx = c->px;
c->ppy = c->py;
- c->px = c->px + c->vx * ndt / 1000 / 1000 / 1000;
- c->py = c->py + c->vy * ndt / 1000 / 1000 / 1000;
+ c->vx += c->ax * ndt / 1000 / 1000 / 1000;
+ c->vy += c->ay * ndt / 1000 / 1000 / 1000;
+ c->px += c->vx * ndt / 1000 / 1000 / 1000;
+ c->py += c->vy * ndt / 1000 / 1000 / 1000;
// bind within the window
if (c->px - c->r < 0) {
@@ -473,28 +489,23 @@ game_play(void)
struct timespec ts;
for(int i = 0; i < NUM_RECT; i++){
- circle[i].ppx = circle[i].px = rand() * (float)win_width / (float)RAND_MAX;
- circle[i].ppy = circle[i].py = rand() * (float)win_height / (float)RAND_MAX;
- circle[i].vx = rand() * 300.0 / (float)RAND_MAX - 150;
- circle[i].vy = rand() * 300.0 / (float)RAND_MAX - 150;
- circle[i].r = rand() * 30.0 / (float)RAND_MAX + 5;
- circle[i].m = circle[i].r * circle[i].r;
+ block[i].ppx = block[i].px = 40 * (i + 1) % win_width;
+ block[i].ppy = block[i].py = 400 ;
+ block[i].ax = 0;
+ block[i].ay = 0;
+ block[i].vx = 0;
+ block[i].vy = 0;
+ block[i].w = block[i].h = 40;
+ block[i].m = block[i].w * block[i].h;
}
-/*
- circle[0].ppx = circle[0].px = 100;
- circle[0].ppy = circle[0].py = 400;
- circle[0].vx = 100;
- circle[0].vy = -100;
- circle[0].r = 100;
- circle[0].m = circle[0].r * circle[0].r;
- circle[1].ppx = circle[1].px = 400;
- circle[1].ppy = circle[1].py = 100;
- circle[1].vx = -100;
- circle[1].vy = 200;
- circle[1].r = 100;
- circle[1].m = circle[1].r * circle[1].r;
-
-*/
+ player.ppx = player.px = 100;
+ player.ppy = player.py = 300 ;
+ player.vx = 0;
+ player.vy = 0;
+ player.ax = 0;
+ player.ay = GRAVITY;
+ player.w = player.h = 20;
+ player.m = player.w * player.h;
while (next_menu == GAME_PLAY){
clock_gettime(CLOCK_MONOTONIC, &ts);
@@ -507,16 +518,16 @@ game_play(void)
t0 = ts.tv_nsec;
int collision[NUM_RECT] = {0};
- for (int j = 0; j < SUB_TICK; j++) {
+ for (int k = 0; k < SUB_TICK; k++) {
+ rect_next_tick(&player, 1000 * 1000 * 1000 / FPS / SUB_TICK);
for (int i = 0; i < NUM_RECT; i++)
- circle_next_tick(&circle[i], 1000 * 1000 * 1000 / FPS / SUB_TICK);
+ rect_next_tick(&block[i], 1000 * 1000 * 1000 / FPS / SUB_TICK);
- for (int i = 0; i < NUM_RECT; i++)
- for (int j = i + 1; j < NUM_RECT; j++) {
- circle_handle_collision_elastic(&circle[i], &circle[j]);
- if (circle_test_collision(&circle[i], &circle[j]))
- collision[i] = collision[j] = 1;
- }
+ for (int i = 0; i < NUM_RECT; i++){
+ rect_handle_collision_mf(&player, &block[i]);
+ if (rect_test_collision(&player, &block[i]))
+ collision[i] = 1;
+ }
}
for (int i = 0; i < NUM_RECT; i++)
if (collision[i] == 1)
@@ -547,12 +558,15 @@ game_play(void)
win_width, win_height, // width and height
False);
for (int i = 0; i < NUM_RECT; i++) {
- XDrawArc(display, window, sgc[i],
- circle[i].px - circle[i].r,
- circle[i].py - circle[i].r, // position
- circle[i].r * 2, circle[i].r * 2,
- 0, 360 << 6); // width and height
+ XDrawRectangle(display, window, sgc[i],
+ block[i].px,
+ block[i].py, // position
+ block[i].w, block[i].h);
}
+ XDrawRectangle(display, window, gc,
+ player.px,
+ player.py, // position
+ player.w, player.h);
}
XSetForeground(display, gc, 0x00FFFF);
}