2017-04-14 137 views
0

当试图了解Pygame中的Sprite时,我遇到了一些小问题。Sprite上的Pygame对象没有显示

首先,我已经投入了大量的精力与精灵,但我没有得到它的工作。

我已经完成了在将一个对象绘制到sprite上但是对象在运行程序时不显示的情况下在互联网上显示的指南。

任何建议将更受欢迎!

tick = 0 
sprite = pygame.Surface([20, 20]) 
sprite.fill(white) 
sprite.set_colorkey(white) 
rect = sprite.get_rect() 
rect.x = 400 
rect.y = 300 


while tick < 100: 

    screen.fill(black) 

    pygame.draw.rect(sprite, red, [rect.x, rect.y, 20, 20]) 
    pygame.display.update() 
    clock.tick(10) 
    tick += 1 


pygame.quit() 
quit() 
+1

你用白色填充表面,然后设置白色的colorkey。删除的颜色键,它应该工作。关于colorkeys [这里](http://stackoverflow.com/documentation/pygame/7079/drawing-on-the-screen/23788/transparency#t=201704140013573679697) –

+0

这是比这更错误。 – jsbueno

回答

-1

如果你想开始做一些编程与pygame的直接使用精灵,这将是一个好主意,做可用教程的一些初步的研究得到一些基本的了解如何使用GUI和能够处理用户应用程序事件一般工作。

另一种选择是从更简单的事情开始,可以直接看到结果,而不需要编写必要的(精灵需要组和类)庞大的初步代码。

除了大量难以理解的Pygame sprites教程外,还有一些提供了很好的代码示例和相当好的解释。我建议你仔细看看:

Introduction to sprites --- sprite_collect_blocks.py

,然后再回来这里与其他问题,如果有的话。

下面我提供了两段代码,以便您可以将它们彼此进行比较以查看我之前所讲的内容。

两者的第一部分是您自己的代码,稍作修改。它在屏幕上创建了三个块,它实际上与精灵无关(因为在另一个答案中也是如此),但是显示了如何用矩形填充显示的Pygame屏幕。仔细阅读另一个答案,因为它们提供了你自己的代码随附的问题的良好解释。

import pygame 

WHITE = (255,255,255) 
BLACK = (0,0,0) 
RED = (255,0,0) 
BLUE = (0,0,255) 

# Set the height and width of the screen: 
DISPLAY_width = 640 
DISPLAY_height = 480 
DISPLAY = pygame.display.set_mode([DISPLAY_width, DISPLAY_height]) 
DISPLAY.fill(WHITE) 

# See here: [(3) stackoverflow.com/.../pygame-drawing-a-rectangle][3] 
pygame.draw.rect(DISPLAY,BLACK,(120,40,50,50)) 
pygame.draw.rect(DISPLAY,RED ,(520,355,50,50)) 
pygame.draw.rect(DISPLAY,BLUE ,(320,240,50,50)) 
pygame.display.flip() # without .flip() you get only black screen 

pygame.display.init() # IMPORTANT ! 

clock = pygame.time.Clock() 
tick = 0 
while tick < 100: 
    clock.tick(10) 
    tick += 1 

# BETTER do it as follows: 
""" 
done = False 
while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
""" 

pygame.quit() 
import sys 
sys.exit() 

这里什么上面的代码创建的屏幕截图:

Pygame window the code above creates

另一段代码是实际使用代码精灵。精灵通常与图像一起使用。这就需要首先得到scripts目录中的一些图片:

red dotpurple dotorange dotgrey dotblue dot enter image description here

这里进入第二一段代码(与TRUE精灵),它产生在屏幕上多MOTION(这里只是一个静态截图)。所有的点是在运动以不同的速度填充所述最初黑屏黄色:

enter image description here

strPythonScriptHeader = """ 
# pygCodeSample_UsageOfGroupsAndSprites.py    updated: 2017-04-12 09:17 
# Copyright (c) 2005, Claudio at stackoverflow.com created: 2005-01-24 19:43 
""" 
# ############################################################################# 
# VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV 
def pygCodeSample_UsageOfGroupsAndSprites(): 
    strMainInfo = """/* 
    WATCH OUT using pygame.sprite.Sprite and pygame.sprite.RenderClear classes: 
    - the order of Sprites in a Group is not necessary the order in which the 
     Sprites are drawn to the screen. 
    - the .clear() method of a Group clears the entire image square, not as 
     expected only the non-transparent part of it. 
    */""" 
    print(strMainInfo) 
    strSpriteTutorial = """/* 
    The 'pygame.sprite' library provides classes representing images and 
    group of images which are to be drawn to the screen during the game. 
    The concept of a 'Sprite' is to provide a class which objects represent 
    real-world physical objects which can be viewed on the computer screen. 
    The Sprite class is not used directly. It is used to derive own classes 
    of customized Sprites from it: 
     class CustomizedSpriteDerivedFromSpriteClass(pygame.sprite.Sprite): ... 
    Within the definition of the CustomizedSpriteDerivedFromSpriteClass 
    it is necessary to define a member variable 
     .image  (class CustomizedSpriteDerivedFromSpriteClass) 
    storing an 'Image' object which is to draw to the screen and the 
    member variable 
     .rect  (class CustomizedSpriteDerivedFromSpriteClass) 
    storing a 'Rect' object holding the target position of the 'Image' 
    on the screen. 
    Usually both variables are set when running the 
     .__init__() (class CustomizedSpriteDerivedFromSpriteClass) 
    function which should contain as first command: 
     pygame.sprite.Sprite.__init__(self) 
    Further is it necessary to define also an 
     .update() (class CustomizedSpriteDerivedFromSpriteClass) 
    function which provides the code of rules telling how the by the Sprite 
    graphically represented physical objects should change when the .update() 
    function is executed. 
    The main purpose of a 'Sprite' is to be added to a container(called Group) 
    which is an object of the pygame.sprite.RenderClear class: 
     pGobjGroupOfSprites = pygame.sprite.RenderClear() 
    ready to hold any number of Sprite objects. A single Sprite is added to it 
    using 
     pGobjGroupOfSprites.add(pGobjCustomizedSpriteDerivedFromSpriteClass) 
    call. 
    The pGobjGroupOfSprites object of the pygame.sprite.RenderClear class is 
    equipped with following methods: 
     pGobjGroupOfSprites.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground) 
     pGobjGroupOfSprites.update() 
     pGobjGroupOfSprites.draw(pGobjSCREENsurface) 
    .draw() draws all Sprites to as parameter passed pGobjSCREENsurface 
     using the .image and .rect properties defined as member variables 
     within the CustomizedSpriteDerivedFromSpriteClass class. 
    .clear() draws those parts of the pGobjImage_SCREENsurfaceBackground image 
     to the pGobjSCREENsurface which are the Rects representing the 
     positions of the Sprites on the screen using the .rect property of Sprites. 
    .update() runs the .update() method of each Sprite stored in the GroupOfSprites. 
    Finally the main game loop looks like: 

     while True: 
     evaluateInputEvents() 

     pGobjGroupOfSprites_Hero.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground) 
     pGobjGroupOfSprites_Hero.update() 
     pGobjGroupOfSprites_Hero.draw(pGobjSCREENsurface) 

     pGobjGroupOfSprites_Enemy.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground) 
     pGobjGroupOfSprites_Enemy.update() 
     pGobjGroupOfSprites_Enemy.draw(pGobjSCREENsurface) 

     pGobjGroupOfSprites_Widgets.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground) 
     pGobjGroupOfSprites_Widgets.update() 
     pGobjGroupOfSprites_Widgets.draw(pGobjSCREENsurface) 

     adjustFrameRateTo(intTargetFrameRate) # time.sleep()/time.clock() 

     pGdefUpdateScreenWithDataStoredIn_pGobjSCREENsurface # == pygame.display.flip 
     #:while 

    How many and which Groups should be used and how to spread all the Sprites 
    used in the game over the defined Groups is part of the architecture of 
    the game code which is designed according to the needs of the game and the 
    individual programming style. 
    */""" 
    print(strSpriteTutorial) 

    mPGvDctRectOfSCREENsurface = { 'left' : 0, 'right' : 640, 'top' : 0, 'bottom' : 480 } 
    mPGvIntScreenOriginX = mPGvDctRectOfSCREENsurface['left'] 
    mPGvIntScreenOriginY = mPGvDctRectOfSCREENsurface['top'] 
    mPGvIntScreenWidth = mPGvDctRectOfSCREENsurface['right'] - mPGvDctRectOfSCREENsurface['left'] 
    mPGvIntScreenHeight = mPGvDctRectOfSCREENsurface['bottom'] - mPGvDctRectOfSCREENsurface['top'] 
    mPGvTplScreenSize = (mPGvIntScreenWidth, mPGvIntScreenHeight) 

    import pygame 
    pGdefUpdateScreenWithDataStoredIn_pGobjSCREENsurface = pygame.display.flip 

    class pGobjCustomizedSprite_Dot(pygame.sprite.Sprite): 

    def __init__(self, strColorOfDot, tplVelocity): 
     # Intialize the Sprite class: 
     pygame.sprite.Sprite.__init__(self) 

     # .image property is used by the pGobjGroup hosting the sprite, so it is 
     # necessary to set it here in order to make this Sprite useful: 
     self.image = pygame.image.load(
      r'dot-32x32_'+strColorOfDot+'.gif' 
    ) # picture shows a coloured dot and has a transparent background 

     # .rect property is used by the pGobjGroup hosting the sprite, so it is 
     # necessary to set it here in order to make this Sprite useful: 
     self.rect = self.image.get_rect() 
     self.rect.centerx = 320 
     self.rect.centery = 240 

     # definition of other properties not necessary for usage of this class 
     # as a Sprite, but useful for internal purposes of setting the position 
     # of the rectangle within the .update() method: 
     self.x_velocity = tplVelocity[0] 
     self.y_velocity = tplVelocity[1] 
    #:def 

    # .update() method is used by the pGobjGroup hosting the sprite in its 
    # .update() command, so it is necessary to define it here in order to 
    # make this Sprite useful: 
    def update(self): 
     self.rect.move_ip((self.x_velocity, self.y_velocity)) 
     if(self.rect.left <= 0 or self.rect.right >= mPGvIntScreenWidth): 
      self.x_velocity = -(self.x_velocity) 
     #:if 
     if self.rect.top <= 0 or self.rect.bottom >= mPGvIntScreenHeight: 
      self.y_velocity = -(self.y_velocity) 
     #:if 
    #:def 
    #:class 

    pygame.init() 

    # pGobjSCREENsurface = pygame.display.set_mode(mPGvTplScreenSize, pygame.constants.DOUBLEBUF) 
    pGobjSCREENsurface = pygame.display.set_mode(mPGvTplScreenSize) # faster than with DOUBLEBUF 

    pGobjGroupOfSprites_spriteDot = pygame.sprite.RenderClear() 
    pGobjImage_SCREENsurfaceBackground = pygame.image.load(
    r'rect-640x480_yellow.bmp' 
) # picture shows yellow background 

    intLstLength = 0 
    dctTplVelocity = {} 
    import random 
    while(intLstLength < 5): 
    tplVelocity = (random.randint(1, 5), random.randint(1, 5)) 
    if(tplVelocity in dctTplVelocity): pass 
    else: 
     intLstLength+=1 
     dctTplVelocity[tplVelocity] = intLstLength 
    #:if/else 
    #:while 
    lstTplVelocity = list(dctTplVelocity.keys()) 
    # pGspriteRedDot = pGobjCustomizedSprite_Dot('red' ,lstTplVelocity[0]) 
    # pGspriteWhiteDot = pGobjCustomizedSprite_Dot('purple',lstTplVelocity[1]) 
    # pGspriteGreyDot = pGobjCustomizedSprite_Dot('grey' ,lstTplVelocity[2]) 
    # pGspriteBlueDot = pGobjCustomizedSprite_Dot('blue' ,lstTplVelocity[3]) 
    # pGspriteOrangeDot = pGobjCustomizedSprite_Dot('orange',lstTplVelocity[4]) 
    pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('red' ,lstTplVelocity[0])) 
    pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('purple',lstTplVelocity[1])) 
    pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('grey' ,lstTplVelocity[2])) 
    pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('blue' ,lstTplVelocity[3])) 
    pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('orange',lstTplVelocity[4])) 

    print 
    print(' target frame rate [frames/second] == 100.0 : ') 
    intLoopCounter = 0 

    import time 
    clock = time.clock 
    sleep = time.sleep 
    import sys 

    fltTime=clock() 
    fltStartTime = clock() 

    blnExitMainGameLoop = False 

    fltFrameRate  = 100.0 
    fltTimeToNextFrame = 1.0/fltFrameRate 
    import time 
    while True: 
    time.sleep(0.03) 
    # --- 
    # ----------------------------------------------------- 
    # Processing of user input: 
    pGlstObjEvent = pygame.event.get() 
    for pGobjEvent in pGlstObjEvent: 
     if pGobjEvent.type == pygame.constants.QUIT: 
     blnExitMainGameLoop = True 
     #:if 
     if pGobjEvent.type == pygame.constants.KEYDOWN: 
     if pGobjEvent.key == pygame.constants.K_ESCAPE: 
      blnExitMainGameLoop = True 
     if pGobjEvent.key == pygame.constants.K_s: 
      time.sleep(21) 
      blnExitMainGameLoop = False 
     #:if 
     #:if 
    #:for 
    if(blnExitMainGameLoop): 
     pygame.display.quit() 
     break 
    #:if 

    # --- 
    # ----------------------------------------------------- 
    # output of texts with infos to console window: 
    if(intLoopCounter%100 == 99): 
     print(' %5.2f '%(fltFrameRate/(clock()-fltTime),),) 
     fltTime=clock() 
    #:if 
    intLoopCounter += 1 
    # print intLoopCounter, 

    # --- 
    # ----------------------------------------------------- 
    # preparing and drawing graphic output to screen: 
    pGobjGroupOfSprites_spriteDot.update() 
    pGobjGroupOfSprites_spriteDot.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground) 
    pGobjGroupOfSprites_spriteDot.draw(pGobjSCREENsurface) 

    # --- 
    # ----------------------------------------------------- 
    # adjusting frame rate to 100 frames/second: 
    # fltFrameRate  = 100.0 
    # fltTimeToNextFrame = 1.0/fltFrameRate 
    fltTargetTime = fltTimeToNextFrame*intLoopCounter 
    fltTimeDiff = fltTargetTime-(clock()-fltStartTime) 
    if(fltTimeDiff > 0.8*fltTimeToNextFrame): sleep(0.8*fltTimeToNextFrame) 
    fltTimeDiff = fltTargetTime-(clock()-fltStartTime) 
    if(fltTimeDiff > 0.1*fltTimeToNextFrame): sleep(0.1*fltTimeToNextFrame) 
    fltTimeDiff = fltTargetTime-(clock()-fltStartTime) 
    while(fltTimeDiff > 0): 
     fltTimeDiff = (fltTimeToNextFrame*intLoopCounter)-(clock()-fltStartTime) 
    #:while 

    # --- 
    # ----------------------------------------------------- 
    # displaying prepared graphic output: 
    pGdefUpdateScreenWithDataStoredIn_pGobjSCREENsurface() 

    #:while 
