使用鼠标移动多个单独的图像

时间:2019-04-24 14:27:04

标签: python pygame chess pygame-surface

所以我正在为一个项目制作国际象棋游戏。我正在使用pygame来创建游戏。我尚未实现实际的国际象棋裁定和AI,目前正在尝试使每个部件都可以通过鼠标移动。我创建了棋盘和棋子。目前,我已经在其中一张/图像上设置了一种鼠标移动形式,但是效果不是很好。我希望能够拖放片段图像。 我希望能够将每个棋子拖放到棋盘上的任何位置,并使它们从其在棋盘上的当前位置开始。

到目前为止,我已经使用mx和我的工具通过鼠标单击来移动一件,但是效果不是很好。我也不确定如何通过鼠标拖放来移动每个作品。

import pygame
from pygame.locals import *

def draw_board(the_board):

    pygame.init()
    pygame.display.set_caption('Chess World!')

    colours = [(255,100,10), (230,220,170)]    

    n = len(the_board)         

    square = 600 // 8    
    surfsize = 6 * square     


    surface = pygame.display.set_mode([1000, 600])
    white = (255,255,255)
    surface.fill(white)
    pygame.display.update()

    redpawn = pygame.image.load('redpawn.png')
    redbishop = pygame.image.load('redbishop.png')
    redrook = pygame.image.load('redrook.png')
    redknight = pygame.image.load('redknight.png')
    redking = pygame.image.load('redking.png')
    redqueen = pygame.image.load('redqueen.png')

    whitepawn = pygame.image.load('whitepawn.png')
    whitebishop = pygame.image.load('whitebishop.png')
    whiterook = pygame.image.load('whiterook.png')
    whiteknight = pygame.image.load('whiteknight.png')
    whiteking = pygame.image.load('whiteking.png')
    whitequeen = pygame.image.load('whitequeen.png')

    mx,my = pygame.mouse.get_pos()
    while True:


        instance = pygame.event.poll()
        if instance.type == pygame.QUIT:
            break;

        for row in range(n):           
            c_index = row % 2           
            for column in range(n):       
                the_square = (column*square, row*square, square, square)
                surface.fill(colours[c_index], the_square)                
                c_index = (c_index + 1) % 2         #colour index



        for event in pygame.event.get():
            if event.type == MOUSEBUTTONDOWN:
                mx,my = pygame.mouse.get_pos()


        surface.blit(redpawn,(mx-50,my-50))       #originally (10,80)
        surface.blit(redpawn,(85,80))
        surface.blit(redpawn,(160,80))
        surface.blit(redpawn,(235,80))
        surface.blit(redpawn,(310,80))
        surface.blit(redpawn,(385,80))
        surface.blit(redpawn,(460,80))
        surface.blit(redpawn,(535,80))
        surface.blit(redrook,(10,0))
        surface.blit(redknight,(85,0))
        surface.blit(redbishop,(160,0))
        surface.blit(redqueen,(235,0))
        surface.blit(redking,(310,0))
        surface.blit(redbishop,(385,0))
        surface.blit(redknight,(460,0))
        surface.blit(redrook,(535,0))


        surface.blit(whitepawn,(10,455))
        surface.blit(whitepawn,(85,455))
        surface.blit(whitepawn,(160,455))
        surface.blit(whitepawn,(235,455))
        surface.blit(whitepawn,(310,455))
        surface.blit(whitepawn,(385,455))
        surface.blit(whitepawn,(460,455))
        surface.blit(whitepawn,(535,455))
        surface.blit(whiterook,(10,525))
        surface.blit(whiteknight,(85,525))
        surface.blit(whitebishop,(160,525))
        surface.blit(whitequeen,(235,525))
        surface.blit(whiteking,(310,525))
        surface.blit(whitebishop,(385,525))
        surface.blit(whiteknight,(460,525))
        surface.blit(whiterook,(535,525))

        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    draw_board([6, 4, 2, 0, 5, 7, 1, 3])

执行的实际结果在一定程度上得到了满足,因为单击鼠标可以移动红棋但不能很好地移动,并且如果将棋子移到板上,则如果不断移动,它会重复打印到该空间中。 我希望能够将所有国际象棋棋子全盘移动。我目前不关心每件作品的动作限制。我愿意提出批评,并希望对此代码进行修改。

0 个答案:

没有答案