Python Forum
Moving chess piece using mouse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving chess piece using mouse
#1
Hi,(note: I have a general knowledge on programming python and pygame as I do A-Level computer science)

I am working on a project of programming chess, using python and pygame. So far, I have programmed the GUIs needed, such as the menu screen, and have made buttons that functions, such as an exit button. However, I have hit a block, which is how do I actually program all the logic behind it? I wish to blit images representing the different pieces, such as the rook and knight, and I want it so the user clicks on a piece, then click on a valid space, in which the piece will then move there. Any help will be much appreciated!

I have thought of when the mouse cursor is in the range of the images, it will ask an if statement whether or not the mouse button has been pressed, then it will allow the user to click an empty space to move the image. I was thinking of blitting the same image to the new space, but then I have the problem of 'removing' the previous image (if that makes sense). I have tried searching the internet for a solution, but I have had troubles, so here I am.

Please ask for more detail if my post lacks sufficient detail. To put it simply, I want to blit an image, then move it using the mouse. However, if there's a more efficient way, I will also consider that. Thank you.
Reply
#2
So you have different squares, which could be in different states (empty, Rook, Pawn, etc). Have you considered using a state machine to handle the rendering?

Here's a pretty good article if you're not familiar with the concept: http://gameprogrammingpatterns.com/state.html
Reply
#3
Thank you for your reply, I shall have a look at the links
Reply
#4
Cool. If you need more help, please share some code so we have something more specific we can talk about :)
Reply
#5
Have you done a more simple program yet such as pong? Or is this your very first game?
Recommended Tutorials:
Reply
#6
Hi, I have done simple programs, such as rock, paper, scissors, code that involves the exploration of if and while statements, but the only problem is implementing GUIs to more complicated programs, such as moving a chess piece on a board; specifically utilising pygame. However, to answer your question, this is not the first time I have programmed a game, but the first time programming a complex game, only because I want to try object orientated programming, and with chess there are many opportunities to use classes and inheritance.
Reply
#7
You can use classes and inheritance in pong too.

I was more referring to whether or not you programmed GUI games before? Is this your firstbgame using pygame?

The reason i ask this is because you have not shown any of the code you built already. There is no way to tell if you are utilizing pygame mechanics properly such as rects for positioning and rendering, or whether you have 1 main game loop or spaghetti code of many, utilizing OOP, etc. So its hard to judge where you are experience-wise.

Chess would be a good second or third GUI game. Pong is a decent first game because its small and simple. You can more easily fix OOP issues, spaghetti code, not using pygame rects, etc. In pong. You can code correctly before moving on to more advanced games.

I would code a sample of your intended action but i cant get to a desktop at this moment.
Recommended Tutorials:
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Chess on Tkinter Snapping Issues Trinx 6 4,743 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