#:def pygCodeSample_UsageOfGroupsAndSprites() 
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

if __name__ == '__main__': 
    print(strPythonScriptHeader) 
    pygCodeSample_UsageOfGroupsAndSprites() 
    import sys 
    if int(sys.version[0]) < 3 : # Python 2 : 
     raw_input('(exit with ENTER) #> OK? ') 
     sys.exit() 
    else:       # Python 3 : 
     input('(exit with ENTER) #> OK? ') 
     sys.exit() 
#:if 
pygameConstantsForUsageInKeyEVENTS = ['ACTIVEEVENT', 'ANYFORMAT', 'ASYNCBLIT', 'AUDIO_S16', 'AUDIO_S16LSB', 'AUDIO_S16MSB', 'AUDIO_S16SYS', 'AUDIO_S8', 'AUDIO_U16', 'AUDIO_U16LSB', 'AUDIO_U16MSB', 'AUDIO_U16SYS', 'AUDIO_U8', 'BIG_ENDIAN', 'BLEND_ADD', 'BLEND_MAX', 'BLEND_MIN', 'BLEND_MULT', 'BLEND_PREMULTIPLIED', 'BLEND_RGBA_ADD', 'BLEND_RGBA_MAX', 'BLEND_RGBA_MIN', 'BLEND_RGBA_MULT', 'BLEND_RGBA_SUB', 'BLEND_RGB_ADD', 'BLEND_RGB_MAX', 'BLEND_RGB_MIN', 'BLEND_RGB_MULT', 'BLEND_RGB_SUB', 'BLEND_SUB', 'BUTTON_X1', 'BUTTON_X2', 'DOUBLEBUF', 'FULLSCREEN', 'GL_ACCELERATED_VISUAL', 'GL_ACCUM_ALPHA_SIZE', 'GL_ACCUM_BLUE_SIZE', 'GL_ACCUM_GREEN_SIZE', 'GL_ACCUM_RED_SIZE', 'GL_ALPHA_SIZE', 'GL_BLUE_SIZE', 'GL_BUFFER_SIZE', 'GL_DEPTH_SIZE', 'GL_DOUBLEBUFFER', 'GL_GREEN_SIZE', 'GL_MULTISAMPLEBUFFERS', 'GL_MULTISAMPLESAMPLES', 'GL_RED_SIZE', 'GL_STENCIL_SIZE', 'GL_STEREO', 'GL_SWAP_CONTROL', 'HAT_CENTERED', 'HAT_DOWN', 'HAT_LEFT', 'HAT_LEFTDOWN', 'HAT_LEFTUP', 'HAT_RIGHT', 'HAT_RIGHTDOWN', 'HAT_RIGHTUP', 'HAT_UP', 'HWACCEL', 'HWPALETTE', 'HWSURFACE', 'IYUV_OVERLAY', 'JOYAXISMOTION', 'JOYBALLMOTION', 'JOYBUTTONDOWN', 'JOYBUTTONUP', 'JOYHATMOTION', 'KEYDOWN', 'KEYUP', 'KMOD_ALT', 'KMOD_CAPS', 'KMOD_CTRL', 'KMOD_LALT', 'KMOD_LCTRL', 'KMOD_LMETA', 'KMOD_LSHIFT', 'KMOD_META', 'KMOD_MODE', 'KMOD_NONE', 'KMOD_NUM', 'KMOD_RALT', 'KMOD_RCTRL', 'KMOD_RMETA', 'KMOD_RSHIFT', 'KMOD_SHIFT', 'K_0', 'K_1', 'K_2', 'K_3', 'K_4', 'K_5', 'K_6', 'K_7', 'K_8', 'K_9', 'K_AMPERSAND', 'K_ASTERISK', 'K_AT', 'K_BACKQUOTE', 'K_BACKSLASH', 'K_BACKSPACE', 'K_BREAK', 'K_CAPSLOCK', 'K_CARET', 'K_CLEAR', 'K_COLON', 'K_COMMA', 'K_DELETE', 'K_DOLLAR', 'K_DOWN', 'K_END', 'K_EQUALS', 'K_ESCAPE', 'K_EURO', 'K_EXCLAIM', 'K_F1', 'K_F10', 'K_F11', 'K_F12', 'K_F13', 'K_F14', 'K_F15', 'K_F2', 'K_F3', 'K_F4', 'K_F5', 'K_F6', 'K_F7', 'K_F8', 'K_F9', 'K_FIRST', 'K_GREATER', 'K_HASH', 'K_HELP', 'K_HOME', 'K_INSERT', 'K_KP0', 'K_KP1', 'K_KP2', 'K_KP3', 'K_KP4', 'K_KP5', 'K_KP6', 'K_KP7', 'K_KP8', 'K_KP9', 'K_KP_DIVIDE', 'K_KP_ENTER', 'K_KP_EQUALS', 'K_KP_MINUS', 'K_KP_MULTIPLY', 'K_KP_PERIOD', 'K_KP_PLUS', 'K_LALT', 'K_LAST', 'K_LCTRL', 'K_LEFT', 'K_LEFTBRACKET', 'K_LEFTPAREN', 'K_LESS', 'K_LMETA', 'K_LSHIFT', 'K_LSUPER', 'K_MENU', 'K_MINUS', 'K_MODE', 'K_NUMLOCK', 'K_PAGEDOWN', 'K_PAGEUP', 'K_PAUSE', 'K_PERIOD', 'K_PLUS', 'K_POWER', 'K_PRINT', 'K_QUESTION', 'K_QUOTE', 'K_QUOTEDBL', 'K_RALT', 'K_RCTRL', 'K_RETURN', 'K_RIGHT', 'K_RIGHTBRACKET', 'K_RIGHTPAREN', 'K_RMETA', 'K_RSHIFT', 'K_RSUPER', 'K_SCROLLOCK', 'K_SEMICOLON', 'K_SLASH', 'K_SPACE', 'K_SYSREQ', 'K_TAB', 'K_UNDERSCORE', 'K_UNKNOWN', 'K_UP', 'K_a', 'K_b', 'K_c', 'K_d', 'K_e', 'K_f', 'K_g', 'K_h', 'K_i', 'K_j', 'K_k', 'K_l', 'K_m', 'K_n', 'K_o', 'K_p', 'K_q', 'K_r', 'K_s', 'K_t', 'K_u', 'K_v', 'K_w', 'K_x', 'K_y', 'K_z', 'LIL_ENDIAN', 'MOUSEBUTTONDOWN', 'MOUSEBUTTONUP', 'MOUSEMOTION', 'NOEVENT', 'NOFRAME', 'NUMEVENTS', 'OPENGL', 'OPENGLBLIT', 'PREALLOC', 'QUIT', 'RESIZABLE', 'RLEACCEL', 'RLEACCELOK', 'SCRAP_BMP', 'SCRAP_CLIPBOARD', 'SCRAP_PBM', 'SCRAP_PPM', 'SCRAP_SELECTION', 'SCRAP_TEXT', 'SRCALPHA', 'SRCCOLORKEY', 'SWSURFACE', 'SYSWMEVENT', 'TIMER_RESOLUTION', 'USEREVENT', 'USEREVENT_DROPFILE', 'UYVY_OVERLAY', 'VIDEOEXPOSE', 'VIDEORESIZE', 'YUY2_OVERLAY', 'YV12_OVERLAY', 'YVYU_OVERLAY', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] 

上述代码配备有大体积的评论。正如我多年前从pygame开始并试图了解精灵时所写的,目标是写得清楚明了,以便理解。现在你可以判断自己,我的风格是不言自明的变量命名和提供的解释和信息,是多么的成功。

+0

为什么downvote ??? – Claudio

+0

我不是downvoter - 但(1)你没有回答。 (2)停止程序的部分代码相当混乱 - 没有什么帮助; (3)你警告OP不应该尝试像sprites这样的高级事物 - 当他根本不使用pygame sprites的时候。其中任何一个都可能会让你退缩 – jsbueno

+0

@jsbueno感谢您的回复。嗯...(1)从我的角度来看,我回答了这个问题,提供了非常好的在线精灵资源,并解释了什么是使他们工作所需要的(集体和班级)。在我看来,解释精灵的编程超出了预期答案的范围。 (2)我看不出有什么严重的问题停止程序(3),我可以从OPs代码中“读取”关于计划实现的内容的内容,我可以很清楚地看到将自制的东西显示在屏幕和我的代码做到了。关于OP响应的趣闻。 – Claudio

0

所以,你的代码有两个主要的问题。 首先,用“白色”填充正在调用精灵的表面,然后将“白色”设置为该表面的透明色,并调用set_colorkey。所以,无论如何什么都不能显示出来。

