[英]Pygame is displaying 10x10 grid of different colored squares as black
I need help with a pygame project I'm doing. 我正在做的pygame项目需要帮助。 Right now it is just supposed to display a 10x10 grid of different colored squares, however it just displays black.
现在,它应该只显示10x10的不同颜色正方形的网格,但是它只显示黑色。 It doesn't give me any error messages.
它没有给我任何错误消息。
Here is my code, could someone please tell me what I am doing wrong? 这是我的代码,有人可以告诉我我做错了吗?
import random, sys, copy, os, pygame
from pygame.locals import *
pygame.init()
FPS = 30
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (147, 147, 147)
ORANGE = (255, 165, 0)
BGCOLOR = BLACK
TEXTCOLOR = WHITE
DISPLAYSURF = pygame.display.set_mode((400, 400))
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
FPSCLOCK = pygame.time.Clock()
#Pygame works where the graph has no negative
#The Y axis also starts at 0 ON TOP then GOES DOWN
XMAPCORD = 0
YMAPCORD = 0
def drawWall():
pygame.draw.rect(DISPLAYSURF, BLACK, (XMAPCORD, YMAPCORD, 40, 40), 0)
def drawStone():
pygame.draw.rect(DISPLAYSURF, GRAY, (XMAPCORD, YMAPCORD, 40, 40), 0)
def drawGoal():
pygame.draw.rect(DISPLAYSURF, ORANGE, (XMAPCORD, YMAPCORD, 40, 40), 0)
pygame.display.set_caption('Dungeon Escape')
currentLevel = [
'w','w','w','w','g','g','w','w','w','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','s','s','s','s','s','s','s','s','w',
'w','w','w','w','w','w','w','w','w','w'
]
playerPosition = currentLevel
for i in range(0,100):
if currentLevel[i] == 'w':
drawWall()
XMAPCORD = XMAPCORD + 40
if currentLevel[i] == 's':
drawStone()
XMAPCORD = XMAPCORD + 40
elif currentLevel[i] == 'g':
drawGoal()
XMAPCORD = XMAPCORD + 40
if XMAPCORD == 400 and YMAPCORD == 0:
break
elif i % 10 == 0:
YMAPCORD = YMAPCORD + 40
XMAPCORD = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
FPS = 30
fpsClock = pygame.time.Clock()
fps = True
while fps:
pygame.display.update()
fpsClock.tick(FPS)
pygame.display.update()
Your whole program is stuck in the first while
loop since it has no way of breaking out. 您的整个程序都陷入了第一个
while
循环中,因为它无法中断。 As you can see: 如你看到的:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
The only way to leave, is to exit. 离开的唯一方法是退出。
What you should do instead is to combine both of your while
loops. 相反,您应该做的是结合两个
while
循环。
running = True
FPS = 30
fpsClock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
Side note: it's not recommended to call sys.exit()
in the middle of your script with pygame. 注意:不建议在脚本中间使用pygame调用
sys.exit()
。 You should instead change it to running = False
to let the program exit normally. 您应该改为将其更改为
running = False
以使程序正常退出。
Another side note: you don't need two fps clock. 另一个注意事项:您不需要两个fps时钟。 Get rid of the first one:
FPSCLOCK = pygame.time.Clock()
. 摆脱第一个:
FPSCLOCK = pygame.time.Clock()
。 And that playerPosition = currentLevel
is not useful since nowhere in your code uses it. 而且那个
playerPosition = currentLevel
因为代码中没有地方使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.