'using methods of differents objects in pygame in a while True, chess project [duplicate]
I'm new to pygame, and OOP in python ( used a lot of java before) For a personal project, i'm trying to make a chess game, but I have a problem.
I'm displaying my board with an array of values. The pieces are where they should be.
Now, I'm trying to moove theses pieces, and to do so, the first step is to be able to return the name of the case I am clicking in.
But the fact is that it's not working every time. I'm using a while True
statement to get the event, and if my mouse cursos is in the dimensions of the case, it's supposed to return the name of the case.
The fact that this is in a while True
statement, updates every time the case that is calling this method, and if it's not the right case that is called when i'm clicking, the method don't return the name of the case.
# initialisation board
def initBoard() : # if <0 white, >0 black, 1= Pun , 5 = Tower,2= Knight,3 Bishop, 9 = Queen , 50 = King
board =[[-5,-2,-3,-9,-50,-3,-2,-5],
[-1,-1,-1,-1,-1,-1,-1,-1],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1],
[5,2,3,50,9,3,2,5]]
return board
board = initBoard()
class Case:
def __init__(self,ecran,nom,x,y,color,piece):
self.nom = nom
self.x = x
self.y = y
self.piece = piece
self.color =color
# load piece
#choose the right name of the piece i want to use, it works so it's useless to show you this
def getNom(self):
return self.nom
def activeCase(self):
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
continuer = False
if event.type == pygame.MOUSEBUTTONDOWN:
x,y = pygame.mouse.get_pos()
print ("cursor is in : ",x,y)
print ("you are in case : ", self.nom)
if self.x <= x <= self.x+dimensionCase and self.y <= y <= self.y+dimensionCase: # if cursor in case , return the name of the case
print(self.nom)
#constants
dimensions =(700,700)
dimensionCase = 70
ecran = pygame.display.set_mode(dimensions)
continuer = True
listeCases = [] # array of cases I will create
letter=65 # ASCII CODE for letter A, will be usefull for piece name
number=8 #number's name of piece (will begin to A8, B8 , C8, ... ,A1)
yInitial,xInitiale =100,100 # where begins the piece's placement
pygame.draw.rect(ecran,(100,100,100), (0,0,800,800)) # background
while continuer:
letter=65 # ASCII CODE for letter A, will be usefull for piece name
number=8 #number's name of piece (will begin to A8, B8 , C8, ... ,A1)
x,y= xInitiale,yInitial
for i in range (8):
x=xInitiale
for j in range (8):
if (i + j) %2 == 0 : couleur = (201,209,242) # color
else : couleur = (89, 113, 212) # color
nomCase= chr(letter)+str(number)## case name ( to get the name from A8 to H1)
letter+=1 ##increase the letter for name
case= Case(ecran,nomCase,x,y,couleur,board[i][j]) #creation of the case
case.activeCase() # activate the case I just created
listeCases.append(case)
pygame.display.flip() # actualise
x+=dimensionCase #increase of case dimension for placement
y+=dimensionCase #increase y for placment
letter=65 #piece letter
number-=1 ## piece number
for event in pygame.event.get(): ## THIS PART IS NOT WORKING aymore Idk why
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
continuer = False
if event.type == pygame.MOUSEBUTTONUP:
x,y = pygame.mouse.get_pos()
for cases in listeCases:
print(cases.activeCase()) #why this doesn't work ?
pygame.quit()
When I click multiple times in the same case (A1) :
cursor is in : 140 131
you are in case : G8
cursor is in : 140 131
you are in case : G6
cursor is in : 140 131
you are in case : F2
cursor is in : 140 131
you are in case : D8
Tkanks for your help
Solution 1:[1]
Indeed, I used for event in pygame.event.get():
at 2 differents times. All I had to do was removing for event in pygame.event.get():
from my method and usings parmeters to receive event
def activeCase(self,event):
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
continuer = False
if event.type == pygame.MOUSEBUTTONDOWN:
x,y = pygame.mouse.get_pos()
if self.x <= x <= self.x+dimensionCase and self.y <= y <= self.y+dimensionCase: # if cursos in case , return the name of the case
print(self.nom)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Zakozak90 |