M
M
Makar Gerasimov2017-01-02 23:09:12
Pascal
Makar Gerasimov, 2017-01-02 23:09:12

How to make a managed interface?

Good evening. Faced a problem called "term paper". Subject reads: "write a game 'Balda'". I actually started writing the interface, developed a class that draws a 5x5 chess grid using the GraphABC module.
The essence of the game: there are two players, a random word is initially given, it is necessary to find another word or a word + symbol in an empty cell.
Actually the problem: I draw the cells, but I can not control them. For example, so that the player could use the keys (UP, DOWN, LEFT, RIGHT) and move the cursor (just a square of a different color) on the grid, and if the cell is empty, it was possible to write a letter. And then somehow write down the resulting new word (to search in the dictionary)
76996088e33b481da85735d489424516.png
Project:

uses GraphABC, ABCButtons, System, UserFriendly, Canvas;

 
var
  gameBtn: ButtonABC;
  
{Сгенерировать игровой экран}
procedure initGamePage();
begin
  SetWindowSize(500,400); 
  gameBtn.Destroy;
  Window.Clear();
  
  var number := 5;
  var canvas := new RowRectanglesStamp(30,30,50,50,number);
  
  canvas.Stamp;
  for var i:=1 to number-1 do
  begin
    canvas.y += canvas.height - 1;
    canvas.Stamp;
  end;
end; {initGamePage}
{Сгенерировать стартовый экран}
procedure initStartPage();
begin
  SetWindowSize(250,200);
  SetWindowIsFixedSize(true);
  SetWindowCaption('Игра: Балда (вер. 1)');

  gameBtn := generateButton('СТАРТ', 20, clwhite, true, true, 0, 20);
  gameBtn.OnClick := initGamePage;
  
  addText('Игра Балда', 20, clred, false, true, 0, 50);
end; {initStartPage}

begin
  initStartPage;
end.

UserFriendly.pas:
unit UserFriendly;

interface
  uses GraphABC, ABCButtons;
  function generateButton(name: string; fontSize: integer; color: System.Drawing.Color; hCenter: boolean := false; vCenter: boolean := false; x: integer := 0; y: integer := 0): ButtonABC;
  procedure addText(str: string; fontSize: integer; color: System.Drawing.Color; hCenter: boolean := false; vCenter: boolean := false; x: integer := 0; y: integer := 0);
implementation
{Добавление кнопки на полотно}
function generateButton: ButtonABC;
begin
  Font.Size := fontSize;
  var textWidth := TextWidth(name);
  var textHeight := TextHeight(name);
  
  if vCenter=true then
    x += (Window.Width - textWidth) div 2;
  
  if hCenter=true then
    y += (Window.Height - textHeight) div 2;
    
  Result := new ButtonABC(x,y,textWidth,textHeight,name,color);
end; {generateButton}
{Добавление текста на полотно}
procedure addText;
begin
  Font.Size := fontSize;
  Font.Color := color;

  var textWidth := TextWidth(str);
  var textHeight := TextHeight(str);
  
  if vCenter=true then
    x += (Window.Width - textWidth) div 2;
  
  if hCenter=true then
    y += (Window.Height - textHeight) div 2;
  
  TextOut(x, y, str);
end; {addText}

end.

Canvas.pas:
unit Canvas;

interface
  uses GraphABC;
  type 
    RectangleStamp = class
      x,y,w,h: integer;
      constructor (xx,yy,ww,hh: integer);
      begin
        x := xx; y := yy;
        w := ww; h := hh;
      end;
      procedure Stamp;
      begin
        Rectangle(x,y,x+w,y+h);
      end;
    end;
  
  type
    RowRectanglesStamp = class
      x,
      y,
      width,
      height,
      number: integer;
      constructor (_x,_y,_width,_height,_number: integer);
      begin
        x := _x;
        y := _y;
        width := _width;
        height := _height;
        number := _number;
      end;
      procedure Stamp;
      begin
        var _result := new RectangleStamp(x,y,width,height);
        _result.Stamp;
        for var i:=1 to number-1 do
        begin
          _result.x += _result.w - 1;
          _result.Stamp;
        end;
      end;
    end;
  implementation
