Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

在默认窗口下小方块的移动

  • Loading branch information...
commit 1524df7fbc5710358ac7a9c6249890cfe72ea30c 1 parent 9c7110e
Yang Zongze authored

Showing 3 changed files with 127 additions and 0 deletions. Show diff stats Hide diff stats

  1. +1 0  .gitignore
  2. +108 0 main.c
  3. +18 0 tetris.h
1  .gitignore
@@ -5,3 +5,4 @@
5 5 *.o
6 6 *.i
7 7 tetris
  8 +a.out
108 main.c
... ... @@ -0,0 +1,108 @@
  1 +/*
  2 +file:
  3 + main.h
  4 +
  5 +author:
  6 + lrtfm <yzz1215@live.cn>
  7 + lrtfm.github.com
  8 +date:
  9 + 2012-03-03 21:15:42
  10 +*/
  11 +
  12 +#include "tetris.h"
  13 +int x = 40;
  14 +int y = 0;
  15 +
  16 +void move_handler(GtkWidget *widget, GdkEventKey *event, gpointer data)
  17 +{
  18 + switch(event->keyval) {
  19 + //case GDK_Up:
  20 + //y-=10;
  21 + //break;
  22 + case GDK_Left:
  23 + x-=10;
  24 + break;
  25 + case GDK_Right:
  26 + x+=10;
  27 + break;
  28 + case GDK_Down:
  29 + y+=10;
  30 + break;
  31 + default:break;
  32 + }
  33 + gtk_widget_queue_draw(widget);
  34 +}
  35 +
  36 +
  37 +void expose_handler(GtkWidget *widget, gpointer data)
  38 +{
  39 + cairo_t *cr;
  40 +
  41 + cr = gdk_cairo_create(widget->window);
  42 +
  43 + cairo_set_source_rgb (cr, 0, 0.3, 0.8);
  44 + cairo_rectangle(cr, x, y, 10, 10);
  45 + cairo_fill (cr);
  46 +
  47 + cairo_destroy(cr);
  48 +}
  49 +
  50 +static gboolean time_handler (GtkWidget *widget)
  51 +{
  52 + y +=10;
  53 + gtk_widget_queue_draw(widget);
  54 + return TRUE;
  55 +}
  56 +
  57 +
  58 +
  59 +int main(int argc, char **argv)
  60 +{
  61 + GtkWidget *window;
  62 + GtkWidget *main_area;
  63 +
  64 + gtk_init(&argc, &argv);
  65 +
  66 + window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  67 + if (!window){
  68 + g_print("Could not creat gtk+-window!\n");
  69 + return 2;
  70 + }
  71 +
  72 + g_signal_connect(G_OBJECT(window),
  73 + "destroy",
  74 + G_CALLBACK(gtk_main_quit),
  75 + NULL);
  76 +
  77 +
  78 + main_area = gtk_drawing_area_new();
  79 + if (!main_area){
  80 + g_print("could not creat drawing-area!\n");
  81 + return 3;
  82 + }
  83 + gtk_container_add(GTK_CONTAINER (window), main_area);
  84 + /*g_signal_connect_after(G_OBJECT(main_area),
  85 + "realize",
  86 + G_CALLBACK(realize_handler),
  87 + NULL);*/
  88 +
  89 + g_signal_connect(G_OBJECT(main_area),
  90 + "expose-event",
  91 + G_CALLBACK(expose_handler),
  92 + NULL);
  93 +
  94 + g_signal_connect(G_OBJECT(window),
  95 + "key-press-event",
  96 + G_CALLBACK(move_handler),
  97 + NULL);
  98 +
  99 + //gtk_widget_set_app_paintable(main_area, TRUE);
  100 + gtk_widget_show(main_area);
  101 + gtk_widget_show(window);
  102 + g_timeout_add(500, (GSourceFunc) time_handler, (gpointer) window);
  103 + gtk_main();
  104 +
  105 + return 0;
  106 +}
  107 +
  108 +
18 tetris.h
... ... @@ -0,0 +1,18 @@
  1 +/*
  2 +file:
  3 + tetris.h
  4 +
  5 +author:
  6 + lrtfm <yzz1215@live.cn>
  7 + lrtfm.github.com
  8 +date:
  9 + 2012-03-03 21:06:27
  10 +*/
  11 +
  12 +#include <cairo.h>
  13 +#include <gtk/gtk.h>
  14 +#include <gdk/gdkkeysyms.h>
  15 +#define GDK_Left 0xff51
  16 +#define GDK_Up 0xff52
  17 +#define GDK_Right 0xff53
  18 +#define GDK_Down 0xff54

0 comments on commit 1524df7

Please sign in to comment.
Something went wrong with that request. Please try again.