在pygame中看不到绘制的图像

时间:2015-07-02 21:12:12

标签: python pygame

我在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()

文本按预期显示在屏幕上,但不显示图像。我做错了什么?

2 个答案:

答案 0 :(得分:0)

默认情况下,

Surface个实例为黑色,因此您必须fill才能在黑色背景中看到它:

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

答案 1 :(得分:0)

您绘制圆圈的表面:

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

的大小只有3x3像素。

然后你绘制圆圈:

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

位置x, y,但x, y实际上是200, 200,因此您将其绘制在image表面的可见区域之外。

您可以完全跳过创建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()

此外,您无需致电pygame.display.flip() pygame.display.update()