试试 Pygame。它不仅可以让您显示图像,还可以帮助您移动它们并与它们进行交互。如果您打算制作国际象棋游戏,我相信 Pygame 会有所帮助!如果你真的想坚持使用 SVG,那么......我想我只是想帮忙。
如果您想要示例代码,请在下面发表评论。希望这会有所帮助!
好的,我已经完成了。到目前为止,游戏允许您将棋子移动到允许的位置。但是剩下的事情我会留给你,因为这需要一些时间。如果您在 pygame 中需要任何指导,请再次发表评论,我会尽力提供帮助。代码如下:
import pygame, time, sys, random
from pygame.locals import *
# Initialize pygame (this is nessseccary for it to run all it's commands)
pygame.init()
# Draw the screen. The width and the height define how long and how wide the window is
width = 600
height = 500
window = pygame.display.set_mode((width, height))
TRANSwindow = window.convert_alpha()
# this sets the window title to "Chess!"
pygame.display.set_caption("Chess!")
# define some colours that we might need for later decoration
aqua = 0, 255, 255
black = 0, 0, 0
blue = 0, 0, 255
fuchsia = 255, 0, 255
gray = 128, 128, 128
green = 0, 128, 0
lime = 0, 255, 0
maroon = 128, 0, 0
navy_blue = 0, 0, 128
olive = 128, 128, 0
purple = 128, 0, 128
red = 255, 0, 0
silver = 192, 192, 192
teal = 0, 128, 128
white = 255, 255, 255
yellow = 255, 255, 0
# Make a group that contains the sprite pieces that we need
pieces = pygame.sprite.Group()
# Make a group that contains the grid called "board" that we need
grid = pygame.sprite.Group()
# Make a group that contains the allowed click spaces called "ClickSpaces" that we need
ClickSpaces = pygame.sprite.Group()
class Board(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("yoHZB.png")
self.image = pygame.transform.scale(self.image,(600,500))
self.rect = self.image.get_rect()
class Piece(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Hr7mM.png")
self.image = pygame.transform.scale(self.image,(75,65))
self.rect = self.image.get_rect()
class ClickSpace(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("I6rbE.png")
self.image = pygame.transform.scale(self.image, (75, 65))
self.rect = self.image.get_rect()
for i in range(8):
piece = Piece()
piece.rect.x = i*75
piece.rect.y = 0
pieces.add(piece)
for i in range(8):
piece = Piece()
piece.rect.x = i*75
piece.rect.y = 65
pieces.add(piece)
for i in range(8):
piece = Piece()
piece.rect.x = i*75
piece.rect.y = 380
pieces.add(piece)
for i in range(8):
piece = Piece()
piece.rect.x = i*75
piece.rect.y = 440
pieces.add(piece)
board = Board()
grid.add(board)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONUP:
mouse = pygame.mouse.get_pos()
for piece in pieces:
if piece.rect.collidepoint(mouse):
print("clicked")
for i in range(2):
clickspace = ClickSpace()
clickspace.rect.x = piece.rect.left
clickspace.rect.y = piece.rect.top+((i+1)*65)
ClickSpaces.add(clickspace)
print("added clickspace")
ClickSpaces.draw(window)
pygame.display.update()
wait = True
while wait == True:
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONUP:
mouse = pygame.mouse.get_pos()
for clickspace in ClickSpaces:
if clickspace.rect.collidepoint(mouse):
print("clickspace clicked")
piece.rect.x = clickspace.rect.left
piece.rect.y = clickspace.rect.top
ClickSpaces.empty()
wait = False
else:
ClickSpaces.empty()
wait = False
window.fill(white)
grid.draw(window)
pieces.draw(window)
ClickSpaces.draw(window)
pygame.display.update()
一旦你复制了代码并将其放入 python 中。保存项目,然后下载以下图片并将它们与您的代码放在同一个文件中: