I'd made a project using pygame in python around 3 months ago. This was my first big project after I started learning how to program in college.
Now I'd like to know just how good my programming syntax and style is. Whether there are any weird irritants that simply defy the unwritten rules of programming in python. I'd like to know them so that I can nip the evil in the bud.
Could someone please go through the code, and tell me where somethings have been implemented as they should have been and where something else might work better... Or if in general a better (read 'more pythonic'- I've never really understood what people mean when they say that btw.), style would work and what is it.
As of now, the chess works. The only thing is you gotta control both the black and white pieces by yourself.
Please just review the code.
For the parts I think might not be implemented the way they could be:
1) Implementing a timer as:
import time
time_change_var=time.asctime().split()[3].split(":")[2]
time_elapsed=0
then in the main loop of game cycles:
time_change_check=time.asctime().split()[3].split(":")[2]
if time_change_var!=time_change_check:
time_elapsed+=1
time_change_var=time_change_check
time_elapsed_print=font.render("%d:%d"%(time_elapsed/60,time_elapsed%60),True,white)
screen.blit(time_elapsed_print,[730,600])
screen.blit(font.render("Time Elapsed",True,black),[660,565])
2) Taking 2 consecutive clicks from the user that are valid
A click is valid when: (i). First click is on a box containing a piece (ii). Second click is on a box containing either a piece of other colour or is empty I am doing it like this:
input_read=0
if len(m)==4:
X1=m[0] #assigning final inputs
Y1=m[1]
X2=m[2]
Y2=m[3]
m=[] # empty the buffer input list
input_read=1
if not input_read:
event=pygame.event.wait()
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
if len(m)==2:
if event.button==1:
x1,y1=-1,-1
pos = pygame.mouse.get_pos()
x2 = pos[0]/80+1
y2 = pos[1]/80+1
m += [x2] + [y2]
moves=[]
else:
if event.button==1:
pos = pygame.mouse.get_pos()
x1 = pos[0]/80+1
y1 = pos[1]/80+1
for elements in pieces_in_play:
if x1 == elements.x and y1 == elements.y:
if elements.colour==col:
f=elements
m += [x1] + [y1]
3) Building classes for the different pieces
I have built a class for each of the different type of piece. The class contains the object's name, colour,type,survival,x,y positions and the corresponding function which checks whether its possible to move the piece according to the input.It also kills the opponents piece if its already there on the destination box.
class BlackBishop(object):
n=1
def __init__(self):
self.name=int("2"+str(BlackBishop.n)+"0")
self.colour = 0
self.type = 2
self.survive = 1
self.y=8
if BlackBishop.n==1:
self.x=3
else:
self.x=6
BlackBishop.n += 1
self.image = pygame.image.load("./images/bishop_b.png")
def check(self,X1,Y1,X2,Y2):
if (1<=X1<=8 and 1<=Y1<=8 and 1<=X2<=8 and 1<=Y2<=8):
if abs(Y2-Y1)==abs(X2-X1):
m=(Y2-Y1)/(X2-X1)
q=[]
t=[]
if X2>X1:
x=X1
if Y2>Y1:
y=Y1
q.append((x,y))
while (x!=X2 and y!=Y2)and(x>0 and y>0):
x+=1
y+=1
q.append((x,y))
else:
y=Y1
q.append((x,y))
while (x!=X2 and y!=Y2)and(x>0 and y>0):
x+=1
y-=1
q.append((x,y))
else:
x=X1
if Y2>Y1:
y=Y1
q.append((x,y))
while (x!=X2 and y!=Y2)and(x>0 and y>0):
x-=1
y+=1
q.append((x,y))
else:
y=Y1
q.append((x,y))
while(x!=X2 and y!=Y2)and(x>0 and y>0):
x-=1
y-=1
q.append((x,y))
for coordinate in q:
for check_piece in pieces_in_play:
if coordinate==(check_piece.x,check_piece.y):
if check_piece!=self:
if check_piece.colour==0:
return 0
elif check_piece.colour==1 and coordinate==(X2,Y2):
check_piece.survive=0
killcount(check_piece)
return 1
else:
return 0
else:
return 1
else:
return 0
else:
return -1