其次,您正在Surface上绘制矩形,而不是在屏幕上。所以,你确实把它画成红色,但你不会在屏幕上“印记”它 - 你调用pygame.draw.rect来绘制你正在调用的Sprite本身,而不是在屏幕上。

你的代码根本不显示你声明一个屏幕 - 我想你已经做到了正确,但是请记住,你总是应该发布一个完整的例子来获得你到达的行为。如果您的代码中存在错误以设置屏幕,我无法了解它。另外,我已经改变了我们的呼吁draw.rect调用screen.blit:将杜绝任何图像是目前的块传输方法的表面所有者精灵 - 在这种情况下,屏幕本身:

import pygame 
screen = pygame.display.set_mode((800,600)) 
white = 255, 255, 255 
tick = 0 
sprite = pygame.Surface([20, 20]) 
sprite.fill(white) 
sprite.set_colorkey(white) 
rect = sprite.get_rect() 
rect.x = 400 
rect.y = 300 


while tick < 100: 

    screen.fill(black) 
    screen.blit(sprite, (rect.x, rect.y)) 

    pygame.display.update() 
    clock.tick(10) 
    tick += 1 

pygame.quit() 
quit() 

随着该你应该很快看到一个白色的矩形 - 从他们开始,你可以开始改进你的代码来延迟程序结束,或者等待用户事件关闭屏幕并让事情滚动。

顺便说一句,在pygame的谈话,表面倒不认为是“精灵” - 一个Sprite是指被用作“组”成员的特殊类在其一系列的辅助功能会有效,当你开始组织一个更完整的游戏或动画序列。

0

Pygame documents是一个很好的朋友。

1.为什么要填写sprite白色,然后将其颜色键设置为白色?这只会让精灵透明。见set_colorkey

sprite = pygame.Surface([20, 20]) 
sprite.fill(white) 
sprite.set_colorkey(white) 

如果您需要红色sprite,只需创建一个并将其填充为红色。

sprite = pygame.Surface((20, 20)) 
sprite.fill(red) 

2.什么pygame.draw.rect做的仅仅是借鉴了Surface矩形形状。所以,如果你想画一个红色的矩形屏幕上,只是

pygame.draw.rect(screen, red, (0, 0, 20, 20)) 

或者,如果你想显示在屏幕上,它通常是更有效的精灵,​​用blit

screen.blit(sprite, (0, 0)) 

玩得开心与pygame :)