简体   繁体   English

Pygame将不同颜色的正方形的10x10网格显示为黑色

[英]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.

相关问题 从数组列表制作10x10网格 - Making a 10x10 grid from a list of arrays 无法在python中制作10x10的矩阵 - not be able to make a matrix 10x10 in python 在10x10 python熊猫数据框中添加值(不包括对角线) - Addition of values in a 10x10 python pandas dataframe excluding the diagonal 使用10x10交叉验证时如何计算ROC? - How to calculate the ROC when using 10x10 cross validation? (Python3)在python数组上存储10x10游戏地图 - (Python3) Storing a 10x10 game map on a python array Python & Numpy - 将每个单元格为 20x20 数组的 10x10 数组重塑为 10x10x20x20 - Python & Numpy - reshape 10x10 array where each cell is 20x20 array into a 10x10x20x20 如何在python中的空背景图像(200x200)中随机添加20张图像(10x10)? - How can I randomly add 20 images(10x10) in an empty background image (200x200) in python? 从 100x100 pytorch 张量获取一个 10x10 的补丁,该张量具有环形环绕边界 - Get a 10x10 patch from a 100x100 pytorch tensor with torus style wrap around the boundries 我有一个100个元素的列表,我想使用导入csv将其列表到excel中的10x10表中 - I have a list of 100 elements and I would like to tabulate it into a 10x10 table in excel, using import csv PyGame 通过附加方块制作网格 - PyGame making grid by appending squares
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM