Recent

Author Topic: Drawing a chessboard  (Read 999 times)

Soob

  • New member
  • *
  • Posts: 8
Drawing a chessboard
« on: May 29, 2020, 09:15:48 pm »
Hi,
I realize there have probably been a lot of topics like that but I would like to ask you something. I underline that I don't need ready-written codes but I'd want to have some advice.

I would like to draw a simple 8x8 chessboard. I know it's probably an amateur-level task for many of y'all here but I always sucked when it comes to drawing in Pascal. I simply don't know how the drawing works. But drawing is the one problem cause even if I used canvas or TShape and drew the chessboard I don't know how to "communicate" with a particular squares, for example I would like to click on e4 square and the program should signal that e4 has been clicked.

Could someone try and explain to me where should I start with it? I would really appreciate that! :)

lucamar

  • Hero Member
  • *****
  • Posts: 2920
Re: Drawing a chessboard
« Reply #1 on: May 29, 2020, 09:25:01 pm »
A simple way would be to use one TShape per square (perhaps inside a TPanel to keep them together efforlestly), setting its Tag to some number, either consecutive 1..64 or as a mask of 6+6 bits refencing the column/row. Then, in a common OnClick event handler, you can interrogate the Tag to know which square was clicked. Shouldn't be too difficult ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

TRon

  • Sr. Member
  • ****
  • Posts: 270
Re: Drawing a chessboard
« Reply #2 on: May 29, 2020, 09:36:06 pm »
A static chess-board ? i think i would go for a TDrawGrid https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tdrawgrid.html

Handoko

  • Hero Member
  • *****
  • Posts: 3543
  • My goal: build my own game engine using Lazarus
Re: Drawing a chessboard
« Reply #3 on: May 29, 2020, 09:38:20 pm »
@Soob

In game programming, you do not manipulate objects directly on the screen. Instead you create a virtual world, let's call it game world.

In most cases, you can use a 2 dimension array for the game world. Each position in the array (let's call it cell), can be occupied by an object. But for more complex games, each cell can have more than one object.

If you don't mind following a long boring tutorial, I recommend you to read the posts I wrote to explain a simple snake game:
https://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.html#msg258381

In the snake game tutorial, each cell can be empty or occupied by a snake's body or a fruit. That's why:

Type
  TItem = (Empty, Snake, Fruit)
;

var
  GameWorld: array[1..WorldWidth, 1..WorldHeight] of TItem;

Note:
For beginner friendly reason, the array start from 1. But 'real' programmers count from 0.

Here is the most important thing when write a game:

Don't manipulate objects directly on the screen. Instead, you do it on the game world. Then write a procedure to draw the game world to the screen. When user moves the mouse or presses a button or key, process the user input based on the game world not the screen.

Have fun.

---edit---
Lucamar's suggestion is okay. It is beginner friendly, it starts simpler and easier but when you adds more features, it will be less (perfomance) efficient and harder to maintain.
« Last Edit: May 30, 2020, 06:48:13 am by Handoko »

Soob

  • New member
  • *
  • Posts: 8
Re: Drawing a chessboard
« Reply #4 on: May 29, 2020, 09:49:23 pm »
Thank you guys for such a quick response. I'll think it through and see what I'll come up with.

I'm not a professional coder and I'll never be one and sometimes I find it hard when it comes to visualization of something. Even though I can make some more complex things, I still struggle with drawing etc 'cause it seems kind of unnatural for me.

I'm going to read the snake post now and see if it makes it more clear for me :)
thanks again!

btw. @Handoko, I know I can create an array of squares but I want it to appear on the screen. If I can create it only virtual, it won't appear on my screen and my biggest trouble so far here is to draw it correctly
« Last Edit: May 29, 2020, 09:54:51 pm by Soob »

Handoko

  • Hero Member
  • *****
  • Posts: 3543
  • My goal: build my own game engine using Lazarus
Re: Drawing a chessboard
« Reply #5 on: May 29, 2020, 09:56:51 pm »
Try to understand the DrawGameWorld procedure in the code I provided in the link.

lucamar

  • Hero Member
  • *****
  • Posts: 2920
Re: Drawing a chessboard
« Reply #6 on: May 30, 2020, 03:05:28 am »
Frankly, for a chess game I wouldn't bother much with the drawing unless you want some "battle chess" kind of thing.

Unlike "action" games, even Handoko's relatively "simple" snake demo, in chess and alike it's much more important the "engine" (the part that plays chess or whatever) and drawing is (can be) rather simple (draw grid, draw pieces); you don't need extremely short drawing times to make the game responsive.

Even so, it's rather simpe to draw the board in a bitmap at the start of the program, and for each move copy the board to another bitmap, overlay the pieces, copy (assign) to, say, a TImage.Picture and get the position of a click (and calculate the square it's in) in the OnMouseDown/Up event. That's another way to do it, BTW  ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Fred vS

  • Hero Member
  • *****
  • Posts: 1760
    • StrumPract is the musicians best friend
Re: Drawing a chessboard
« Reply #7 on: May 30, 2020, 04:44:57 am »
Hello.

There are the excellent Chess Pascal engines of Roland Chastain:

https://github.com/rchastain/alouette

https://github.com/rchastain/durandal

https://github.com/rchastain/moustique


And his great UCI chess GUI developed in Pascal too:

https://github.com/rchastain/eschecs

« Last Edit: May 30, 2020, 04:47:21 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs

Handoko

  • Hero Member
  • *****
  • Posts: 3543
  • My goal: build my own game engine using Lazarus
Re: Drawing a chessboard
« Reply #8 on: May 30, 2020, 06:09:46 am »
I always sucked when it comes to drawing in Pascal. I simply don't know how the drawing works.

You can use TCanvas (on your mainform: Form1.Canvas). TCanvas is a bit slow but should be enough for simple games. One of the great thing about TCanvas is, it is relatively easy to use compare to the others.
https://wiki.lazarus.freepascal.org/TCanvas


I don't know how to "communicate" with a particular squares, for example I would like to click on e4 square and the program should signal that e4 has been clicked.

These actually are 2 things you need to solve:
- Highlight the cell (signal that e4 has been clicked)
- Detect which cell the mouse is currently hovering

Highlighting the Cell

If you use my approach, this can be easily done. You just need to add ActiveX and ActiveY variables, and in the DrawGameWorld procedure, you draw a something on the cell[ActiveX, ActiveY]. Maybe a red rectangle to mark that the cell is selected.

Detecting Which Cell the Mouse is Currently Hovering

This one is a bit difficult if you are not good in math. You need to know on which PosX and PosY the cell start to draw, the CellSize and the MouseX, MouseY position.

For example:

PosX := 20; // Cell [0, 0] start to draw on this location
PosY := 30; // Cell [0, 0] start to draw on this location
CellSize := 40; // The width and height of a single cell
MouseX := 300;
MouseY := 200;

// The calculation becomes
HoveringX := (300 - 20) div CellSize
HoveringY := (200 - 30) div CellSize


If (HoveringX < 0) or (HoveringY < 0) or (HoveringX > MaxX) or (HoveringY > MaxY) then the mouse pointer is not hovering on any cell.

Thaddy

  • Hero Member
  • *****
  • Posts: 10189
Re: Drawing a chessboard
« Reply #9 on: May 30, 2020, 09:54:24 am »
Also note you can use a font to draw the pieces:
http://www.enpassant.dk/chess/fonteng.htm
I am more like donkey than shrek

Soob

  • New member
  • *
  • Posts: 8
Re: Drawing a chessboard
« Reply #10 on: May 31, 2020, 05:50:15 pm »
thanks guys for recommendations but I don't want any pieces ;) It would be related to chess but no pieces are needed in my project ;)

Fred vS

  • Hero Member
  • *****
  • Posts: 1760
    • StrumPract is the musicians best friend
Re: Drawing a chessboard
« Reply #11 on: May 31, 2020, 05:59:27 pm »
It would be related to chess but no pieces are needed in my project ;)

Did you read the post about Chess Engines?

https://forum.lazarus.freepascal.org/index.php/topic,50016.msg364151.html#msg364151

I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs

Thaddy

  • Hero Member
  • *****
  • Posts: 10189
Re: Drawing a chessboard
« Reply #12 on: May 31, 2020, 06:05:49 pm »
I guess no, because those (and some more) are excellent.

 we gave him al that he needs to know:
- How to draw a board
- How to draw the pieces
- Examples of chess engines written in Pascal.

What does he need more?

(note chess players are usually good coders too, so I doubt his chess ratings. Anyway good questions get good answers.)
« Last Edit: May 31, 2020, 06:10:33 pm by Thaddy »
I am more like donkey than shrek

Fred vS

  • Hero Member
  • *****
  • Posts: 1760
    • StrumPract is the musicians best friend
Re: Drawing a chessboard
« Reply #13 on: May 31, 2020, 06:11:08 pm »
because those (and some more) are excellent.

And for me even too excellent.
I did not win once against  each of Roland engines.  :-X
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 1760
    • StrumPract is the musicians best friend
Re: Drawing a chessboard
« Reply #14 on: May 31, 2020, 06:23:47 pm »
Imho, very interesting too is the console application:

https://github.com/rchastain/eschecs-console

It makes play engine vs engine.
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs

 

TinyPortal © 2005-2018