breakout.pp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. program breakout;
  2. {$mode objfpc}
  3. uses glib,gdk,gtk,blocks;
  4. Type
  5. TBreakOutWindow = Class(TObject)
  6. Public
  7. window,
  8. area : PGtkWidget;
  9. BreakOut : TBreakOut;
  10. end;
  11. Var
  12. GameWindow : TBreakOutWindow;
  13. Function Close( widget : PGtkWidget ;
  14. event : PGdkEvent;
  15. data : gpointer) : boolean; cdecl;
  16. Begin
  17. gtk_main_quit();
  18. Close := false;
  19. End;
  20. function Exposed(Widget: PGtkWidget;
  21. event : PGdkEventExpose;
  22. Data : gpointer) : Integer; cdecl;
  23. begin
  24. TBreakOutWindow(Data).BreakOut.Draw(Event);
  25. result:=0;
  26. end;
  27. function KeyPress (Widget: PGtkWidget;
  28. event : PGdkEventKey;
  29. Data : gpointer) : Integer; cdecl;
  30. begin
  31. with TBreakOutWindow(Data).BreakOut do
  32. Case event^.keyval of
  33. gdk_left : Pad.Goleft;
  34. gdk_right : Pad.GoRight;
  35. gdk_down : Pad.Stop;
  36. Ord(' ') : NextBall;
  37. end;
  38. Result:=0;
  39. end;
  40. function Step (data : Gpointer): integer;cdecl;
  41. Var
  42. Rect : TGdkRectangle;
  43. begin
  44. With TBreakOutWindow(Data) do
  45. begin
  46. With Breakout do
  47. begin
  48. Step;
  49. Draw(Nil);
  50. end;
  51. end;
  52. Result:=integer(True);
  53. end;
  54. Begin
  55. gtk_init( @argc, @argv );
  56. GameWindow:=TBreakOutWindow.Create;
  57. With GameWindow do
  58. begin
  59. window := gtk_window_new( GTK_WINDOW_TOPLEVEL );
  60. gtk_window_set_policy(PgtkWindow(Window),0,0,1);
  61. gtk_signal_connect (GTK_OBJECT (window), 'delete_event',
  62. GTK_SIGNAL_FUNC(@Close), NIL);
  63. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  64. area := gtk_drawing_area_new();
  65. gtk_container_add( GTK_CONTAINER(window), Area);
  66. BreakOut:=TBreakOut.Create(area);
  67. With BreakOut.BlockList do
  68. begin
  69. TotalRows:=20;
  70. TotalColumns:=10;
  71. StartRow:=15;
  72. BlockRows:=5;
  73. BlockSpacing:=2;
  74. end;
  75. gtk_signal_connect (GTK_OBJECT (area),'expose_event',
  76. GTK_SIGNAL_FUNC(@Exposed),GameWindow);
  77. gtk_drawing_area_size (PGTKDRAWINGAREA(area),600,400);
  78. gtk_widget_set_events(window,GDK_KEY_RELEASE_MASK);
  79. gtk_signal_connect(PGTKObject(Window),'key_press_event',
  80. GTK_SIGNAL_FUNC(@KeyPress),GameWindow);
  81. gtk_timeout_add(50,@Step,GameWindow);
  82. gtk_widget_show_all( window );
  83. gtk_main();
  84. end;
  85. End.
  86. end.