Python Pygame基于字符串的关卡绘制一次,然后消失
我正在使用Python 2.7。我无法重新绘制基于字符串的级别。它在第一帧画得很好,但在那之后,它似乎不再画了。我一直在想一个解决办法。以下是完整的源代码:Python Pygame基于字符串的关卡绘制一次,然后消失,python,for-loop,pygame,Python,For Loop,Pygame,我正在使用Python 2.7。我无法重新绘制基于字符串的级别。它在第一帧画得很好,但在那之后,它似乎不再画了。我一直在想一个解决办法。以下是完整的源代码: import pygame from pygame.locals import * xScan = 0 yScan = 0 level = "WWWWWWWW" \ "WSSSSSSW" \ "WSSSSSSW" \ "WSSSSSSW" \ "WSSSSSSW" \
import pygame
from pygame.locals import *
xScan = 0
yScan = 0
level = "WWWWWWWW" \
"WSSSSSSW" \
"WSSSSSSW" \
"WSSSSSSW" \
"WSSSSSSW" \
"WWWWWWWW"
class Player():
def __init__(self, color, x, y, width, height, outline_size):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.outline_size = outline_size
def draw(self):
pygame.draw.rect(screen, self.color, [self.x * 80, self.y * 80, self.width * 80, self.height * 80], self.outline_size)
def draw_level():
for tile in level:
global xScan, yScan
if tile == "W":
pygame.draw.rect(screen, [0, 0, 0], [xScan * 80, yScan * 80, 80, 80], 0)
elif tile == "S":
pygame.draw.rect(screen, [120, 120, 120], [xScan * 80, yScan * 80, 80, 80], 1)
if xScan >= 0 and xScan < 7:
xScan += 1
elif xScan == 7:
xScan = 0
yScan += 1
elif yScan == 5 and xScan == 7:
xScan = 0
yScan = 0
def update():
screen.fill([255, 255, 255])
draw_level()
player.draw()
pygame.display.flip()
pygame.init()
screen = pygame.display.set_mode([640, 480])
pygame.display.set_caption("Squares n' Tiles!")
player = Player([0, 100, 255], 1, 1, 1, 1, 0)
running = True
while running:
clock = pygame.time.Clock()
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_UP and player.y * 80 >= 160:
player.y -= 1
if event.key == K_DOWN and player.y * 80 < 320:
player.y += 1
if event.key == K_LEFT and player.x * 80 >= 160:
player.x -= 1
if event.key == K_RIGHT and player.x * 80 < 480:
player.x += 1
if event.key == K_c or event.key == K_r:
player.x = 1
player.y = 1
if event.type == pygame.QUIT:
running = False
update()
pygame.quit()
上面代码的唯一问题是左上角的平铺无法绘制。我如何改变我的代码使其正常工作?在每一帧上,我都希望屏幕充满白色,重新绘制关卡,重新绘制玩家,然后翻转屏幕。提前感谢您的帮助。我也希望得到关于我的代码结构的建议。问题是没有执行此块:
elif yScan == 5 and xScan == 7:
xScan = 0
yScan = 0
这是因为在级别
字符串中循环每个字符,因此达到的最大值为xScan=6,yScan=5
相反,在进入绘图级别功能时,只需初始化xScan
和yScan
def draw_level():
xScan = 0 # no need for these to be global
yScan = 0
for tile in level:
if tile == "W":
pygame.draw.rect(screen, [0, 0, 0], [xScan * 80, yScan * 80, 80, 80], 0)
elif tile == "S":
pygame.draw.rect(screen, [120, 120, 120], [xScan * 80, yScan * 80, 80, 80], 1)
if xScan >= 0 and xScan < 7:
xScan += 1
elif xScan == 7:
xScan = 0
yScan += 1
然后你可以像这样在地图上迭代:
def draw_level():
for yScan, row in enumerate(level.split('\n')):
for xScan, tile in enumerate(row):
if tile == "W":
pygame.draw.rect(screen, [0, 0, 0], [xScan * 80, yScan * 80, 80, 80], 0)
elif tile == "S":
pygame.draw.rect(screen, [120, 120, 120], [xScan * 80, yScan * 80, 80, 80], 1)
enumerate
获取列表(或iterable)并为列表中的每个项目返回(索引、项目)对,例如:
>>> a = 'test'
>>> for i, c in enumerate(a):
... print('i is {}, c is {}'.format(i, c))
...
i is 0, c is t
i is 1, c is e
i is 2, c is s
i is 3, c is t
谢谢我很感激。
level = "WWWWWWWW\n" \
"WSSSSSSW\n" \
"WSSSSSSW\n" \
"WSSSSSSW\n" \
"WSSSSSSW\n" \
"WWWWWWWW\n" # note the new line characters (\n)
def draw_level():
for yScan, row in enumerate(level.split('\n')):
for xScan, tile in enumerate(row):
if tile == "W":
pygame.draw.rect(screen, [0, 0, 0], [xScan * 80, yScan * 80, 80, 80], 0)
elif tile == "S":
pygame.draw.rect(screen, [120, 120, 120], [xScan * 80, yScan * 80, 80, 80], 1)
>>> a = 'test'
>>> for i, c in enumerate(a):
... print('i is {}, c is {}'.format(i, c))
...
i is 0, c is t
i is 1, c is e
i is 2, c is s
i is 3, c is t