OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pygame Offset Grid by Fraction or Pixels [duplicate]

  • Thread starter Thread starter Nathan Kamprath
  • Start date Start date
N

Nathan Kamprath

Guest
I am working on a Chess block by following a YT video, but I want my board more centered. I did that, however, now my event.get() event.pos isn't working quite right. About 15 pixels from the bottom of the square actually is considered the square below (The board is offset at the top by 15 pixels). While I could just move the board up, I would like to keep the board where it is. Is there a way to offset the pygame coordinates? Is there a way to get a decimal to work with my code in the y-axis? If the code below is too long, I have attached the link to my github Chess Folder on GitHub

Code:
import pygame as pg

pg.init()

width = 1500
height = 1000
screen = pg.display.set_mode([width, height])
pg.display.set_caption("Play Chess")
font = pg.font.Font("freesansbold.ttf", 20)
big_font = pg.font.Font("freesansbold.ttf", 50)
timer = pg.time.Clock()
fps = 60
blocks = 8 ** 2
turn_select = 0
selection = 100
valid_moves = []

def Draw_Board():
    for i in range(blocks // 2):
        column = i % 4
        row = i // 4

        if row % 2 == 0:
            pg.draw.rect(screen, 'cadetblue', [900 - (column * 200), (row * 100) + 15, 100, 100])
        else:
            pg.draw.rect(screen, 'cadetblue', [1000 - (column * 200), (row * 100) + 15, 100, 100])

        if (row + 1) % 2 == 0:
            pg.draw.rect(screen, 'silver', [900 - (column * 200), (row * 100) + 15, 100, 100])
        else:
            pg.draw.rect(screen, 'silver', [1000 - (column * 200), (row * 100) + 15, 100, 100])


running = True
while running:
    timer.tick(fps)
    screen.fill("darkgreen")

    Draw_Board()
    if turn_select < 2:
        if selection < 2:
                pg.draw.rect(
                    screen, 'red', 
                    [white_loc[i][0] * 100 + 300, 
                        white_loc[i][1] * 100 + 16, 99, 99], 1)
    
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False

        if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            x_coor = event.pos[0] // 100 - 3
            y_coor = event.pos[1] // 100 
            #I want to put -0.15 here but it won't work
            
            clk_coor = (x_coor, y_coor)

            #For White
            if turn_select < 2:
                if clk_coor in white_loc:
                    selection = white_loc.index(clk_coor)
                    if turn_select == 0:
                        turn_select = 1
                if clk_coor in valid_moves and selection != 100:
                    white_loc[selection] = clk_coor
                    if clk_coor in black_loc:
                        black_piece = black_loc.index(clk_coor)
                        captured_white.append(black_pieces[black_piece])
                        black_pieces.pop(black_piece)
                        black_loc.pop(black_piece)
                    
                    black_ops = check_ops(black_pieces, black_loc, 'black')
                    white_ops = check_ops(white_pieces, white_loc, 'white')
                    turn_select = 2
                    selection = 100
                    valid_moves = []

    pg.display.flip()
pg.quit()
<p>I am working on a Chess block by following a YT video, but I want my board more centered. I did that, however, now my event.get() event.pos isn't working quite right. About 15 pixels from the bottom of the square actually is considered the square below (The board is offset at the top by 15 pixels). While I could just move the board up, I would like to keep the board where it is. Is there a way to offset the pygame coordinates? Is there a way to get a decimal to work with my code in the y-axis? If the code below is too long, I have attached the link to my github <a href="https://github.com/NathanKamprath/Minigames/tree/main/Chess" rel="nofollow noreferrer">Chess Folder on GitHub</a></p>
<pre><code>import pygame as pg

pg.init()

width = 1500
height = 1000
screen = pg.display.set_mode([width, height])
pg.display.set_caption("Play Chess")
font = pg.font.Font("freesansbold.ttf", 20)
big_font = pg.font.Font("freesansbold.ttf", 50)
timer = pg.time.Clock()
fps = 60
blocks = 8 ** 2
turn_select = 0
selection = 100
valid_moves = []

def Draw_Board():
for i in range(blocks // 2):
column = i % 4
row = i // 4

if row % 2 == 0:
pg.draw.rect(screen, 'cadetblue', [900 - (column * 200), (row * 100) + 15, 100, 100])
else:
pg.draw.rect(screen, 'cadetblue', [1000 - (column * 200), (row * 100) + 15, 100, 100])

if (row + 1) % 2 == 0:
pg.draw.rect(screen, 'silver', [900 - (column * 200), (row * 100) + 15, 100, 100])
else:
pg.draw.rect(screen, 'silver', [1000 - (column * 200), (row * 100) + 15, 100, 100])


running = True
while running:
timer.tick(fps)
screen.fill("darkgreen")

Draw_Board()
if turn_select < 2:
if selection < 2:
pg.draw.rect(
screen, 'red',
[white_loc[0] * 100 + 300,
white_loc[1] * 100 + 16, 99, 99], 1)

for event in pg.event.get():
if event.type == pg.QUIT:
running = False

if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
x_coor = event.pos[0] // 100 - 3
y_coor = event.pos[1] // 100
#I want to put -0.15 here but it won't work

clk_coor = (x_coor, y_coor)

#For White
if turn_select < 2:
if clk_coor in white_loc:
selection = white_loc.index(clk_coor)
if turn_select == 0:
turn_select = 1
if clk_coor in valid_moves and selection != 100:
white_loc[selection] = clk_coor
if clk_coor in black_loc:
black_piece = black_loc.index(clk_coor)
captured_white.append(black_pieces[black_piece])
black_pieces.pop(black_piece)
black_loc.pop(black_piece)

black_ops = check_ops(black_pieces, black_loc, 'black')
white_ops = check_ops(white_pieces, white_loc, 'white')
turn_select = 2
selection = 100
valid_moves = []

pg.display.flip()
pg.quit()


</code></pre>
 

Latest posts

I
Replies
0
Views
1
Isaac P. Liu
I
U
Replies
0
Views
1
user3658366
U
G
Replies
0
Views
1
Giampaolo Levorato
G
M
Replies
0
Views
1
Marcelo Rodrigo Nascimento
M
Top