-1

I'm trying to code chess in python using pygame. I have the board, pieces and graphic board successfully designed. I'm trying to create a event that tracks and saves the mouse movements. I'm calling a function and passing event, and postion, but it just crashes my program, any advice...?

Error:

'pygame.event.Event' object has no attribute 'pos'

import pygame
import sys

from const import *
from game import Game
from dragger import Dragger


    


class Main:

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode( (WIDTH, HEIGHT) )
        pygame.display.set_caption("Chess")
        self.game = Game()

    

    def mainloop(self):

        screen = self.screen
        game = self.game
        board = self.game.board
        dragger = self.game.dragger
        

        while True:
           self.game.show_bg(screen)
           game.show_pieces(screen)

           if dragger.dragging:
                dragger.update_blit(screen)


           
           for event in pygame.event.get():
                #click
                if event.type == pygame.MOUSEBUTTONDOWN:
                    dragger.update_mouse(event.pos)

                clicked_row = dragger.mouseY // SQSIZE
                clicked_col = dragger.mouseX // SQSIZE

                #if clicked square has a piece
                if board.squares[clicked_row][clicked_col].has_piece():
                    piece = board.squares[clicked_row][clicked_col].piece
                    dragger.save_initial(event.pos)# (this code crashes program)
                    dragger.drag_piece(piece)
                    
                    
    

                #mouse motion
                elif event.type == pygame.MOUSEMOTION:
                      if dragger.dragging:
                          dragger.update_mouse(event.pos)
                          game.show_bg(screen)
                          game.show_pieces(screen)
                          dragger.update_blit(screen)
                #click release
                elif event.type == pygame.MOUSEBUTTONUP:
                    dragger.undrag_piece()
                #quit application
                elif event.type == pygame.QUIT:
                    
                    pygame.quit()
                    sys.exit()

        
       
       
           pygame.display.update()
        




main = Main()
main.mainloop()

Ive commented the line of code out and the program runs successful,

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Looks like an indentation issue - the lines starting from and including `clicked_row = dragger.mouseY // SQSIZE` up to but **not** including `elif event.type == pygame.MOUSEMOTION:` should be indented an additional level? – Iain Shelvington Mar 30 '23 at 02:42

0 Answers0