Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.57 KB | None | 0 0
  1. import gtk.Main;
  2. import gtk.MainWindow;
  3. import gtk.CssProvider;
  4. import gdk.Display;
  5. import gdk.Screen;
  6. import gtk.StyleContext;
  7. import std.path;
  8. import std.stdio;
  9. import std.random;
  10. import std.conv;
  11. import gtk.Button;
  12. import gtk.Grid;
  13. import gdk.Event;
  14. import gtk.Widget;
  15. import glib.ListG;
  16. import glib.Timeout;
  17. import core.thread;
  18.  
  19. class Window : MainWindow{
  20.     private int level = 1;
  21.     private int[] rndButtonBlink;
  22.     private int[] userInput;
  23.     private Button[4] bArr;
  24.     private int index = 0;
  25.     this(int width, int height, string title, string wd){
  26.         super(title);
  27.         setDefaultSize(width, height);
  28.  
  29.         Button btn_0 = new Button();
  30.         btn_0.getStyleContext().addClass("green");
  31.         auto call0 = &btn0ClickedEvent;
  32.         btn_0.addOnButtonRelease(call0);
  33.  
  34.         Button btn_1 = new Button();
  35.         btn_1.getStyleContext().addClass("yellow");
  36.         auto call1 = &btn1ClickedEvent;
  37.         btn_1.addOnButtonRelease(call1);
  38.  
  39.         Button btn_2 = new Button();
  40.         btn_2.getStyleContext().addClass("blue");
  41.         auto call2 = &btn2ClickedEvent;
  42.         btn_2.addOnButtonRelease(call2);
  43.  
  44.         Button btn_3 = new Button();
  45.         btn_3.getStyleContext().addClass("red");
  46.         auto call3 = &btn3ClickedEvent;
  47.         btn_3.addOnButtonRelease(call3);
  48.  
  49.         bArr = [btn_0,btn_1,btn_2,btn_3];
  50.  
  51.         Grid grid = new Grid();
  52.         grid.setColumnSpacing(2);
  53.         grid.setRowSpacing(2);
  54.         grid.setColumnHomogeneous(true);
  55.         grid.setRowHomogeneous(true);
  56.         grid.attach(btn_0,1,1,1,1);
  57.         grid.attach(btn_1,1,2,1,1);
  58.         grid.attach(btn_2,2,1,1,1);
  59.         grid.attach(btn_3,2,2,1,1);
  60.  
  61.         string cssPath = dirName(wd) ~ "\\" ~ "SimonSays.css";
  62.  
  63.         CssProvider provider = new CssProvider();
  64.         provider.loadFromPath(cssPath);
  65.  
  66.         Display display = Display.getDefault();
  67.         Screen screen = display.getDefaultScreen();
  68.         StyleContext.addProviderForScreen(screen, provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  69.  
  70.         randomizeButtons();
  71.         letButtonsFlash();
  72.  
  73.         add(grid);
  74.         showAll();
  75.     }
  76.     private void randomizeButtons(){
  77.         rndButtonBlink = new int[level];
  78.         userInput = new int[level];
  79.         userInput.length = 0;
  80.         for(int i = 0; i < level; i++){
  81.             rndButtonBlink[i] = uniform(0,4);
  82.         }
  83.     }
  84.     private void letButtonsFlash(){
  85.         foreach(Button btn;bArr){
  86.             btn.setSensitive(false);
  87.         }
  88.         for(int i = 0; i < level; i++){
  89.             index = i;
  90.             Button currentButton = bArr[rndButtonBlink[i]]; //Array holds randomized Buttons
  91.             ListG list = currentButton.getStyleContext().listClasses();
  92.             string CSSClassName = to!string(cast(char*)list.next().data);
  93.             currentButton.getStyleContext().addClass(CSSClassName ~ "-flash");
  94.             Timeout t = new Timeout(() => this.timeout_delay(currentButton),1,false);
  95.         }
  96.  
  97.         foreach(Button btn;bArr){
  98.             btn.setSensitive(true);
  99.         }
  100.     }
  101.     bool timeout_delay(Button currentButton){
  102.         ListG list = currentButton.getStyleContext().listClasses();
  103.         string CSSClassName = to!string(cast(char*)list.next().data);
  104.         writeln(CSSClassName); //try to debug
  105.         currentButton.getStyleContext().removeClass(CSSClassName ~ "-flash");
  106.         return false;
  107.     }
  108.     bool btn0ClickedEvent(Event e, Widget widget){
  109.         userInput ~= 0;
  110.         checkForWin();
  111.         return true;
  112.     }
  113.     bool btn1ClickedEvent(Event e, Widget widget){
  114.         userInput ~= 1;
  115.         checkForWin();
  116.         return true;
  117.     }
  118.     bool btn2ClickedEvent(Event e, Widget widget){
  119.         userInput ~= 2;
  120.         checkForWin();
  121.         return true;
  122.     }
  123.     bool btn3ClickedEvent(Event e, Widget widget){
  124.         userInput ~= 3;
  125.         checkForWin();
  126.         return true;
  127.     }
  128.     int checkIfCorrect(){
  129.         writeln("Level: " ~ to!string(level));
  130.         writeln(userInput);
  131.         if(userInput.length > rndButtonBlink.length){
  132.             writeln("UserInput too long");
  133.             return 0;
  134.         }
  135.         if(userInput.length < rndButtonBlink.length){
  136.             return 2;
  137.         }
  138.         if(userInput.length == rndButtonBlink.length){
  139.             for(int i = 0; i < level; i++){
  140.                 if(rndButtonBlink[i] != userInput[i])
  141.                     return 0;
  142.             }
  143.         }
  144.         return 1;
  145.     }
  146.     void checkForWin(){
  147.         int val = checkIfCorrect();
  148.         if(val == 0){
  149.             level = 1;
  150.             writeln("Loose!");
  151.             nextRound();
  152.         }
  153.         else if(val == 1){
  154.             level++;
  155.             writeln("Win!");
  156.             nextRound();
  157.         }
  158.     }
  159.     void nextRound(){
  160.         randomizeButtons();
  161.         letButtonsFlash();
  162.     }
  163. }
  164.  
  165. void main(string[] args){
  166.     Main.init(args);
  167.     auto win = new Window(250,250,"Tutorial", args[0]);
  168.     Main.run();
  169. }
  170.  
  171. ------------------------------------------------------------
  172.  
  173. Css:
  174. .green{
  175.     background-image:none;
  176.     background-color:green;
  177. }
  178. .yellow{
  179.     background-image:none;
  180.     background-color:yellow;
  181. }
  182. .blue{
  183.     background-image:none;
  184.     background-color:blue;
  185. }
  186. .red{
  187.     background-image:none;
  188.     background-color:red;
  189. }
  190. .green-flash{
  191.     background-image:none;
  192.     background-color:#90EE90;
  193. }
  194. .yellow-flash{
  195.     background-image:none;
  196.     background-color:#FFFFE0;
  197. }
  198. .blue-flash{
  199.     background-image:none;
  200.     background-color:#ADD8E6;
  201. }
  202. .red-flash{
  203.     background-image:none;
  204.     background-color:#FFB6C1;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement