Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gtk.Main;
- import gtk.MainWindow;
- import gtk.CssProvider;
- import gdk.Display;
- import gdk.Screen;
- import gtk.StyleContext;
- import std.path;
- import std.stdio;
- import std.random;
- import std.conv;
- import gtk.Button;
- import gtk.Grid;
- import gdk.Event;
- import gtk.Widget;
- import glib.ListG;
- import glib.Timeout;
- import core.thread;
- class Window : MainWindow{
- private int level = 1;
- private int[] rndButtonBlink;
- private int[] userInput;
- private Button[4] bArr;
- private int index = 0;
- this(int width, int height, string title, string wd){
- super(title);
- setDefaultSize(width, height);
- Button btn_0 = new Button();
- btn_0.getStyleContext().addClass("green");
- auto call0 = &btn0ClickedEvent;
- btn_0.addOnButtonRelease(call0);
- Button btn_1 = new Button();
- btn_1.getStyleContext().addClass("yellow");
- auto call1 = &btn1ClickedEvent;
- btn_1.addOnButtonRelease(call1);
- Button btn_2 = new Button();
- btn_2.getStyleContext().addClass("blue");
- auto call2 = &btn2ClickedEvent;
- btn_2.addOnButtonRelease(call2);
- Button btn_3 = new Button();
- btn_3.getStyleContext().addClass("red");
- auto call3 = &btn3ClickedEvent;
- btn_3.addOnButtonRelease(call3);
- bArr = [btn_0,btn_1,btn_2,btn_3];
- Grid grid = new Grid();
- grid.setColumnSpacing(2);
- grid.setRowSpacing(2);
- grid.setColumnHomogeneous(true);
- grid.setRowHomogeneous(true);
- grid.attach(btn_0,1,1,1,1);
- grid.attach(btn_1,1,2,1,1);
- grid.attach(btn_2,2,1,1,1);
- grid.attach(btn_3,2,2,1,1);
- string cssPath = dirName(wd) ~ "\\" ~ "SimonSays.css";
- CssProvider provider = new CssProvider();
- provider.loadFromPath(cssPath);
- Display display = Display.getDefault();
- Screen screen = display.getDefaultScreen();
- StyleContext.addProviderForScreen(screen, provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
- randomizeButtons();
- letButtonsFlash();
- add(grid);
- showAll();
- }
- private void randomizeButtons(){
- rndButtonBlink = new int[level];
- userInput = new int[level];
- userInput.length = 0;
- for(int i = 0; i < level; i++){
- rndButtonBlink[i] = uniform(0,4);
- }
- }
- private void letButtonsFlash(){
- foreach(Button btn;bArr){
- btn.setSensitive(false);
- }
- for(int i = 0; i < level; i++){
- index = i;
- Button currentButton = bArr[rndButtonBlink[i]]; //Array holds randomized Buttons
- ListG list = currentButton.getStyleContext().listClasses();
- string CSSClassName = to!string(cast(char*)list.next().data);
- currentButton.getStyleContext().addClass(CSSClassName ~ "-flash");
- Timeout t = new Timeout(() => this.timeout_delay(currentButton),1,false);
- }
- foreach(Button btn;bArr){
- btn.setSensitive(true);
- }
- }
- bool timeout_delay(Button currentButton){
- ListG list = currentButton.getStyleContext().listClasses();
- string CSSClassName = to!string(cast(char*)list.next().data);
- writeln(CSSClassName); //try to debug
- currentButton.getStyleContext().removeClass(CSSClassName ~ "-flash");
- return false;
- }
- bool btn0ClickedEvent(Event e, Widget widget){
- userInput ~= 0;
- checkForWin();
- return true;
- }
- bool btn1ClickedEvent(Event e, Widget widget){
- userInput ~= 1;
- checkForWin();
- return true;
- }
- bool btn2ClickedEvent(Event e, Widget widget){
- userInput ~= 2;
- checkForWin();
- return true;
- }
- bool btn3ClickedEvent(Event e, Widget widget){
- userInput ~= 3;
- checkForWin();
- return true;
- }
- int checkIfCorrect(){
- writeln("Level: " ~ to!string(level));
- writeln(userInput);
- if(userInput.length > rndButtonBlink.length){
- writeln("UserInput too long");
- return 0;
- }
- if(userInput.length < rndButtonBlink.length){
- return 2;
- }
- if(userInput.length == rndButtonBlink.length){
- for(int i = 0; i < level; i++){
- if(rndButtonBlink[i] != userInput[i])
- return 0;
- }
- }
- return 1;
- }
- void checkForWin(){
- int val = checkIfCorrect();
- if(val == 0){
- level = 1;
- writeln("Loose!");
- nextRound();
- }
- else if(val == 1){
- level++;
- writeln("Win!");
- nextRound();
- }
- }
- void nextRound(){
- randomizeButtons();
- letButtonsFlash();
- }
- }
- void main(string[] args){
- Main.init(args);
- auto win = new Window(250,250,"Tutorial", args[0]);
- Main.run();
- }
- ------------------------------------------------------------
- Css:
- .green{
- background-image:none;
- background-color:green;
- }
- .yellow{
- background-image:none;
- background-color:yellow;
- }
- .blue{
- background-image:none;
- background-color:blue;
- }
- .red{
- background-image:none;
- background-color:red;
- }
- .green-flash{
- background-image:none;
- background-color:#90EE90;
- }
- .yellow-flash{
- background-image:none;
- background-color:#FFFFE0;
- }
- .blue-flash{
- background-image:none;
- background-color:#ADD8E6;
- }
- .red-flash{
- background-image:none;
- background-color:#FFB6C1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement