我在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()
文本按预期显示在屏幕上,但不显示图像。我做错了什么?
答案 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()