【问题标题】:screen.blit image giving a black screen and drawing shows up when i close the pygame window当我关闭 pygame 窗口时,出现黑屏和绘图的 screen.blit 图像
【发布时间】:2020-12-13 18:36:57
【问题描述】:

我想为我的游戏加载游戏片段,但是当我尝试对它们进行 blit 时,整个屏幕都变黑了。关闭窗口时,整个绘图会显示一秒钟。 到目前为止的代码:

import pygame
import math

board_lines = [
    ( 13,15,462,15 ), ( 13,469,462,469 ), #lin1 and line2,outer rect
    ( 62,86,409,86 ), ( 62,389,409,389 ), #line3 and l4,mid reect
    ( 114,186,360,186 ), ( 114,318,360,318 ), #line5,l6,internl rect
    ( 13,15,13,469 ), ( 462,12,462,469 ), #line9,l10,left and right sides
    ( 62,86,62,389 ), ( 409,85,409,389 ), #l7,l8left and right sides
    ( 114,186,114,316), ( 360,187,360,318 ), #l11,lin12left and right sides
    ( 237,15,237,186 ), ( 237,469,237,320 ), #upper V.line,lowerV
    ( 13,242,113,242 ), ( 360,242,462,242 ) #rIGHT LEFT hoRIZONTAL LINE
] 

pygame.init() 

intersectionPoints = []
for i, line1 in enumerate(board_lines):
    for line2 in board_lines[i:]:
        isectP = lineLineIntersect(line1[:2], line1[2:], line2[:2], line2[2:])
        if isectP:
            intersectionPoints.append(isectP)
 
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW_P = pygame.image.load('yellowgoti.png')
BLUE_P = pygame.image.load('bluegoti.png')
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 20
HEIGHT = 20
 
# This sets the margin between each cell
MARGIN = 5
 
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
for row in range(19):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(19):
        grid[row].append(0)  # Append a cell
 
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
 
WINDOW_SIZE = [800, 600]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
            
            
    # Set the screen background
    screen.fill(BLACK)

#screen.background(YELLOW_P,,600)
    # Draw the grid
for row in range(19):
    for column in range(19):
        color = WHITE
        if grid[row][column] == 1:
            color = GREEN
                
    for line in board_lines:
        pygame.draw.line(screen, RED, line[:2], line[2:], 3)

    for isectP in intersectionPoints:
        pygame.draw.circle(screen, GREEN, isectP, 5)

screen.blit(BLUE_P,(400,200))
    # Limit to 60 frames per second
clock.tick(60)
pygame.display.flip()
 
pygame.quit() 

在代码中使用 blit 的位置是否正确。 如果不是这样,那么我可以在代码中的哪个位置对屏幕进行blit。 在此先感谢您的帮助。省略了部分代码:

【问题讨论】:

  • 这是一个从for row in range(19): 开始的简单缩进问题 基本上你的主循环的后半部分需要缩进(pygame.quit() 除外)才能成为循环的一部分。
  • 我已经评论了整个嵌套的 for 循环,但仍然是同样的问题

标签: python pygame blit


【解决方案1】:

按照@Kingsley 的建议缩进主循环解决了问题:

WINDOW_SIZE = [800, 600]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(BLACK)
    screen.blit(BLUE_P,(400,200))

    for line in board_lines:
        pygame.draw.line(screen, RED, line[:2], line[2:], 3)

    for isectP in intersectionPoints:
        pygame.draw.circle(screen, GREEN, isectP, 5)
    clock.tick(60)
    pygame.display.flip()

pygame.quit()

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多