Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
import os
import pygame
import math
import random
import time
##GLOBAL CONSTANTS
size=(640,1020)
w,h=size=(int(size[0]),int(size[1]))
##GLOBAL VAR
mouseTilePos=[0,0]
moveStartPos=[-1,-1]
moving=False
lastMove=[]
startPos=[["BR","BN","BB","BK","BQ","BB","BN","BR"],
["BP","BP","BP","BP","BP","BP","BP","BP"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["WP","WP","WP","WP","WP","WP","WP","WP"],
["WR","WN","WB","WK","WQ","WB","WN","WR"]]
corrImg={
"BP":pygame.image.load("blackPawn.png"),
"BB":pygame.image.load("blackBishop.png"),
"BN":pygame.image.load("blackKnight.png"),
"BR":pygame.image.load("blackRook.png"),
"BQ":pygame.image.load("blackQueen.png"),
"BK":pygame.image.load("blackKing.png"),
"WP":pygame.image.load("whitePawn.png"),
"WB":pygame.image.load("whiteBishop.png"),
"WN":pygame.image.load("whiteKnight.png"),
"WR":pygame.image.load("whiteRook.png"),
"WQ":pygame.image.load("whiteQueen.png"),
"WK":pygame.image.load("whiteKing.png")
}##The dictionary containing the images
##PYGAME AND WINDOW SETUP
pygame.display.set_caption(str("Sphinx"))
wn=pygame.display.set_mode((w,h))
pygame.init()
##Utility Functions
def upscaleImages(sizeMul=2,origSize=22): ## Corregge la grandezza delle immagini
for elem in corrImg:
corrImg[elem]=pygame.transform.scale(corrImg[elem],(origSize*sizeMul,origSize*sizeMul))
def checkBoundaries(move): ## I AM SUCH AN MLG PRO PROGRAMMER INVECE DI METTERE 8 COSE NELL'IF HO MESSO 2*2*2=8 COmbinazioni col for. Ci sarei stato di meno senza? Si. Lo faccio comunque? YAAAAAAAAAAH
print("Move: ",move)
for i in [0,1]:
for k in [0,1]:
if move[i][k]<0 or move[i][k]>7:
return False
return True
def handleMovement(mPos, click, board):
global moving
global moveStartPos
global lastMove
if click:
if moving:
print("Trascinando")
else:
moving=True
moveStartPos=mPos
else:
if moving:
tempMove=[moveStartPos,mPos]
if board.move(tempMove):
lastMove=tempMove
Saver.saveMove(lastMove)
moveStartPos=(-1,-1)
moving=False
def color(charPiece):##MOLTO UTILE
return charPiece[0]
##CLASSES
##The board class.
class Board:
charBoard=[] ##The array containing pieces.
def __init__(self,board="__unset"): ##INITIALIZATION
if board == "__unset": ##EMPTY BOARD
for i in range(8):
toApp=[]
for k in range(8):
toApp.append("VV")
self.charBoard.append(toApp)
else:
self.charBoard=board ##PRESET BOARD
def checkForChecks(self,move):
return False
def checkLegal(self,move): ##Checks if a move is legal
if checkBoundaries(move): ##Checks if the move within the board boundaries
if not self.checkForChecks(move): ##Checks if the move makes the king in check
return True ##Move is good
return False ##Move is bad
def getMoveList(self):
res=[]
for y in range(len(self.charBoard)):
for x in range(len(self.charBoard[y])):
if self.charBoard[y][x] != "VV":
toApp= MoveBook.possible(self,(x,y))
if toApp != []:
res.append(toApp)
return res
def move(self,mv,check=True):
if check:
if not self.checkLegal(mv):
return False
if color(self.charBoard[mv[0][1]][mv[0][0]]) == color(self.charBoard[mv[1][1]][mv[1][0]]):
return False
self.charBoard[mv[1][1]][mv[1][0]]=self.charBoard[mv[0][1]][mv[0][0]]
self.charBoard[mv[0][1]][mv[0][0]]="VV"
return True
def drawPieces(board):
for y in range(len(board)):
for x in range(len(board[y])):
piece=board[y][x]
if piece != "VV":
wn.blit(corrImg[board[y][x]],(64*x+76 - 2,64*y+76 - 2))
def drawBoard():
black=0
pygame.draw.rect(wn,(45,45,45),(0,0,w,h))
for y in range(8):
black=1-black
for x in range(8):
black=1-black
if moveStartPos[0]!= -1 and moveStartPos[1]==x and moveStartPos[0]==y:
pygame.draw.rect(wn,(255,13,27),(64+64*moveStartPos[1],64+64*moveStartPos[0],64,64))
elif len(lastMove)>0 and ((x,y) in lastMove):
pygame.draw.rect(wn,(215,136,29),(64+64*x,64+64*y,64,64))
elif((x,y)==mouseTilePos):
pygame.draw.rect(wn,(255,216,0),(64+64*x,64+64*y,64,64))
else:
pygame.draw.rect(wn,(255*black,255*black,255*black),(64+64*x,64+64*y,64,64))
def draw(self):
Board.drawBoard()
Board.drawPieces(self.charBoard)
##THE SAVE CLASS
class Saver:
saves=[]
curr=0
def initialize():
saves=[]
curr=0
def exec(moves,board):
board.charBoard=[["BR","BN","BB","BK","BQ","BB","BN","BR"],
["BP","BP","BP","BP","BP","BP","BP","BP"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["VV","VV","VV","VV","VV","VV","VV","VV"],
["WP","WP","WP","WP","WP","WP","WP","WP"],
["WR","WN","WB","WK","WQ","WB","WN","WR"]]
for i in range(moves):
board.move(Saver.saves[i],False)
Saver.saves=Savee.saves[:moves]
if(len(Saver.saves)>0):
lastmove=Saver.saves[-1]
else:
lastmove=[]
Saver.curr=moves
def saveMove(move):
Saver.saves.append(move)
Saver.curr+=1
def takeBack():
Saver.exec(Saver.curr-1)
class MoveBook:
def possible(board,piecePos):
piece=board.charBoard[piecePos[1]][piecePos[0]]
col=piece[0]
pieceKind=piece[1]
x=piecePos[0]
y=piecePos[1]
res=[]
print("Analyzing piece "+pieceKind+", color "+col+", Position :",(x,y))
if pieceKind=="P": #PAWN
try:
if color(board.charBoard[y][x+1]) != col and color(board.charBoard[y][x+1])!="V":
print("PawnTakes")
res.append([piecePos,(x+1,y)])
except:
print("Non disp :D")
try:
if color(board.charBoard[y][x-1])!= col and color(board.charBoard[y][x-1])!="V":
print("PawnTakes")
res.append([piecePos,(x-1,y)])
except:
print("Non disp :D")
try:
if color(board.charBoard[y+(1-2*(col=="W"))][x]) != col:
print("MoveUp")
res.append([piecePos,(x,y+(1-2*(col=="W")))])
try:
if color(board.charBoard[y+2*(1-2*(col=="W"))][x]) != col:
print("MoveUp2")
res.append([piecePos,(x,y+2*(1-2*(col=="W")))])
except:
print("Non disp :D")
except:
print("Non disp :D")
elif pieceKind=="K": #KING
for i in [-1,0,1]:
for k in [-1,0,1]:
try:
if(y+i>=0 and x+k>=0):
print("King is checking for neighbour: ",(x+k,y+i))
print("Founded: ",board.charBoard[y+i][x+k])
if i==0 and k==0:
print("This is self")
next
elif color(board.charBoard[y+i][x+k]) != col and color(board.charBoard[y+i][x+k]) != "V":
res.append([piecePos,(x+k,y+i)])
print("K move found")
except:
print("Non disp :D")
return res
board=Board(startPos)
print(board.getMoveList())
def main (fps=60):
global moving
board=Board(startPos) ##Our main board
running = True
clock=pygame.time.Clock()
upscaleImages()
while running:
pos = pygame.mouse.get_pos()
mouseLeftDown, pressed2, pressed3 = pygame.mouse.get_pressed()
handleMovement(pos,mouseLeftDown,board)
board.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
elif event.type == pygame.KEYUP :
if not moving and event.key == pygame.K_LEFT :
Saver.takeBack()
pygame.display.update()
clock.tick(fps)
main()
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team