我的精灵在程序中无法正确显示。地图显示正常,但仅当我按右上角的大X退出程序时,精灵才会显示一秒钟(但当我按 ESC 退出时,精灵不会显示)
显示精灵代码:
class player():
def __init__(self, x, y, width, height, walkCount):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.up = False
self.down = False
self.left = False
self.right = False
self.up = False
self.down = False
self.walkCount = walkCount
def redrawGameWindow(self, window):
dest = (self.x, self.y)
if self.walkCount + 1 >= 30:
self.walkCount = 0
if self.left:
window.blit(protagL[self.walkCount//3], dest)
self.walkCount += 1
elif self.right:
window.blit(protagR[self.walkCount//3], dest)
self.walkCount += 1
elif self.up:
window.blit(protagU[self.walkCount//3], dest)
self.walkCount += 1
elif self.down:
window.blit(protagD[self.walkCount//3], dest)
self.walkCount += 1
else:
window.blit(protagL[self.walkCount//3], dest)
pygame.display.update()
类代码:
def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
justification = justification[0].upper()
# text = text.strip().replace('\r','').split('\n')
max_width = 0
text_bitmaps = []
# Convert all the text into bitmaps, calculate the justification width
for char in text:
text_bitmap = the_font.render(char, True, colour)
text_width = text_bitmap.get_width()
text_bitmaps.append((text_width, text_bitmap))
if (max_width < text_width):
max_width = text_width
# Paint all the text bitmaps to the screen with justification
for (width, bitmap) in text_bitmaps:
xPos = x
width_diff = max_width - width
if justification == 'R': # right-justify
xPos = x + width_diff
elif justification == 'C': # centre-justify
xPos = x + (width_diff // 2)
screen.blit(bitmap, (xPos, y))
y += bitmap.get_height()
pygame.display.update()
地图显示代码:
def movePlayer(board):
clock = pygame.time.Clock()
run = True
while run:
clock.tick(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
arrowKeys = pygame.key.get_pressed()
if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
board.x -= board.vel
board.left = True
board.right = False
board.up = False
board.down = False
elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
board.x += board.vel
board.right = True
board.left = False
board.up = False
board.down = False
elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
board.y -= board.vel
board.right = False
board.left = False
board.up = True
board.down = False
elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
board.y += board.vel
board.right = False
board.left = False
board.up = False
board.down = True
elif arrowKeys[pygame.K_ESCAPE]: # ESC key
stopGame()
else:
board.right = False
board.left = False
board.up = False
board.down = False
board.walkCount = 0
board.redrawGameWindow(window)
pygame.display.quit()
字符显示代码:
def main():
inFile = open("map(TEST).txt", "r")
text = inFile.read().splitlines() # splitlines is to exclude the '\n'
inFile.close()
window.fill((0, 0, 0))
myfont = pygame.font.SysFont("Calibri", 35)
window.fill((0,0,0))
board = player(30, 45, 64, 64, 0)
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
while True:
# movePlayer(board)
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
movePlayer(board)
主要代码:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 156, in <module>
main()
File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 153, in main
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 83, in multilineRender
screen.blit(bitmap, (xPos, y))
pygame.error: display Surface quit
我还应该提到当我退出程序时,出现此错误:
public class UuidDecoder implements Decoder.TextStream<UUID>, Decoder, Decoder.Binary<UUID>, Decoder.Text<UUID> { public UuidDecoder() { System.out.println("OK!"); } @Override public void init(EndpointConfig config) { } @Override public void destroy() { } @Override public UUID decode(Reader reader) throws DecodeException, IOException { return UUID.fromString(CharStreams.toString(reader)); } @Override public UUID decode(ByteBuffer bytes) throws DecodeException { return UUID.fromString(new String(bytes.array())); } @Override public boolean willDecode(ByteBuffer bytes) { try { UUID.fromString(new String(bytes.array())); return true; } catch (IllegalArgumentException e) { return false; } } @Override public UUID decode(String s) throws DecodeException { return UUID.fromString(s); } @Override public boolean willDecode(String s) { try { UUID.fromString(s); return true; } catch (IllegalArgumentException e) { return false; } } }
...但是我怀疑这有关系。
答案 0 :(得分:0)
从pygame.display.quit()
表单中删除movePlayer
并将while
中的movePlayer
循环转到选择if
。请注意,您在main
中有一个循环,请使用它。
此外,我建议主要进行pygame.display.update()
。
主应用程序循环必须:
def main():
inFile = open("map(TEST).txt", "r")
text = inFile.read().splitlines() # splitlines is to exclude the '\n'
inFile.close()
myfont = pygame.font.SysFont("Calibri", 35)
board = player(30, 45, 64, 64, 0)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(15)
# clear the display
window.fill((0, 0, 0))
# draw the scene
board.redrawGameWindow(window)
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
# update the display
pygame.display.update()
# handle the events and update the objects
run = movePlayer(board)
movePlayer
必须返回true
,因为游戏仍在运行,否则false
:
def movePlayer(board):
run = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
arrowKeys = pygame.key.get_pressed()
if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
board.x -= board.vel
board.left, board.right, board.up, board.down = True, False, False, False
elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
board.x += board.vel
board.left, board.right, board.up, board.down = False, True, False, False
elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
board.y -= board.vel
board.left, board.right, board.up, board.down = False, False, True, False
elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
board.y += board.vel
board.left, board.right, board.up, board.down = False, False, False, True
elif arrowKeys[pygame.K_ESCAPE]: # ESC key
stopGame()
else:
board.left, board.right, board.up, board.down = False, False, False, False
board.walkCount = 0
return run
multilineRender
不执行pygame.display.update()
:
def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
justification = justification[0].upper()
# text = text.strip().replace('\r','').split('\n')
max_width = 0
text_bitmaps = []
# Convert all the text into bitmaps, calculate the justification width
for char in text:
text_bitmap = the_font.render(char, True, colour)
text_width = text_bitmap.get_width()
text_bitmaps.append((text_width, text_bitmap))
if (max_width < text_width):
max_width = text_width
# Paint all the text bitmaps to the screen with justification
for (width, bitmap) in text_bitmaps:
xPos = x
width_diff = max_width - width
if justification == 'R': # right-justify
xPos = x + width_diff
elif justification == 'C': # centre-justify
xPos = x + (width_diff // 2)
screen.blit(bitmap, (xPos, y))
y += bitmap.get_height()