PyGame Sprites Occasionally Not Drawing

2019-06-14 12:24发布

问题:

I am trying to make a basic card game using PyGame. I am currently just trying to draw a single card to the screen. The weird thing is, occasionally it will draw and occasionally it won't. Below is my code:

import pygame
from pygame.locals import *
from socket import *
import sys
import os
import math
import getopt
import random

def load_png(name) :
    # Loads an image and returns the image object
    fullname = os.path.join('/home/edge/Downloads/Playing Cards/PNG-cards-1.3', name)

    image = pygame.image.load(fullname)
    if image.get_alpha is None :
        image = image.convert()
    else :
        image = image.convert_alpha()

    return image, image.get_rect()

class Card(pygame.sprite.Sprite) :
    def __init__(self, suit, val) :
        pygame.sprite.Sprite.__init__(self)
        self.suit = suit
        self.val  = val
        self.image, self.rect = load_png(val + '_of_' + suit + '.png')
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        #self.rect.inflate(-.5, -.5)

def main() :
    pygame.init()

    pygame.display.set_caption('Card Game Thingy')

    screen = pygame.display.set_mode( (1250, 650) )
    background = pygame.Surface(screen.get_size() )
    background = background.convert()
    background.fill( (0, 0, 0) )

    x = Card('diamonds', '2')
    cardSprite = pygame.sprite.RenderPlain(x)

    screen.blit(background, (0, 0) )

    cardSprite.draw(screen)
    clock = pygame.time.Clock()
    # Game Loop
    while True :
        clock.tick(60)
        for event in pygame.event.get() :
            if event.type == QUIT :
                return
            elif event.type == KEYDOWN :
                if event.key == K_DOWN :
                    return

        cardSprite.draw(screen)

if __name__ == '__main__' :
    main()

回答1:

You have to update the display in every loop in the while loop with

pygame.display.update()