end.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
RedHairOnMyHead, 2017-01-03
@ThePyzhov

It's too confusing to do Balda through a dictionary. It's easier to make a check by another player: player A entered a word and when he pressed the "OK" button, player B displays a text box with a choice whether to apply the entered word or not (i.e. whether such a word exists or not).
I have never seen anyone use UP, DOWN - arrows. Use the mouse. Track simply the coordinates of the cursor hitting the cell cell, and if it is empty, highlight the caret so that the user can enter a letter. And then launch the word selection by alternately clicking on adjacent letters until the "OK" button is pressed, or apply.

M
Mercury13, 2017-01-03
@Mercury13

The point here is this. Window operating systems operate in event mode: the “redraw” event has arrived, we are drawing; the click event came, we click. But since curricula need to be extremely simple, it’s much more convenient to write: draw, wait for a keystroke - like in the days of DOS, when the processor time was completely yours from the entry point to the transfer of control, when there was direct access to video memory ... For console programs, all this eventfulness hidden, in a cunning way at the OS level, synchronizing two threads: one is responsible for maintaining the console window, the second is the program itself. And Pascal.ABC had to set up the simplest emulation, making "video memory" in the form of an internal buffer.
So you have to connect events - see pascalabc.net/downloads/pabcnethelp/scr/PABCUnits/...- and see what holes there are in this abstraction. And they, according to Spolsky, will definitely be: TCP is a guaranteed delivery protocol, but who guarantees it when the wire is physically pulled out ...

E
Evgeny Shatunov, 2017-01-03
@MarkusD

Makar Gerasimov , dachshund, for starters, switch off from drawing and perceiving the process with your eyes.
The first thing you need is a model of the playing field. Model - from the word "data". First of all, you need to represent the game as a data model, not a visual representation. Here, let's try ...
A notebook sheet in a cage resembles what? - that's right, a two-dimensional array. Therefore, it is convenient to represent the playing field as a two-dimensional array. What is the type of array cell? - since we have a game about words that are spelled in cells, then, perhaps, symbols should be stored in cells.
Krutyak, they came up with a playing field in which symbols are stored! Just note that the symbol in the cell may not be.
Okay, let's move on. We have two players who can write words.
Yeah ... If you write, then you need to know where. The user needs an indication where he will write now - specify the cell in which he will now put the character. And the cells are in a two-dimensional array. It will be most convenient to store the coordinates of the current user cell!
Therefore, the user is, at a minimum, determined by the coordinates of the cell selected by him in the field.
Each user has a turn, during the move the user can change the position of his selected cell and try to enter a word. Therefore, only the selected cell of the current user can be controlled by events from the keys. At the same time, one should not forget to limit the control over the size of the playing field.
When the user enters a word, it is necessary to limit the movement of its selection to neighboring cells only. The user put the first character, that's it, he enters the word.
And mind you, this is all completely untethered from the graphical representation. The first thing you should always think about is the data model.
At this stage of the description, we have a field, users with their highlights, and entered words. The data model gives a complete picture of how to present it to the user.
Well, is it a problem to draw a two-dimensional array of characters? And by coordinates, paint over a cell selected by the user with a different color?
How to let the user highlight words? Once the user has entered their letter, they can be allowed to navigate to cells with letters already entered and mark them as a word. The entered word can either be checked in the dictionary, or asked to confirm the correctness of the word by all users.
When your game data model is at least a little ready, you can easily draw it even in the console, even on the surface of the window.
A model is a set of classes with relationships. Field, game, player, move, word, dictionary, word table... word symbol(?). You know the full list of entities better than me. Just think at the data level. Then you can manage them. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question