disappearing gravity vector

This commit is contained in:
Antonio Carzaniga 2019-07-29 15:49:42 +02:00
parent 370ac137ff
commit 5c9d152eaf

34
balls.c
View File

@ -140,15 +140,22 @@ static void update_state () {
static GtkWidget * window; static GtkWidget * window;
static cairo_t * cr = 0; static cairo_t * cr = 0;
static int gravity_vector_countdown = 0;
static int gravity_vector_init = 300;
static void draw_gravity_vector() { static void draw_gravity_vector() {
cairo_new_path(cr); if (gravity_vector_countdown != 0) {
cairo_move_to(cr, width/2, height/2); cairo_new_path(cr);
cairo_line_to(cr, width/2 + g_x, height/2 + g_y); cairo_move_to(cr, width/2, height/2);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); cairo_line_to(cr, width/2 + g_x, height/2 + g_y);
cairo_set_line_width(cr, 1.0); cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_stroke(cr); cairo_set_line_width(cr, 1.0);
cairo_arc(cr, width/2 + g_x, height/2 + g_y, 3, 0, 2*M_PI); cairo_stroke(cr);
cairo_fill(cr); cairo_arc(cr, width/2 + g_x, height/2 + g_y, 3, 0, 2*M_PI);
cairo_fill(cr);
if (gravity_vector_countdown > 0)
--gravity_vector_countdown;
}
} }
static void draw_balls_onto_window () { static void draw_balls_onto_window () {
@ -194,20 +201,23 @@ static gint keyboard_input (GtkWidget *widget, GdkEventKey *event) {
switch(event->keyval) { switch(event->keyval) {
case GDK_KEY_Up: case GDK_KEY_Up:
g_y -= 10; g_y -= 10;
gravity_vector_countdown = gravity_vector_init;
break; break;
case GDK_KEY_Down: case GDK_KEY_Down:
g_y += 10; g_y += 10;
gravity_vector_countdown = gravity_vector_init;
break; break;
case GDK_KEY_Left: case GDK_KEY_Left:
g_x -= 10; g_x -= 10;
gravity_vector_countdown = gravity_vector_init;
break; break;
case GDK_KEY_Right: case GDK_KEY_Right:
g_x += 10; g_x += 10;
gravity_vector_countdown = gravity_vector_init;
break; break;
case GDK_KEY_F: case GDK_KEY_G:
case GDK_KEY_f: case GDK_KEY_g:
g_x = rand() % 201 - 100; gravity_vector_countdown = gravity_vector_init;
g_y = rand() % 201 - 100;
break; break;
case GDK_KEY_Q: case GDK_KEY_Q:
case GDK_KEY_q: case GDK_KEY_q: