PyGame 精灵偶尔不绘图 [英] PyGame Sprites Occasionally Not Drawing
本文介绍了PyGame 精灵偶尔不绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 PyGame 制作一个基本的纸牌游戏.我目前只是想在屏幕上绘制一张卡片.奇怪的是,有时它会绘制,有时不会.下面是我的代码:
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()
推荐答案
你必须在 while 循环中的每个循环中更新显示
You have to update the display in every loop in the while loop with
pygame.display.update()
这篇关于PyGame 精灵偶尔不绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文