PyGame Sprites Occasionally Not Drawing

会有一股神秘感。 提交于 2019-12-23 04:37:12

问题


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


来源:https://stackoverflow.com/questions/41604052/pygame-sprites-occasionally-not-drawing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!