简体   繁体   English

绘制的图像在Pygame中不可见

[英]Drawn image is not visible in pygame

I have the following code in pygame (irrelevant stuff removed): 我在pygame中有以下代码(删除了不相关的内容):

import pygame, sys
from pygame.locals import *

pygame.init()
resolution = 1360,768
screen = pygame.display.set_mode((resolution),0,32)

font = pygame.font.SysFont("arial", 24)
black = 0,0,0
white = 255,255,255

x = 200
y = 200

while True:
    image = pygame.Surface([3,3],SRCALPHA)  # creates a surface to draw the protagonist on
    protagonist=pygame.draw.circle(image, white, (x,y), 3, 3) # draws the protagonist on the surface image
    for event in pygame.event.get():
        keystate = pygame.key.get_pressed()
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if keystate[K_ESCAPE]:
                pygame.quit()
                sys.exit()
    screen.fill(black)
    text = font.render("This text appears on the screen", 1, (white))
    screen.blit(text, (100, 100)) 
    screen.blit(image,(x, y)) # This does not appear on the screen
    pygame.display.flip()
    pygame.display.update()

The text appears on the screen as intended, but not the image. 文本按预期显示在屏幕上,但不显示图像。 What am I doing wrong? 我究竟做错了什么?

Surface实例默认情况下是黑色的,因此您必须fill才能在黑色背景中看到它:

image.fill((255, 255, 255)) # fill the image Surface to white instead of default black

The surface you draw the circle on: 您在其上绘制圆的表面:

image = pygame.Surface([3,3],SRCALPHA)

is only 3x3 pixels in size. 尺寸仅为3x3像素。

Then you draw the circle: 然后画圆:

pygame.draw.circle(image, white, (x,y), 3, 3)

at position x, y , but x, y is actually 200, 200 , so you draw it way outside the visible area of the image surface. 在位置x, y ,但x, y实际上是200, 200 ,因此您将其绘制在image表面可见区域之外。


You can skip creating the image surface altogether and draw directly to the screen surface: 您可以完全跳过创建image表面,而直接绘制到screen表面:

while True:
    for event in pygame.event.get():
        keystate = pygame.key.get_pressed()
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if keystate[K_ESCAPE]:
                pygame.quit()
                sys.exit()
    screen.fill(black)
    pygame.draw.circle(image, white, (x,y), 3, 3)
    text = font.render("This text appears on the screen", 1, (white))
    screen.blit(text, (100, 100)) 
    pygame.display.update()

Also, you don't need to call pygame.display.flip() and pygame.display.update() 另外,您不需要调用pygame.display.flip() pygame.display.update()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM