Python Forum
Moving chess piece using mouse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving chess piece using mouse
#8
 import pygame

pygame.init() 

#-- window dimension --#
screen_width = 720 
screen_height = 720 

#-- colours RGB --#
black = (0,0,0)
red =(255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
mediumpurple = (147,112,219)
sgigray96 = (244,244,244)
bright_black = (100,0,0)
bright_mediumpurple = (247,112,219)
bright_sgigray96 = (255,100,255)
bright_red = (155,0,0)

gameScreen = pygame.display.set_mode((screen_width,screen_height)) 
pygame.display.set_caption('CHESS') 
clock = pygame.time.Clock() 
MENU = pygame.image.load('MENU BOI.png')
w_pawn = pygame.image.load('chess_piece_pawn.png')

##def clik_n_drag():
##    x = 30
##    y = 575
##    x_width = 54
##    y_height = 54
##    
##    mouse = pygame.mouse.get_pos()
##    clik = pygame.mouse.get_pressed()
##    drag = 0
##
##    gameScreen.blit(w_pawn, (x,y))
##
##    if clik[0] == 1 and x + x_width > mouse[0] > x and y + y_height > mouse[1] > y:
##        drag = 1
##    elif clik[0] == 0:
##        drag = 0
##    elif drag == 1:
##        x = mouse[0] - (x_width/2)
##        y = mouse[1] - (y_height/2)

def clik_n_drag():
    x = 30
    y = 575
    gameScreen.blit(w_pawn, (x,y,))
    clik = pygame.mouse.get_pressed()
    (x_new, y_new) = pygame.mouse.get_pos()
    drag = False

    if drag == False:
        if clik[0] == 1:
            drag = True

    if drag == True:
        if clik[0] == 1:
            (x, y) = (x_new, y_new)
        else:
            drag = False

def text_objectives(text,font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

#def result_display(text):
    

def menu(x,y):
    gameScreen.blit(MENU, (x,y))

x = 0
y = 0
     
def bttn(msg,x,y,w,h,ina,act,action=None):
    mouse = pygame.mouse.get_pos()
    clik = pygame.mouse.get_pressed()
    #print(mouse)

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameScreen,act,(x,y,w,h))
        if clik[0] == 1 and action != None:
            if action == "exc":
                specialquit()
            elif action == "ss":
                statsPage()
     
            elif action == "gm1":
                classicMode()
            elif action == "gm2":
                mayhemMode()
            elif action == "gm3":
                dumbAIMode()

            elif action == "ins1":
                classicInfo()
            elif action == "ins2":
                mayhemInfo()
            elif action == "ins3":
                dumbAIInfo()
            elif action == "mm":
                menuLoop()
                
    else:
        pygame.draw.rect(gameScreen,ina,(x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf",15)
    textSurf, textRect = text_objectives(msg,smallText)
    textRect.center = ((x+(w/2)),(y+(h/2)))
    gameScreen.blit(textSurf, textRect)

def specialquit():
    pygame.quit()
    quit()

def menuLoop():
    game = False

    while not game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game = True

        gameScreen.fill(white) 
        menu(x,y)

        bttn("classic", 190,545,280,70, black,bright_black, "gm1")
        bttn("mayhem", 30,630,210,60, mediumpurple,bright_mediumpurple, "gm2")
        bttn("dumb AI", 260,630,210,60, black,bright_black, "gm3")
        bttn("classic", 515,600,180,45, black,bright_black, "ins1")
        bttn("mayhem", 515,650,85,45, mediumpurple,bright_mediumpurple, "ins2")
        bttn("dumb AI", 610,650,85,45, black,bright_black, "ins3")
        bttn("exit", 630,15,75,60, red,bright_red, "exc")
        bttn("stats", 630,85,75,60, sgigray96,bright_sgigray96, "ss")
        
        pygame.display.update()
        clock.tick(60) 

    pygame.quit() 
    quit()

def classicMode():
    im = pygame.image.load('classic_gamemode_screen.png')
    game = False

    while not game:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                game = True

        gameScreen.fill(white)
        gameScreen.blit(im, (x,y))
        bttn("main menu", 520,215,185,65, sgigray96,bright_sgigray96, "mm")
        clik_n_drag()
        pygame.display.update()
        clock.tick(60)
        
def mayhemMode():
    im = pygame.image.load('mayhem_gamemode_screen.png')
    game = False

    while not game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                game = True

        gameScreen.fill(white)
        gameScreen.blit(im, (x,y))
        bttn("main menu", 520,195,185,40, sgigray96,bright_sgigray96, "mm")
        pygame.display.update() 
        clock.tick(60)
        
def dumbAIMode():
    im = pygame.image.load('dumbAI_gamemode_screen.png')
    game = False

    while not game:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                game = True

        gameScreen.fill(white)
        gameScreen.blit(im, (x,y))
        bttn("main menu", 520,215,185,65, sgigray96,bright_sgigray96, "mm")
        pygame.display.update()
        clock.tick(60)

def statsPage():
    im = pygame.image.load('stats_screen.png')
    game = False

    while not game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                game = True

        gameScreen.fill(white)
        gameScreen.blit(im, (x,y))
        bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
        pygame.display.update()
        clock.tick(60)

def classicInfo():
    im = pygame.image.load('classic_ins_screen.png')
    game = False

    while not game:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                game = True
    
        gameScreen.fill(white) 
        gameScreen.blit(im, (x,y))
        bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
        pygame.display.update()

def mayhemInfo():
    im = pygame.image.load('mayhem_ins_screen.png')
    game = False

    while not game:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                game = True
    
        gameScreen.fill(white) 
        gameScreen.blit(im, (x,y))
        bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
        pygame.display.update()

def dumbAIInfo():
    im = pygame.image.load('dumbAI_ins_screen.png')
    game = False

    while not game:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                game = True
    
        gameScreen.fill(white) 
        gameScreen.blit(im, (x,y))
        bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
        pygame.display.update()

menuLoop()
pygame.quit()
quit() 
This is all what I've got, which are the GUIs, if it helps.
Reply


Messages In This Thread
Moving chess piece using mouse - by reepio13 - Feb-24-2018, 05:47 PM
RE: Moving chess piece using mouse - by nilamo - Feb-24-2018, 06:30 PM
RE: Moving chess piece using mouse - by reepio13 - Feb-24-2018, 06:38 PM
RE: Moving chess piece using mouse - by nilamo - Feb-24-2018, 06:41 PM
RE: Moving chess piece using mouse - by metulburr - Feb-24-2018, 07:39 PM
RE: Moving chess piece using mouse - by reepio13 - Feb-24-2018, 08:53 PM
RE: Moving chess piece using mouse - by metulburr - Feb-24-2018, 09:52 PM
RE: Moving chess piece using mouse - by reepio13 - Feb-24-2018, 11:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Chess on Tkinter Snapping Issues Trinx 6 4,872 Jan-15-2019, 02:07 AM
Last Post: Trinx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020