精灵没有闪电到表面pygame
我正在 pygame 中下棋,但棋子不会为他们而转移到表面上。我该如何解决这个问题?
import pygame
pygame.init()
size = (600,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("chess")
clock = pygame.time.Clock()
chessboard = pygame.image.load("chess_board.png")
whitepawn = pygame.image.load("whitepawn.png")
blackpawn = pygame.image.load("blackpawn.png")
class pawn(pygame.sprite.Sprite):
def __init__(self,colour,x,y):
self.x = x
self.y = y
moved = False
self.colour = colour
pygame.sprite.Sprite.__init__(self)
self.im = pygame.surface.Surface((75,75))
def showpiece(self):
if self.colour == "white":
pygame.Surface.blit(self.im,whitepawn,(self.x,self.y))
elif self.colour == "black":
pygame.Surface.blit(self.im,blackpawn,(self.x,self.y))
num1 = 0
pawns = []
for i in range (8):
pawns.append(pawn("black",0+num1,75))
pawns.append(pawn("white",0+num1,450))
num1 += 75
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(chessboard,[0,0])
for n in range (len(pawns)):
pawns[n].showpiece()
pygame.display.flip()
clock.tick(60)
另外,clock.tick
的意义何在?我知道它可以控制 fps,但为什么需要限制呢?
I'm making chess in pygame, but the pieces don't blit onto the surface for them. How could I fix this?
import pygame
pygame.init()
size = (600,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("chess")
clock = pygame.time.Clock()
chessboard = pygame.image.load("chess_board.png")
whitepawn = pygame.image.load("whitepawn.png")
blackpawn = pygame.image.load("blackpawn.png")
class pawn(pygame.sprite.Sprite):
def __init__(self,colour,x,y):
self.x = x
self.y = y
moved = False
self.colour = colour
pygame.sprite.Sprite.__init__(self)
self.im = pygame.surface.Surface((75,75))
def showpiece(self):
if self.colour == "white":
pygame.Surface.blit(self.im,whitepawn,(self.x,self.y))
elif self.colour == "black":
pygame.Surface.blit(self.im,blackpawn,(self.x,self.y))
num1 = 0
pawns = []
for i in range (8):
pawns.append(pawn("black",0+num1,75))
pawns.append(pawn("white",0+num1,450))
num1 += 75
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.blit(chessboard,[0,0])
for n in range (len(pawns)):
pawns[n].showpiece()
pygame.display.flip()
clock.tick(60)
Also what is the point in clock.tick
? I know it controls fps, but why does that need limiting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将精灵
blit
显示在Surface(屏幕
)上:我建议使用
pygame.Rect
存储棋子的位置:You have to
blit
the Sprites on the display Surface (screen
):I suggest to use
pygame.Rect
to store the positions of the pieces: